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