mirror of https://github.com/status-im/nimplay.git
{.noinit.} rampage
This commit is contained in:
parent
df60ceb49a
commit
bf63214632
|
@ -106,9 +106,9 @@ proc generate_defines(keywords: seq[string], global_ctx: GlobalContext): (NimNod
|
|||
if "msg.value" in keywords:
|
||||
var tmp_func_name = fmt"msg_value_func"
|
||||
stmts.add(parseStmt("proc " & tmp_func_name & """(): uint128 =
|
||||
var ba: array[16, byte]
|
||||
var ba {.noinit.}: array[16, byte]
|
||||
getCallValue(addr ba)
|
||||
var val: Stuint[128]
|
||||
var val {.noinit.}: Stuint[128]
|
||||
{.pragma: restrict, codegenDecl: "$# __restrict $#".}
|
||||
let r_ptr {.restrict.} = cast[ptr array[128, byte]](addr val)
|
||||
for i, b in ba:
|
||||
|
|
|
@ -74,12 +74,9 @@ proc get_new_proc_def(event_sig: EventSignature): NimNode =
|
|||
discard
|
||||
"""
|
||||
)
|
||||
echo treeRepr(new_proc)
|
||||
|
||||
new_proc
|
||||
|
||||
|
||||
|
||||
proc generate_log_func*(log_keyword: string, global_ctx: GlobalContext): (NimNode, string) =
|
||||
# To make a log statement we allocate a chunk of data memory.
|
||||
# This buffer contains:
|
||||
|
|
|
@ -67,7 +67,7 @@ proc get_local_output_type_conversion(tmp_result_name, tmp_result_converted_name
|
|||
of "uint128", "wei_value":
|
||||
var ident_node = newIdentNode(tmp_result_converted_name)
|
||||
var conversion_node = parseStmt(unindent(fmt"""
|
||||
var {tmp_result_converted_name}: array[32, byte]
|
||||
var {tmp_result_converted_name} {{.noinit.}}: array[32, byte]
|
||||
{tmp_result_converted_name}[16..31] = toByteArrayBE({tmp_result_name})
|
||||
"""))
|
||||
return (ident_node, conversion_node)
|
||||
|
@ -208,26 +208,26 @@ proc handle_event_defines(event_def: NimNode, global_ctx: var GlobalContext) =
|
|||
global_ctx.events[event_name] = event_sig
|
||||
|
||||
|
||||
proc get_util_functions(): NimNode =
|
||||
quote do:
|
||||
# template copy_into_ba(to_ba: var untyped, offset: int, from_ba: untyped) =
|
||||
# proc copy_into_ba(to_ba: var auto, offset: int, from_ba: auto) =
|
||||
proc copy_into_ba(to_ba: var auto, offset: int, from_ba: auto) =
|
||||
for i, x in from_ba:
|
||||
to_ba[offset + i] = x
|
||||
template get_util_functions() {.dirty.} =
|
||||
proc copy_into_ba(to_ba: var auto, offset: int, from_ba: auto) =
|
||||
for i, x in from_ba:
|
||||
to_ba[offset + i] = x
|
||||
|
||||
proc assertNotPayable() =
|
||||
var b {.noinit.}: array[16, byte]
|
||||
getCallValue(addr b)
|
||||
if Uint128.fromBytesBE(b) > 0.stuint(128):
|
||||
revert(nil, 0)
|
||||
proc assertNotPayable() =
|
||||
var b {.noinit.}: array[16, byte]
|
||||
getCallValue(addr b)
|
||||
if Uint128.fromBytesBE(b) > 0.stuint(128):
|
||||
revert(nil, 0)
|
||||
|
||||
|
||||
proc get_getter_func(var_struct: VariableType): NimNode =
|
||||
parseStmt(fmt"""
|
||||
proc {var_struct.name}*():{var_struct.var_type} {{.self.}} = ## generated getter
|
||||
self.{var_struct.name}
|
||||
""")[0]
|
||||
let
|
||||
proc_name_node = newIdentNode(var_struct.name)
|
||||
proc_return_type_node = newIdentNode(var_struct.var_type)
|
||||
storage_name_node = newIdentNode(var_struct.name)
|
||||
quote do:
|
||||
proc `proc_name_node`*():`proc_return_type_node` {.self.} = ## generated getter
|
||||
self.`storage_name_node`
|
||||
|
||||
|
||||
proc handle_contract_interface(in_stmts: NimNode): NimNode =
|
||||
|
@ -236,7 +236,7 @@ proc handle_contract_interface(in_stmts: NimNode): NimNode =
|
|||
function_signatures = newSeq[FunctionSignature]()
|
||||
global_ctx = GlobalContext()
|
||||
|
||||
main_out_stmts.add(get_util_functions())
|
||||
main_out_stmts.add(getAst(get_util_functions()))
|
||||
# var util_funcs = get_util_functions()
|
||||
# discard util_funcs
|
||||
|
||||
|
@ -328,43 +328,23 @@ proc handle_contract_interface(in_stmts: NimNode): NimNode =
|
|||
call_and_copy_block.add(parseStmt("assertNotPayable()"))
|
||||
|
||||
for idx, param in func_sig.inputs:
|
||||
var static_param_size = get_byte_size_of(param.var_type)
|
||||
var tmp_var_name = fmt"{func_sig.name}_param_{idx}"
|
||||
var tmp_var_converted_name = fmt"{func_sig.name}_param_{idx}_converted"
|
||||
var
|
||||
static_param_size = get_byte_size_of(param.var_type)
|
||||
tmp_var_name = fmt"{func_sig.name}_param_{idx}"
|
||||
tmp_var_converted_name = fmt"{func_sig.name}_param_{idx}_converted"
|
||||
# var <tmp_name>: <type>
|
||||
call_and_copy_block.add(
|
||||
nnkVarSection.newTree(
|
||||
nnkIdentDefs.newTree(
|
||||
newIdentNode(tmp_var_name),
|
||||
nnkBracketExpr.newTree(
|
||||
newIdentNode("array"),
|
||||
newLit(static_param_size),
|
||||
newIdentNode("byte")
|
||||
),
|
||||
newEmptyNode()
|
||||
)
|
||||
)
|
||||
)
|
||||
# callDataCopy(addr <tmp_name>, <offset>, <len>)
|
||||
call_and_copy_block.add(
|
||||
nnkCall.newTree(
|
||||
newIdentNode("callDataCopy"),
|
||||
nnkCommand.newTree(
|
||||
newIdentNode("addr"),
|
||||
newIdentNode(tmp_var_name)
|
||||
),
|
||||
newLit(start_offset),
|
||||
newLit(static_param_size)
|
||||
)
|
||||
)
|
||||
|
||||
parseStmt(unindent(fmt"""
|
||||
var {tmp_var_name} {{.noinit.}}: array[32, byte]
|
||||
callDataCopy(addr {tmp_var_name}, 4, 32)
|
||||
""")))
|
||||
|
||||
# Get conversion code if necessary.
|
||||
let (ident_node, convert_node) = get_local_input_type_conversion(
|
||||
tmp_var_name,
|
||||
tmp_var_converted_name,
|
||||
param.var_type
|
||||
)
|
||||
echo treeRepr(ident_node)
|
||||
if not (ident_node.kind == nnkEmpty):
|
||||
if not (convert_node.kind == nnkEmpty):
|
||||
call_and_copy_block.add(convert_node)
|
||||
|
|
|
@ -17,7 +17,7 @@ proc generate_storage_get_func*(storage_keword: string, global_ctx: GlobalContex
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(): uint256 =
|
||||
var
|
||||
tmp: array[32, byte]
|
||||
tmp {{.noinit.}}: array[32, byte]
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
storageLoad(pos, addr tmp)
|
||||
return Uint256.fromBytesBE(tmp)
|
||||
|
@ -27,8 +27,8 @@ proc generate_storage_get_func*(storage_keword: string, global_ctx: GlobalContex
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(): {var_info.var_type} =
|
||||
var
|
||||
tmp: array[32, byte]
|
||||
tmp_ba: array[16, byte]
|
||||
tmp {{.noinit.}}: array[32, byte]
|
||||
tmp_ba {{.noinit.}}: array[16, byte]
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
storageLoad(pos, addr tmp)
|
||||
for i in 0..15:
|
||||
|
@ -40,7 +40,7 @@ proc generate_storage_get_func*(storage_keword: string, global_ctx: GlobalContex
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(): address =
|
||||
var
|
||||
tmp: array[32, byte]
|
||||
tmp {{.noinit.}}: array[32, byte]
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
storageLoad(pos, addr tmp)
|
||||
var out_var: address
|
||||
|
@ -54,8 +54,8 @@ proc generate_storage_get_func*(storage_keword: string, global_ctx: GlobalContex
|
|||
elif var_info.var_type == "bytes32":
|
||||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(): bytes32 =
|
||||
var
|
||||
tmp: bytes32
|
||||
var
|
||||
tmp {{.noinit.}}: bytes32
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
storageLoad(pos, addr tmp)
|
||||
return tmp
|
||||
|
@ -76,7 +76,7 @@ proc generate_storage_set_func*(storage_keyword: string, global_ctx: GlobalConte
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(value:uint256) =
|
||||
var
|
||||
tmp: array[32, byte] = value.toByteArrayBE
|
||||
tmp {{.noinit.}}: array[32, byte] = value.toByteArrayBE
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
storageStore(pos, addr tmp)
|
||||
""")
|
||||
|
@ -85,7 +85,7 @@ proc generate_storage_set_func*(storage_keyword: string, global_ctx: GlobalConte
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(value: {var_info.var_type}) =
|
||||
var
|
||||
tmp: array[32, byte]
|
||||
tmp {{.noinit.}}: array[32, byte]
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
tmp_ba = value.toByteArrayBE
|
||||
for i in 0..15:
|
||||
|
@ -97,7 +97,7 @@ proc generate_storage_set_func*(storage_keyword: string, global_ctx: GlobalConte
|
|||
var new_proc = parseStmt(fmt"""
|
||||
proc {new_proc_name}(value: address) =
|
||||
var
|
||||
tmp: array[32, byte]
|
||||
tmp {{.noinit.}}: array[32, byte]
|
||||
pos = {$slot_number}.stuint(32).toByteArrayBE
|
||||
for i, b in value:
|
||||
tmp[12 + i] = b
|
||||
|
|
Loading…
Reference in New Issue