Skip to content

Commit

Permalink
Split different cast variants into separate instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Feb 14, 2025
1 parent b6e8f27 commit 462e842
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 179 deletions.
25 changes: 18 additions & 7 deletions bbq/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,25 @@ func (c *Compiler[_]) VisitCastingExpression(expression *ast.CastingExpression)
castingTypes := c.ExtendedElaboration.CastingExpressionTypes(expression)
index := c.getOrAddType(castingTypes.TargetType)

castKind := opcode.CastKindFrom(expression.Operation)

c.codeGen.Emit(
opcode.InstructionCast{
var castInstruction opcode.Instruction
switch expression.Operation {
case ast.OperationCast:
castInstruction = opcode.InstructionSimpleCast{
TypeIndex: index,
Kind: castKind,
},
)
}
case ast.OperationFailableCast:
castInstruction = opcode.InstructionFailableCast{
TypeIndex: index,
}
case ast.OperationForceCast:
castInstruction = opcode.InstructionForceCast{
TypeIndex: index,
}
default:
panic(errors.NewUnreachableError())
}

c.codeGen.Emit(castInstruction)
return
}

Expand Down
45 changes: 0 additions & 45 deletions bbq/opcode/castkind.go

This file was deleted.

10 changes: 0 additions & 10 deletions bbq/opcode/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ func emitPathDomain(code *[]byte, domain common.PathDomain) {
emitByte(code, byte(domain))
}

// CastKind

func decodeCastKind(ip *uint16, code []byte) CastKind {
return CastKind(decodeByte(ip, code))
}

func emitCastKind(code *[]byte, kind CastKind) {
emitByte(code, byte(kind))
}

// CompositeKind

func decodeCompositeKind(ip *uint16, code []byte) common.CompositeKind {
Expand Down
88 changes: 74 additions & 14 deletions bbq/opcode/instructions.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions bbq/opcode/instructions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,43 @@
- name: "value"
type: "value"

- name: "cast"
- name: "simpleCast"
description:
Pops a value off the stack, casts it to the given type, and then pushes it back on to the stack.
operands:
- name: "typeIndex"
type: "index"
- name: "kind"
type: "castKind"
valueEffects:
pop:
- name: "value"
type: "value"
push:
- name: "value"
type: "value"

- name: "failableCast"
description:
Pops a value off the stack, casts it to the given type.
If the value is a subtype of the given type, then casted value is pushed back on to the stack.
If the value is not a subtype of the given type, then a `nil` is pushed to the stack instead.
operands:
- name: "typeIndex"
type: "index"
valueEffects:
pop:
- name: "value"
type: "value"
push:
- name: "value"
type: "optional"

- name: "forceCast"
description:
Pops a value off the stack, force-casts it to the given type, and then pushes it back on to the stack.
Panics if the value is not a subtype of the given type.
operands:
- name: "typeIndex"
type: "index"
valueEffects:
pop:
- name: "value"
Expand Down
7 changes: 6 additions & 1 deletion bbq/opcode/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ const (
Unwrap
Destroy
Transfer
Cast
SimpleCast
FailableCast
ForceCast
_
_
_
_
_
_
Expand Down
72 changes: 37 additions & 35 deletions bbq/opcode/opcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 462e842

Please sign in to comment.