Added memory-like support and new opcodes to asm.

This commit is contained in:
Richard Moore 2020-09-04 01:20:35 -04:00
parent 83db8a6bd1
commit 6fd3bb62d1
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
2 changed files with 35 additions and 1 deletions

View File

@ -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<Operation> {
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 {
}
(<Bytecode>ops).getOperation = function(offset: number): Operation {
if (offset >= bytes.length) {
return {
opcode: Opcode.from("STOP"),
offset: offset,
length: 1
};
}
return (offsets[offset] || null);
};
(<Bytecode>ops).getByte = function(offset: number): number {
if (offset >= bytes.length) {
return 0x00;
}
return bytes[offset];
};
(<Bytecode>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);
};
(<Bytecode>ops).byteLength = bytes.length;
return (<Bytecode>ops);
}

View File

@ -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" },