Commit graph

254 commits

Author SHA1 Message Date
Evgeniy Stratonikov
b66b853285 compiler: drop stack after inline
Some control-flow statements drop stack items, for example
`return` when it is used inside of `range` loop.
For inlined calls this `return` should drop only portion of stack
which belongs to inlined call.
2021-03-01 11:48:42 +03:00
Evgeniy Stratonikov
df5314f286 compiler: refactor syscall handling
Close #941.
2021-02-24 12:51:15 +03:00
Evgeniy Stratonikov
d16ef53653 compiler: support ellipsis for append of non-byte slices
NeoVM lacks opcode for array append, thus some
kind of loop is needed for this.
2021-02-17 17:14:32 +03:00
Evgeniy Stratonikov
fc3b840335 compiler: append bytes >0x80 properly
NeoVM encoding for int(0x80) is []byte{0x80, 0x00} and
byte constant can be used both as integer and as an element
of byte-slice. Thus special case needs to be taken in
`append`.
2021-02-17 16:34:34 +03:00
Evgeniy Stratonikov
6e560c6c9f compiler: allow to inline var arg functions 2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
339187a56d compiler: count locals number properly 2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
57a0377c81 compiler: load nil directly on inline 2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
3484025065 compiler: load constants directly on inline 2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
1ae0d022dd compiler: support basic inlining 2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
1f238ce6fd compiler: do not emit RET twice for nested BlockStmt
When function finishes with `*ast.BlockStmt`, last return
in that block should be looked for.
2021-02-15 18:48:33 +03:00
Evgeniy Stratonikov
560aff6155 compiler: adjust init/_deploy method offsets during optimization
Because `_initialize` and `_deploy` methods encompass multiple
Go functions, their offsets are stored differently and need
special treatment.
2021-02-11 15:59:02 +03:00
Evgeniy Stratonikov
73a75cc27a compiler: do not convert interop types on assertion 2021-02-08 13:11:39 +03:00
Evgeniy Stratonikov
6e866b622a compiler: implement NEO contract wrapper 2021-02-08 11:44:39 +03:00
Evgeniy Stratonikov
e5d8c1c985 compiler: allow conversion to types from external packages 2021-02-08 11:02:45 +03:00
Evgeniy Stratonikov
c1cc7e6f9d native: add additional parameters to deploy
1. Management contract has 2 overloads of `deploy` method.
2. Normal contracts should have `_deploy` with 2 parameters.
2021-01-29 12:19:08 +03:00
Evgeniy Stratonikov
f0fe03117a compiler: optimize struct copy a bit
POPITEM is cheaper than PUSH + PICKITEM.
2021-01-19 09:46:01 +03:00
Evgenii Stratonikov
1c0c331e25 core: update System.Contract.Call syscall
1. Remove `System.Contract.CallEx`.
2. Extend number of parameters.
3. Add return value count to `VM.Context`.
2021-01-14 18:23:36 +03:00
Roman Khimov
459ad521ab *: fix misspellings spotted by goreportcard 2020-12-28 17:27:04 +03:00
Roman Khimov
a3f91ba8c5
Merge pull request #1603 from nspcc-dev/compiler/types
compiler: enforce `Hash160` and `Hash256` size in literals
2020-12-10 14:55:03 +03:00
Evgenii Stratonikov
ff4880249d compiler: enforce Hash160 and Hash256 size in literals
Can be useful to prevent small typos.
2020-12-10 14:11:28 +03:00
Evgenii Stratonikov
37a8550215 compiler: add contract.CallEx interop 2020-12-10 13:45:10 +03:00
Evgenii Stratonikov
cbf26f315c compiler: save both VM and smartcontract types
VM types are used in debugger, while smartcontract ones are used in
manifest. We can't save only one of them, because conversion in either
side is lossy:
1. VM has `Array` and `Struct` but smartcontract only has `Array`.
2. Smartcontract has `Hash160` etc, which are all `ByteString` or
`Buffer` in VM.

And to spice things a bit more, return type in debugger can be `Void`,
which corresponds to no real stackitem type (as it must exist).
2020-12-09 22:35:22 +03:00
Evgenii Stratonikov
573d1d10c0 cli: add tests for contract command 2020-12-04 11:05:48 +03:00
Evgenii Stratonikov
75a9a42403 compiler: check emitted event names
Check that all `Notify` invocations in source correspond to some event
in manifest.
Helpful for typos such as `transfer` instead of `Transfer`.
2020-11-26 13:49:58 +03:00
Evgenii Stratonikov
1f2d76a1c2 compiler: fix manifest method offset
While optimizing jumps, old offsets should be compared
with the method offset before optimization, not with
the constantly changing value.
2020-10-14 15:04:33 +03:00
Evgenii Stratonikov
c4a8770215 compiler: support _deploy method 2020-10-06 19:12:35 +03:00
Evgenii Stratonikov
6701e8cda0 compiler: allow to use local variables in init()
Because body of multiple `init()` functions constitute single
method in contract, we initialize slot with maximum amount of
locals encounterered in any of `init()` functions and clear them
before emitting body of each instance of `init()`.
2020-10-06 19:08:32 +03:00
Evgenii Stratonikov
b2a3a0851e emit: accept multiple opcodes in Opcode() 2020-10-06 18:03:25 +03:00
Evgenii Stratonikov
5f3b8c6d51 compiler: allow variables in byte-slice literals 2020-09-24 20:20:34 +03:00
Roman Khimov
5f22bdfecf
Merge pull request #1375 from nspcc-dev/compiler/types
Smartcontract types definition in interops
2020-09-16 14:43:50 +03:00
Evgenii Stratonikov
bcc11cbd74 compiler: support removing slice elements
Go-way of removing elements from slice is via `append` builtin.
There is a separate opcode for removing elements from
Arrays, which is cheaper and supported in this commit.
2020-09-15 16:33:43 +03:00
Evgenii Stratonikov
78948ef7af compiler: emit error for non-byte subslices
They are not supported for now, as VM has only
`SUBSTR` opcode for Buffers (`[]byte` in Go).
2020-09-15 16:21:44 +03:00
Evgenii Stratonikov
37f7363386 interop: return struct pointers where needed
`Transaction`, `Block` and `Contract` are represented as
`Array`s in VM, so we must return pointers.

Revert a1f98f92.
2020-09-09 13:10:04 +03:00
Evgenii Stratonikov
7483e3b054 compiler: support delete() builtin 2020-09-06 15:49:41 +03:00
Evgenii Stratonikov
18369c489e compiler: do not allocate slotes for unused "_" vars 2020-09-06 15:27:46 +03:00
Evgenii Stratonikov
3af7ce8c6b compiler: allow to use copy() return value 2020-09-02 15:29:59 +03:00
Evgenii Stratonikov
3d8c7af66c compiler: handle void call to recover()
Other builtins such as `len` and `make` can be ignored,
because not-assigning `make` result is catched by parser.
2020-09-02 15:20:43 +03:00
Evgenii Stratonikov
5d82b82efb compiler: support recover() 2020-08-27 10:28:50 +03:00
Evgenii Stratonikov
14ea3c2228 compiler: do not log panic message
In NEO3 THROW accepts an argument and exceptions can be handled, so
there is no need to log it.
2020-08-27 10:28:50 +03:00
Evgenii Stratonikov
fa1d1a8b00 compiler: support defer statement
Compile `defer` statement to a TRY/ENDFINALLY opcode pair.
2020-08-27 10:28:50 +03:00
Evgenii Stratonikov
84c36326f5 compiler: allow init statement in if 2020-08-27 10:28:50 +03:00
Evgenii Stratonikov
d73f3cd24c compiler: support calling function literals 2020-08-27 10:28:50 +03:00
Roman Khimov
962f45f35e
Merge pull request #1361 from nspcc-dev/feature/iota
compiler: support `iota`
2020-08-25 15:50:20 +03:00
Evgenii Stratonikov
05ef951055 compiler: support iota 2020-08-25 10:22:58 +03:00
Roman Khimov
a1fbe51bca
Merge pull request #1352 from nspcc-dev/compiler/make
Fix MEMCPY, support `copy` and `make` in compiler
2020-08-25 09:33:16 +03:00
Evgenii Stratonikov
69989e1227 compiler: support copy() 2020-08-25 08:53:29 +03:00
Evgenii Stratonikov
0f11116040 compiler: support make() 2020-08-25 08:53:28 +03:00
Evgenii Stratonikov
354645fbe3 compiler: allow to use switch without tag 2020-08-24 19:35:25 +03:00
Evgenii Stratonikov
e58616b975 compiler: drop unused call results
Close #683.
2020-08-24 19:34:51 +03:00
Evgenii Stratonikov
9dc3edf351 compiler: make use of extended JMP* opcodes 2020-08-24 11:19:54 +03:00
Evgenii Stratonikov
51f3baf68e compiler: refactor BinaryExpr handling
Reuse code between if conditions and expression context.
2020-08-24 09:46:11 +03:00
Evgenii Stratonikov
4d04c56efb compiler: make toShortForm accept an opcode 2020-08-24 09:44:44 +03:00
Evgenii Stratonikov
1be1b8de9e compiler: merge && and || processing 2020-08-24 09:44:44 +03:00
Evgenii Stratonikov
59367c96d1 compiler: allow to use += on strings 2020-08-24 09:44:44 +03:00
Evgenii Stratonikov
fd7af77895 compiler: refactor convertToken func
Move `getEqualityOpcode` into `convertToken`.
`convertToken` does not need codegen.
2020-08-24 09:44:44 +03:00
Evgenii Stratonikov
ae88c77a8a compiler: process nil comparisons separately 2020-08-24 09:44:16 +03:00
Evgenii Stratonikov
e4af295080 compiler: process constant first in BinaryExpr handling 2020-08-23 12:24:03 +03:00
Evgenii Stratonikov
2b73508561 compiler: emit short jumps while short-circuiting
Unless there is some `ast.Walk` between current instruction and jump
target, we can calculate the offset precisely.
2020-08-21 09:43:05 +03:00
Evgenii Stratonikov
984aba3113 compiler: process last instructin in writeJumps
It is unlikely that we will emit a script with a JMP in the end,
but `writeJumps` must work correctly even in such case.
2020-08-21 09:43:05 +03:00
Evgenii Stratonikov
cfa62e7051 compiler: emit short jumps where possible
Convert long jumps to short ones if the offset
can be represented in a single byte.

Close #805.
2020-08-21 09:43:05 +03:00
Evgenii Stratonikov
181569c2a1 compiler: allow to alias interop packages
Fix #397.
2020-08-19 10:13:36 +03:00
Evgenii Stratonikov
7854dcfd8f core: replace interop names with named constants 2020-08-14 14:21:54 +03:00
Evgenii Stratonikov
553e57c2c4 compiler: make sequence points on global var/const declarations 2020-08-11 11:12:30 +03:00
Evgenii Stratonikov
40fa7c0f6e compiler: emit all used files in DebugInfo.Documents 2020-08-11 11:12:29 +03:00
Roman Khimov
e1d3223505
Merge pull request #1247 from nspcc-dev/feature/pointers
Support pointers in compiler
2020-08-06 13:53:08 +03:00
Evgenii Stratonikov
f2107bfbc4 compiler: copy structs when passing as arguments
In Go structs must be copied when used as arguments.
To do so we must clone struct on VM level.
This is done by appending this struct to an intermediate array.
2020-08-05 13:14:38 +03:00
Evgenii Stratonikov
4488f61777 compiler: compile init even if there are no globals
`init` can be useful even if no globals are present,
e.g. we can use some syscall inside.
2020-08-05 12:59:53 +03:00
Evgenii Stratonikov
6f2759be3a compiler: process packages in deterministic order 2020-08-05 11:00:25 +03:00
Evgenii Stratonikov
b771d2d024 compiler: allow to use init function
Process `init()` functions after global variables has been processed.
It's body is saved into the `_initialize` method where all
initialization is performed.
2020-08-05 11:00:25 +03:00
Evgenii Stratonikov
d54c60ded3 compiler: support pointer dereferencing for structs
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-08-04 11:14:07 +03:00
Evgenii Stratonikov
eb047b12a7 compiler: support creating pointers to struct
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-08-04 11:14:07 +03:00
Evgenii Stratonikov
a781d299e0 compiler: use fully-qualified names for tracking functions
Function name now consists of 3 parts:
1) full package path
2) method receiver type (if any)
3) function name itself .

Fix #1150.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 12:07:06 +03:00
Evgenii Stratonikov
528c184f00 compiler: allow to use exported constants
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 12:06:42 +03:00
Evgenii Stratonikov
6df019913d compiler: allow to use exported variables
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 12:06:42 +03:00
Evgenii Stratonikov
b8d7e93459 compiler: make ForEachFile accept a package
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 12:06:42 +03:00
Evgenii Stratonikov
7009417325 compiler: allow to declare global variables in multiple files
Traverse and count globals across all used files.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2020-07-31 12:06:42 +03:00
Evgenii Stratonikov
685d44dbc1 *: support _initialize method in contracts
Invoke `_initialize` method on every call if present.
In NEO3 there is no entrypoint and methods are invoked by offset,
thus `Main` function is no longer required.
We still have special `Main` method in tests to simplify them.
2020-07-27 13:00:35 +03:00
Evgenii Stratonikov
d2ddf7b7cb *: support invoking methods by offset
Allow to invoke methods by offset:
1. Every invoked contract must have manifest.
2. Check arguments count on invocation.
3. Change AppCall to a regular syscall.
4. Add test suite for `System.Contract.Call`.
2020-07-27 13:00:35 +03:00
Evgenii Stratonikov
e87eba51f9 compiler: emit bytecode for unused exported functions
Exported functions should always be present in byte-code.
This will be needed later when arbitrary method calls are allowed.
2020-07-27 13:00:35 +03:00
Roman Khimov
2800179ce0
Merge pull request #1226 from nspcc-dev/fix/pusha
vm: make offset in PUSHA relative
2020-07-23 23:40:21 +03:00
Evgenii Stratonikov
b8843a2dfa vm: make offset in PUSHA relative
Follow neo-project/neo-vm#317 .
2020-07-23 13:07:30 +03:00
Anna Shaleva
2bbe218547 compiler: move SHA256 from builtins to syscalls
Now it can be processed as a normal syscall.
2020-07-22 16:58:32 +03:00
Anna Shaleva
a1f98f92fe compiler: add ConvertResultToStruct flag
Part of #1055.

There'll be a lot of interops which result with a struct on stack instead
of interop interface, and sometimes their names are the same, so it's
unrelyable to take into account interop name only and don't pay
attention to it's API (package).

Also sort syscalls by package and name.
2020-07-17 08:19:43 +03:00
Anna Shaleva
c4f7b06974 core, compiler: return struct from GetScriptContainer interop
Closes #1173
2020-07-14 06:04:48 +03:00
Roman Khimov
bc1d6791b9 compiler: allow to append multiple elements 2020-07-09 13:59:43 +03:00
Roman Khimov
76925fe3e0 compiler: slices must default to nil 2020-07-09 13:07:21 +03:00
Roman Khimov
eee8ac1655 compiler: fix initialization of struct fields, fix #1164
Make `s{}` initializers work. Deduplicate code a bit along the way. The test
added follows bf6aa02dcf test.
2020-07-09 12:27:21 +03:00
Anna Shaleva
56a5c9db11 compiler: compile appcall with dynamic argument
Closes #1160
2020-07-08 19:49:14 +03:00
Roman Khimov
ebe37d1a46 compiler: check for NULL when calculating len(), fix #1153 2020-07-07 21:49:21 +03:00
Anna Shaleva
c3a0998cae compiler: fix bug in codegen
Main function (as far as the others uncknown to codegen at the moment of
`convertFuncDecl`) didn't have package set. Fixed.
2020-07-07 13:28:38 +03:00
Evgenii Stratonikov
26cfae7c9a compiler: support shadowing via Block statements 2020-06-30 10:31:52 +03:00
Evgenii Stratonikov
40bacc6775 compiler: support variable shadowing
Closes #1131.
2020-06-30 10:31:52 +03:00
Evgenii Stratonikov
1ee4acbdbc compiler: manage variables in a separate varScope struct
Abstract var scope management from the funcScope.
2020-06-30 10:31:51 +03:00
Evgenii Stratonikov
057e1e2806 compiler: support ... variadic calls 2020-06-27 10:42:30 +03:00
Evgenii Stratonikov
a88ac44147 compiler: support variadic function definitions
Pack variadic arguments into a slice. This currently doen't work with
byte slices though.
2020-06-26 20:05:10 +03:00
Roman Khimov
9ca87855a6
Merge pull request #1101 from nspcc-dev/feature/convert
Support type conversions
2020-06-25 18:30:03 +03:00
Evgenii Stratonikov
6ddaed3927 compiler: allow to omit struct field names in literals 2020-06-24 19:33:58 +03:00
Evgenii Stratonikov
5a615d8178 compiler: allow to omit types for nested slices 2020-06-24 19:25:08 +03:00
Evgenii Stratonikov
2c5ab95b8a compiler: convert to ByteArray for string variables
Convert to ByteArray when converting variable to `string`, because
underlying byte-slice changes should not affect result string.
2020-06-24 18:59:36 +03:00
Evgenii Stratonikov
1d275ceb65 compiler: distinguish between type conversions and function calls
RN every identifier in call expression is considered to be lambda.
But it also can be type expression, so we need to distinguish between
these cases.
2020-06-24 18:46:32 +03:00