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

[WIP] Compiler+VM POC #3648

Draft
wants to merge 104 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
b17343d
PoC: Build Bytecode Quickly
turbolent Oct 21, 2022
4afa28b
Add struct init
SupunS Feb 27, 2023
43808d5
Add struct field access
SupunS Feb 28, 2023
6c6519b
Use atree for struct values
SupunS Mar 1, 2023
1c3d8e7
Add type check during value transfer
SupunS Mar 1, 2023
1608f15
Fix non-determinism in global variables
SupunS Mar 2, 2023
e222138
Merge branch 'supun/stack-vm-structs' of https://github.com/onflow/ca…
SupunS Mar 2, 2023
15bb002
Add type check during value transfer
SupunS Mar 1, 2023
84892d8
Add resource types
SupunS Mar 3, 2023
384a616
Merge branch 'supun/stack-vm-structs' of https://github.com/onflow/ca…
SupunS Mar 3, 2023
737fe24
Refactor tests
SupunS Mar 3, 2023
22de01e
Add composite functions. Pass location to constructor
SupunS Mar 6, 2023
4fcbe79
Support imports
SupunS Mar 7, 2023
6ce24a7
Support importing contracts
SupunS Mar 7, 2023
ece09b4
Add contract initialization
SupunS Mar 8, 2023
2c94c75
Fix contract loading
SupunS Mar 8, 2023
a83a915
Improve linker
SupunS Mar 11, 2023
aa19efc
Unify instance-method invocation and static-method invocation
SupunS Mar 13, 2023
3455f08
Add native function invocation support
SupunS Mar 14, 2023
26ae063
Start basics needed for FT transfer
SupunS Mar 28, 2023
0a776cc
Add transaction execution support
SupunS Mar 29, 2023
8dc6199
Add interface method invocation
SupunS Apr 11, 2023
804e4c4
Run FT transfer account setup transaction
SupunS Apr 11, 2023
8611b11
Run FT transfer transaction
SupunS Apr 12, 2023
2144c43
Support transaction parameters
SupunS Apr 12, 2023
61d7a01
Add auth account borrow
SupunS May 14, 2024
2970b2c
Support type arguments
SupunS May 15, 2024
0fe0ca7
Use single storage across execution
SupunS May 16, 2024
5acd4ec
Fix member get/set
SupunS May 16, 2024
6a0e25f
Fix transaction execute block argument passing
SupunS May 16, 2024
06c68e9
Add account link function
SupunS May 17, 2024
02c3d3d
Add public account functions
SupunS May 22, 2024
f15e983
Improve test
SupunS May 22, 2024
5bf8ca0
Add FT transfer benchmark
SupunS May 23, 2024
21719e9
Fix parameter dropping in invocation
SupunS May 23, 2024
a637bed
Reuse constants in the pool
SupunS May 23, 2024
f90703a
Optimize
SupunS May 23, 2024
8030c6a
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Jul 9, 2024
11e88c8
Fix key not found error handling
SupunS Jul 9, 2024
1e66eb5
Add more account type functions
SupunS Jul 10, 2024
b90fb6f
Add meta info to SimpleCompositeValue
SupunS Jul 10, 2024
5c3bd9a
Fix FT transfer transaction
SupunS Jul 11, 2024
50323db
Refactor
SupunS Jul 11, 2024
6a7bdad
Fix FT token tranfer
SupunS Jul 11, 2024
2a1b1e5
Update interpreter FT tranfer benchmarck
SupunS Jul 12, 2024
cbcdf7b
Cleanup code
SupunS Jul 16, 2024
a72b6f8
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Aug 2, 2024
4ee489a
Update to match latest changes in master
SupunS Aug 2, 2024
359435d
Move vm tests to a dedicated directory
SupunS Aug 2, 2024
8f781d1
Refactor tests
SupunS Aug 2, 2024
799cda2
Add resource invalidation test
SupunS Aug 6, 2024
540c09d
Add support for arrays
SupunS Aug 7, 2024
d13f04c
Add support for references
SupunS Aug 8, 2024
cb2a8be
Add resource reference tracking
SupunS Oct 23, 2024
e240da7
Move contarcts and transactions used in tests to a separate file
SupunS Oct 23, 2024
71ac7eb
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Oct 24, 2024
de92828
Move compiler and vm to top level
SupunS Oct 24, 2024
f6d578b
Add interpreter FT transfer test
SupunS Oct 25, 2024
c9871e2
Add FT token benchmark for interpreter as a runtime test
SupunS Oct 25, 2024
966036c
Refactor and cleanup
SupunS Oct 25, 2024
b7004d6
Rename Context to ExecutableProgram
SupunS Oct 25, 2024
4c937a5
Update licence headers
SupunS Oct 25, 2024
7ee292c
Print FT transfer compiled codes
SupunS Oct 28, 2024
d2a9fcc
Skip TestResourceLossViaSelfRugPull test
SupunS Oct 28, 2024
b2195be
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Oct 28, 2024
78780d5
Fix tests
SupunS Oct 28, 2024
69a1753
Merge branch 'master' of https://github.com/onflow/cadence into supun…
SupunS Nov 1, 2024
c61f9b2
Use type-index in the 'New' instruction
SupunS Nov 6, 2024
ad3620e
Rename 'CastType' to 'CastKind'
SupunS Nov 6, 2024
907fe6e
Refactor and fix lint
SupunS Nov 7, 2024
b8e44e7
Recursively link dynamically imported programs
SupunS Nov 8, 2024
50606ff
Fix concurrent map access
SupunS Nov 8, 2024
e04c0be
Add runtime sub-type checking
SupunS Nov 14, 2024
e5c0dbe
Improve tests
SupunS Nov 20, 2024
405516e
Move locals out of stackframe
SupunS Nov 25, 2024
22ae521
Fix missing returns
SupunS Nov 26, 2024
2d9b4b7
optimization: make the callframe a non-pointer
SupunS Nov 27, 2024
35c74a9
Merge pull request #3704 from onflow/supun/vm-cadence-1.0-optimized
SupunS Nov 28, 2024
e8627af
improve and test instruction printer
turbolent Nov 29, 2024
83f76f6
Merge pull request #3706 from onflow/bastian/improve-and-test-instruc…
turbolent Nov 30, 2024
c01300e
Lazily load fields for simple composite values
SupunS Dec 6, 2024
75bd3cf
refactor instruction encoding
turbolent Dec 11, 2024
e26c88f
add license headers
turbolent Dec 11, 2024
d02289b
improve names of indices
turbolent Dec 11, 2024
140113c
refactor instruction decoding
turbolent Dec 11, 2024
6b85f2b
Merge pull request #3713 from onflow/bastian/refactor-instruction-enc…
turbolent Dec 11, 2024
8c6338a
improve encoding of uint16 values
turbolent Dec 11, 2024
fa12063
add instruction types, refactor encoding and decoding to use them
turbolent Dec 11, 2024
b7e9a77
refactor instruction printing
turbolent Dec 11, 2024
a0c6faf
improve file name
turbolent Dec 12, 2024
4a7a40a
Merge pull request #3714 from onflow/bastian/refactor-instruction-dec…
turbolent Dec 12, 2024
a8603ec
use one codegen and retarget it to the current function
turbolent Dec 12, 2024
9e560c1
retarget VM to use instructions, avoid bytecode encoding/decoding
turbolent Dec 12, 2024
9ef3c0f
Merge pull request #3715 from onflow/bastian/retarget-compiler
turbolent Dec 12, 2024
d9e61fb
generate the code for the instructions from a file that defines all i…
turbolent Dec 14, 2024
c6756ff
add the generated instructions
turbolent Dec 14, 2024
22370f7
group instructions into categories
turbolent Dec 16, 2024
b2c56c9
Merge pull request #3716 from onflow/bastian/generate-instruction-code
SupunS Dec 16, 2024
03cce84
Make field name a part of the get/set field instruction
SupunS Dec 16, 2024
8502948
Move string arguments of instructions to the constant pool
SupunS Dec 16, 2024
4ac394b
Fix instruction pretty printing
SupunS Dec 16, 2024
a474836
document effects on value stack and control stack
turbolent Dec 16, 2024
a24debb
Merge pull request #3718 from onflow/supun/improve-instructions
SupunS Dec 17, 2024
ed25841
Merge pull request #3719 from onflow/bastian/document-effects
turbolent Dec 17, 2024
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
34 changes: 34 additions & 0 deletions bbq/commons/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package commons

const (
InitFunctionName = "init"
TransactionWrapperCompositeName = "transaction"
TransactionExecuteFunctionName = "transaction.execute"
TransactionPrepareFunctionName = "transaction.prepare"
LogFunctionName = "log"
PanicFunctionName = "panic"
GetAccountFunctionName = "getAccount"

// Names used by generated constructs

ProgramInitFunctionName = "$_init_"
TransactionGeneratedParamPrefix = "$_param_"
)
56 changes: 56 additions & 0 deletions bbq/commons/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package commons

import (
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/bbq"
"github.com/onflow/cadence/bbq/opcode"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/sema"
)

type ImportHandler func(location common.Location) *bbq.Program[opcode.Instruction]

type LocationHandler func(identifiers []ast.Identifier, location common.Location) ([]ResolvedLocation, error)

type ResolvedLocation = sema.ResolvedLocation

func ResolveLocation(
locationHandler LocationHandler,
identifiers []ast.Identifier,
location common.Location,
) ([]ResolvedLocation, error) {

// If no location handler is available,
// default to resolving to a single location that declares all identifiers

if locationHandler == nil {
return []ResolvedLocation{
{
Location: location,
Identifiers: identifiers,
},
}, nil
}

// A location handler is available,
// use it to resolve the location / identifiers
return locationHandler(identifiers, location)
}
51 changes: 51 additions & 0 deletions bbq/commons/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package commons

import (
"bytes"

"github.com/onflow/cadence/common"
"github.com/onflow/cadence/interpreter"
)

func TypeQualifiedName(typeName, functionName string) string {
if typeName == "" {
return functionName
}

return typeName + "." + functionName
}

func LocationToBytes(location common.Location) ([]byte, error) {
var buf bytes.Buffer
enc := interpreter.CBOREncMode.NewStreamEncoder(&buf)

err := interpreter.EncodeLocation(enc, location)
if err != nil {
return nil, err
}

err = enc.Flush()
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}
88 changes: 88 additions & 0 deletions bbq/compiler/codegen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package compiler

import (
"github.com/onflow/cadence/bbq/opcode"
"github.com/onflow/cadence/errors"
)

type CodeGen[E any] interface {
Offset() int
SetTarget(code *[]E)
Emit(instruction opcode.Instruction)
PatchJump(offset int, newTarget uint16)
}

// ByteCodeGen is a CodeGen implementation that emits bytecode
type ByteCodeGen struct {
target *[]byte
}

var _ CodeGen[byte] = &ByteCodeGen{}

func (g *ByteCodeGen) Offset() int {
return len(*g.target)
}

func (g *ByteCodeGen) SetTarget(target *[]byte) {
g.target = target
}

func (g *ByteCodeGen) Emit(instruction opcode.Instruction) {
instruction.Encode(g.target)
}

func (g *ByteCodeGen) PatchJump(offset int, newTarget uint16) {
opcode.PatchJump(g.target, offset, newTarget)
}

// InstructionCodeGen is a CodeGen implementation that emits opcode.Instruction
type InstructionCodeGen struct {
target *[]opcode.Instruction
}

var _ CodeGen[opcode.Instruction] = &InstructionCodeGen{}

func (g *InstructionCodeGen) Offset() int {
return len(*g.target)
}

func (g *InstructionCodeGen) SetTarget(target *[]opcode.Instruction) {
g.target = target
}

func (g *InstructionCodeGen) Emit(instruction opcode.Instruction) {
*g.target = append(*g.target, instruction)
}

func (g *InstructionCodeGen) PatchJump(offset int, newTarget uint16) {
switch ins := (*g.target)[offset].(type) {
case opcode.InstructionJump:
ins.Target = newTarget
(*g.target)[offset] = ins

case opcode.InstructionJumpIfFalse:
ins.Target = newTarget
(*g.target)[offset] = ins

default:
panic(errors.NewUnreachableError())
}
}
Loading