-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
313 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Ethereum} from "../../src/index"; | ||
|
||
const asm = Ethereum(); | ||
|
||
asm.code(_ => { | ||
_('ADD'); | ||
_('MUL'); | ||
_('PUSH1', [1]); | ||
_('add'); | ||
}); | ||
|
||
console.log(String(asm)); | ||
console.log(asm.compile()); | ||
console.log(String(asm)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import Asm from "./Asm"; | ||
import PresetX64 from "./presets/PresetX64"; | ||
import PresetEthereum from "./presets/PresetEthereum"; | ||
|
||
export const X64 = (opts?) => new Asm(PresetX64(opts)); | ||
export const Ethereum = (opts?) => new Asm(PresetEthereum(opts)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function formatOctet(octet) { | ||
const neg = octet < 0 ? '-' : ''; | ||
|
||
octet = Math.abs(octet); | ||
|
||
return octet <= 0xF ? neg + '0x0' + octet.toString(16).toUpperCase() : neg + '0x' + octet.toString(16).toUpperCase(); | ||
} | ||
|
||
export default formatOctet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import formatOctet from "./formatOctet"; | ||
|
||
function formatOctets (octets: number[] | Buffer | Uint8Array, maxLength = 200) { | ||
if (octets.length < maxLength) { | ||
const out = []; | ||
|
||
for (let i = 0; i < octets.length; i++) | ||
out.push(formatOctet(octets[i])); | ||
|
||
return out.join(', '); | ||
} else | ||
return `[${this.bytes()} bytes]`; | ||
} | ||
|
||
export default formatOctets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {ConstantEthereum} from "./operand"; | ||
import {IPushable} from "../../expression"; | ||
|
||
class ImmediateEthereum { | ||
constant: ConstantEthereum; | ||
|
||
constructor (constant: ConstantEthereum) { | ||
this.constant = constant; | ||
} | ||
|
||
write (bin: IPushable) { | ||
const {octets} = this.constant; | ||
|
||
for (let i = 0; i < octets.length; i++) | ||
bin.push(octets[i]); | ||
} | ||
} | ||
|
||
export default ImmediateEthereum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Mnemonic from "../../Mnemonic"; | ||
import {SIZE_ETHEREUM} from "./operand"; | ||
|
||
class MnemonicEthereum extends Mnemonic { | ||
operandSize: SIZE_ETHEREUM = SIZE_ETHEREUM.S0; | ||
|
||
getName () { | ||
return this.mnemonic; | ||
} | ||
} | ||
|
||
export default MnemonicEthereum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Plugin from "../Plugin"; | ||
import {opcodes} from './table'; | ||
import {InstructionEthereum} from "./instruction"; | ||
import MnemonicEthereum from "./MnemonicEthereum"; | ||
import {ConstantEthereum, SIZE_ETHEREUM, TOctetsEthereum} from "./operand"; | ||
import {Constant, Operand, Operands} from "../../operand"; | ||
|
||
class PluginEthereum extends Plugin { | ||
onAsm (asm) { | ||
asm.hooks.command.tap('PluginEthereum', (name, args) => { | ||
name = name.toUpperCase(); | ||
|
||
const opcode = opcodes[name]; | ||
|
||
if (typeof opcode === 'number') { | ||
if (name === 'PUSH') { | ||
this.push.apply(this, args); | ||
} else if ((name[0] === 'P') && (name.substr(0, 4) === 'PUSH')) { | ||
const size = parseInt(name.substr(4)); | ||
return this.pushX(args[0], size); | ||
} else { | ||
return this.mnemonic(name, opcode); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
mnemonic (name, opcode) { | ||
const instruction = new InstructionEthereum(new MnemonicEthereum(name, opcode)); | ||
|
||
return this.asm.insert(instruction); | ||
} | ||
|
||
pushX (octets: TOctetsEthereum, X = 1) { | ||
const name = 'PUSH' + X; | ||
const mnemonic = new MnemonicEthereum(name, opcodes[name]); | ||
|
||
mnemonic.operandSize = SIZE_ETHEREUM['S' + X]; | ||
|
||
const bytes = mnemonic.operandSize >> 3; | ||
const operands = new Operands([new ConstantEthereum(octets)]); | ||
const instruction = new InstructionEthereum(mnemonic, operands); | ||
|
||
return this.asm.insert(instruction); | ||
} | ||
|
||
push (data, size = 8) { | ||
|
||
} | ||
|
||
operands (uiOperands) { | ||
const operands = new Operands(); | ||
} | ||
} | ||
|
||
export default PluginEthereum; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
https://github.com/zack-bitcoin/ethereum-assembly | ||
|
||
``` | ||
3 3 mul | ||
60 03 PUSH1 3 | ||
60 03 PUSH1 3 | ||
02 MUL | ||
foo jump | ||
60 0f PUSH1 0x0F ($15) | ||
56 JUMP | ||
27 0 0 log1 \this code wont be run. we jumped over it. | ||
60 1b PUSH1 0x1B ($27) | ||
60 00 PUSH1 0 | ||
60 00 PUSH1 0 | ||
a1 LOG 1 | ||
5b JUMPDEST | ||
61 01 a5 PUSH2 0x01A5 ($421) | ||
60 00 PUSH1 0 | ||
60 00 PUSH1 0 | ||
jumpdest foo | ||
420 0 0 log1 \this code runs 9 times (from 3 and 3 we multiplied above) | ||
a1 LOG 1 | ||
1 swap1 sub \run 9 times | ||
60 01 PUSH1 0x01 | ||
90 SWAP1 | ||
03 SUB | ||
dup iszero iszero foo jumpi \this jumps us back to foo | ||
80 DUP1 | ||
15 ISZERO | ||
15 ISZERO | ||
60 0f PUSH1 0x0F ($15) | ||
57 JUMPI | ||
06 0 0 log1 | ||
60 06 PUSH1 0x06 | ||
60 00 PUSH1 0x00 | ||
60 00 PUSH1 0x00 | ||
a1 LOG1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {Instruction} from "../../instruction"; | ||
import {IPushable} from "../../expression"; | ||
import {Operands} from "../../operand"; | ||
import MnemonicEthereum from "./MnemonicEthereum"; | ||
import ImmediateEthereum from "./ImmediateEthereum"; | ||
import {ConstantEthereum} from "./operand"; | ||
|
||
export class InstructionEthereum extends Instruction { | ||
mnemonic: MnemonicEthereum; | ||
length = 1; | ||
lengthMax = 1; | ||
|
||
immediate: ImmediateEthereum = null; | ||
|
||
constructor (mnemonic: MnemonicEthereum, ops: Operands = null, opts?: object) { | ||
super(ops, opts); | ||
|
||
this.mnemonic = mnemonic; | ||
} | ||
|
||
write (bin: IPushable) { | ||
bin.push(this.mnemonic.opcode); | ||
|
||
if (this.immediate) | ||
this.immediate.write(bin); | ||
} | ||
|
||
build (): this { | ||
super.build(); | ||
|
||
|
||
if (this.ops && this.ops.list.length) { | ||
const constant = this.ops.getFirstOfClass(ConstantEthereum) as ConstantEthereum; | ||
|
||
this.length += constant.octets.length; | ||
this.lengthMax += constant.octets.length; | ||
|
||
if (constant) | ||
this.immediate = new ImmediateEthereum(constant); | ||
} | ||
|
||
return this; | ||
} | ||
} |
Oops, something went wrong.