Skip to content

Commit 00e822a

Browse files
committed
Refactored memory-slice parameters.
1 parent ab71289 commit 00e822a

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

lib/Clar2EVM/compile.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ and compile_public_function ?(response_only=false) features env index (name, _,
148148
else compile_body_with_any_return
149149
in
150150
let body = compile_body body |> List.concat in
151-
let postlude = [ (* (ok ...) or (err ...) expected on top of stack *)
152-
EVM.zero; (* offset = memory address 0 *)
153-
EVM.MSTORE; (* MSTORE offset, value *)
154-
EVM.from_int 0x20; (* length = 256 bits *)
155-
EVM.zero; (* offset = memory address 0 *)
156-
EVM.RETURN; (* RETURN offset, length *)
157-
EVM.STOP; (* redundant, but a good marker for EOF *)
158-
] in
151+
let postlude = (* (ok ...) or (err ...) expected on top of stack *)
152+
[
153+
EVM.zero; (* offset = memory address 0 *)
154+
EVM.MSTORE; (* MSTORE offset, value *)
155+
]
156+
@ EVM.return' (0, 32)
157+
@ EVM.stop (* redundant, but a good marker for EOF *)
158+
in
159159
(1 + index, prelude @ body @ postlude)
160160

161161
and compile_private_function _features env index (_, _, body) =
@@ -295,7 +295,7 @@ and compile_expression env = function
295295
| UnwrapPanic input ->
296296
let input = compile_expression env input in
297297
input @ compile_branch [EVM.DUP 1; EVM.ISZERO]
298-
[EVM.POP; EVM.zero; EVM.zero; EVM.REVERT]
298+
(EVM.pop @ EVM.revert (0, 0))
299299
[EVM.SLOAD]
300300

301301
| VarGet var ->
@@ -332,7 +332,7 @@ and compile_expression env = function
332332
| Clarity.Buff _ | Int | Uint ->
333333
let input_size = size_of_expression value in
334334
let input_mstore = compile_mstore_of_expression env value in
335-
input_mstore @ EVM.staticcall_hash160 0 input_size 0 @ EVM.pop
335+
input_mstore @ EVM.staticcall_hash160 (0, input_size) 0 @ EVM.pop
336336
| t -> unsupported_function "hash160" t
337337
end
338338

@@ -341,7 +341,7 @@ and compile_expression env = function
341341
| Clarity.Buff _ | Int | Uint ->
342342
let input_size = size_of_expression value in
343343
let value = compile_expression env value in
344-
EVM.mstore 0 value @ EVM.sha3 0 input_size
344+
EVM.mstore 0 value @ EVM.sha3 (0, input_size)
345345
| t -> unsupported_function "keccak256" t
346346
end
347347

@@ -367,7 +367,7 @@ and compile_expression env = function
367367
| Clarity.Buff _ | Int | Uint ->
368368
let input_size = size_of_expression value in
369369
let input_mstore = compile_mstore_of_expression env value in
370-
input_mstore @ EVM.staticcall_sha256 0 input_size 0 @ EVM.pop
370+
input_mstore @ EVM.staticcall_sha256 (0, input_size) 0 @ EVM.pop
371371
| t -> unsupported_function "sha256" t
372372
end
373373

@@ -413,7 +413,7 @@ and compile_static_print_call value =
413413
let payload = EVM.ABI.encode_with_signature signature args in
414414
let payload_size = String.length payload in
415415
let mstore = EVM.mstore_bytes 0 payload in
416-
mstore @ EVM.staticcall log_addr 0 payload_size 0 0 @ EVM.pop
416+
mstore @ EVM.staticcall log_addr (0, payload_size) (0, 0) @ EVM.pop
417417

418418
and compile_mstore_of_expression ?(offset=0) env expr =
419419
EVM.mstore offset (compile_expression env expr)

lib/EVM/construct.ml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type addr = string
44

55
type ptr = int
66

7+
type slice = ptr * int
8+
79
let rec addr_of_int z =
810
match from_int z with PUSH (_, s) -> s | _ -> unreachable ()
911

@@ -101,13 +103,22 @@ let origin = [ORIGIN]
101103

102104
let pop = [POP]
103105

104-
let sha3 input_ptr input_size = [from_int input_size; from_int input_ptr; SHA3]
106+
let return' = function
107+
| data_ptr, data_size when data_ptr = data_size -> [from_int data_size; DUP 1; RETURN]
108+
| data_ptr, data_size -> [from_int data_size; from_int data_ptr; RETURN]
109+
110+
let revert = function
111+
| data_ptr, data_size when data_ptr = data_size -> [from_int data_size; DUP 1; REVERT]
112+
| data_ptr, data_size -> [from_int data_size; from_int data_ptr; REVERT]
113+
114+
let sha3 = function
115+
| input_ptr, input_size -> [from_int input_size; from_int input_ptr; SHA3]
105116

106117
let sload key = [from_int key; SLOAD]
107118

108119
let sstore key val' = val' @ [from_int key; SSTORE]
109120

110-
let staticcall ?(gas=0) addr input_ptr input_size output_ptr output_size =
121+
let staticcall ?(gas=0) addr (input_ptr, input_size) (output_ptr, output_size) =
111122
[
112123
from_int output_size;
113124
from_ptr output_ptr;
@@ -118,11 +129,11 @@ let staticcall ?(gas=0) addr input_ptr input_size output_ptr output_size =
118129
STATICCALL (* gas, addr, argsOffset, argsLength, retOffset, retLength *)
119130
]
120131

121-
let staticcall_hash160 input_ptr input_size output_ptr =
122-
staticcall (addr_of_int 0x03) input_ptr input_size output_ptr 32
132+
let staticcall_hash160 (input_ptr, input_size) output_ptr =
133+
staticcall (addr_of_int 0x03) (input_ptr, input_size) (output_ptr, 32)
123134

124-
let staticcall_sha256 input_ptr input_size output_ptr =
125-
staticcall (addr_of_int 0x02) input_ptr input_size output_ptr 32
135+
let staticcall_sha256 (input_ptr, input_size) output_ptr =
136+
staticcall (addr_of_int 0x02) (input_ptr, input_size) (output_ptr, 32)
126137

127138
let stop = [STOP]
128139

lib/EVM/construct.mli

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type addr = string
44

55
type ptr = int
66

7+
type slice = ptr * int
8+
79
val addr_of_int : int -> addr
810

911
val ptr_of_int : int -> ptr
@@ -74,17 +76,21 @@ val origin : opcode list
7476

7577
val pop : opcode list
7678

77-
val sha3 : ptr -> int -> opcode list
79+
val return' : slice -> opcode list
80+
81+
val revert : slice -> opcode list
82+
83+
val sha3 : slice -> opcode list
7884

7985
val sload : int -> opcode list
8086

8187
val sstore : int -> opcode list -> opcode list
8288

83-
val staticcall : ?gas:int -> addr -> ptr -> int -> ptr -> int -> opcode list
89+
val staticcall : ?gas:int -> addr -> slice -> slice -> opcode list
8490

85-
val staticcall_hash160 : ptr -> int -> ptr -> opcode list
91+
val staticcall_hash160 : slice -> ptr -> opcode list
8692

87-
val staticcall_sha256 : ptr -> int -> ptr -> opcode list
93+
val staticcall_sha256 : slice -> ptr -> opcode list
8894

8995
val stop : opcode list
9096

test/contracts.t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ kv-store.clar:
2727
panic.clar:
2828

2929
$ clarc -t opcode ../../../../../etc/examples/panic.clar
30-
PUSH1 0x00 PUSH1 0x00 SSTORE PUSH1 0x63 DUP1 PUSH1 0x10 PUSH1 0x00 CODECOPY
30+
PUSH1 0x00 PUSH1 0x00 SSTORE PUSH1 0x62 DUP1 PUSH1 0x10 PUSH1 0x00 CODECOPY
3131
PUSH1 0x00 RETURN PUSH1 0xe0 PUSH1 0x02 EXP PUSH1 0x00 CALLDATALOAD DIV DUP1
32-
PUSH4 0xc2187034 EQ PUSH1 0x3b JUMPI DUP1 PUSH4 0x4700d305 EQ PUSH1 0x50
33-
JUMPI STOP JUMPDEST PUSH1 0x00 SLOAD DUP1 ISZERO ISZERO PC PUSH1 0x10 ADD
34-
JUMPI POP PUSH1 0x00 PUSH1 0x00 REVERT PC PUSH1 0x07 ADD JUMP JUMPDEST SLOAD
32+
PUSH4 0xc2187034 EQ PUSH1 0x3a JUMPI DUP1 PUSH4 0x4700d305 EQ PUSH1 0x4f
33+
JUMPI STOP JUMPDEST PUSH1 0x00 SLOAD DUP1 ISZERO ISZERO PC PUSH1 0x0f ADD
34+
JUMPI POP PUSH1 0x00 DUP1 REVERT PC PUSH1 0x07 ADD JUMP JUMPDEST SLOAD
3535
JUMPDEST SWAP1 JUMP STOP JUMPDEST POP PC PUSH1 0x07 ADD PUSH1 0x1e JUMP
3636
JUMPDEST PUSH1 0x01 PUSH1 0x00 MSTORE PUSH1 0x20 PUSH1 0x00 RETURN STOP
3737
JUMPDEST POP PC PUSH1 0x07 ADD PUSH1 0x1e JUMP JUMPDEST PUSH1 0x00 MSTORE

test/functions.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ unwrap-err!:
452452

453453
unwrap-panic:
454454

455+
$ clarc -t opcode -f only-function=test <<EOF
456+
> (define-read-only (test) (unwrap-panic none))
457+
> EOF
458+
PUSH1 0x00 DUP1 ISZERO ISZERO PC PUSH1 0x0f ADD JUMPI POP PUSH1 0x00 DUP1
459+
REVERT PC PUSH1 0x07 ADD JUMP JUMPDEST SLOAD JUMPDEST PUSH1 0x00 MSTORE
460+
PUSH1 0x20 PUSH1 0x00 RETURN STOP
461+
455462
unwrap!:
456463

457464
use-trait:

0 commit comments

Comments
 (0)