@@ -48,6 +48,9 @@ public Value interpret(ExecutionStack execStack, Frame frame) {
4848 if (retInst .value () instanceof Operand .ConstantOperand constantOperand ) {
4949 execStack .stack [base ] = new Value .IntegerValue (constantOperand .value );
5050 }
51+ else if (retInst .value () instanceof Operand .NullConstantOperand ) {
52+ execStack .stack [base ] = new Value .NullValue ();
53+ }
5154 else if (retInst .value () instanceof Operand .RegisterOperand registerOperand ) {
5255 execStack .stack [base ] = execStack .stack [base +registerOperand .frameSlot ()];
5356 }
@@ -63,6 +66,9 @@ else if (retInst.value() instanceof Operand.RegisterOperand registerOperand) {
6366 else if (moveInst .from () instanceof Operand .ConstantOperand constantOperand ) {
6467 execStack .stack [base + toReg .frameSlot ()] = new Value .IntegerValue (constantOperand .value );
6568 }
69+ else if (moveInst .from () instanceof Operand .NullConstantOperand ) {
70+ execStack .stack [base + toReg .frameSlot ()] = new Value .NullValue ();
71+ }
6672 else throw new IllegalStateException ();
6773 }
6874 else throw new IllegalStateException ();
@@ -107,6 +113,9 @@ else if (cbrInst.condition() instanceof Operand.ConstantOperand constantOperand)
107113 else if (arg instanceof Operand .ConstantOperand constantOperand ) {
108114 execStack .stack [base + reg ] = new Value .IntegerValue (constantOperand .value );
109115 }
116+ else if (arg instanceof Operand .NullConstantOperand ) {
117+ execStack .stack [base + reg ] = new Value .NullValue ();
118+ }
110119 reg += 1 ;
111120 }
112121 // Call function
@@ -135,31 +144,60 @@ else if (arg instanceof Operand.ConstantOperand constantOperand) {
135144 case Instruction .Binary binaryInst -> {
136145 long x , y ;
137146 long value = 0 ;
138- if (binaryInst .left () instanceof Operand .ConstantOperand constant )
139- x = constant .value ;
140- else if (binaryInst .left () instanceof Operand .RegisterOperand registerOperand )
141- x = ((Value .IntegerValue ) execStack .stack [base + registerOperand .frameSlot ()]).value ;
142- else throw new IllegalStateException ();
143- if (binaryInst .right () instanceof Operand .ConstantOperand constant )
144- y = constant .value ;
145- else if (binaryInst .right () instanceof Operand .RegisterOperand registerOperand )
146- y = ((Value .IntegerValue ) execStack .stack [base + registerOperand .frameSlot ()]).value ;
147- else throw new IllegalStateException ();
148- switch (binaryInst .binOp ) {
149- case "+" : value = x + y ; break ;
150- case "-" : value = x - y ; break ;
151- case "*" : value = x * y ; break ;
152- case "/" : value = x / y ; break ;
153- case "%" : value = x % y ; break ;
154- case "==" : value = x == y ? 1 : 0 ; break ;
155- case "!=" : value = x != y ? 1 : 0 ; break ;
156- case "<" : value = x < y ? 1 : 0 ; break ;
157- case ">" : value = x > y ? 1 : 0 ; break ;
158- case "<=" : value = x <= y ? 1 : 0 ; break ;
159- case ">=" : value = x <= y ? 1 : 0 ; break ;
160- default : throw new IllegalStateException ();
147+ boolean intOp = true ;
148+ if (binaryInst .binOp .equals ("==" ) || binaryInst .binOp .equals ("!=" )) {
149+ Operand .RegisterOperand nonNullLitOperand = null ;
150+ if (binaryInst .left () instanceof Operand .NullConstantOperand ) {
151+ nonNullLitOperand = (Operand .RegisterOperand )binaryInst .right ();
152+ }
153+ else if (binaryInst .right () instanceof Operand .NullConstantOperand ) {
154+ nonNullLitOperand = (Operand .RegisterOperand )binaryInst .left ();
155+ }
156+ if (nonNullLitOperand != null ) {
157+ intOp = false ;
158+ Value otherValue = execStack .stack [base + nonNullLitOperand .frameSlot ()];
159+ switch (binaryInst .binOp ) {
160+ case "==" : {
161+ value = otherValue instanceof Value .NullValue ? 1 : 0 ;
162+ break ;
163+ }
164+ case "!=" : {
165+ value = otherValue instanceof Value .NullValue ? 0 : 1 ;
166+ break ;
167+ }
168+ default :
169+ throw new IllegalStateException ();
170+ }
171+ execStack .stack [base + binaryInst .result ().frameSlot ()] = new Value .IntegerValue (value );
172+ }
173+ }
174+ if (intOp ) {
175+ if (binaryInst .left () instanceof Operand .ConstantOperand constant )
176+ x = constant .value ;
177+ else if (binaryInst .left () instanceof Operand .RegisterOperand registerOperand )
178+ x = ((Value .IntegerValue ) execStack .stack [base + registerOperand .frameSlot ()]).value ;
179+ else throw new IllegalStateException ();
180+ if (binaryInst .right () instanceof Operand .ConstantOperand constant )
181+ y = constant .value ;
182+ else if (binaryInst .right () instanceof Operand .RegisterOperand registerOperand )
183+ y = ((Value .IntegerValue ) execStack .stack [base + registerOperand .frameSlot ()]).value ;
184+ else throw new IllegalStateException ();
185+ switch (binaryInst .binOp ) {
186+ case "+" : value = x + y ; break ;
187+ case "-" : value = x - y ; break ;
188+ case "*" : value = x * y ; break ;
189+ case "/" : value = x / y ; break ;
190+ case "%" : value = x % y ; break ;
191+ case "==" : value = x == y ? 1 : 0 ; break ;
192+ case "!=" : value = x != y ? 1 : 0 ; break ;
193+ case "<" : value = x < y ? 1 : 0 ; break ;
194+ case ">" : value = x > y ? 1 : 0 ; break ;
195+ case "<=" : value = x <= y ? 1 : 0 ; break ;
196+ case ">=" : value = x <= y ? 1 : 0 ; break ;
197+ default : throw new IllegalStateException ();
198+ }
199+ execStack .stack [base + binaryInst .result ().frameSlot ()] = new Value .IntegerValue (value );
161200 }
162- execStack .stack [base + binaryInst .result ().frameSlot ()] = new Value .IntegerValue (value );
163201 }
164202 case Instruction .NewArray newArrayInst -> {
165203 execStack .stack [base + newArrayInst .destOperand ().frameSlot ()] = new Value .ArrayValue (newArrayInst .type );
@@ -172,6 +210,9 @@ else if (binaryInst.right() instanceof Operand.RegisterOperand registerOperand)
172210 if (arrayAppendInst .value () instanceof Operand .ConstantOperand constant ) {
173211 arrayValue .values .add (new Value .IntegerValue (constant .value ));
174212 }
213+ else if (arrayAppendInst .value () instanceof Operand .NullConstantOperand ) {
214+ arrayValue .values .add (new Value .NullValue ());
215+ }
175216 else if (arrayAppendInst .value () instanceof Operand .RegisterOperand registerOperand ) {
176217 arrayValue .values .add (execStack .stack [base + registerOperand .frameSlot ()]);
177218 }
@@ -193,6 +234,9 @@ else if (arrayStoreInst.indexOperand() instanceof Operand.RegisterOperand regist
193234 if (arrayStoreInst .sourceOperand () instanceof Operand .ConstantOperand constantOperand ) {
194235 value = new Value .IntegerValue (constantOperand .value );
195236 }
237+ else if (arrayStoreInst .sourceOperand () instanceof Operand .NullConstantOperand ) {
238+ value = new Value .NullValue ();
239+ }
196240 else if (arrayStoreInst .sourceOperand () instanceof Operand .RegisterOperand registerOperand ) {
197241 value = execStack .stack [base + registerOperand .frameSlot ()];
198242 }
@@ -221,6 +265,9 @@ else if (arrayLoadInst.indexOperand() instanceof Operand.RegisterOperand registe
221265 if (setFieldInst .sourceOperand () instanceof Operand .ConstantOperand constant ) {
222266 value = new Value .IntegerValue (constant .value );
223267 }
268+ else if (setFieldInst .sourceOperand () instanceof Operand .NullConstantOperand ) {
269+ value = new Value .NullValue ();
270+ }
224271 else if (setFieldInst .sourceOperand () instanceof Operand .RegisterOperand registerOperand ) {
225272 value = execStack .stack [base + registerOperand .frameSlot ()];
226273 }
0 commit comments