Beispiel #1
0
def generate_lll_nodes(
        global_ctx: GlobalContext,
        no_optimize: bool) -> Tuple[LLLnode, LLLnode, FunctionSignatures]:
    """
    Generate the intermediate representation (LLL) from the contextualized AST.

    This phase also includes LLL-level optimizations.

    This function returns three values: deployment bytecode, runtime bytecode
    and the function signatures of the contract

    Arguments
    ---------
    global_ctx : GlobalContext
        Contextualized Vyper AST

    Returns
    -------
    (LLLnode, LLLnode)
        LLL to generate deployment bytecode
        LLL to generate runtime bytecode
    """
    lll_nodes, lll_runtime, function_sigs = module.parse_tree_to_lll(
        global_ctx)
    if not no_optimize:
        lll_nodes = optimizer.optimize(lll_nodes)
        lll_runtime = optimizer.optimize(lll_runtime)
    return lll_nodes, lll_runtime, function_sigs
Beispiel #2
0
def generate_lll_nodes(
        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
    ---------
    global_ctx : GlobalContext
        Contextualized Vyper AST

    Returns
    -------
    (LLLnode, LLLnode)
        LLL to generate deployment bytecode
        LLL to generate runtime bytecode
    """
    lll_nodes, lll_runtime = parser.parse_tree_to_lll(global_ctx)
    lll_nodes = optimizer.optimize(lll_nodes)
    lll_runtime = optimizer.optimize(lll_runtime)
    return lll_nodes, lll_runtime
Beispiel #3
0
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
Beispiel #4
0
 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=VyperContract,
     )
     return contract
Beispiel #5
0
            def __init__(self, lll_node, name):
                # TODO figure out how to fix this circular import
                from vyper.lll.optimizer import optimize

                self.lll_node = lll_node
                # for caching purposes, see if the lll_node will be optimized
                # because a non-literal expr could turn into a literal,
                # (e.g. `(add 1 2)`)
                # TODO this could really be moved into optimizer.py
                self.should_cache = optimize(lll_node).is_complex_lll

                # a named LLL variable which represents the
                # output of `lll_node`
                self.lll_var = LLLnode.from_list(
                    name, typ=lll_node.typ, location=lll_node.location, encoding=lll_node.encoding
                )
Beispiel #6
0
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])
    lll = optimizer.optimize(lll)
    if "ir" in output_formats:
        compiler_data["ir"] = 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
Beispiel #7
0
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