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" |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 15 | |
| 16 | #include "AsmExpr.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCInst.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCStreamer.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SourceMgr.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 24 | bool AsmParser::Error(SMLoc L, const char *Msg) { |
| 25 | Lexer.PrintMessage(L, Msg); |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | bool AsmParser::TokError(const char *Msg) { |
| 30 | Lexer.PrintMessage(Lexer.getLoc(), Msg); |
| 31 | return true; |
| 32 | } |
| 33 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 34 | bool AsmParser::Run() { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 35 | // Prime the lexer. |
| 36 | Lexer.Lex(); |
| 37 | |
| 38 | while (Lexer.isNot(asmtok::Eof)) |
| 39 | if (ParseStatement()) |
| 40 | return true; |
| 41 | |
| 42 | return false; |
| 43 | } |
| 44 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 45 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 46 | void AsmParser::EatToEndOfStatement() { |
| 47 | while (Lexer.isNot(asmtok::EndOfStatement) && |
| 48 | Lexer.isNot(asmtok::Eof)) |
| 49 | Lexer.Lex(); |
| 50 | |
| 51 | // Eat EOL. |
| 52 | if (Lexer.is(asmtok::EndOfStatement)) |
| 53 | Lexer.Lex(); |
| 54 | } |
| 55 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 57 | /// ParseParenExpr - Parse a paren expression and return it. |
| 58 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 59 | /// |
| 60 | /// parenexpr ::= expr) |
| 61 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 62 | bool AsmParser::ParseParenExpr(AsmExpr *&Res) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 63 | if (ParseExpression(Res)) return true; |
| 64 | if (Lexer.isNot(asmtok::RParen)) |
| 65 | return TokError("expected ')' in parentheses expression"); |
| 66 | Lexer.Lex(); |
| 67 | return false; |
| 68 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 70 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 71 | /// primaryexpr ::= (parenexpr |
| 72 | /// primaryexpr ::= symbol |
| 73 | /// primaryexpr ::= number |
| 74 | /// primaryexpr ::= ~,+,- primaryexpr |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 75 | bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 76 | switch (Lexer.getKind()) { |
| 77 | default: |
| 78 | return TokError("unknown token in expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 79 | case asmtok::Exclaim: |
| 80 | Lexer.Lex(); // Eat the operator. |
| 81 | if (ParsePrimaryExpr(Res)) |
| 82 | return true; |
| 83 | Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res); |
| 84 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 85 | case asmtok::Identifier: |
| 86 | // This is a label, this should be parsed as part of an expression, to |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 87 | // handle things like LFOO+4. |
| 88 | Res = new AsmSymbolRefExpr(Ctx.GetOrCreateSymbol(Lexer.getCurStrVal())); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 89 | Lexer.Lex(); // Eat identifier. |
| 90 | return false; |
| 91 | case asmtok::IntVal: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 92 | Res = new AsmConstantExpr(Lexer.getCurIntVal()); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 93 | Lexer.Lex(); // Eat identifier. |
| 94 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 95 | case asmtok::LParen: |
| 96 | Lexer.Lex(); // Eat the '('. |
| 97 | return ParseParenExpr(Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 98 | case asmtok::Minus: |
| 99 | Lexer.Lex(); // Eat the operator. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 100 | if (ParsePrimaryExpr(Res)) |
| 101 | return true; |
| 102 | Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res); |
| 103 | return false; |
| 104 | case asmtok::Plus: |
| 105 | Lexer.Lex(); // Eat the operator. |
| 106 | if (ParsePrimaryExpr(Res)) |
| 107 | return true; |
| 108 | Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res); |
| 109 | return false; |
| 110 | case asmtok::Tilde: |
| 111 | Lexer.Lex(); // Eat the operator. |
| 112 | if (ParsePrimaryExpr(Res)) |
| 113 | return true; |
| 114 | Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res); |
| 115 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 118 | |
| 119 | /// ParseExpression - Parse an expression and return it. |
| 120 | /// |
| 121 | /// expr ::= expr +,- expr -> lowest. |
| 122 | /// expr ::= expr |,^,&,! expr -> middle. |
| 123 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 124 | /// expr ::= primaryexpr |
| 125 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 126 | bool AsmParser::ParseExpression(AsmExpr *&Res) { |
| 127 | Res = 0; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 128 | return ParsePrimaryExpr(Res) || |
| 129 | ParseBinOpRHS(1, Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 131 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 132 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
| 133 | AsmExpr *Expr; |
| 134 | |
| 135 | if (ParseExpression(Expr)) |
| 136 | return true; |
| 137 | |
| 138 | if (!Expr->EvaluateAsAbsolute(Ctx, Res)) |
| 139 | return TokError("expected absolute expression"); |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | static unsigned getBinOpPrecedence(asmtok::TokKind K, |
| 145 | AsmBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 146 | switch (K) { |
| 147 | default: return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 148 | |
| 149 | // Lowest Precedence: &&, || |
| 150 | case asmtok::AmpAmp: |
| 151 | Kind = AsmBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 152 | return 1; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 153 | case asmtok::PipePipe: |
| 154 | Kind = AsmBinaryExpr::LOr; |
| 155 | return 1; |
| 156 | |
| 157 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= |
| 158 | case asmtok::Plus: |
| 159 | Kind = AsmBinaryExpr::Add; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 160 | return 2; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 161 | case asmtok::Minus: |
| 162 | Kind = AsmBinaryExpr::Sub; |
| 163 | return 2; |
| 164 | case asmtok::EqualEqual: |
| 165 | Kind = AsmBinaryExpr::EQ; |
| 166 | return 2; |
| 167 | case asmtok::ExclaimEqual: |
| 168 | case asmtok::LessGreater: |
| 169 | Kind = AsmBinaryExpr::NE; |
| 170 | return 2; |
| 171 | case asmtok::Less: |
| 172 | Kind = AsmBinaryExpr::LT; |
| 173 | return 2; |
| 174 | case asmtok::LessEqual: |
| 175 | Kind = AsmBinaryExpr::LTE; |
| 176 | return 2; |
| 177 | case asmtok::Greater: |
| 178 | Kind = AsmBinaryExpr::GT; |
| 179 | return 2; |
| 180 | case asmtok::GreaterEqual: |
| 181 | Kind = AsmBinaryExpr::GTE; |
| 182 | return 2; |
| 183 | |
| 184 | // Intermediate Precedence: |, &, ^ |
| 185 | // |
| 186 | // FIXME: gas seems to support '!' as an infix operator? |
| 187 | case asmtok::Pipe: |
| 188 | Kind = AsmBinaryExpr::Or; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 189 | return 3; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 190 | case asmtok::Caret: |
| 191 | Kind = AsmBinaryExpr::Xor; |
| 192 | return 3; |
| 193 | case asmtok::Amp: |
| 194 | Kind = AsmBinaryExpr::And; |
| 195 | return 3; |
| 196 | |
| 197 | // Highest Precedence: *, /, %, <<, >> |
| 198 | case asmtok::Star: |
| 199 | Kind = AsmBinaryExpr::Mul; |
| 200 | return 4; |
| 201 | case asmtok::Slash: |
| 202 | Kind = AsmBinaryExpr::Div; |
| 203 | return 4; |
| 204 | case asmtok::Percent: |
| 205 | Kind = AsmBinaryExpr::Mod; |
| 206 | return 4; |
| 207 | case asmtok::LessLess: |
| 208 | Kind = AsmBinaryExpr::Shl; |
| 209 | return 4; |
| 210 | case asmtok::GreaterGreater: |
| 211 | Kind = AsmBinaryExpr::Shr; |
| 212 | return 4; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 218 | /// Res contains the LHS of the expression on input. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 219 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 220 | while (1) { |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 221 | AsmBinaryExpr::Opcode Kind; |
| 222 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 223 | |
| 224 | // If the next token is lower precedence than we are allowed to eat, return |
| 225 | // successfully with what we ate already. |
| 226 | if (TokPrec < Precedence) |
| 227 | return false; |
| 228 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 229 | Lexer.Lex(); |
| 230 | |
| 231 | // Eat the next primary expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 232 | AsmExpr *RHS; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 233 | if (ParsePrimaryExpr(RHS)) return true; |
| 234 | |
| 235 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 236 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 237 | AsmBinaryExpr::Opcode Dummy; |
| 238 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 239 | if (TokPrec < NextTokPrec) { |
| 240 | if (ParseBinOpRHS(Precedence+1, RHS)) return true; |
| 241 | } |
| 242 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 243 | // Merge LHS and RHS according to operator. |
| 244 | Res = new AsmBinaryExpr(Kind, Res, RHS); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 251 | /// ParseStatement: |
| 252 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 253 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 254 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 255 | bool AsmParser::ParseStatement() { |
| 256 | switch (Lexer.getKind()) { |
| 257 | default: |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 258 | return TokError("unexpected token at start of statement"); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 259 | case asmtok::EndOfStatement: |
| 260 | Lexer.Lex(); |
| 261 | return false; |
| 262 | case asmtok::Identifier: |
| 263 | break; |
| 264 | // TODO: Recurse on local labels etc. |
| 265 | } |
| 266 | |
| 267 | // If we have an identifier, handle it as the key symbol. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 268 | SMLoc IDLoc = Lexer.getLoc(); |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 269 | const char *IDVal = Lexer.getCurStrVal(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 270 | |
| 271 | // Consume the identifier, see what is after it. |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 272 | switch (Lexer.Lex()) { |
| 273 | case asmtok::Colon: |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 274 | // identifier ':' -> Label. |
| 275 | Lexer.Lex(); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 276 | |
| 277 | // Since we saw a label, create a symbol and emit it. |
| 278 | // FIXME: If the label starts with L it is an assembler temporary label. |
| 279 | // Why does the client of this api need to know this? |
| 280 | Out.EmitLabel(Ctx.GetOrCreateSymbol(IDVal)); |
| 281 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 282 | return ParseStatement(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 283 | |
| 284 | case asmtok::Equal: |
| 285 | // identifier '=' ... -> assignment statement |
| 286 | Lexer.Lex(); |
| 287 | |
| 288 | return ParseAssignment(IDVal, false); |
| 289 | |
| 290 | default: // Normal instruction or directive. |
| 291 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 295 | if (IDVal[0] == '.') { |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 296 | // FIXME: This should be driven based on a hash lookup and callback. |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 297 | if (!strcmp(IDVal, ".section")) |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 298 | return ParseDirectiveDarwinSection(); |
| 299 | if (!strcmp(IDVal, ".text")) |
| 300 | // FIXME: This changes behavior based on the -static flag to the |
| 301 | // assembler. |
| 302 | return ParseDirectiveSectionSwitch("__TEXT,__text", |
| 303 | "regular,pure_instructions"); |
| 304 | if (!strcmp(IDVal, ".const")) |
| 305 | return ParseDirectiveSectionSwitch("__TEXT,__const"); |
| 306 | if (!strcmp(IDVal, ".static_const")) |
| 307 | return ParseDirectiveSectionSwitch("__TEXT,__static_const"); |
| 308 | if (!strcmp(IDVal, ".cstring")) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 309 | return ParseDirectiveSectionSwitch("__TEXT,__cstring", |
| 310 | "cstring_literals"); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 311 | if (!strcmp(IDVal, ".literal4")) |
| 312 | return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals"); |
| 313 | if (!strcmp(IDVal, ".literal8")) |
| 314 | return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals"); |
| 315 | if (!strcmp(IDVal, ".literal16")) |
| 316 | return ParseDirectiveSectionSwitch("__TEXT,__literal16", |
| 317 | "16byte_literals"); |
| 318 | if (!strcmp(IDVal, ".constructor")) |
| 319 | return ParseDirectiveSectionSwitch("__TEXT,__constructor"); |
| 320 | if (!strcmp(IDVal, ".destructor")) |
| 321 | return ParseDirectiveSectionSwitch("__TEXT,__destructor"); |
| 322 | if (!strcmp(IDVal, ".fvmlib_init0")) |
| 323 | return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0"); |
| 324 | if (!strcmp(IDVal, ".fvmlib_init1")) |
| 325 | return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1"); |
| 326 | if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC. |
| 327 | return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs", |
| 328 | "self_modifying_code+pure_instructions,5"); |
| 329 | // FIXME: .picsymbol_stub on PPC. |
| 330 | if (!strcmp(IDVal, ".data")) |
| 331 | return ParseDirectiveSectionSwitch("__DATA,__data"); |
| 332 | if (!strcmp(IDVal, ".static_data")) |
| 333 | return ParseDirectiveSectionSwitch("__DATA,__static_data"); |
| 334 | if (!strcmp(IDVal, ".non_lazy_symbol_pointer")) |
| 335 | return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer", |
| 336 | "non_lazy_symbol_pointers"); |
| 337 | if (!strcmp(IDVal, ".lazy_symbol_pointer")) |
| 338 | return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer", |
| 339 | "lazy_symbol_pointers"); |
| 340 | if (!strcmp(IDVal, ".dyld")) |
| 341 | return ParseDirectiveSectionSwitch("__DATA,__dyld"); |
| 342 | if (!strcmp(IDVal, ".mod_init_func")) |
| 343 | return ParseDirectiveSectionSwitch("__DATA,__mod_init_func", |
| 344 | "mod_init_funcs"); |
| 345 | if (!strcmp(IDVal, ".mod_term_func")) |
| 346 | return ParseDirectiveSectionSwitch("__DATA,__mod_term_func", |
| 347 | "mod_term_funcs"); |
| 348 | if (!strcmp(IDVal, ".const_data")) |
| 349 | return ParseDirectiveSectionSwitch("__DATA,__const", "regular"); |
| 350 | |
| 351 | |
| 352 | // FIXME: Verify attributes on sections. |
| 353 | if (!strcmp(IDVal, ".objc_class")) |
| 354 | return ParseDirectiveSectionSwitch("__OBJC,__class"); |
| 355 | if (!strcmp(IDVal, ".objc_meta_class")) |
| 356 | return ParseDirectiveSectionSwitch("__OBJC,__meta_class"); |
| 357 | if (!strcmp(IDVal, ".objc_cat_cls_meth")) |
| 358 | return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth"); |
| 359 | if (!strcmp(IDVal, ".objc_cat_inst_meth")) |
| 360 | return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth"); |
| 361 | if (!strcmp(IDVal, ".objc_protocol")) |
| 362 | return ParseDirectiveSectionSwitch("__OBJC,__protocol"); |
| 363 | if (!strcmp(IDVal, ".objc_string_object")) |
| 364 | return ParseDirectiveSectionSwitch("__OBJC,__string_object"); |
| 365 | if (!strcmp(IDVal, ".objc_cls_meth")) |
| 366 | return ParseDirectiveSectionSwitch("__OBJC,__cls_meth"); |
| 367 | if (!strcmp(IDVal, ".objc_inst_meth")) |
| 368 | return ParseDirectiveSectionSwitch("__OBJC,__inst_meth"); |
| 369 | if (!strcmp(IDVal, ".objc_cls_refs")) |
| 370 | return ParseDirectiveSectionSwitch("__OBJC,__cls_refs"); |
| 371 | if (!strcmp(IDVal, ".objc_message_refs")) |
| 372 | return ParseDirectiveSectionSwitch("__OBJC,__message_refs"); |
| 373 | if (!strcmp(IDVal, ".objc_symbols")) |
| 374 | return ParseDirectiveSectionSwitch("__OBJC,__symbols"); |
| 375 | if (!strcmp(IDVal, ".objc_category")) |
| 376 | return ParseDirectiveSectionSwitch("__OBJC,__category"); |
| 377 | if (!strcmp(IDVal, ".objc_class_vars")) |
| 378 | return ParseDirectiveSectionSwitch("__OBJC,__class_vars"); |
| 379 | if (!strcmp(IDVal, ".objc_instance_vars")) |
| 380 | return ParseDirectiveSectionSwitch("__OBJC,__instance_vars"); |
| 381 | if (!strcmp(IDVal, ".objc_module_info")) |
| 382 | return ParseDirectiveSectionSwitch("__OBJC,__module_info"); |
| 383 | if (!strcmp(IDVal, ".objc_class_names")) |
| 384 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 385 | if (!strcmp(IDVal, ".objc_meth_var_types")) |
| 386 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 387 | if (!strcmp(IDVal, ".objc_meth_var_names")) |
| 388 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 389 | if (!strcmp(IDVal, ".objc_selector_strs")) |
| 390 | return ParseDirectiveSectionSwitch("__OBJC,__selector_strs"); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 391 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 392 | // Assembler features |
| 393 | if (!strcmp(IDVal, ".set")) |
| 394 | return ParseDirectiveSet(); |
| 395 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 396 | // Data directives |
| 397 | |
| 398 | if (!strcmp(IDVal, ".ascii")) |
| 399 | return ParseDirectiveAscii(false); |
| 400 | if (!strcmp(IDVal, ".asciz")) |
| 401 | return ParseDirectiveAscii(true); |
| 402 | |
| 403 | // FIXME: Target hooks for size? Also for "word", "hword". |
| 404 | if (!strcmp(IDVal, ".byte")) |
| 405 | return ParseDirectiveValue(1); |
| 406 | if (!strcmp(IDVal, ".short")) |
| 407 | return ParseDirectiveValue(2); |
| 408 | if (!strcmp(IDVal, ".long")) |
| 409 | return ParseDirectiveValue(4); |
| 410 | if (!strcmp(IDVal, ".quad")) |
| 411 | return ParseDirectiveValue(8); |
| 412 | if (!strcmp(IDVal, ".fill")) |
| 413 | return ParseDirectiveFill(); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 414 | if (!strcmp(IDVal, ".org")) |
| 415 | return ParseDirectiveOrg(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 416 | if (!strcmp(IDVal, ".space")) |
| 417 | return ParseDirectiveSpace(); |
| 418 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 419 | Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now"); |
| 420 | EatToEndOfStatement(); |
| 421 | return false; |
| 422 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 424 | MCInst Inst; |
| 425 | if (ParseX86InstOperands(Inst)) |
| 426 | return true; |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 427 | |
| 428 | if (Lexer.isNot(asmtok::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 429 | return TokError("unexpected token in argument list"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 430 | |
| 431 | // Eat the end of statement marker. |
| 432 | Lexer.Lex(); |
| 433 | |
| 434 | // Instruction is good, process it. |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 435 | outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands() |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 436 | << " operands.\n"; |
| 437 | |
| 438 | // Skip to end of line for now. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 442 | bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) { |
| 443 | int64_t Value; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 444 | if (ParseAbsoluteExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 445 | return true; |
| 446 | |
| 447 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 448 | return TokError("unexpected token in assignment"); |
| 449 | |
| 450 | // Eat the end of statement marker. |
| 451 | Lexer.Lex(); |
| 452 | |
| 453 | // Get the symbol for this name. |
| 454 | // FIXME: Handle '.'. |
| 455 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
| 456 | Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet); |
| 457 | |
| 458 | return false; |
| 459 | } |
| 460 | |
| 461 | /// ParseDirectiveSet: |
| 462 | /// ::= .set identifier ',' expression |
| 463 | bool AsmParser::ParseDirectiveSet() { |
| 464 | if (Lexer.isNot(asmtok::Identifier)) |
| 465 | return TokError("expected identifier after '.set' directive"); |
| 466 | |
| 467 | const char *Name = Lexer.getCurStrVal(); |
| 468 | |
| 469 | if (Lexer.Lex() != asmtok::Comma) |
| 470 | return TokError("unexpected token in '.set'"); |
| 471 | Lexer.Lex(); |
| 472 | |
| 473 | return ParseAssignment(Name, true); |
| 474 | } |
| 475 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 476 | /// ParseDirectiveSection: |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 477 | /// ::= .section identifier (',' identifier)* |
| 478 | /// FIXME: This should actually parse out the segment, section, attributes and |
| 479 | /// sizeof_stub fields. |
| 480 | bool AsmParser::ParseDirectiveDarwinSection() { |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 481 | if (Lexer.isNot(asmtok::Identifier)) |
| 482 | return TokError("expected identifier after '.section' directive"); |
| 483 | |
| 484 | std::string Section = Lexer.getCurStrVal(); |
| 485 | Lexer.Lex(); |
| 486 | |
| 487 | // Accept a comma separated list of modifiers. |
| 488 | while (Lexer.is(asmtok::Comma)) { |
| 489 | Lexer.Lex(); |
| 490 | |
| 491 | if (Lexer.isNot(asmtok::Identifier)) |
| 492 | return TokError("expected identifier in '.section' directive"); |
| 493 | Section += ','; |
| 494 | Section += Lexer.getCurStrVal(); |
| 495 | Lexer.Lex(); |
| 496 | } |
| 497 | |
| 498 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 499 | return TokError("unexpected token in '.section' directive"); |
| 500 | Lexer.Lex(); |
| 501 | |
| 502 | Out.SwitchSection(Ctx.GetSection(Section.c_str())); |
| 503 | return false; |
| 504 | } |
| 505 | |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 506 | bool AsmParser::ParseDirectiveSectionSwitch(const char *Section, |
| 507 | const char *Directives) { |
| 508 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 509 | return TokError("unexpected token in section switching directive"); |
| 510 | Lexer.Lex(); |
| 511 | |
| 512 | std::string SectionStr = Section; |
| 513 | if (Directives && Directives[0]) { |
| 514 | SectionStr += ","; |
| 515 | SectionStr += Directives; |
| 516 | } |
| 517 | |
| 518 | Out.SwitchSection(Ctx.GetSection(Section)); |
| 519 | return false; |
| 520 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 521 | |
| 522 | /// ParseDirectiveAscii: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 523 | /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 524 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { |
| 525 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 526 | for (;;) { |
| 527 | if (Lexer.isNot(asmtok::String)) |
| 528 | return TokError("expected string in '.ascii' or '.asciz' directive"); |
| 529 | |
| 530 | // FIXME: This shouldn't use a const char* + strlen, the string could have |
| 531 | // embedded nulls. |
| 532 | // FIXME: Should have accessor for getting string contents. |
| 533 | const char *Str = Lexer.getCurStrVal(); |
| 534 | Out.EmitBytes(Str + 1, strlen(Str) - 2); |
| 535 | if (ZeroTerminated) |
| 536 | Out.EmitBytes("\0", 1); |
| 537 | |
| 538 | Lexer.Lex(); |
| 539 | |
| 540 | if (Lexer.is(asmtok::EndOfStatement)) |
| 541 | break; |
| 542 | |
| 543 | if (Lexer.isNot(asmtok::Comma)) |
| 544 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); |
| 545 | Lexer.Lex(); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | Lexer.Lex(); |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | /// ParseDirectiveValue |
| 554 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 555 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
| 556 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 557 | for (;;) { |
| 558 | int64_t Expr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 559 | if (ParseAbsoluteExpression(Expr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 560 | return true; |
| 561 | |
| 562 | Out.EmitValue(MCValue::get(Expr), Size); |
| 563 | |
| 564 | if (Lexer.is(asmtok::EndOfStatement)) |
| 565 | break; |
| 566 | |
| 567 | // FIXME: Improve diagnostic. |
| 568 | if (Lexer.isNot(asmtok::Comma)) |
| 569 | return TokError("unexpected token in directive"); |
| 570 | Lexer.Lex(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | Lexer.Lex(); |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | /// ParseDirectiveSpace |
| 579 | /// ::= .space expression [ , expression ] |
| 580 | bool AsmParser::ParseDirectiveSpace() { |
| 581 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 582 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 583 | return true; |
| 584 | |
| 585 | int64_t FillExpr = 0; |
| 586 | bool HasFillExpr = false; |
| 587 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 588 | if (Lexer.isNot(asmtok::Comma)) |
| 589 | return TokError("unexpected token in '.space' directive"); |
| 590 | Lexer.Lex(); |
| 591 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 592 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 593 | return true; |
| 594 | |
| 595 | HasFillExpr = true; |
| 596 | |
| 597 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 598 | return TokError("unexpected token in '.space' directive"); |
| 599 | } |
| 600 | |
| 601 | Lexer.Lex(); |
| 602 | |
| 603 | if (NumBytes <= 0) |
| 604 | return TokError("invalid number of bytes in '.space' directive"); |
| 605 | |
| 606 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
| 607 | for (uint64_t i = 0, e = NumBytes; i != e; ++i) |
| 608 | Out.EmitValue(MCValue::get(FillExpr), 1); |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | /// ParseDirectiveFill |
| 614 | /// ::= .fill expression , expression , expression |
| 615 | bool AsmParser::ParseDirectiveFill() { |
| 616 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 617 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 618 | return true; |
| 619 | |
| 620 | if (Lexer.isNot(asmtok::Comma)) |
| 621 | return TokError("unexpected token in '.fill' directive"); |
| 622 | Lexer.Lex(); |
| 623 | |
| 624 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 625 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 626 | return true; |
| 627 | |
| 628 | if (Lexer.isNot(asmtok::Comma)) |
| 629 | return TokError("unexpected token in '.fill' directive"); |
| 630 | Lexer.Lex(); |
| 631 | |
| 632 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 633 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 634 | return true; |
| 635 | |
| 636 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 637 | return TokError("unexpected token in '.fill' directive"); |
| 638 | |
| 639 | Lexer.Lex(); |
| 640 | |
| 641 | if (FillSize != 1 && FillSize != 2 && FillSize != 4) |
| 642 | return TokError("invalid '.fill' size, expected 1, 2, or 4"); |
| 643 | |
| 644 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 645 | Out.EmitValue(MCValue::get(FillExpr), FillSize); |
| 646 | |
| 647 | return false; |
| 648 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 649 | |
| 650 | /// ParseDirectiveOrg |
| 651 | /// ::= .org expression [ , expression ] |
| 652 | bool AsmParser::ParseDirectiveOrg() { |
| 653 | int64_t Offset; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 654 | if (ParseAbsoluteExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 655 | return true; |
| 656 | |
| 657 | // Parse optional fill expression. |
| 658 | int64_t FillExpr = 0; |
| 659 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 660 | if (Lexer.isNot(asmtok::Comma)) |
| 661 | return TokError("unexpected token in '.org' directive"); |
| 662 | Lexer.Lex(); |
| 663 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 664 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 665 | return true; |
| 666 | |
| 667 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 668 | return TokError("unexpected token in '.org' directive"); |
| 669 | } |
| 670 | |
| 671 | Lexer.Lex(); |
| 672 | |
| 673 | Out.EmitValueToOffset(MCValue::get(Offset), FillExpr); |
| 674 | |
| 675 | return false; |
| 676 | } |