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