Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Compiler] Support basic casting operations in VM #3767

Merged
merged 5 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in bbq/opcode/instruction.go

View workflow job for this annotation

GitHub Actions / Lint

: # github.com/onflow/cadence/bbq/opcode [github.com/onflow/cadence/bbq/opcode.test]
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
Expand Down Expand Up @@ -94,16 +94,6 @@
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 and 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
Loading