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" |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSymbol.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SourceMgr.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 25 | void AsmParser::Warning(SMLoc L, const char *Msg) { |
| 26 | Lexer.PrintMessage(L, Msg, "warning"); |
| 27 | } |
| 28 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 29 | bool AsmParser::Error(SMLoc L, const char *Msg) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 30 | Lexer.PrintMessage(L, Msg, "error"); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 31 | return true; |
| 32 | } |
| 33 | |
| 34 | bool AsmParser::TokError(const char *Msg) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 35 | Lexer.PrintMessage(Lexer.getLoc(), Msg, "error"); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 36 | return true; |
| 37 | } |
| 38 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 39 | bool AsmParser::Run() { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 40 | // Prime the lexer. |
| 41 | Lexer.Lex(); |
| 42 | |
| 43 | while (Lexer.isNot(asmtok::Eof)) |
| 44 | if (ParseStatement()) |
| 45 | return true; |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 50 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 51 | void AsmParser::EatToEndOfStatement() { |
| 52 | while (Lexer.isNot(asmtok::EndOfStatement) && |
| 53 | Lexer.isNot(asmtok::Eof)) |
| 54 | Lexer.Lex(); |
| 55 | |
| 56 | // Eat EOL. |
| 57 | if (Lexer.is(asmtok::EndOfStatement)) |
| 58 | Lexer.Lex(); |
| 59 | } |
| 60 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 62 | /// ParseParenExpr - Parse a paren expression and return it. |
| 63 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 64 | /// |
| 65 | /// parenexpr ::= expr) |
| 66 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 67 | bool AsmParser::ParseParenExpr(AsmExpr *&Res) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 68 | if (ParseExpression(Res)) return true; |
| 69 | if (Lexer.isNot(asmtok::RParen)) |
| 70 | return TokError("expected ')' in parentheses expression"); |
| 71 | Lexer.Lex(); |
| 72 | return false; |
| 73 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 74 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 75 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 76 | /// primaryexpr ::= (parenexpr |
| 77 | /// primaryexpr ::= symbol |
| 78 | /// primaryexpr ::= number |
| 79 | /// primaryexpr ::= ~,+,- primaryexpr |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 80 | bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 81 | switch (Lexer.getKind()) { |
| 82 | default: |
| 83 | return TokError("unknown token in expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 84 | case asmtok::Exclaim: |
| 85 | Lexer.Lex(); // Eat the operator. |
| 86 | if (ParsePrimaryExpr(Res)) |
| 87 | return true; |
| 88 | Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res); |
| 89 | return false; |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 90 | case asmtok::Identifier: { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 91 | // 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] | 92 | // handle things like LFOO+4. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 93 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal()); |
| 94 | |
| 95 | // If this is use of an undefined symbol then mark it external. |
| 96 | if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym)) |
| 97 | Sym->setExternal(true); |
| 98 | |
| 99 | Res = new AsmSymbolRefExpr(Sym); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 100 | Lexer.Lex(); // Eat identifier. |
| 101 | return false; |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 103 | case asmtok::IntVal: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 104 | Res = new AsmConstantExpr(Lexer.getCurIntVal()); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 105 | Lexer.Lex(); // Eat identifier. |
| 106 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 107 | case asmtok::LParen: |
| 108 | Lexer.Lex(); // Eat the '('. |
| 109 | return ParseParenExpr(Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 110 | case asmtok::Minus: |
| 111 | Lexer.Lex(); // Eat the operator. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 112 | if (ParsePrimaryExpr(Res)) |
| 113 | return true; |
| 114 | Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res); |
| 115 | return false; |
| 116 | case asmtok::Plus: |
| 117 | Lexer.Lex(); // Eat the operator. |
| 118 | if (ParsePrimaryExpr(Res)) |
| 119 | return true; |
| 120 | Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res); |
| 121 | return false; |
| 122 | case asmtok::Tilde: |
| 123 | Lexer.Lex(); // Eat the operator. |
| 124 | if (ParsePrimaryExpr(Res)) |
| 125 | return true; |
| 126 | Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res); |
| 127 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 130 | |
| 131 | /// ParseExpression - Parse an expression and return it. |
| 132 | /// |
| 133 | /// expr ::= expr +,- expr -> lowest. |
| 134 | /// expr ::= expr |,^,&,! expr -> middle. |
| 135 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 136 | /// expr ::= primaryexpr |
| 137 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 138 | bool AsmParser::ParseExpression(AsmExpr *&Res) { |
| 139 | Res = 0; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 140 | return ParsePrimaryExpr(Res) || |
| 141 | ParseBinOpRHS(1, Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 143 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 144 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
| 145 | AsmExpr *Expr; |
| 146 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 147 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 148 | if (ParseExpression(Expr)) |
| 149 | return true; |
| 150 | |
| 151 | if (!Expr->EvaluateAsAbsolute(Ctx, Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 152 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 153 | |
| 154 | return false; |
| 155 | } |
| 156 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 157 | bool AsmParser::ParseRelocatableExpression(MCValue &Res) { |
| 158 | AsmExpr *Expr; |
| 159 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 160 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 161 | if (ParseExpression(Expr)) |
| 162 | return true; |
| 163 | |
| 164 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 165 | return Error(StartLoc, "expected relocatable expression"); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
Daniel Dunbar | 2c3f00c | 2009-07-02 02:09:07 +0000 | [diff] [blame] | 170 | bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) { |
| 171 | AsmExpr *Expr; |
| 172 | |
| 173 | SMLoc StartLoc = Lexer.getLoc(); |
| 174 | if (ParseParenExpr(Expr)) |
| 175 | return true; |
| 176 | |
| 177 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) |
| 178 | return Error(StartLoc, "expected relocatable expression"); |
| 179 | |
| 180 | return false; |
| 181 | } |
| 182 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 183 | static unsigned getBinOpPrecedence(asmtok::TokKind K, |
| 184 | AsmBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 185 | switch (K) { |
| 186 | default: return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 187 | |
| 188 | // Lowest Precedence: &&, || |
| 189 | case asmtok::AmpAmp: |
| 190 | Kind = AsmBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 191 | return 1; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 192 | case asmtok::PipePipe: |
| 193 | Kind = AsmBinaryExpr::LOr; |
| 194 | return 1; |
| 195 | |
| 196 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= |
| 197 | case asmtok::Plus: |
| 198 | Kind = AsmBinaryExpr::Add; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 199 | return 2; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 200 | case asmtok::Minus: |
| 201 | Kind = AsmBinaryExpr::Sub; |
| 202 | return 2; |
| 203 | case asmtok::EqualEqual: |
| 204 | Kind = AsmBinaryExpr::EQ; |
| 205 | return 2; |
| 206 | case asmtok::ExclaimEqual: |
| 207 | case asmtok::LessGreater: |
| 208 | Kind = AsmBinaryExpr::NE; |
| 209 | return 2; |
| 210 | case asmtok::Less: |
| 211 | Kind = AsmBinaryExpr::LT; |
| 212 | return 2; |
| 213 | case asmtok::LessEqual: |
| 214 | Kind = AsmBinaryExpr::LTE; |
| 215 | return 2; |
| 216 | case asmtok::Greater: |
| 217 | Kind = AsmBinaryExpr::GT; |
| 218 | return 2; |
| 219 | case asmtok::GreaterEqual: |
| 220 | Kind = AsmBinaryExpr::GTE; |
| 221 | return 2; |
| 222 | |
| 223 | // Intermediate Precedence: |, &, ^ |
| 224 | // |
| 225 | // FIXME: gas seems to support '!' as an infix operator? |
| 226 | case asmtok::Pipe: |
| 227 | Kind = AsmBinaryExpr::Or; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 228 | return 3; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 229 | case asmtok::Caret: |
| 230 | Kind = AsmBinaryExpr::Xor; |
| 231 | return 3; |
| 232 | case asmtok::Amp: |
| 233 | Kind = AsmBinaryExpr::And; |
| 234 | return 3; |
| 235 | |
| 236 | // Highest Precedence: *, /, %, <<, >> |
| 237 | case asmtok::Star: |
| 238 | Kind = AsmBinaryExpr::Mul; |
| 239 | return 4; |
| 240 | case asmtok::Slash: |
| 241 | Kind = AsmBinaryExpr::Div; |
| 242 | return 4; |
| 243 | case asmtok::Percent: |
| 244 | Kind = AsmBinaryExpr::Mod; |
| 245 | return 4; |
| 246 | case asmtok::LessLess: |
| 247 | Kind = AsmBinaryExpr::Shl; |
| 248 | return 4; |
| 249 | case asmtok::GreaterGreater: |
| 250 | Kind = AsmBinaryExpr::Shr; |
| 251 | return 4; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 257 | /// Res contains the LHS of the expression on input. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 258 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 259 | while (1) { |
Daniel Dunbar | 5133063 | 2009-06-29 21:14:21 +0000 | [diff] [blame] | 260 | AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 261 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 262 | |
| 263 | // If the next token is lower precedence than we are allowed to eat, return |
| 264 | // successfully with what we ate already. |
| 265 | if (TokPrec < Precedence) |
| 266 | return false; |
| 267 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 268 | Lexer.Lex(); |
| 269 | |
| 270 | // Eat the next primary expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 271 | AsmExpr *RHS; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 272 | if (ParsePrimaryExpr(RHS)) return true; |
| 273 | |
| 274 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 275 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 276 | AsmBinaryExpr::Opcode Dummy; |
| 277 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 278 | if (TokPrec < NextTokPrec) { |
| 279 | if (ParseBinOpRHS(Precedence+1, RHS)) return true; |
| 280 | } |
| 281 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 282 | // Merge LHS and RHS according to operator. |
| 283 | Res = new AsmBinaryExpr(Kind, Res, RHS); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 287 | |
| 288 | |
| 289 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 290 | /// ParseStatement: |
| 291 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 292 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 293 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 294 | bool AsmParser::ParseStatement() { |
| 295 | switch (Lexer.getKind()) { |
| 296 | default: |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 297 | return TokError("unexpected token at start of statement"); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 298 | case asmtok::EndOfStatement: |
| 299 | Lexer.Lex(); |
| 300 | return false; |
| 301 | case asmtok::Identifier: |
| 302 | break; |
| 303 | // TODO: Recurse on local labels etc. |
| 304 | } |
| 305 | |
| 306 | // If we have an identifier, handle it as the key symbol. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 307 | SMLoc IDLoc = Lexer.getLoc(); |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 308 | const char *IDVal = Lexer.getCurStrVal(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 309 | |
| 310 | // Consume the identifier, see what is after it. |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 311 | switch (Lexer.Lex()) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 312 | case asmtok::Colon: { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 313 | // identifier ':' -> Label. |
| 314 | Lexer.Lex(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 315 | |
| 316 | // Diagnose attempt to use a variable as a label. |
| 317 | // |
| 318 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 319 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 320 | // implicitly marked as external. |
| 321 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal); |
| 322 | if (Sym->getSection()) |
| 323 | return Error(IDLoc, "invalid symbol redefinition"); |
| 324 | if (Ctx.GetSymbolValue(Sym)) |
| 325 | return Error(IDLoc, "symbol already used as assembler variable"); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 326 | |
| 327 | // Since we saw a label, create a symbol and emit it. |
| 328 | // FIXME: If the label starts with L it is an assembler temporary label. |
| 329 | // Why does the client of this api need to know this? |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 330 | Out.EmitLabel(Sym); |
| 331 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 332 | return ParseStatement(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 333 | } |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 334 | |
| 335 | case asmtok::Equal: |
| 336 | // identifier '=' ... -> assignment statement |
| 337 | Lexer.Lex(); |
| 338 | |
| 339 | return ParseAssignment(IDVal, false); |
| 340 | |
| 341 | default: // Normal instruction or directive. |
| 342 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 346 | if (IDVal[0] == '.') { |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 347 | // FIXME: This should be driven based on a hash lookup and callback. |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 348 | if (!strcmp(IDVal, ".section")) |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 349 | return ParseDirectiveDarwinSection(); |
| 350 | if (!strcmp(IDVal, ".text")) |
| 351 | // FIXME: This changes behavior based on the -static flag to the |
| 352 | // assembler. |
| 353 | return ParseDirectiveSectionSwitch("__TEXT,__text", |
| 354 | "regular,pure_instructions"); |
| 355 | if (!strcmp(IDVal, ".const")) |
| 356 | return ParseDirectiveSectionSwitch("__TEXT,__const"); |
| 357 | if (!strcmp(IDVal, ".static_const")) |
| 358 | return ParseDirectiveSectionSwitch("__TEXT,__static_const"); |
| 359 | if (!strcmp(IDVal, ".cstring")) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 360 | return ParseDirectiveSectionSwitch("__TEXT,__cstring", |
| 361 | "cstring_literals"); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 362 | if (!strcmp(IDVal, ".literal4")) |
| 363 | return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals"); |
| 364 | if (!strcmp(IDVal, ".literal8")) |
| 365 | return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals"); |
| 366 | if (!strcmp(IDVal, ".literal16")) |
| 367 | return ParseDirectiveSectionSwitch("__TEXT,__literal16", |
| 368 | "16byte_literals"); |
| 369 | if (!strcmp(IDVal, ".constructor")) |
| 370 | return ParseDirectiveSectionSwitch("__TEXT,__constructor"); |
| 371 | if (!strcmp(IDVal, ".destructor")) |
| 372 | return ParseDirectiveSectionSwitch("__TEXT,__destructor"); |
| 373 | if (!strcmp(IDVal, ".fvmlib_init0")) |
| 374 | return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0"); |
| 375 | if (!strcmp(IDVal, ".fvmlib_init1")) |
| 376 | return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1"); |
| 377 | if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC. |
| 378 | return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs", |
| 379 | "self_modifying_code+pure_instructions,5"); |
| 380 | // FIXME: .picsymbol_stub on PPC. |
| 381 | if (!strcmp(IDVal, ".data")) |
| 382 | return ParseDirectiveSectionSwitch("__DATA,__data"); |
| 383 | if (!strcmp(IDVal, ".static_data")) |
| 384 | return ParseDirectiveSectionSwitch("__DATA,__static_data"); |
| 385 | if (!strcmp(IDVal, ".non_lazy_symbol_pointer")) |
| 386 | return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer", |
| 387 | "non_lazy_symbol_pointers"); |
| 388 | if (!strcmp(IDVal, ".lazy_symbol_pointer")) |
| 389 | return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer", |
| 390 | "lazy_symbol_pointers"); |
| 391 | if (!strcmp(IDVal, ".dyld")) |
| 392 | return ParseDirectiveSectionSwitch("__DATA,__dyld"); |
| 393 | if (!strcmp(IDVal, ".mod_init_func")) |
| 394 | return ParseDirectiveSectionSwitch("__DATA,__mod_init_func", |
| 395 | "mod_init_funcs"); |
| 396 | if (!strcmp(IDVal, ".mod_term_func")) |
| 397 | return ParseDirectiveSectionSwitch("__DATA,__mod_term_func", |
| 398 | "mod_term_funcs"); |
| 399 | if (!strcmp(IDVal, ".const_data")) |
| 400 | return ParseDirectiveSectionSwitch("__DATA,__const", "regular"); |
| 401 | |
| 402 | |
| 403 | // FIXME: Verify attributes on sections. |
| 404 | if (!strcmp(IDVal, ".objc_class")) |
| 405 | return ParseDirectiveSectionSwitch("__OBJC,__class"); |
| 406 | if (!strcmp(IDVal, ".objc_meta_class")) |
| 407 | return ParseDirectiveSectionSwitch("__OBJC,__meta_class"); |
| 408 | if (!strcmp(IDVal, ".objc_cat_cls_meth")) |
| 409 | return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth"); |
| 410 | if (!strcmp(IDVal, ".objc_cat_inst_meth")) |
| 411 | return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth"); |
| 412 | if (!strcmp(IDVal, ".objc_protocol")) |
| 413 | return ParseDirectiveSectionSwitch("__OBJC,__protocol"); |
| 414 | if (!strcmp(IDVal, ".objc_string_object")) |
| 415 | return ParseDirectiveSectionSwitch("__OBJC,__string_object"); |
| 416 | if (!strcmp(IDVal, ".objc_cls_meth")) |
| 417 | return ParseDirectiveSectionSwitch("__OBJC,__cls_meth"); |
| 418 | if (!strcmp(IDVal, ".objc_inst_meth")) |
| 419 | return ParseDirectiveSectionSwitch("__OBJC,__inst_meth"); |
| 420 | if (!strcmp(IDVal, ".objc_cls_refs")) |
| 421 | return ParseDirectiveSectionSwitch("__OBJC,__cls_refs"); |
| 422 | if (!strcmp(IDVal, ".objc_message_refs")) |
| 423 | return ParseDirectiveSectionSwitch("__OBJC,__message_refs"); |
| 424 | if (!strcmp(IDVal, ".objc_symbols")) |
| 425 | return ParseDirectiveSectionSwitch("__OBJC,__symbols"); |
| 426 | if (!strcmp(IDVal, ".objc_category")) |
| 427 | return ParseDirectiveSectionSwitch("__OBJC,__category"); |
| 428 | if (!strcmp(IDVal, ".objc_class_vars")) |
| 429 | return ParseDirectiveSectionSwitch("__OBJC,__class_vars"); |
| 430 | if (!strcmp(IDVal, ".objc_instance_vars")) |
| 431 | return ParseDirectiveSectionSwitch("__OBJC,__instance_vars"); |
| 432 | if (!strcmp(IDVal, ".objc_module_info")) |
| 433 | return ParseDirectiveSectionSwitch("__OBJC,__module_info"); |
| 434 | if (!strcmp(IDVal, ".objc_class_names")) |
| 435 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 436 | if (!strcmp(IDVal, ".objc_meth_var_types")) |
| 437 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 438 | if (!strcmp(IDVal, ".objc_meth_var_names")) |
| 439 | return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals"); |
| 440 | if (!strcmp(IDVal, ".objc_selector_strs")) |
| 441 | return ParseDirectiveSectionSwitch("__OBJC,__selector_strs"); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 443 | // Assembler features |
| 444 | if (!strcmp(IDVal, ".set")) |
| 445 | return ParseDirectiveSet(); |
| 446 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 447 | // Data directives |
| 448 | |
| 449 | if (!strcmp(IDVal, ".ascii")) |
| 450 | return ParseDirectiveAscii(false); |
| 451 | if (!strcmp(IDVal, ".asciz")) |
| 452 | return ParseDirectiveAscii(true); |
| 453 | |
| 454 | // FIXME: Target hooks for size? Also for "word", "hword". |
| 455 | if (!strcmp(IDVal, ".byte")) |
| 456 | return ParseDirectiveValue(1); |
| 457 | if (!strcmp(IDVal, ".short")) |
| 458 | return ParseDirectiveValue(2); |
| 459 | if (!strcmp(IDVal, ".long")) |
| 460 | return ParseDirectiveValue(4); |
| 461 | if (!strcmp(IDVal, ".quad")) |
| 462 | return ParseDirectiveValue(8); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 463 | |
| 464 | // FIXME: Target hooks for IsPow2. |
| 465 | if (!strcmp(IDVal, ".align")) |
| 466 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 467 | if (!strcmp(IDVal, ".align32")) |
| 468 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 469 | if (!strcmp(IDVal, ".balign")) |
| 470 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
| 471 | if (!strcmp(IDVal, ".balignw")) |
| 472 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
| 473 | if (!strcmp(IDVal, ".balignl")) |
| 474 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
| 475 | if (!strcmp(IDVal, ".p2align")) |
| 476 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
| 477 | if (!strcmp(IDVal, ".p2alignw")) |
| 478 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
| 479 | if (!strcmp(IDVal, ".p2alignl")) |
| 480 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 481 | |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 482 | if (!strcmp(IDVal, ".org")) |
| 483 | return ParseDirectiveOrg(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 484 | |
| 485 | if (!strcmp(IDVal, ".fill")) |
| 486 | return ParseDirectiveFill(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 487 | if (!strcmp(IDVal, ".space")) |
| 488 | return ParseDirectiveSpace(); |
| 489 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 490 | // Symbol attribute directives |
| 491 | if (!strcmp(IDVal, ".globl") || !strcmp(IDVal, ".global")) |
| 492 | return ParseDirectiveSymbolAttribute(MCStreamer::Global); |
| 493 | if (!strcmp(IDVal, ".hidden")) |
| 494 | return ParseDirectiveSymbolAttribute(MCStreamer::Hidden); |
| 495 | if (!strcmp(IDVal, ".indirect_symbol")) |
| 496 | return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol); |
| 497 | if (!strcmp(IDVal, ".internal")) |
| 498 | return ParseDirectiveSymbolAttribute(MCStreamer::Internal); |
| 499 | if (!strcmp(IDVal, ".lazy_reference")) |
| 500 | return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference); |
| 501 | if (!strcmp(IDVal, ".no_dead_strip")) |
| 502 | return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip); |
| 503 | if (!strcmp(IDVal, ".private_extern")) |
| 504 | return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern); |
| 505 | if (!strcmp(IDVal, ".protected")) |
| 506 | return ParseDirectiveSymbolAttribute(MCStreamer::Protected); |
| 507 | if (!strcmp(IDVal, ".reference")) |
| 508 | return ParseDirectiveSymbolAttribute(MCStreamer::Reference); |
| 509 | if (!strcmp(IDVal, ".weak")) |
| 510 | return ParseDirectiveSymbolAttribute(MCStreamer::Weak); |
| 511 | if (!strcmp(IDVal, ".weak_definition")) |
| 512 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition); |
| 513 | if (!strcmp(IDVal, ".weak_reference")) |
| 514 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference); |
| 515 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 516 | Warning(IDLoc, "ignoring directive for now"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 517 | EatToEndOfStatement(); |
| 518 | return false; |
| 519 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 520 | |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 521 | MCInst Inst; |
Daniel Dunbar | d9627e1 | 2009-06-30 23:38:38 +0000 | [diff] [blame] | 522 | if (ParseX86InstOperands(IDVal, Inst)) |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 523 | return true; |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 524 | |
| 525 | if (Lexer.isNot(asmtok::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 526 | return TokError("unexpected token in argument list"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 527 | |
| 528 | // Eat the end of statement marker. |
| 529 | Lexer.Lex(); |
| 530 | |
| 531 | // Instruction is good, process it. |
Daniel Dunbar | 0eebb05 | 2009-07-01 06:35:48 +0000 | [diff] [blame] | 532 | Out.EmitInstruction(Inst); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 533 | |
| 534 | // Skip to end of line for now. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 535 | return false; |
| 536 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 537 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 538 | bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 539 | // FIXME: Use better location, we should use proper tokens. |
| 540 | SMLoc EqualLoc = Lexer.getLoc(); |
| 541 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 542 | MCValue Value; |
| 543 | if (ParseRelocatableExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 544 | return true; |
| 545 | |
| 546 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 547 | return TokError("unexpected token in assignment"); |
| 548 | |
| 549 | // Eat the end of statement marker. |
| 550 | Lexer.Lex(); |
| 551 | |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 552 | // Diagnose assignment to a label. |
| 553 | // |
| 554 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 555 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 556 | // implicitly marked as external. |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 557 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 558 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 559 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 560 | if (Sym->getSection()) |
| 561 | return Error(EqualLoc, "invalid assignment to symbol emitted as a label"); |
| 562 | if (Sym->isExternal()) |
| 563 | return Error(EqualLoc, "invalid assignment to external symbol"); |
| 564 | |
| 565 | // Do the assignment. |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 566 | Out.EmitAssignment(Sym, Value, IsDotSet); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 567 | |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | /// ParseDirectiveSet: |
| 572 | /// ::= .set identifier ',' expression |
| 573 | bool AsmParser::ParseDirectiveSet() { |
| 574 | if (Lexer.isNot(asmtok::Identifier)) |
| 575 | return TokError("expected identifier after '.set' directive"); |
| 576 | |
| 577 | const char *Name = Lexer.getCurStrVal(); |
| 578 | |
| 579 | if (Lexer.Lex() != asmtok::Comma) |
| 580 | return TokError("unexpected token in '.set'"); |
| 581 | Lexer.Lex(); |
| 582 | |
| 583 | return ParseAssignment(Name, true); |
| 584 | } |
| 585 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 586 | /// ParseDirectiveSection: |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 587 | /// ::= .section identifier (',' identifier)* |
| 588 | /// FIXME: This should actually parse out the segment, section, attributes and |
| 589 | /// sizeof_stub fields. |
| 590 | bool AsmParser::ParseDirectiveDarwinSection() { |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 591 | if (Lexer.isNot(asmtok::Identifier)) |
| 592 | return TokError("expected identifier after '.section' directive"); |
| 593 | |
| 594 | std::string Section = Lexer.getCurStrVal(); |
| 595 | Lexer.Lex(); |
| 596 | |
| 597 | // Accept a comma separated list of modifiers. |
| 598 | while (Lexer.is(asmtok::Comma)) { |
| 599 | Lexer.Lex(); |
| 600 | |
| 601 | if (Lexer.isNot(asmtok::Identifier)) |
| 602 | return TokError("expected identifier in '.section' directive"); |
| 603 | Section += ','; |
| 604 | Section += Lexer.getCurStrVal(); |
| 605 | Lexer.Lex(); |
| 606 | } |
| 607 | |
| 608 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 609 | return TokError("unexpected token in '.section' directive"); |
| 610 | Lexer.Lex(); |
| 611 | |
| 612 | Out.SwitchSection(Ctx.GetSection(Section.c_str())); |
| 613 | return false; |
| 614 | } |
| 615 | |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 616 | bool AsmParser::ParseDirectiveSectionSwitch(const char *Section, |
| 617 | const char *Directives) { |
| 618 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 619 | return TokError("unexpected token in section switching directive"); |
| 620 | Lexer.Lex(); |
| 621 | |
| 622 | std::string SectionStr = Section; |
| 623 | if (Directives && Directives[0]) { |
| 624 | SectionStr += ","; |
| 625 | SectionStr += Directives; |
| 626 | } |
| 627 | |
| 628 | Out.SwitchSection(Ctx.GetSection(Section)); |
| 629 | return false; |
| 630 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 631 | |
| 632 | /// ParseDirectiveAscii: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 633 | /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 634 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { |
| 635 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 636 | for (;;) { |
| 637 | if (Lexer.isNot(asmtok::String)) |
| 638 | return TokError("expected string in '.ascii' or '.asciz' directive"); |
| 639 | |
| 640 | // FIXME: This shouldn't use a const char* + strlen, the string could have |
| 641 | // embedded nulls. |
| 642 | // FIXME: Should have accessor for getting string contents. |
| 643 | const char *Str = Lexer.getCurStrVal(); |
| 644 | Out.EmitBytes(Str + 1, strlen(Str) - 2); |
| 645 | if (ZeroTerminated) |
| 646 | Out.EmitBytes("\0", 1); |
| 647 | |
| 648 | Lexer.Lex(); |
| 649 | |
| 650 | if (Lexer.is(asmtok::EndOfStatement)) |
| 651 | break; |
| 652 | |
| 653 | if (Lexer.isNot(asmtok::Comma)) |
| 654 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); |
| 655 | Lexer.Lex(); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | Lexer.Lex(); |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | /// ParseDirectiveValue |
| 664 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 665 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
| 666 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 667 | for (;;) { |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 668 | MCValue Expr; |
| 669 | if (ParseRelocatableExpression(Expr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 670 | return true; |
| 671 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 672 | Out.EmitValue(Expr, Size); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 673 | |
| 674 | if (Lexer.is(asmtok::EndOfStatement)) |
| 675 | break; |
| 676 | |
| 677 | // FIXME: Improve diagnostic. |
| 678 | if (Lexer.isNot(asmtok::Comma)) |
| 679 | return TokError("unexpected token in directive"); |
| 680 | Lexer.Lex(); |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | Lexer.Lex(); |
| 685 | return false; |
| 686 | } |
| 687 | |
| 688 | /// ParseDirectiveSpace |
| 689 | /// ::= .space expression [ , expression ] |
| 690 | bool AsmParser::ParseDirectiveSpace() { |
| 691 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 692 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 693 | return true; |
| 694 | |
| 695 | int64_t FillExpr = 0; |
| 696 | bool HasFillExpr = false; |
| 697 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 698 | if (Lexer.isNot(asmtok::Comma)) |
| 699 | return TokError("unexpected token in '.space' directive"); |
| 700 | Lexer.Lex(); |
| 701 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 702 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 703 | return true; |
| 704 | |
| 705 | HasFillExpr = true; |
| 706 | |
| 707 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 708 | return TokError("unexpected token in '.space' directive"); |
| 709 | } |
| 710 | |
| 711 | Lexer.Lex(); |
| 712 | |
| 713 | if (NumBytes <= 0) |
| 714 | return TokError("invalid number of bytes in '.space' directive"); |
| 715 | |
| 716 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
| 717 | for (uint64_t i = 0, e = NumBytes; i != e; ++i) |
| 718 | Out.EmitValue(MCValue::get(FillExpr), 1); |
| 719 | |
| 720 | return false; |
| 721 | } |
| 722 | |
| 723 | /// ParseDirectiveFill |
| 724 | /// ::= .fill expression , expression , expression |
| 725 | bool AsmParser::ParseDirectiveFill() { |
| 726 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 727 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 728 | return true; |
| 729 | |
| 730 | if (Lexer.isNot(asmtok::Comma)) |
| 731 | return TokError("unexpected token in '.fill' directive"); |
| 732 | Lexer.Lex(); |
| 733 | |
| 734 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 735 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 736 | return true; |
| 737 | |
| 738 | if (Lexer.isNot(asmtok::Comma)) |
| 739 | return TokError("unexpected token in '.fill' directive"); |
| 740 | Lexer.Lex(); |
| 741 | |
| 742 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 743 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 744 | return true; |
| 745 | |
| 746 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 747 | return TokError("unexpected token in '.fill' directive"); |
| 748 | |
| 749 | Lexer.Lex(); |
| 750 | |
| 751 | if (FillSize != 1 && FillSize != 2 && FillSize != 4) |
| 752 | return TokError("invalid '.fill' size, expected 1, 2, or 4"); |
| 753 | |
| 754 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 755 | Out.EmitValue(MCValue::get(FillExpr), FillSize); |
| 756 | |
| 757 | return false; |
| 758 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 759 | |
| 760 | /// ParseDirectiveOrg |
| 761 | /// ::= .org expression [ , expression ] |
| 762 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 763 | MCValue Offset; |
| 764 | if (ParseRelocatableExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 765 | return true; |
| 766 | |
| 767 | // Parse optional fill expression. |
| 768 | int64_t FillExpr = 0; |
| 769 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 770 | if (Lexer.isNot(asmtok::Comma)) |
| 771 | return TokError("unexpected token in '.org' directive"); |
| 772 | Lexer.Lex(); |
| 773 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 774 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 775 | return true; |
| 776 | |
| 777 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 778 | return TokError("unexpected token in '.org' directive"); |
| 779 | } |
| 780 | |
| 781 | Lexer.Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 782 | |
| 783 | // FIXME: Only limited forms of relocatable expressions are accepted here, it |
| 784 | // has to be relative to the current section. |
| 785 | Out.EmitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 786 | |
| 787 | return false; |
| 788 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 789 | |
| 790 | /// ParseDirectiveAlign |
| 791 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 792 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
| 793 | int64_t Alignment; |
| 794 | if (ParseAbsoluteExpression(Alignment)) |
| 795 | return true; |
| 796 | |
| 797 | SMLoc MaxBytesLoc; |
| 798 | bool HasFillExpr = false; |
| 799 | int64_t FillExpr = 0; |
| 800 | int64_t MaxBytesToFill = 0; |
| 801 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 802 | if (Lexer.isNot(asmtok::Comma)) |
| 803 | return TokError("unexpected token in directive"); |
| 804 | Lexer.Lex(); |
| 805 | |
| 806 | // The fill expression can be omitted while specifying a maximum number of |
| 807 | // alignment bytes, e.g: |
| 808 | // .align 3,,4 |
| 809 | if (Lexer.isNot(asmtok::Comma)) { |
| 810 | HasFillExpr = true; |
| 811 | if (ParseAbsoluteExpression(FillExpr)) |
| 812 | return true; |
| 813 | } |
| 814 | |
| 815 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 816 | if (Lexer.isNot(asmtok::Comma)) |
| 817 | return TokError("unexpected token in directive"); |
| 818 | Lexer.Lex(); |
| 819 | |
| 820 | MaxBytesLoc = Lexer.getLoc(); |
| 821 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 822 | return true; |
| 823 | |
| 824 | if (Lexer.isNot(asmtok::EndOfStatement)) |
| 825 | return TokError("unexpected token in directive"); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | Lexer.Lex(); |
| 830 | |
| 831 | if (!HasFillExpr) { |
| 832 | // FIXME: Sometimes fill with nop. |
| 833 | FillExpr = 0; |
| 834 | } |
| 835 | |
| 836 | // Compute alignment in bytes. |
| 837 | if (IsPow2) { |
| 838 | // FIXME: Diagnose overflow. |
| 839 | Alignment = 1 << Alignment; |
| 840 | } |
| 841 | |
| 842 | // Diagnose non-sensical max bytes to fill. |
| 843 | if (MaxBytesLoc.isValid()) { |
| 844 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 845 | Warning(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 846 | "many bytes, ignoring"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 847 | return false; |
| 848 | } |
| 849 | |
| 850 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 851 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 852 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 853 | MaxBytesToFill = 0; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
| 858 | Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill); |
| 859 | |
| 860 | return false; |
| 861 | } |
| 862 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 863 | /// ParseDirectiveSymbolAttribute |
| 864 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
| 865 | bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { |
| 866 | if (Lexer.isNot(asmtok::EndOfStatement)) { |
| 867 | for (;;) { |
| 868 | if (Lexer.isNot(asmtok::Identifier)) |
| 869 | return TokError("expected identifier in directive"); |
| 870 | |
| 871 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal()); |
| 872 | Lexer.Lex(); |
| 873 | |
| 874 | // If this is use of an undefined symbol then mark it external. |
| 875 | if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym)) |
| 876 | Sym->setExternal(true); |
| 877 | |
| 878 | Out.EmitSymbolAttribute(Sym, Attr); |
| 879 | |
| 880 | if (Lexer.is(asmtok::EndOfStatement)) |
| 881 | break; |
| 882 | |
| 883 | if (Lexer.isNot(asmtok::Comma)) |
| 884 | return TokError("unexpected token in directive"); |
| 885 | Lexer.Lex(); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | Lexer.Lex(); |
| 890 | return false; |
| 891 | } |