{.noinit.} rampage

This commit is contained in:
Jacques Wagener 2019-09-04 16:26:07 +02:00
parent df60ceb49a
commit bf63214632
No known key found for this signature in database
GPG Key ID: C294D1025DA0E923
4 changed files with 38 additions and 61 deletions

View File

@ -106,9 +106,9 @@ proc generate_defines(keywords: seq[string], global_ctx: GlobalContext): (NimNod
if "msg.value" in keywords: if "msg.value" in keywords:
var tmp_func_name = fmt"msg_value_func" var tmp_func_name = fmt"msg_value_func"
stmts.add(parseStmt("proc " & tmp_func_name & """(): uint128 = stmts.add(parseStmt("proc " & tmp_func_name & """(): uint128 =
var ba: array[16, byte] var ba {.noinit.}: array[16, byte]
getCallValue(addr ba) getCallValue(addr ba)
var val: Stuint[128] var val {.noinit.}: Stuint[128]
{.pragma: restrict, codegenDecl: "$# __restrict $#".} {.pragma: restrict, codegenDecl: "$# __restrict $#".}
let r_ptr {.restrict.} = cast[ptr array[128, byte]](addr val) let r_ptr {.restrict.} = cast[ptr array[128, byte]](addr val)
for i, b in ba: for i, b in ba:

View File

@ -74,12 +74,9 @@ proc get_new_proc_def(event_sig: EventSignature): NimNode =
discard discard
""" """
) )
echo treeRepr(new_proc)
new_proc new_proc
proc generate_log_func*(log_keyword: string, global_ctx: GlobalContext): (NimNode, string) = proc generate_log_func*(log_keyword: string, global_ctx: GlobalContext): (NimNode, string) =
# To make a log statement we allocate a chunk of data memory. # To make a log statement we allocate a chunk of data memory.
# This buffer contains: # This buffer contains:

View File

@ -67,7 +67,7 @@ proc get_local_output_type_conversion(tmp_result_name, tmp_result_converted_name
of "uint128", "wei_value": of "uint128", "wei_value":
var ident_node = newIdentNode(tmp_result_converted_name) var ident_node = newIdentNode(tmp_result_converted_name)
var conversion_node = parseStmt(unindent(fmt""" 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}) {tmp_result_converted_name}[16..31] = toByteArrayBE({tmp_result_name})
""")) """))
return (ident_node, conversion_node) 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 global_ctx.events[event_name] = event_sig
proc get_util_functions(): NimNode = template get_util_functions() {.dirty.} =
quote do: proc copy_into_ba(to_ba: var auto, offset: int, from_ba: auto) =
# template copy_into_ba(to_ba: var untyped, offset: int, from_ba: untyped) = for i, x in from_ba:
# proc copy_into_ba(to_ba: var auto, offset: int, from_ba: auto) = to_ba[offset + i] = x
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() = proc assertNotPayable() =
var b {.noinit.}: array[16, byte] var b {.noinit.}: array[16, byte]
getCallValue(addr b) getCallValue(addr b)
if Uint128.fromBytesBE(b) > 0.stuint(128): if Uint128.fromBytesBE(b) > 0.stuint(128):
revert(nil, 0) revert(nil, 0)
proc get_getter_func(var_struct: VariableType): NimNode = proc get_getter_func(var_struct: VariableType): NimNode =
parseStmt(fmt""" let
proc {var_struct.name}*():{var_struct.var_type} {{.self.}} = ## generated getter proc_name_node = newIdentNode(var_struct.name)
self.{var_struct.name} proc_return_type_node = newIdentNode(var_struct.var_type)
""")[0] 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 = proc handle_contract_interface(in_stmts: NimNode): NimNode =
@ -236,7 +236,7 @@ proc handle_contract_interface(in_stmts: NimNode): NimNode =
function_signatures = newSeq[FunctionSignature]() function_signatures = newSeq[FunctionSignature]()
global_ctx = GlobalContext() global_ctx = GlobalContext()
main_out_stmts.add(get_util_functions()) main_out_stmts.add(getAst(get_util_functions()))
# var util_funcs = get_util_functions() # var util_funcs = get_util_functions()
# discard util_funcs # discard util_funcs
@ -328,43 +328,23 @@ proc handle_contract_interface(in_stmts: NimNode): NimNode =
call_and_copy_block.add(parseStmt("assertNotPayable()")) call_and_copy_block.add(parseStmt("assertNotPayable()"))
for idx, param in func_sig.inputs: for idx, param in func_sig.inputs:
var static_param_size = get_byte_size_of(param.var_type) var
var tmp_var_name = fmt"{func_sig.name}_param_{idx}" static_param_size = get_byte_size_of(param.var_type)
var tmp_var_converted_name = fmt"{func_sig.name}_param_{idx}_converted" tmp_var_name = fmt"{func_sig.name}_param_{idx}"
tmp_var_converted_name = fmt"{func_sig.name}_param_{idx}_converted"
# var <tmp_name>: <type> # var <tmp_name>: <type>
call_and_copy_block.add( call_and_copy_block.add(
nnkVarSection.newTree( parseStmt(unindent(fmt"""
nnkIdentDefs.newTree( var {tmp_var_name} {{.noinit.}}: array[32, byte]
newIdentNode(tmp_var_name), callDataCopy(addr {tmp_var_name}, 4, 32)
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)
)
)
# Get conversion code if necessary. # Get conversion code if necessary.
let (ident_node, convert_node) = get_local_input_type_conversion( let (ident_node, convert_node) = get_local_input_type_conversion(
tmp_var_name, tmp_var_name,
tmp_var_converted_name, tmp_var_converted_name,
param.var_type param.var_type
) )
echo treeRepr(ident_node)
if not (ident_node.kind == nnkEmpty): if not (ident_node.kind == nnkEmpty):
if not (convert_node.kind == nnkEmpty): if not (convert_node.kind == nnkEmpty):
call_and_copy_block.add(convert_node) call_and_copy_block.add(convert_node)

View File

@ -17,7 +17,7 @@ proc generate_storage_get_func*(storage_keword: string, global_ctx: GlobalContex
var new_proc = parseStmt(fmt""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(): uint256 = proc {new_proc_name}(): uint256 =
var var
tmp: array[32, byte] tmp {{.noinit.}}: array[32, byte]
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
storageLoad(pos, addr tmp) storageLoad(pos, addr tmp)
return Uint256.fromBytesBE(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""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(): {var_info.var_type} = proc {new_proc_name}(): {var_info.var_type} =
var var
tmp: array[32, byte] tmp {{.noinit.}}: array[32, byte]
tmp_ba: array[16, byte] tmp_ba {{.noinit.}}: array[16, byte]
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
storageLoad(pos, addr tmp) storageLoad(pos, addr tmp)
for i in 0..15: 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""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(): address = proc {new_proc_name}(): address =
var var
tmp: array[32, byte] tmp {{.noinit.}}: array[32, byte]
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
storageLoad(pos, addr tmp) storageLoad(pos, addr tmp)
var out_var: address 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": elif var_info.var_type == "bytes32":
var new_proc = parseStmt(fmt""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(): bytes32 = proc {new_proc_name}(): bytes32 =
var var
tmp: bytes32 tmp {{.noinit.}}: bytes32
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
storageLoad(pos, addr tmp) storageLoad(pos, addr tmp)
return tmp return tmp
@ -76,7 +76,7 @@ proc generate_storage_set_func*(storage_keyword: string, global_ctx: GlobalConte
var new_proc = parseStmt(fmt""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(value:uint256) = proc {new_proc_name}(value:uint256) =
var var
tmp: array[32, byte] = value.toByteArrayBE tmp {{.noinit.}}: array[32, byte] = value.toByteArrayBE
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
storageStore(pos, addr tmp) storageStore(pos, addr tmp)
""") """)
@ -85,7 +85,7 @@ proc generate_storage_set_func*(storage_keyword: string, global_ctx: GlobalConte
var new_proc = parseStmt(fmt""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(value: {var_info.var_type}) = proc {new_proc_name}(value: {var_info.var_type}) =
var var
tmp: array[32, byte] tmp {{.noinit.}}: array[32, byte]
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
tmp_ba = value.toByteArrayBE tmp_ba = value.toByteArrayBE
for i in 0..15: 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""" var new_proc = parseStmt(fmt"""
proc {new_proc_name}(value: address) = proc {new_proc_name}(value: address) =
var var
tmp: array[32, byte] tmp {{.noinit.}}: array[32, byte]
pos = {$slot_number}.stuint(32).toByteArrayBE pos = {$slot_number}.stuint(32).toByteArrayBE
for i, b in value: for i, b in value:
tmp[12 + i] = b tmp[12 + i] = b