Fuel ASM

build crates.io docs discord

Instruction set for the FuelVM.

Compile features

Example

```rust use fuel_asm::; use Opcode::;

// A sample program to perform ecrecover let program = vec![ MOVE(0x10, 0x01), // set r[0x10] := $one SLLI(0x20, 0x10, 5), // set r[0x20] := r[0x10] << 5 == 32 SLLI(0x21, 0x10, 6), // set r[0x21] := r[0x10] << 6 == 64 ALOC(0x21), // alloc r[0x21] == 64 to the heap ADDI(0x10, 0x07, 1), // set r[0x10] := $hp + 1 (allocated heap) MOVE(0x11, 0x04), // set r[0x11] := $ssp ADD(0x12, 0x04, 0x20), // set r[0x12] := $ssp + r[0x20] ECR(0x10, 0x11, 0x12), // recover public key in memory[r[0x10], 64] RET(0x01), // return 1 ];

// Convert program to bytes representation let bytes: Vec = program.iter().copied().collect();

// A program can be reconstructed from an iterator of bytes let restored = Opcode::frombytesiter(bytes.iter().copied());

assert_eq!(program, restored);

// Every instruction can be described as u32 big-endian bytes let halfwords: Vec = program.iter().copied().map(u32::from).collect(); let bytes = halfwords.iter().copied().map(u32::tobebytes).flatten(); let restored = Opcode::frombytesiter(bytes);

assert_eq!(program, restored);

// We can also reconstruct the instructions individually let restored: Vec = halfwords.iter().copied().map(Opcode::from).collect();

assert_eq!(program, restored);

// We have an unchecked variant for optimal performance let restored: Vec = halfwords .iter() .copied() .map(|w| unsafe { Opcode::frombytesunchecked(&w.tobebytes()) }) .collect();

assert_eq!(program, restored);

// Finally, we have [Instruction] to allow optimal runtime parsing of the components of the // opcode // // Opcode itself is only but an abstraction/helper to facilitate visualization, but the VM is // expected to use raw instructions let instrs: Vec = program.iter().copied().map(Instruction::from).collect(); let restored: Vec = instrs.iter().copied().map(Opcode::from).collect();

assert_eq!(program, restored);

// An instruction is composed by the opcode representation registers Id and immediate values asserteq!(instrs[1].op(), OpcodeRepr::SLLI as u8); asserteq!(instrs[1].ra(), 0x20); asserteq!(instrs[1].rb(), 0x10); asserteq!(instrs[1].imm12(), 5); ```