11package com .compilerprogramming .ezlang .compiler ;
22
3+ import com .compilerprogramming .ezlang .exceptions .CompilerException ;
34import com .compilerprogramming .ezlang .types .Type ;
45
56import java .util .ArrayList ;
@@ -30,11 +31,10 @@ public abstract class Instruction {
3031 protected Operand [] uses ;
3132 public BasicBlock block ;
3233
33- protected Instruction (int opcode , Operand ... uses ) {
34+ protected Instruction (int opcode ) {
3435 this .opcode = opcode ;
3536 this .def = null ;
36- this .uses = new Operand [uses .length ];
37- System .arraycopy (uses , 0 , this .uses , 0 , uses .length );
37+ this .uses = new Operand [0 ];
3838 }
3939 protected Instruction (int opcode , Operand .RegisterOperand def , Operand ... uses ) {
4040 this .opcode = opcode ;
@@ -49,9 +49,23 @@ public String toString() {
4949 return toStr (new StringBuilder ()).toString ();
5050 }
5151
52+ /**
53+ * Does this instruction define a var?
54+ */
5255 public boolean definesVar () { return def != null ; }
56+ /**
57+ * If the instruction defines a var then return the Register else null
58+ */
5359 public Register def () { return def != null ? def .reg : null ; }
60+ public void replaceDef (Register newDef ) {
61+ if (def == null ) throw new IllegalStateException ();
62+ def = def .copy (newDef );
63+ }
5464
65+ /**
66+ * Get the registers used by this instruction. Non register operands are
67+ * not included.
68+ */
5569 public List <Register > uses () {
5670 List <Register > useList = null ;
5771 for (int i = 0 ; i < uses .length ; i ++) {
@@ -64,10 +78,14 @@ public List<Register> uses() {
6478 if (useList == null ) useList = Collections .emptyList ();
6579 return useList ;
6680 }
67- public void replaceDef (Register newReg ) {
68- if (def == null ) throw new IllegalStateException ();
69- def = def .copy (newReg );
70- }
81+
82+ /**
83+ * Replaces existing register uses with new ones. This api is not great
84+ * as it requires user to supply registers in the same order as returned by
85+ * the method {@link #uses()}.
86+ *
87+ * FIXME replace this with a better api
88+ */
7189 public void replaceUses (Register [] newUses ) {
7290 int j = 0 ;
7391 for (int i = 0 ; i < uses .length ; i ++) {
@@ -76,7 +94,14 @@ public void replaceUses(Register[] newUses) {
7694 uses [i ] = registerOperand .copy (newUses [j ++]);
7795 }
7896 }
97+ // Sanity check that we replaced the full set of registers
98+ if (j != newUses .length )
99+ throw new CompilerException ("Error - supplied registers do not replace all uses" );
79100 }
101+
102+ /**
103+ * Replaces all occurrences of source with target in the uses list of the instruction
104+ */
80105 public boolean replaceUse (Register source , Register target ) {
81106 boolean replaced = false ;
82107 for (int i = 0 ; i < uses .length ; i ++) {
@@ -88,23 +113,18 @@ public boolean replaceUse(Register source, Register target) {
88113 }
89114 return replaced ;
90115 }
91- public void replaceWithConstant (Register register , Operand .ConstantOperand constantOperand ) {
116+
117+ /**
118+ * Replaces all uses of given register with the constant operand
119+ */
120+ public void replaceUseWithConstant (Register register , Operand .ConstantOperand constantOperand ) {
92121 for (int i = 0 ; i < uses .length ; i ++) {
93122 Operand operand = uses [i ];
94123 if (operand != null && operand instanceof Operand .RegisterOperand registerOperand && registerOperand .reg .id == register .id ) {
95124 uses [i ] = constantOperand ;
96125 }
97126 }
98127 }
99- public static class NoOp extends Instruction {
100- public NoOp () {
101- super (I_NOOP );
102- }
103- @ Override
104- public StringBuilder toStr (StringBuilder sb ) {
105- return sb .append ("noop" );
106- }
107- }
108128
109129 public static class Move extends Instruction {
110130 public Move (Operand from , Operand to ) {
@@ -384,7 +404,7 @@ public Register def() {
384404 throw new UnsupportedOperationException ();
385405 }
386406 @ Override
387- public void replaceDef (Register newReg ) {
407+ public void replaceDef (Register newDef ) {
388408 throw new UnsupportedOperationException ();
389409 }
390410 @ Override
0 commit comments