Archived
Switch to generic visitor pattern.
This commit is contained in:
@@ -21,13 +21,6 @@ import java.io.IOException;
|
||||
|
||||
public class Instruction {
|
||||
|
||||
public Instruction() {
|
||||
op = 0;
|
||||
r = 0;
|
||||
n = 0;
|
||||
d = 0;
|
||||
}
|
||||
|
||||
// Java has no type synonyms, so the following representations are
|
||||
// assumed:
|
||||
//
|
||||
@@ -37,26 +30,36 @@ public class Instruction {
|
||||
// Operand = -32767..+32767; {16 bits signed}
|
||||
|
||||
// Represents TAM instructions.
|
||||
public int op; // OpCode
|
||||
public int r; // RegisterNumber
|
||||
public int n; // Length
|
||||
public int d; // Operand
|
||||
final OpCode opCode;
|
||||
final Register register;
|
||||
final int length;
|
||||
int operand; // Not final to allow for patching jump address
|
||||
|
||||
public Instruction(OpCode opcode, Register register, int length, int operand) {
|
||||
this.opCode = opcode;
|
||||
this.register = register;
|
||||
this.length = length;
|
||||
this.operand = operand;
|
||||
}
|
||||
|
||||
public void setOperand(int operand) {
|
||||
this.operand = operand;
|
||||
}
|
||||
|
||||
public void write(DataOutputStream output) throws IOException {
|
||||
output.writeInt(op);
|
||||
output.writeInt(r);
|
||||
output.writeInt(n);
|
||||
output.writeInt(d);
|
||||
output.writeInt(opCode.ordinal());
|
||||
output.writeInt(register.ordinal());
|
||||
output.writeInt(length);
|
||||
output.writeInt(operand);
|
||||
}
|
||||
|
||||
public static Instruction read(DataInputStream input) throws IOException {
|
||||
Instruction inst = new Instruction();
|
||||
try {
|
||||
inst.op = input.readInt();
|
||||
inst.r = input.readInt();
|
||||
inst.n = input.readInt();
|
||||
inst.d = input.readInt();
|
||||
return inst;
|
||||
var opCode = OpCode.values()[input.readInt()];
|
||||
var register = Register.values()[input.readInt()];
|
||||
var length = input.readInt();
|
||||
var operand = input.readInt();
|
||||
return new Instruction(opCode, register, length, operand);
|
||||
} catch (EOFException s) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -31,10 +31,6 @@ public final class Machine {
|
||||
|
||||
// INSTRUCTIONS
|
||||
|
||||
// Operation codes
|
||||
public final static int LOADop = 0, LOADAop = 1, LOADIop = 2, LOADLop = 3, STOREop = 4, STOREIop = 5, CALLop = 6,
|
||||
CALLIop = 7, RETURNop = 8, PUSHop = 10, POPop = 11, JUMPop = 12, JUMPIop = 13, JUMPIFop = 14, HALTop = 15;
|
||||
|
||||
// CODE STORE
|
||||
|
||||
public static Instruction[] code = new Instruction[1024];
|
||||
@@ -46,9 +42,6 @@ public final class Machine {
|
||||
|
||||
// REGISTER NUMBERS
|
||||
|
||||
public final static int CBr = 0, CTr = 1, PBr = 2, PTr = 3, SBr = 4, STr = 5, HBr = 6, HTr = 7, LBr = 8,
|
||||
L1r = LBr + 1, L2r = LBr + 2, L3r = LBr + 3, L4r = LBr + 4, L5r = LBr + 5, L6r = LBr + 6, CPr = 15;
|
||||
|
||||
// DATA REPRESENTATION
|
||||
|
||||
public final static int booleanSize = 1, characterSize = 1, integerSize = 1, addressSize = 1,
|
||||
@@ -58,14 +51,4 @@ public final class Machine {
|
||||
|
||||
falseRep = 0, trueRep = 1, maxintRep = 32767;
|
||||
|
||||
// ADDRESSES OF PRIMITIVE ROUTINES
|
||||
|
||||
public final static int idDisplacement = 1, notDisplacement = 2, andDisplacement = 3, orDisplacement = 4,
|
||||
succDisplacement = 5, predDisplacement = 6, negDisplacement = 7, addDisplacement = 8, subDisplacement = 9,
|
||||
multDisplacement = 10, divDisplacement = 11, modDisplacement = 12, ltDisplacement = 13, leDisplacement = 14,
|
||||
geDisplacement = 15, gtDisplacement = 16, eqDisplacement = 17, neDisplacement = 18, eolDisplacement = 19,
|
||||
eofDisplacement = 20, getDisplacement = 21, putDisplacement = 22, geteolDisplacement = 23,
|
||||
puteolDisplacement = 24, getintDisplacement = 25, putintDisplacement = 26, newDisplacement = 27,
|
||||
disposeDisplacement = 28;
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package Triangle.AbstractMachine;
|
||||
|
||||
public enum OpCode {
|
||||
LOAD, LOADA, LOADI, LOADL, STORE, STOREI, CALL, CALLI, RETURN, PUSH, POP, JUMP, JUMPI, JUMPIF, HALT
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package Triangle.AbstractMachine;
|
||||
|
||||
public enum Primitive {
|
||||
ID, NOT, AND, OR, SUCC, PRED, NEG, ADD, SUB, MULT, DIV, MOD, LT, LE, GE, GT, EQ, NE, EOL, EOF, GET, PUT, GETEOL,
|
||||
PUTEOL, GETINT, PUTINT, NEW, DISPOSE
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package Triangle.AbstractMachine;
|
||||
|
||||
public enum Register {
|
||||
CB, CT, PB, PT, SB, ST, HB, HT, LB, L1, L2, L3, L4, L5, L6, CP
|
||||
}
|
||||
Reference in New Issue
Block a user