Created
November 9, 2009 01:22
-
-
Save WardCunningham/229608 to your computer and use it in GitHub Desktop.
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
// Copyright (c) 2003 Cunningham & Cunningham, Inc. | |
// Read license.txt in this directory. | |
package Code; | |
public class Code { | |
static protected StringBuffer emited; | |
public void run (State s) {}; | |
public String toString () {emited = new StringBuffer(); emit(); return emited.toString();} | |
public Code op() {return this;} | |
void emit () {}; | |
void emit (String op, Reg d, int i) {emit (op, d+","+i);} | |
void emit (String op, Reg d, Reg s) {emit (op, d+","+s);} | |
void emit (String op, Reg d) {emit (op+" "+d);} | |
void emit (String op, String a) {emit (op+" "+a);} | |
void emit (String op) {emited.append(op);} | |
// Operatons //////////////////////////////// | |
static public Code nop() { | |
return new Code () { | |
public void run(State s) { | |
s.ticks += 1; | |
} | |
public void emit() { | |
emit ("nop"); | |
} | |
}; | |
} | |
static public Code set(final Reg reg, final byte literal) { | |
return new Code () { | |
public void run(State state) { | |
reg.value = literal; | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("ldi", reg, literal); | |
} | |
}; | |
} | |
static public Code set(final Reg dest, final Reg source) { | |
return new Code () { | |
public void run(State state) { | |
dest.value = source.value; | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("mov", dest, source); | |
} | |
}; | |
} | |
static public Code out(final Reg dest, final Reg source) { | |
return new Code () { | |
public void run(State state) { | |
dest.value = source.value; | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("out", dest, source); | |
} | |
}; | |
} | |
static public Code add(final Reg reg, final byte literal) { | |
return new Code () { | |
public void run(State state) { | |
reg.value += literal; | |
state.result(reg.value); | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("addi", reg, literal); | |
} | |
}; | |
} | |
static public Code add (final Reg dest, final Reg source) { | |
return new Code () { | |
public void run(State state) { | |
dest.value += source.value; | |
state.result(dest.value); | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("add", dest, source); | |
} | |
}; | |
} | |
static public Code asr (final Reg dest) { | |
return new Code () { | |
public void run(State state) { | |
dest.value /= 2; | |
state.ticks += 1; | |
} | |
public void emit() { | |
emit("asr", dest); | |
} | |
}; | |
} | |
static public Code rjmp (final Label target) { | |
return new Code () { | |
public void run(State state) { | |
state.jump(target); | |
state.ticks += 2; | |
} | |
public void emit() { | |
emit("rjmp", target.label); | |
} | |
}; | |
} | |
static public Code brpl (final Label target) { | |
return new Code () { | |
public void run(State state) { | |
if (!state.negative) { | |
state.jump(target); | |
state.ticks += 2; | |
} else { | |
state.ticks += 1; | |
} | |
} | |
public void emit() { | |
emit("brpl", target.label); | |
} | |
}; | |
} | |
static public Code seq (final Code a, final Code b) { | |
return new Code () { | |
public void run(State state) { | |
state.queue(b); // will run this after a completes | |
a.run(state); | |
} | |
public void emit() { | |
a.emit(); | |
emit("\n"); | |
b.emit(); | |
} | |
public Code op() { | |
return a; | |
} | |
}; | |
} | |
static public Code seq (Code a, Code b, Code c) { | |
return Code.seq (a, Code.seq (b, c)); | |
} | |
static public Code seq (Code a, Code b, Code c, Code d) { | |
return Code.seq (Code.seq (a, b), Code.seq (c, d)); | |
} | |
static public Code seq (Code a, Code b, Code c, Code d, Code e) { | |
return Code.seq (Code.seq(a, b), Code.seq (c, d, e)); | |
} | |
static public Code seq (Code a, Code b, Code c, Code d, Code e, Code f) { | |
return Code.seq (Code.seq(a, b), Code.seq (c, d), Code.seq (e, f)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment