Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class implements the parser for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "AsmParser.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 15 | #include "llvm/Support/SourceMgr.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 19 | bool AsmParser::Error(SMLoc L, const char *Msg) { |
| 20 | Lexer.PrintMessage(L, Msg); |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | bool AsmParser::TokError(const char *Msg) { |
| 25 | Lexer.PrintMessage(Lexer.getLoc(), Msg); |
| 26 | return true; |
| 27 | } |
| 28 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 29 | bool AsmParser::Run() { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 30 | // Prime the lexer. |
| 31 | Lexer.Lex(); |
| 32 | |
| 33 | while (Lexer.isNot(asmtok::Eof)) |
| 34 | if (ParseStatement()) |
| 35 | return true; |
| 36 | |
| 37 | return false; |
| 38 | } |
| 39 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 40 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 41 | void AsmParser::EatToEndOfStatement() { |
| 42 | while (Lexer.isNot(asmtok::EndOfStatement) && |
| 43 | Lexer.isNot(asmtok::Eof)) |
| 44 | Lexer.Lex(); |
| 45 | |
| 46 | // Eat EOL. |
| 47 | if (Lexer.is(asmtok::EndOfStatement)) |
| 48 | Lexer.Lex(); |
| 49 | } |
| 50 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 52 | struct AsmParser::X86Operand { |
| 53 | enum { |
| 54 | Register, |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 55 | Immediate, |
| 56 | Memory |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 57 | } Kind; |
| 58 | |
| 59 | union { |
| 60 | struct { |
| 61 | unsigned RegNo; |
| 62 | } Reg; |
| 63 | |
| 64 | struct { |
| 65 | // FIXME: Should be a general expression. |
| 66 | int64_t Val; |
| 67 | } Imm; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 68 | |
| 69 | struct { |
| 70 | unsigned SegReg; |
| 71 | int64_t Disp; // FIXME: Should be a general expression. |
| 72 | unsigned BaseReg; |
| 73 | unsigned Scale; |
| 74 | unsigned ScaleReg; |
| 75 | } Mem; |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static X86Operand CreateReg(unsigned RegNo) { |
| 79 | X86Operand Res; |
| 80 | Res.Kind = Register; |
| 81 | Res.Reg.RegNo = RegNo; |
| 82 | return Res; |
| 83 | } |
| 84 | static X86Operand CreateImm(int64_t Val) { |
| 85 | X86Operand Res; |
| 86 | Res.Kind = Immediate; |
| 87 | Res.Imm.Val = Val; |
| 88 | return Res; |
| 89 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 90 | static X86Operand CreateMem(unsigned SegReg, int64_t Disp, unsigned BaseReg, |
| 91 | unsigned Scale, unsigned ScaleReg) { |
| 92 | X86Operand Res; |
| 93 | Res.Kind = Memory; |
| 94 | Res.Mem.SegReg = SegReg; |
| 95 | Res.Mem.Disp = Disp; |
| 96 | Res.Mem.BaseReg = BaseReg; |
| 97 | Res.Mem.Scale = Scale; |
| 98 | Res.Mem.ScaleReg = ScaleReg; |
| 99 | return Res; |
| 100 | } |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | bool AsmParser::ParseX86Operand(X86Operand &Op) { |
| 104 | switch (Lexer.getKind()) { |
| 105 | default: |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 106 | return ParseX86MemOperand(Op); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 107 | case asmtok::Register: |
| 108 | // FIXME: Decode reg #. |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 109 | // FIXME: if a segment register, this could either be just the seg reg, or |
| 110 | // the start of a memory operand. |
Chris Lattner | be9c23f | 2009-06-22 06:02:13 +0000 | [diff] [blame] | 111 | Op = X86Operand::CreateReg(123); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 112 | Lexer.Lex(); // Eat register. |
| 113 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 114 | case asmtok::Dollar: { |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 115 | // $42 -> immediate. |
| 116 | Lexer.Lex(); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 117 | int64_t Val; |
| 118 | if (ParseExpression(Val)) |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 119 | return TokError("expected integer constant"); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 120 | Op = X86Operand::CreateReg(Val); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 121 | return false; |
Chris Lattner | be9c23f | 2009-06-22 06:02:13 +0000 | [diff] [blame] | 122 | case asmtok::Star: |
| 123 | Lexer.Lex(); // Eat the star. |
| 124 | |
| 125 | if (Lexer.is(asmtok::Register)) { |
| 126 | Op = X86Operand::CreateReg(123); |
| 127 | Lexer.Lex(); // Eat register. |
| 128 | } else if (ParseX86MemOperand(Op)) |
| 129 | return true; |
| 130 | |
| 131 | // FIXME: Note that these are 'dereferenced' so that clients know the '*' is |
| 132 | // there. |
| 133 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 134 | } |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 138 | /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale) |
| 139 | bool AsmParser::ParseX86MemOperand(X86Operand &Op) { |
| 140 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 141 | unsigned SegReg = 0; |
| 142 | |
| 143 | |
| 144 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 145 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
| 146 | // only way to do this without lookahead is to eat the ( and see what is after |
| 147 | // it. |
| 148 | int64_t Disp = 0; |
| 149 | if (Lexer.isNot(asmtok::LParen)) { |
| 150 | if (ParseExpression(Disp)) return true; |
| 151 | |
| 152 | // After parsing the base expression we could either have a parenthesized |
| 153 | // memory address or not. If not, return now. If so, eat the (. |
| 154 | if (Lexer.isNot(asmtok::LParen)) { |
| 155 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0); |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | // Eat the '('. |
| 160 | Lexer.Lex(); |
| 161 | } else { |
| 162 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 163 | // so we have to eat the ( to see beyond it. |
| 164 | Lexer.Lex(); // Eat the '('. |
| 165 | |
| 166 | if (Lexer.is(asmtok::Register) || Lexer.is(asmtok::Comma)) { |
| 167 | // Nothing to do here, fall into the code below with the '(' part of the |
| 168 | // memory operand consumed. |
| 169 | } else { |
Chris Lattner | 7031806 | 2009-06-22 06:35:58 +0000 | [diff] [blame] | 170 | // It must be an parenthesized expression, parse it now. |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 171 | if (ParseParenExpr(Disp) || |
| 172 | ParseBinOpRHS(1, Disp)) |
| 173 | return true; |
Chris Lattner | 7031806 | 2009-06-22 06:35:58 +0000 | [diff] [blame] | 174 | |
| 175 | // After parsing the base expression we could either have a parenthesized |
| 176 | // memory address or not. If not, return now. If so, eat the (. |
| 177 | if (Lexer.isNot(asmtok::LParen)) { |
| 178 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | // Eat the '('. |
| 183 | Lexer.Lex(); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 188 | // the rest of the memory operand. |
| 189 | unsigned BaseReg = 0, ScaleReg = 0, Scale = 0; |
| 190 | |
| 191 | if (Lexer.is(asmtok::Register)) { |
| 192 | BaseReg = 123; // FIXME: decode reg # |
| 193 | Lexer.Lex(); // eat the register. |
| 194 | } |
| 195 | |
| 196 | if (Lexer.is(asmtok::Comma)) { |
| 197 | Lexer.Lex(); // eat the comma. |
| 198 | |
| 199 | if (Lexer.is(asmtok::Register)) { |
| 200 | ScaleReg = 123; // FIXME: decode reg # |
| 201 | Lexer.Lex(); // eat the register. |
| 202 | Scale = 1; // If not specified, the scale defaults to 1. |
| 203 | } |
| 204 | |
| 205 | if (Lexer.is(asmtok::Comma)) { |
| 206 | Lexer.Lex(); // eat the comma. |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 207 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 208 | // If present, get and validate scale amount. |
| 209 | if (Lexer.is(asmtok::IntVal)) { |
| 210 | int64_t ScaleVal = Lexer.getCurIntVal(); |
| 211 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8) |
| 212 | return TokError("scale factor in address must be 1, 2, 4 or 8"); |
| 213 | Lexer.Lex(); // eat the scale. |
| 214 | Scale = (unsigned)ScaleVal; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
| 220 | if (Lexer.isNot(asmtok::RParen)) |
| 221 | return TokError("unexpected token in memory operand"); |
| 222 | Lexer.Lex(); // Eat the ')'. |
| 223 | |
| 224 | Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, Scale, ScaleReg); |
| 225 | return false; |
| 226 | } |
| 227 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 228 | /// ParseParenExpr - Parse a paren expression and return it. |
| 229 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 230 | /// |
| 231 | /// parenexpr ::= expr) |
| 232 | /// |
| 233 | bool AsmParser::ParseParenExpr(int64_t &Res) { |
| 234 | if (ParseExpression(Res)) return true; |
| 235 | if (Lexer.isNot(asmtok::RParen)) |
| 236 | return TokError("expected ')' in parentheses expression"); |
| 237 | Lexer.Lex(); |
| 238 | return false; |
| 239 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 240 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 241 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 242 | /// primaryexpr ::= (parenexpr |
| 243 | /// primaryexpr ::= symbol |
| 244 | /// primaryexpr ::= number |
| 245 | /// primaryexpr ::= ~,+,- primaryexpr |
| 246 | bool AsmParser::ParsePrimaryExpr(int64_t &Res) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 247 | switch (Lexer.getKind()) { |
| 248 | default: |
| 249 | return TokError("unknown token in expression"); |
| 250 | case asmtok::Identifier: |
| 251 | // This is a label, this should be parsed as part of an expression, to |
| 252 | // handle things like LFOO+4 |
| 253 | Res = 0; // FIXME. |
| 254 | Lexer.Lex(); // Eat identifier. |
| 255 | return false; |
| 256 | case asmtok::IntVal: |
| 257 | Res = Lexer.getCurIntVal(); |
| 258 | Lexer.Lex(); // Eat identifier. |
| 259 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 260 | case asmtok::LParen: |
| 261 | Lexer.Lex(); // Eat the '('. |
| 262 | return ParseParenExpr(Res); |
| 263 | case asmtok::Tilde: |
| 264 | case asmtok::Plus: |
| 265 | case asmtok::Minus: |
| 266 | Lexer.Lex(); // Eat the operator. |
| 267 | return ParsePrimaryExpr(Res); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 270 | |
| 271 | /// ParseExpression - Parse an expression and return it. |
| 272 | /// |
| 273 | /// expr ::= expr +,- expr -> lowest. |
| 274 | /// expr ::= expr |,^,&,! expr -> middle. |
| 275 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 276 | /// expr ::= primaryexpr |
| 277 | /// |
| 278 | bool AsmParser::ParseExpression(int64_t &Res) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 279 | return ParsePrimaryExpr(Res) || |
| 280 | ParseBinOpRHS(1, Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 282 | |
| 283 | static unsigned getBinOpPrecedence(asmtok::TokKind K) { |
| 284 | switch (K) { |
| 285 | default: return 0; // not a binop. |
| 286 | case asmtok::Plus: |
| 287 | case asmtok::Minus: |
| 288 | return 1; |
| 289 | case asmtok::Pipe: |
| 290 | case asmtok::Caret: |
| 291 | case asmtok::Amp: |
| 292 | case asmtok::Exclaim: |
| 293 | return 2; |
| 294 | case asmtok::Star: |
| 295 | case asmtok::Slash: |
| 296 | case asmtok::Percent: |
| 297 | case asmtok::LessLess: |
| 298 | case asmtok::GreaterGreater: |
| 299 | return 3; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 305 | /// Res contains the LHS of the expression on input. |
| 306 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, int64_t &Res) { |
| 307 | while (1) { |
| 308 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind()); |
| 309 | |
| 310 | // If the next token is lower precedence than we are allowed to eat, return |
| 311 | // successfully with what we ate already. |
| 312 | if (TokPrec < Precedence) |
| 313 | return false; |
| 314 | |
| 315 | //asmtok::TokKind BinOp = Lexer.getKind(); |
| 316 | Lexer.Lex(); |
| 317 | |
| 318 | // Eat the next primary expression. |
| 319 | int64_t RHS; |
| 320 | if (ParsePrimaryExpr(RHS)) return true; |
| 321 | |
| 322 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 323 | // the pending operator take RHS as its LHS. |
| 324 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind()); |
| 325 | if (TokPrec < NextTokPrec) { |
| 326 | if (ParseBinOpRHS(Precedence+1, RHS)) return true; |
| 327 | } |
| 328 | |
| 329 | // Merge LHS/RHS: fixme use the right operator etc. |
| 330 | Res += RHS; |
| 331 | } |
| 332 | } |
| 333 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 334 | |
| 335 | |
| 336 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 337 | /// ParseStatement: |
| 338 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 339 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 340 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 341 | bool AsmParser::ParseStatement() { |
| 342 | switch (Lexer.getKind()) { |
| 343 | default: |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 344 | return TokError("unexpected token at start of statement"); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 345 | case asmtok::EndOfStatement: |
| 346 | Lexer.Lex(); |
| 347 | return false; |
| 348 | case asmtok::Identifier: |
| 349 | break; |
| 350 | // TODO: Recurse on local labels etc. |
| 351 | } |
| 352 | |
| 353 | // If we have an identifier, handle it as the key symbol. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 354 | SMLoc IDLoc = Lexer.getLoc(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 355 | std::string IDVal = Lexer.getCurStrVal(); |
| 356 | |
| 357 | // Consume the identifier, see what is after it. |
| 358 | if (Lexer.Lex() == asmtok::Colon) { |
| 359 | // identifier ':' -> Label. |
| 360 | Lexer.Lex(); |
| 361 | return ParseStatement(); |
| 362 | } |
| 363 | |
| 364 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 365 | if (IDVal[0] == '.') { |
| 366 | Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now"); |
| 367 | EatToEndOfStatement(); |
| 368 | return false; |
| 369 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 371 | // If it's an instruction, parse an operand list. |
| 372 | std::vector<X86Operand> Operands; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 374 | // Read the first operand, if present. Note that we require a newline at the |
| 375 | // end of file, so we don't have to worry about Eof here. |
| 376 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 377 | X86Operand Op; |
| 378 | if (ParseX86Operand(Op)) |
| 379 | return true; |
| 380 | Operands.push_back(Op); |
| 381 | } |
| 382 | |
| 383 | while (Lexer.is(asmtok::Comma)) { |
| 384 | Lexer.Lex(); // Eat the comma. |
| 385 | |
| 386 | // Parse and remember the operand. |
| 387 | X86Operand Op; |
| 388 | if (ParseX86Operand(Op)) |
| 389 | return true; |
| 390 | Operands.push_back(Op); |
| 391 | } |
| 392 | |
| 393 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 394 | return TokError("unexpected token in operand list"); |
| 395 | |
| 396 | // Eat the end of statement marker. |
| 397 | Lexer.Lex(); |
| 398 | |
| 399 | // Instruction is good, process it. |
| 400 | outs() << "Found instruction: " << IDVal << " with " << Operands.size() |
| 401 | << " operands.\n"; |
| 402 | |
| 403 | // Skip to end of line for now. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 404 | return false; |
| 405 | } |