Renaming opcodes and implementing toStrings for all the instructions

This commit is contained in:
Imbus 2024-04-16 15:34:30 +02:00
parent 4b11ef49e7
commit 209050446d
8 changed files with 38 additions and 26 deletions

View file

@ -7,17 +7,5 @@ public abstract class Instruction {
protected abstract Object[] operands(); protected abstract Object[] operands();
@Override public abstract String toString();
public String toString() {
// Buffer to hold our formatted instruction
StringBuilder sb = new StringBuilder();
sb.append(this.opcode());
for (Object operand : this.operands()) {
sb.append(" ");
sb.append(operand.toString());
}
return sb.toString();
}
} }

View file

@ -20,7 +20,11 @@ public class Add extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "add"; return "ADD";
}
public String toString() {
return String.format("%s [%s] %s [%s]", this.opcode(), this.src, this.imm, this.dest);
} }
protected Object[] operands() { protected Object[] operands() {

View file

@ -18,10 +18,14 @@ public class Copy extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "copy"; return "CPY";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] {this.imm, this.dest}; return new Object[] {this.imm, this.dest};
} }
public String toString() {
return String.format("%s %s [%s]", this.opcode(), this.imm, this.dest);
}
} }

View file

@ -10,15 +10,15 @@ public class Halt extends Instruction {
PC.halt(); PC.halt();
} }
public String toString() {
return "HALT";
}
protected String opcode() { protected String opcode() {
return "halt"; return "HLT";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] {}; return new Object[] {};
} }
public String toString() {
return this.opcode();
}
} }

View file

@ -15,10 +15,14 @@ public class Jump extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "jump"; return "JMP";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] {this.index}; return new Object[] {this.index};
} }
public String toString() {
return String.format("%s %s", this.opcode(), this.index);
}
} }

View file

@ -23,10 +23,14 @@ public class JumpEq extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "jumpeq"; return "JEQ";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] {this.index, this.src, this.imm}; return new Object[] {this.index, this.src, this.imm};
} }
public String toString() {
return String.format("%s %s [%s] %s", this.opcode(), this.index, this.src, this.imm);
}
} }

View file

@ -20,10 +20,14 @@ public class Mul extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "mul"; return "MUL";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] { this.src1, this.src2, this.dest }; return new Object[] { this.src1, this.src2, this.dest };
} }
public String toString() {
return String.format("%s [%s] [%s] [%s]", this.opcode(), this.src1, this.src2, this.dest);
}
} }

View file

@ -16,10 +16,14 @@ public class Print extends Instruction {
} }
protected String opcode() { protected String opcode() {
return "print"; return "PRT";
} }
protected Object[] operands() { protected Object[] operands() {
return new Object[] {this.address}; return new Object[] { this.address };
}
public String toString() {
return String.format("%s [%s]", this.opcode(), this.address);
} }
} }