Module evmil::bytecode

source ·
Expand description

Functionality for working with bytecode contracts. This includes support for assembling contracts written in assembly language into bytecode.

Examples

An example contract written in assembly language is:

.code
   push 0x02
   push 0x01
   add
   push lab0
   jumpi
   invalid
lab0:
   jumpdest

The following example illustrates how to parse some assembly language into an assembly, and then assemble it into a bytecode contract.

use evmil::bytecode::Assembly;
use evmil::util::ToHexString;
// Assembly language
let asm = r#"
 .code
 push 0x01
 push 0x02
 add
"#;
// Parse into assembly
let asm = Assembly::from_str(&asm).unwrap();
// Generate (legacy) bytecode
let bytecode = asm.to_legacy_bytes().to_hex_string();
// Check output
assert_eq!(bytecode,"0x6001600201");

Modules

Structs

  • A structured representation of an EVM bytecode contract which is either a legacy contract, or an EVM Object Format (EOF) compatiable contract. Regardless of whether it is legacy or not, a contract is divided into one or more sections. A section is either a code section or a data section. For EOF contracts, the data section should also come last. However, for legacy contracts, they can be interleaved.
  • An iterator over the basic blocks of an instruction sequence. A basic block can only have a single entry point, and may have zero or more exits. For example, consider the following:
  • A block decomposition of a given bytecode sequence. This is a non-overlapping decomponsition of single instructions into instruction blocks.
  • Mechanism for constructing a bytecode Assembly by allowing instructions to be patched before the final assembly is built. For example, consider the problem of constructing an assembly from this assembly language:
  • An iterator over the byte offsets for a given instruction sequence. The byte offset for an instruction its its byte position within the original byte sequence. For example:

Enums

  • An error which arises when attempting to decode a sequence of bytes into a Contract structure. In essence, this indicates the bytecode sequence is malformed in some way. These errors generally apply when decoding EOF containers, since these have essential and required structure.
  • Instructions correspond (roughly speaking) to EVM bytecodes. There are a few points to make about this:
  • Errors which can arise when parsing assembly language and/or assembling it.

Traits

  • A trait for converting zero or more instructions into vector of bytes.
  • A trait for converting something (e.g. a byte sequence) into a vector of instructions.