llvm.org GIT mirror llvm / 8dfbe6c
implement a trivial binary expression parser, we can now parse all of 176.gcc.llc.s git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73950 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
4 changed file(s) with 85 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
7474 while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
7575 *CurPtr == '.' || *CurPtr == '@')
7676 ++CurPtr;
77 CurStrVal.assign(TokStart, CurPtr); // Include %
77 CurStrVal.assign(TokStart, CurPtr);
7878 return asmtok::Identifier;
7979 }
8080
8181 /// LexPercent: Register: %[a-zA-Z0-9]+
8282 asmtok::TokKind AsmLexer::LexPercent() {
8383 if (!isalnum(*CurPtr))
84 return ReturnError(TokStart, "invalid register name");
84 return asmtok::Percent; // Single %.
85
8586 while (isalnum(*CurPtr))
8687 ++CurPtr;
87 CurStrVal.assign(TokStart, CurPtr); // Skip %
88 CurStrVal.assign(TokStart, CurPtr); // Include %
8889 return asmtok::Register;
8990 }
9091
242243 case '*': return asmtok::Star;
243244 case ',': return asmtok::Comma;
244245 case '$': return asmtok::Dollar;
246 case '|': return asmtok::Pipe;
247 case '^': return asmtok::Caret;
248 case '&': return asmtok::Amp;
249 case '!': return asmtok::Exclaim;
245250 case '%': return LexPercent();
246251 case '/': return LexSlash();
247252 case '#': return LexHash();
249254 case '0': case '1': case '2': case '3': case '4':
250255 case '5': case '6': case '7': case '8': case '9':
251256 return LexDigit();
257 case '<':
258 if (*CurPtr == '<') {
259 ++CurPtr;
260 return asmtok::LessLess;
261 }
262 // Don't have any use for bare '<' yet.
263 return ReturnError(TokStart, "invalid character in input");
264 case '>':
265 if (*CurPtr == '>') {
266 ++CurPtr;
267 return asmtok::GreaterGreater;
268 }
269 // Don't have any use for bare '>' yet.
270 return ReturnError(TokStart, "invalid character in input");
252271
253272 // TODO: Quoted identifiers (objc methods etc)
254273 // local labels: [0-9][:]
4141 Plus, Minus, Tilde,
4242 Slash, // '/'
4343 LParen, RParen,
44 Star, Comma, Dollar
44 Star, Comma, Dollar,
45
46 Pipe, Caret, Amp, Exclaim,
47 Percent, LessLess, GreaterGreater
4548 };
4649 }
4750
167167 // memory operand consumed.
168168 } else {
169169 // It must be an parenthesized expression, parse it now.
170 if (ParseParenExpr(Disp)) return true;
170 if (ParseParenExpr(Disp) ||
171 ParseBinOpRHS(1, Disp))
172 return true;
171173
172174 // After parsing the base expression we could either have a parenthesized
173175 // memory address or not. If not, return now. If so, eat the (.
273275 /// expr ::= primaryexpr
274276 ///
275277 bool AsmParser::ParseExpression(int64_t &Res) {
276 return ParsePrimaryExpr(Res);
277 }
278
278 return ParsePrimaryExpr(Res) ||
279 ParseBinOpRHS(1, Res);
280 }
281
282 static unsigned getBinOpPrecedence(asmtok::TokKind K) {
283 switch (K) {
284 default: return 0; // not a binop.
285 case asmtok::Plus:
286 case asmtok::Minus:
287 return 1;
288 case asmtok::Pipe:
289 case asmtok::Caret:
290 case asmtok::Amp:
291 case asmtok::Exclaim:
292 return 2;
293 case asmtok::Star:
294 case asmtok::Slash:
295 case asmtok::Percent:
296 case asmtok::LessLess:
297 case asmtok::GreaterGreater:
298 return 3;
299 }
300 }
301
302
303 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
304 /// Res contains the LHS of the expression on input.
305 bool AsmParser::ParseBinOpRHS(unsigned Precedence, int64_t &Res) {
306 while (1) {
307 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind());
308
309 // If the next token is lower precedence than we are allowed to eat, return
310 // successfully with what we ate already.
311 if (TokPrec < Precedence)
312 return false;
313
314 //asmtok::TokKind BinOp = Lexer.getKind();
315 Lexer.Lex();
316
317 // Eat the next primary expression.
318 int64_t RHS;
319 if (ParsePrimaryExpr(RHS)) return true;
320
321 // If BinOp binds less tightly with RHS than the operator after RHS, let
322 // the pending operator take RHS as its LHS.
323 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind());
324 if (TokPrec < NextTokPrec) {
325 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
326 }
327
328 // Merge LHS/RHS: fixme use the right operator etc.
329 Res += RHS;
330 }
331 }
332
279333
280334
281335
3939 bool ParseX86MemOperand(X86Operand &Op);
4040 bool ParseExpression(int64_t &Res);
4141 bool ParsePrimaryExpr(int64_t &Res);
42 bool ParseBinOpRHS(unsigned Precedence, int64_t &Res);
4243 bool ParseParenExpr(int64_t &Res);
4344 };
4445