Skip to content

Commit

Permalink
feat: start labels
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 3, 2018
1 parent 14ef911 commit 4ecc4e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
4 changes: 4 additions & 0 deletions demo/ethereum/label.ts
Expand Up @@ -4,7 +4,11 @@ const asm = Ethereum();

asm.code(_ => {
_('PUSH', 3);

const label = _('label', 'multiply');
_('MUL', 3);

_('JUMP', label);
});

console.log(String(asm));
31 changes: 18 additions & 13 deletions src/operand.ts
Expand Up @@ -515,6 +515,7 @@ export class Relative extends Variable {
canEvaluate(owner) {
if(!owner || (owner.offset === -1)) return false;
if(this.target.offset === -1) return false;

return true;
}

Expand All @@ -528,8 +529,9 @@ export class Relative extends Variable {
}

canHoldMaxOffset(owner: Expression) {
var value = this.evaluatePreliminary(owner);
var size = this.signed ? Constant.sizeClass(value) : Constant.sizeClassUnsigned(value);
const value = this.evaluatePreliminary(owner);
const size = this.signed ? Constant.sizeClass(value) : Constant.sizeClassUnsigned(value);

return size <= this.size;
}

Expand Down Expand Up @@ -565,24 +567,27 @@ export class Relative extends Variable {
}

toString() {
var result = '';
if(this.result !== null) {
let result = '';

if (this.result !== null)
result = ' = ' + this.result;
}

if(this.target instanceof require('./Label').default) {
var lbl = this.target as Label;
var off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';
if (this.target instanceof require('./Label').default) {
const lbl = this.target as Label;
const off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';

return `<${lbl.getName()}${off}${result}>`;
} else if(this.target.asm) {
} else if (this.target.asm) {
// var lbl = this.target.asm.getStartLabel();
const lbl = this.target.asm.expressions[0] as any as Label;
var expr = `+[${this.target.index}]`;
var off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';
const expr = `+[${this.target.index}]`;
const off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';

return `<${lbl.getName()}${expr}${off}${result}>`;
} else {
var expr = `+[${this.target.index}]`;
var off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';
const expr = `+[${this.target.index}]`;
const off = this.offset ? '+' + (new Constant(this.offset)).toString() : '';

return `<${expr}${off}${result}>`;
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/plugins/ethereum/PluginEthereum.ts
Expand Up @@ -3,7 +3,8 @@ 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";
import {Constant, Operand, Operands, Relative} from "../../operand";
import {Expression} from "../../expression";

class PluginEthereum extends Plugin {
onAsm (asm) {
Expand All @@ -30,9 +31,13 @@ class PluginEthereum extends Plugin {
});
}

mnemonic (name, opcode, octets: number[]) {
if (octets && octets.length) {
this.push(octets);
mnemonic (name, opcode, args: any[]) {
if (args && args.length) {
if (args[0] instanceof Expression) {
console.log('A', args[0].rel());
} else if (typeof args[0] === 'number') {
this.push(args);
}
}

const instruction = new InstructionEthereum(new MnemonicEthereum(name, opcode));
Expand Down Expand Up @@ -71,6 +76,10 @@ class PluginEthereum extends Plugin {
}
} while (true);
}

pushRelative (relative: Relative) {

}
}

export default PluginEthereum;

0 comments on commit 4ecc4e1

Please sign in to comment.