Skip to content

Commit

Permalink
Added memory-like support and new opcodes to asm.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 4, 2020
1 parent 83db8a6 commit 6fd3bb6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/asm/src.ts/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/asm/src.ts/opcodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down

0 comments on commit 6fd3bb6

Please sign in to comment.