def generate_lll_nodes( source_code: str, global_ctx: GlobalContext ) -> Tuple[parser.LLLnode, parser.LLLnode]: """ Generate the intermediate representation (LLL) from the contextualized AST. This phase also includes LLL-level optimizations. This function returns two values, one for generating deployment bytecode and the other for generating runtime bytecode. The remaining compilation phases may be called with either value, depending on the desired final output. Arguments --------- source_code : str srilang source code. global_ctx : GlobalContext Contextualized srilang AST Returns ------- (LLLnode, LLLnode) LLL to generate deployment bytecode LLL to generate runtime bytecode """ lll_nodes, lll_runtime = parser.parse_tree_to_lll(source_code, global_ctx) lll_nodes = optimizer.optimize(lll_nodes) lll_runtime = optimizer.optimize(lll_runtime) return lll_nodes, lll_runtime
def get_compiler_gas_estimate(code, func): lll_nodes = optimizer.optimize(parse_to_lll(code)) if func: return compiler.utils.build_gas_estimates(lll_nodes)[func] + 22000 else: return sum( compiler.utils.build_gas_estimates(lll_nodes).values()) + 22000
def lll_compiler(lll, *args, **kwargs): lll = optimizer.optimize(LLLnode.from_list(lll)) bytecode, _ = compile_lll.assembly_to_evm( compile_lll.compile_to_assembly(lll)) abi = kwargs.get('abi') or [] c = w3.eth.contract(abi=abi, bytecode=bytecode) deploy_transaction = c.constructor() tx_hash = deploy_transaction.transact() address = w3.eth.getTransactionReceipt(tx_hash)['contractAddress'] contract = w3.eth.contract( address, abi=abi, bytecode=bytecode, ContractFactoryClass=srilangContract, ) return contract
def compile_to_lll(input_file, output_formats, show_gas_estimates=False): with open(input_file) as fh: s_expressions = parse_s_exp(fh.read()) if show_gas_estimates: LLLnode.repr_show_gas = True compiler_data = {} lll = LLLnode.from_list(s_expressions[0]) if 'ir' in output_formats: compiler_data['ir'] = lll if 'opt_ir' in output_formats: compiler_data['opt_ir'] = optimizer.optimize(lll) asm = compile_lll.compile_to_assembly(lll) if 'asm' in output_formats: compiler_data['asm'] = asm if 'bytecode' in output_formats: (bytecode, _srcmap) = compile_lll.assembly_to_evm(asm) compiler_data['bytecode'] = '0x' + bytecode.hex() return compiler_data
def test_sha3_32(): lll = ['sha3_32', 0] evm = ['PUSH1', 0, 'PUSH1', 192, 'MSTORE', 'PUSH1', 32, 'PUSH1', 192, 'SHA3'] assert compile_lll.compile_to_assembly(LLLnode.from_list(lll)) == evm assert compile_lll.compile_to_assembly(optimizer.optimize(LLLnode.from_list(lll))) == evm
def test_lll_compile_fail(lll): optimized = optimizer.optimize(LLLnode.from_list(lll[0])) optimized.repr_show_gas = True hand_optimized = LLLnode.from_list(lll[1]) hand_optimized.repr_show_gas = True assert optimized == hand_optimized