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