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 | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 111 | Op = X86Operand::CreateReg(0); |
| 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 | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame^] | 122 | } |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 123 | |
| 124 | //case asmtok::Star: |
| 125 | // * %eax |
| 126 | // * <memaddress> |
| 127 | // Note that these are both "dereferenced". |
| 128 | } |
| 129 | } |
| 130 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame^] | 131 | /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale) |
| 132 | bool AsmParser::ParseX86MemOperand(X86Operand &Op) { |
| 133 | // FIXME: If SegReg ':' (e.g. %gs:), eat and remember. |
| 134 | unsigned SegReg = 0; |
| 135 | |
| 136 | |
| 137 | // We have to disambiguate a parenthesized expression "(4+5)" from the start |
| 138 | // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The |
| 139 | // only way to do this without lookahead is to eat the ( and see what is after |
| 140 | // it. |
| 141 | int64_t Disp = 0; |
| 142 | if (Lexer.isNot(asmtok::LParen)) { |
| 143 | if (ParseExpression(Disp)) return true; |
| 144 | |
| 145 | // After parsing the base expression we could either have a parenthesized |
| 146 | // memory address or not. If not, return now. If so, eat the (. |
| 147 | if (Lexer.isNot(asmtok::LParen)) { |
| 148 | Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // Eat the '('. |
| 153 | Lexer.Lex(); |
| 154 | } else { |
| 155 | // Okay, we have a '('. We don't know if this is an expression or not, but |
| 156 | // so we have to eat the ( to see beyond it. |
| 157 | Lexer.Lex(); // Eat the '('. |
| 158 | |
| 159 | if (Lexer.is(asmtok::Register) || Lexer.is(asmtok::Comma)) { |
| 160 | // Nothing to do here, fall into the code below with the '(' part of the |
| 161 | // memory operand consumed. |
| 162 | } else { |
| 163 | // FIXME: Call ParseParenExpression with the leading ( consumed. |
| 164 | return TokError("FIXME: Paren expr not implemented yet!"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // If we reached here, then we just ate the ( of the memory operand. Process |
| 169 | // the rest of the memory operand. |
| 170 | unsigned BaseReg = 0, ScaleReg = 0, Scale = 0; |
| 171 | |
| 172 | if (Lexer.is(asmtok::Register)) { |
| 173 | BaseReg = 123; // FIXME: decode reg # |
| 174 | Lexer.Lex(); // eat the register. |
| 175 | } |
| 176 | |
| 177 | if (Lexer.is(asmtok::Comma)) { |
| 178 | Lexer.Lex(); // eat the comma. |
| 179 | |
| 180 | if (Lexer.is(asmtok::Register)) { |
| 181 | ScaleReg = 123; // FIXME: decode reg # |
| 182 | Lexer.Lex(); // eat the register. |
| 183 | Scale = 1; // If not specified, the scale defaults to 1. |
| 184 | } |
| 185 | |
| 186 | if (Lexer.is(asmtok::Comma)) { |
| 187 | Lexer.Lex(); // eat the comma. |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 188 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame^] | 189 | // If present, get and validate scale amount. |
| 190 | if (Lexer.is(asmtok::IntVal)) { |
| 191 | int64_t ScaleVal = Lexer.getCurIntVal(); |
| 192 | if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8) |
| 193 | return TokError("scale factor in address must be 1, 2, 4 or 8"); |
| 194 | Lexer.Lex(); // eat the scale. |
| 195 | Scale = (unsigned)ScaleVal; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // Ok, we've eaten the memory operand, verify we have a ')' and eat it too. |
| 201 | if (Lexer.isNot(asmtok::RParen)) |
| 202 | return TokError("unexpected token in memory operand"); |
| 203 | Lexer.Lex(); // Eat the ')'. |
| 204 | |
| 205 | Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, Scale, ScaleReg); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /// ParseExpression - Parse an expression and return it. |
| 211 | /// FIXME: This should handle real expressions, we do something trivial for now. |
| 212 | bool AsmParser::ParseExpression(int64_t &Res) { |
| 213 | switch (Lexer.getKind()) { |
| 214 | default: |
| 215 | return TokError("unknown token in expression"); |
| 216 | case asmtok::Identifier: |
| 217 | // This is a label, this should be parsed as part of an expression, to |
| 218 | // handle things like LFOO+4 |
| 219 | Res = 0; // FIXME. |
| 220 | Lexer.Lex(); // Eat identifier. |
| 221 | return false; |
| 222 | case asmtok::IntVal: |
| 223 | Res = Lexer.getCurIntVal(); |
| 224 | Lexer.Lex(); // Eat identifier. |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 232 | /// ParseStatement: |
| 233 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 234 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 235 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 236 | bool AsmParser::ParseStatement() { |
| 237 | switch (Lexer.getKind()) { |
| 238 | default: |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 239 | return TokError("unexpected token at start of statement"); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 240 | case asmtok::EndOfStatement: |
| 241 | Lexer.Lex(); |
| 242 | return false; |
| 243 | case asmtok::Identifier: |
| 244 | break; |
| 245 | // TODO: Recurse on local labels etc. |
| 246 | } |
| 247 | |
| 248 | // If we have an identifier, handle it as the key symbol. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 249 | SMLoc IDLoc = Lexer.getLoc(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 250 | std::string IDVal = Lexer.getCurStrVal(); |
| 251 | |
| 252 | // Consume the identifier, see what is after it. |
| 253 | if (Lexer.Lex() == asmtok::Colon) { |
| 254 | // identifier ':' -> Label. |
| 255 | Lexer.Lex(); |
| 256 | return ParseStatement(); |
| 257 | } |
| 258 | |
| 259 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 260 | if (IDVal[0] == '.') { |
| 261 | Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now"); |
| 262 | EatToEndOfStatement(); |
| 263 | return false; |
| 264 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 266 | // If it's an instruction, parse an operand list. |
| 267 | std::vector<X86Operand> Operands; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 269 | // Read the first operand, if present. Note that we require a newline at the |
| 270 | // end of file, so we don't have to worry about Eof here. |
| 271 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 272 | X86Operand Op; |
| 273 | if (ParseX86Operand(Op)) |
| 274 | return true; |
| 275 | Operands.push_back(Op); |
| 276 | } |
| 277 | |
| 278 | while (Lexer.is(asmtok::Comma)) { |
| 279 | Lexer.Lex(); // Eat the comma. |
| 280 | |
| 281 | // Parse and remember the operand. |
| 282 | X86Operand Op; |
| 283 | if (ParseX86Operand(Op)) |
| 284 | return true; |
| 285 | Operands.push_back(Op); |
| 286 | } |
| 287 | |
| 288 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 289 | return TokError("unexpected token in operand list"); |
| 290 | |
| 291 | // Eat the end of statement marker. |
| 292 | Lexer.Lex(); |
| 293 | |
| 294 | // Instruction is good, process it. |
| 295 | outs() << "Found instruction: " << IDVal << " with " << Operands.size() |
| 296 | << " operands.\n"; |
| 297 | |
| 298 | // Skip to end of line for now. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 299 | return false; |
| 300 | } |