1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
10 abstract class JBooleanBinaryExpression extends JBinaryExpression {
11
19
20 protected JBooleanBinaryExpression(int line, String operator, JExpression lhs,
21 JExpression rhs) {
22 super(line, operator, lhs, rhs);
23 }
24
25
28 public void codegen(CLEmitter output) {
29 String falseLabel = output.createLabel();
30 String trueLabel = output.createLabel();
31 this.codegen(output, falseLabel, false);
32 output.addNoArgInstruction(ICONST_1); output.addBranchInstruction(GOTO, trueLabel);
34 output.addLabel(falseLabel);
35 output.addNoArgInstruction(ICONST_0); output.addLabel(trueLabel);
37 }
38 }
39
40
43 class JEqualOp extends JBooleanBinaryExpression {
44
51
52 public JEqualOp(int line, JExpression lhs, JExpression rhs) {
53 super(line, "==", lhs, rhs);
54 }
55
56
59 public JExpression analyze(Context context) {
60 lhs = (JExpression) lhs.analyze(context);
61 rhs = (JExpression) rhs.analyze(context);
62 lhs.type().mustMatchExpected(line(), rhs.type());
63 type = Type.BOOLEAN;
64 return this;
65 }
66
67
70 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
71 lhs.codegen(output);
72 rhs.codegen(output);
73 if (lhs.type().isReference()) {
74 output.addBranchInstruction(onTrue ? IF_ACMPEQ : IF_ACMPNE, targetLabel);
75 } else {
76 output.addBranchInstruction(onTrue ? IF_ICMPEQ : IF_ICMPNE, targetLabel);
77 }
78 }
79 }
80
81
84 class JLogicalAndOp extends JBooleanBinaryExpression {
85
92 public JLogicalAndOp(int line, JExpression lhs, JExpression rhs) {
93 super(line, "&&", lhs, rhs);
94 }
95
96
99 public JExpression analyze(Context context) {
100 lhs = (JExpression) lhs.analyze(context);
101 rhs = (JExpression) rhs.analyze(context);
102 lhs.type().mustMatchExpected(line(), Type.BOOLEAN);
103 rhs.type().mustMatchExpected(line(), Type.BOOLEAN);
104 type = Type.BOOLEAN;
105 return this;
106 }
107
108
111 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
112 if (onTrue) {
113 String falseLabel = output.createLabel();
114 lhs.codegen(output, falseLabel, false);
115 rhs.codegen(output, targetLabel, true);
116 output.addLabel(falseLabel);
117 } else {
118 lhs.codegen(output, targetLabel, false);
119 rhs.codegen(output, targetLabel, false);
120 }
121 }
122 }
123
124
127 class JLogicalOrOp extends JBooleanBinaryExpression {
128
135 public JLogicalOrOp(int line, JExpression lhs, JExpression rhs) {
136 super(line, "||", lhs, rhs);
137 }
138
139
142 public JExpression analyze(Context context) {
143 return this;
145 }
146
147
150 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
151 }
153 }
154
155
158 class JNotEqualOp extends JBooleanBinaryExpression {
159
166
167 public JNotEqualOp(int line, JExpression lhs, JExpression rhs) {
168 super(line, "!=", lhs, rhs);
169 }
170
171
174 public JExpression analyze(Context context) {
175 return this;
177 }
178
179
182 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
183 }
185 }
186