diff --git a/packages/asm/src.ts/assembler.ts b/packages/asm/src.ts/assembler.ts index 3033e900dd..ef98720803 100644 --- a/packages/asm/src.ts/assembler.ts +++ b/packages/asm/src.ts/assembler.ts @@ -591,11 +591,16 @@ export class ScopeNode extends LabelledNode { export type Operation = { opcode: Opcode; offset: number; + length: number; pushValue?: string; }; export interface Bytecode extends Array { getOperation(offset: number): Operation; + getByte(offset: number): number; + getBytes(offset: number, length: number): Uint8Array; + byteLength: number; + operationCount: number; } export function disassemble(bytecode: string): Bytecode { @@ -616,7 +621,8 @@ export function disassemble(bytecode: string): Bytecode { const op: Operation = { opcode: opcode, - offset: i + offset: i, + length: 1 }; offsets[i] = op; ops.push(op); @@ -628,6 +634,7 @@ export function disassemble(bytecode: string): Bytecode { const data = ethers.utils.hexlify(bytes.slice(i, i + push)); if (ethers.utils.hexDataLength(data) === push) { op.pushValue = data; + op.length += push; i += push; } else { oob = true; @@ -636,9 +643,34 @@ export function disassemble(bytecode: string): Bytecode { } (ops).getOperation = function(offset: number): Operation { + if (offset >= bytes.length) { + return { + opcode: Opcode.from("STOP"), + offset: offset, + length: 1 + }; + } return (offsets[offset] || null); }; + (ops).getByte = function(offset: number): number { + if (offset >= bytes.length) { + return 0x00; + } + return bytes[offset]; + }; + + (ops).getBytes = function(offset: number, length: number): Uint8Array { + const result = new Uint8Array(length); + result.fill(0); + if (offset < bytes.length) { + result.set(bytes.slice(offset)); + } + return ethers.utils.arrayify(result); + }; + + (ops).byteLength = bytes.length; + return (ops); } diff --git a/packages/asm/src.ts/opcodes.ts b/packages/asm/src.ts/opcodes.ts index 2fcefcb300..d5ec966267 100644 --- a/packages/asm/src.ts/opcodes.ts +++ b/packages/asm/src.ts/opcodes.ts @@ -145,6 +145,8 @@ const _Opcodes: { [ name: string ]: _Opcode } = { number: { value: 0x43, delta: 0, alpha: 1, doc: "blockNumber = number" }, difficulty: { value: 0x44, delta: 0, alpha: 1, doc: "diff = difficulty" }, gaslimit: { value: 0x45, delta: 0, alpha: 1, doc: "gas = gaslimit" }, + chainid: { value: 0x46, delta: 0, alpha: 1, doc: "chainid = chainid" }, + selfbalance: { value: 0x47, delta: 0, alpha: 1, doc: "bal = selfbalance" }, // Stack, Memory, Storage and Flow Operations pop: { value: 0x50, delta: 1, alpha: 0, doc: "stackTopValue = pop" },