1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
package com.seaofnodes.simple.parser;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.parser.Parser.ParseException;
import com.seaofnodes.simple.type.*;
import static com.seaofnodes.simple.util.Utils.TODO;
/** Recursive-descent expression precedence, from assignment down to primaries. */
final class Expressions {
private final Parser P;
Expressions(Parser P) { this.P = P; }
/** Parse: [name '='] expr
*/
Node parseAssignment() {
int old = P.pos();
String name = P._lexer.matchId();
// Just a plain expression, no assignment.
// Distinguish `var==expr` from `var=expr`
if( name==null || Parser.KEYWORDS.contains(name) || !P.matchOpx('=','=') )
{ P.pos(old); return parseExpression(); }
// Find variable to update
Var def = P._scope.lookup(name);
if( def==null )
throw P.error("Undefined name '" + name + "'");
if( def._extern!=0 ) // Parse the C binding as a memory lvalue
{ P.pos(old); return parseExpression(); }
P.references.checkAssignment(def);
return P.references.assign(def,parseAssignment());
}
/**
* Parse an expression of the form:
*
* <pre>
* expression : logicalExpression ('?' assignment (':' assignment)?)?
* </pre>
* @return an expression {@link Node}, never {@code null}
*/
private Node parseExpression() {
Node expr = parseLogical();
return P.match("?") ? P.controlflow.conditional(expr,":") : expr;
}
/**
* Parse a logical expression
*
* <pre>
* logicalExpression : bitwiseExpression (('&&' | '||') logicalExpression)?
* </pre>
* @return a bitwise expression {@link Node}, never {@code null}
*/
Node parseLogical() {
Node lhs = parseBitwise();
while (true) {
if( P.match("&&") ) lhs = P.controlflow.conditional(lhs, "&&");
else if( P.match("||") ) lhs = P.controlflow.conditional(lhs, "||");
else break;
}
return lhs;
}
private Node parseBitwise() {
Node lhs = parseEquality();
while( true ) {
if( P.matchOp('&') ) lhs = new AndNode(P.loc(),lhs,null);
else if( P.matchOp('|') ) lhs = new OrNode(P.loc(),lhs,null);
else if( P.match ("^") ) lhs = new XorNode(P.loc(),lhs,null);
else break;
lhs.setDef(2,parseEquality());
lhs = P.peep(lhs);
}
return lhs;
}
/**
* Parse an eq/ne expression
*/
private Node parseEquality() {
Node lhs = parseComparison();
while( true ) {
boolean eq = false;
if( P.match("==") ) eq = true;
else if( !P.match("!=") ) break;
lhs.keep();
Node rhs = parseComparison();
lhs = P.peep(new BoolNode.EQ(lhs.unkeep(),rhs));
if( !eq ) lhs = P.peep(new NotNode(lhs));
}
return lhs;
}
/**
* Parse an expression of the form:
*
* <pre>
* expr : shiftExpr < shiftExpr <= shiftExpr...
* expr : shiftExpr > shiftExpr >= shiftExpr...
* </pre>
* @return an comparator expression {@link Node}, never {@code null}
*/
private Node parseComparison() {
Node lhs = parseShift();
int dir = parseCompDir();
// No compare
if( dir==0 ) return lhs;
lhs.keep();
// Compare
Node rhs = parseShift().keep();
Node cmp = makeCompBool(dir,lhs.unkeep(),rhs).keep(); // Convert to a bool
// Stacked compares?
int dir0 = parseCompDir();
if( dir0 == 0 ) {
rhs.unkill();
return cmp.unkeep();
}
// Keep the middle operand separately from the boolean result.
// The next test compares adjacent operands, not that boolean.
Node ifNode = new IfNode(P.ctrl(), cmp.unkeep()).peephole();
Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole();
// False side does nothing but capture memory & side-effects
ScopeNode fail = P._scope.dup();
fail.ctrl(ifF);
// Loop over stacked compares. Each loop moves the old RHS to become
// the new LHS, so e.g. (a < b < c < d) compares (LHS:a < RHS:b) then
// compares (LHS:b < RHS:c) then (LHS:c < RHS:d), etc.
while( dir0 != 0 ) {
if( Math.abs(dir) != Math.abs(dir0) )
throw P.error("Mixing relational directions in a chained relational test");
// True side parses next arm of test
P.ctrl(ifT.unkeep());
lhs = rhs; // Reuse the previous RHS exactly once
rhs = parseShift().keep();
cmp = makeCompBool(dir0,lhs.unkeep(),rhs); // Convert to a bool
ifNode = new IfNode(P.ctrl(), cmp).peephole();
ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole();
// Merge result into the fail case
P.ctrl(ifF);
fail.mergeScopes(P._scope.dup(), P.loc()).peephole();
// Check next stacked compare
dir0 = parseCompDir();
}
rhs.unkill();
P.ctrl(ifT.unkeep());
RegionNode r = fail.mergeScopes(P._scope, P.loc()).init();
P._scope = fail;
return new PhiNode("", r,P.con(0),P.con(1)).peephole();
}
private int parseCompDir() {
if( P.match("<=") ) return -1; // Negative for "equals-or-less"
if( P.match(">=") ) return -2; // 1 for <, 2 for >
if( P.match("<" ) ) return 1; // Positive for just "less"
if( P.match(">" ) ) return 2;
return 0;
}
private Node makeCompBool( int dir, Node lhs, Node rhs ) {
if( Math.abs(dir)==2 )
{ Node tmp = lhs; lhs = rhs; rhs = tmp; }
Node cmp = dir < 0 ? new BoolNode.LE(lhs,rhs) : new BoolNode.LT(lhs,rhs);
return P.peep(cmp);
}
/**
* Parse an additive expression
*
* <pre>
* shiftExpr : additiveExpr (('<<' | '>>' | '>>>') additiveExpr)*
* </pre>
* @return a shift expression {@link Node}, never {@code null}
*/
private Node parseShift() {
Node lhs = parseAddition();
while( true ) {
if( P.match("<<") ) lhs = new ShlNode(P.loc(),lhs,null);
else if( P.match(">>>")) lhs = new ShrNode(P.loc(),lhs,null);
else if( P.match(">>") ) lhs = new SarNode(P.loc(),lhs,null);
else break;
lhs.setDef(2,parseAddition());
ParseException err;
if( (err = lhs.err()) != null )
throw err;
lhs = P.peep(lhs);
}
return lhs;
}
/**
* Parse an additive expression
*
* <pre>
* additiveExpr : multiplicativeExpr (('+' | '-') multiplicativeExpr)*
* </pre>
* @return an add expression {@link Node}, never {@code null}
*/
private Node parseAddition() {
Node lhs = parseMultiplication();
while( true ) {
if( P.match("+") ) lhs = new AddNode(lhs,null);
else if( P.match("-") ) lhs = new SubNode(lhs,null);
else break;
lhs.setDef(2,parseMultiplication());
lhs = P.peep(lhs);
}
return lhs;
}
/**
* Parse an multiplicativeExpr expression
*
* <pre>
* multiplicativeExpr : unaryExpr (('*' | '/') unaryExpr)*
* </pre>
* @return a multiply expression {@link Node}, never {@code null}
*/
private Node parseMultiplication() {
var lhs = parseUnary();
while( true ) {
if( P.match("*") ) lhs = new MulNode(lhs,null);
else if( P.match("/") ) lhs = new DivNode(lhs,null);
else break;
lhs.setDef(2,parseUnary());
lhs = P.peep(lhs);
}
return lhs;
}
/**
* Parse a unary minus expression.
*
* <pre>
* unaryExpr : ('-') unaryExpr | '!') unaryExpr | postfixExpr | primaryExpr | '--' Id | '++' Id
* </pre>
* @return a unary expression {@link Node}, never {@code null}
*/
private Node parseUnary() {
// Pre-dec/pre-inc
int old = P.pos();
if( P.match("--") || P.match("++") ) {
int delta = P._lexer.peek(-1)=='+' ? 1 : -1; // Inc vs Dec
int start = P.pos();
String name = P._lexer.matchId();
Var var = name==null ? null : P._scope.lookup(name);
if( var!=null && !(var.type() instanceof TypeMemPtr) ) {
if( var._extern==0 && var._final && !P._scope.canInit(var) )
throw P.error("Cannot reassign final '"+var._name+"'");
// Resolve fields just as a read or postfix update does. Updating
// their declaration's local slot would silently lose the Store.
P.pos(start);
return parsePrimary(delta);
}
// Reset, try again
P.pos(old);
}
if (P.match("-")) return P.peep(new MinusNode(parseUnary()));
if (P.match("!")) return P.peep(new NotNode(parseUnary()));
// Else be a primary expression
return parsePrimary();
}
/**
* Parse a primary expression:
*
* <pre>
* primaryExpr : integerLiteral | "string" | 'char' | true | false | null |
* new Type | '(' expression ')' | Id['++','--'] |
* type['.' Id]*
* </pre>
* @return a primary {@link Node}, never {@code null}
*/
private Node parsePrimary() { return parsePrimary(0); }
// pre is +/-1 for a prefix update of a known name, zero otherwise.
private Node parsePrimary(int pre) {
if( P._lexer.isNumber(P._lexer.peek()) ) return parseLiteral();
if( P._lexer.peek('"') ) return parsePostfix(P.allocations.newString(P._lexer.parseString()));
if( P.matchx("true" ) ) return P.con(1);
if( P.matchx("false") ) return P._code.ZERO;
if( P.matchx("null" ) ) return P.con(Type.NIL);
if( P.match ("'" ) ) return parseChar();
if( P.match ("(" ) ) return parsePostfix(P.require(parseAssignment(), ")"));
if( P.matchx("new" ) ) return parsePostfix(P.allocations.parseNew());
if( P.match ("{" ) ) return parsePostfix(P.require(P.functions.parseFunction(),"}"));
int pos = P.pos();
// Expect an identifier now
String id = P._lexer.matchId();
if( id == null || Parser.KEYWORDS.contains(id) )
throw P.errorSyntax("an identifier or expression");
int pos2 = P.pos();
return P.references.parseName(id,pre,pos,pos2);
}
// Check for assign-update, "x += e0".
// Returns "x + e0"; the caller assigns value.
// if ch is postfix, then lhs is kept and caller will unkeep and return it.
Node opAssign( char ch, Node lhs, Type t ) {
// RHS of the update.
lhs.keep(); // Alive across parseAssignment
Node rhs =
(byte)ch == 1 ? P.con( 1) : // var++
(byte)ch == -1 ? P.con(-1) : // var--
parseAssignment(); // var op= rhs
if( !Parser.postfix(ch) ) lhs.unkeep(); // Allow to die in next peep
// 4 cases:
// int + int ==>> narrow int
// int + flt ==>> error, caller must fail assigning flt into int
// flt + int ==>> use float op, wrap toFloat()
// flt + flt ==>> use float op
Node op = switch(ch) {
case 1, (char)-1,
'+' -> new AddNode(lhs,rhs);
case '-' -> new SubNode(lhs,rhs);
case '*' -> new MulNode(lhs,rhs);
case '/' -> new DivNode(lhs,rhs);
case '|' -> new OrNode(null,lhs,rhs);
case '&' -> new AndNode(null,lhs,rhs);
case '^' -> new XorNode(P.loc(),lhs,rhs);
case Lexer.SHL -> new ShlNode(P.loc(),lhs,rhs);
case Lexer.SAR -> new SarNode(P.loc(),lhs,rhs);
case Lexer.SHR -> new ShrNode(P.loc(),lhs,rhs);
default -> throw TODO();
};
// Convert to float ops, or narrow int types; error if not declared type.
// Also, if postfix LHS is still keep()
return P.conversions.convertExpr(P.peep(op),t);
}
/**
* Parse postfix expression; this can be a field expression, an array
* lookup or a postfix operator like '#'
*
* <pre>
* expr ('.' FIELD)* [ = expr ] // Field reference
* expr '#' // Postfix unary read operator
* expr ['++' | '--' ] // Postfix unary write operator
* expr ('[' expr ']')* = [ = expr ] // Array reference
* expr '(' [args,]* ')' // Function call
* </pre>
*/
Node parsePostfixMethod(Node expr, Node self) {
// TODO: This is too broad because the parser lacks strong types here.
// Some normal functions can end up with a "self" argument.
// Self method call? Self is a TMP-not-CLZ, then assume and pass self.
if( P.match("(") ) {
expr = parsePostfix(P.require(P.functions.functionCall(expr,self),")"));
} else if( expr != self )
self.isKill(); // Not a method call, no need for self
return parsePostfix(expr);
}
Node parsePostfix(Node expr) {
P._lexer.skipWhiteSpace();
Lexer site = P.loc();
String name;
if( P.match(".") ) name = P.requireId();
else if( P.match("#") ) name = "#";
else if( P.match("[") ) name = "[]";
// Can get here without an explicit receiver: "f()" or
// "self.method()(next)(next)(next)". Pass the current function self.
else if( P.match("(") ) return parsePostfix(P.require(P.functions.functionCall(expr,P.references.defaultSelf()),")"));
else return expr; // No postfix
return P.references.parseField(expr,name,0,site);
}
/**
* Parse integer literal
*
* <pre>
* integerLiteral: [1-9][0-9]* | [0]
* floatLiteral: [digits].[digits]?[e [digits]]?[f]?
* </pre>
*/
private ConstantNode parseLiteral() { return P.con(P._lexer.parseNumber()); }
// Already parsed "'"
Node parseChar() {
return P.require(P.con(TypeInteger.constant(P._lexer.parseChar())),"'");
}
}
|