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 | f9bdedd | 2009-08-10 18:15:01 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCSectionMachO.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 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 48 | AsmCond StartingCondState = TheCondState; |
| 49 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 50 | // While we have input, parse each statement. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 51 | while (Lexer.isNot(AsmToken::Eof)) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 52 | // Handle conditional assembly here before calling ParseStatement() |
| 53 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 54 | // If we have an identifier, handle it as the key symbol. |
| 55 | AsmToken ID = Lexer.getTok(); |
| 56 | SMLoc IDLoc = ID.getLoc(); |
| 57 | StringRef IDVal = ID.getString(); |
| 58 | |
| 59 | if (IDVal == ".if" || |
| 60 | IDVal == ".elseif" || |
| 61 | IDVal == ".else" || |
| 62 | IDVal == ".endif") { |
| 63 | if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc)) |
| 64 | continue; |
| 65 | HadError = true; |
| 66 | EatToEndOfStatement(); |
| 67 | continue; |
| 68 | } |
| 69 | } |
| 70 | if (TheCondState.Ignore) { |
| 71 | EatToEndOfStatement(); |
| 72 | continue; |
| 73 | } |
| 74 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 75 | if (!ParseStatement()) continue; |
| 76 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 77 | // We had an error, remember it and recover by skipping to the next line. |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 78 | HadError = true; |
| 79 | EatToEndOfStatement(); |
| 80 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 81 | |
| 82 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 83 | TheCondState.Ignore != StartingCondState.Ignore) |
| 84 | return TokError("unmatched .ifs or .elses"); |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 85 | |
| 86 | return HadError; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 89 | /// ParseConditionalAssemblyDirectives - parse the conditional assembly |
| 90 | /// directives |
| 91 | bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive, |
| 92 | SMLoc DirectiveLoc) { |
| 93 | if (Directive == ".if") |
| 94 | return ParseDirectiveIf(DirectiveLoc); |
| 95 | if (Directive == ".elseif") |
| 96 | return ParseDirectiveElseIf(DirectiveLoc); |
| 97 | if (Directive == ".else") |
| 98 | return ParseDirectiveElse(DirectiveLoc); |
| 99 | if (Directive == ".endif") |
| 100 | return ParseDirectiveEndIf(DirectiveLoc); |
| 101 | return true; |
| 102 | } |
| 103 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 104 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 105 | void AsmParser::EatToEndOfStatement() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 106 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 107 | Lexer.isNot(AsmToken::Eof)) |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 108 | Lexer.Lex(); |
| 109 | |
| 110 | // Eat EOL. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 111 | if (Lexer.is(AsmToken::EndOfStatement)) |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 112 | Lexer.Lex(); |
| 113 | } |
| 114 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 116 | /// ParseParenExpr - Parse a paren expression and return it. |
| 117 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 118 | /// |
| 119 | /// parenexpr ::= expr) |
| 120 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 121 | bool AsmParser::ParseParenExpr(AsmExpr *&Res) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 122 | if (ParseExpression(Res)) return true; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 123 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 124 | return TokError("expected ')' in parentheses expression"); |
| 125 | Lexer.Lex(); |
| 126 | return false; |
| 127 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 129 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 130 | /// primaryexpr ::= (parenexpr |
| 131 | /// primaryexpr ::= symbol |
| 132 | /// primaryexpr ::= number |
| 133 | /// primaryexpr ::= ~,+,- primaryexpr |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 134 | bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 135 | switch (Lexer.getKind()) { |
| 136 | default: |
| 137 | return TokError("unknown token in expression"); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 138 | case AsmToken::Exclaim: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 139 | Lexer.Lex(); // Eat the operator. |
| 140 | if (ParsePrimaryExpr(Res)) |
| 141 | return true; |
| 142 | Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res); |
| 143 | return false; |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 144 | case AsmToken::String: |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 145 | case AsmToken::Identifier: { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 146 | // 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] | 147 | // handle things like LFOO+4. |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 148 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier()); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 149 | |
| 150 | // If this is use of an undefined symbol then mark it external. |
| 151 | if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym)) |
| 152 | Sym->setExternal(true); |
| 153 | |
| 154 | Res = new AsmSymbolRefExpr(Sym); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 155 | Lexer.Lex(); // Eat identifier. |
| 156 | return false; |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 157 | } |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 158 | case AsmToken::Integer: |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 159 | Res = new AsmConstantExpr(Lexer.getTok().getIntVal()); |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 160 | Lexer.Lex(); // Eat token. |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 161 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 162 | case AsmToken::LParen: |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 163 | Lexer.Lex(); // Eat the '('. |
| 164 | return ParseParenExpr(Res); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 165 | case AsmToken::Minus: |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 166 | Lexer.Lex(); // Eat the operator. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 167 | if (ParsePrimaryExpr(Res)) |
| 168 | return true; |
| 169 | Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res); |
| 170 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 171 | case AsmToken::Plus: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 172 | Lexer.Lex(); // Eat the operator. |
| 173 | if (ParsePrimaryExpr(Res)) |
| 174 | return true; |
| 175 | Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res); |
| 176 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 177 | case AsmToken::Tilde: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 178 | Lexer.Lex(); // Eat the operator. |
| 179 | if (ParsePrimaryExpr(Res)) |
| 180 | return true; |
| 181 | Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res); |
| 182 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 185 | |
| 186 | /// ParseExpression - Parse an expression and return it. |
| 187 | /// |
| 188 | /// expr ::= expr +,- expr -> lowest. |
| 189 | /// expr ::= expr |,^,&,! expr -> middle. |
| 190 | /// expr ::= expr *,/,%,<<,>> expr -> highest. |
| 191 | /// expr ::= primaryexpr |
| 192 | /// |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 193 | bool AsmParser::ParseExpression(AsmExpr *&Res) { |
| 194 | Res = 0; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 195 | return ParsePrimaryExpr(Res) || |
| 196 | ParseBinOpRHS(1, Res); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 198 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 199 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
| 200 | AsmExpr *Expr; |
| 201 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 202 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 203 | if (ParseExpression(Expr)) |
| 204 | return true; |
| 205 | |
| 206 | if (!Expr->EvaluateAsAbsolute(Ctx, Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 207 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 212 | bool AsmParser::ParseRelocatableExpression(MCValue &Res) { |
| 213 | AsmExpr *Expr; |
| 214 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 215 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 216 | if (ParseExpression(Expr)) |
| 217 | return true; |
| 218 | |
| 219 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 220 | return Error(StartLoc, "expected relocatable expression"); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 221 | |
| 222 | return false; |
| 223 | } |
| 224 | |
Daniel Dunbar | 2c3f00c | 2009-07-02 02:09:07 +0000 | [diff] [blame] | 225 | bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) { |
| 226 | AsmExpr *Expr; |
| 227 | |
| 228 | SMLoc StartLoc = Lexer.getLoc(); |
| 229 | if (ParseParenExpr(Expr)) |
| 230 | return true; |
| 231 | |
| 232 | if (!Expr->EvaluateAsRelocatable(Ctx, Res)) |
| 233 | return Error(StartLoc, "expected relocatable expression"); |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 238 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 239 | AsmBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 240 | switch (K) { |
| 241 | default: return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 242 | |
| 243 | // Lowest Precedence: &&, || |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 244 | case AsmToken::AmpAmp: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 245 | Kind = AsmBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 246 | return 1; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 247 | case AsmToken::PipePipe: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 248 | Kind = AsmBinaryExpr::LOr; |
| 249 | return 1; |
| 250 | |
| 251 | // Low Precedence: +, -, ==, !=, <>, <, <=, >, >= |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 252 | case AsmToken::Plus: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 253 | Kind = AsmBinaryExpr::Add; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 254 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 255 | case AsmToken::Minus: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 256 | Kind = AsmBinaryExpr::Sub; |
| 257 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 258 | case AsmToken::EqualEqual: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 259 | Kind = AsmBinaryExpr::EQ; |
| 260 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 261 | case AsmToken::ExclaimEqual: |
| 262 | case AsmToken::LessGreater: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 263 | Kind = AsmBinaryExpr::NE; |
| 264 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 265 | case AsmToken::Less: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 266 | Kind = AsmBinaryExpr::LT; |
| 267 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 268 | case AsmToken::LessEqual: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 269 | Kind = AsmBinaryExpr::LTE; |
| 270 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 271 | case AsmToken::Greater: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 272 | Kind = AsmBinaryExpr::GT; |
| 273 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 274 | case AsmToken::GreaterEqual: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 275 | Kind = AsmBinaryExpr::GTE; |
| 276 | return 2; |
| 277 | |
| 278 | // Intermediate Precedence: |, &, ^ |
| 279 | // |
| 280 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 281 | case AsmToken::Pipe: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 282 | Kind = AsmBinaryExpr::Or; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 283 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 284 | case AsmToken::Caret: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 285 | Kind = AsmBinaryExpr::Xor; |
| 286 | return 3; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 287 | case AsmToken::Amp: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 288 | Kind = AsmBinaryExpr::And; |
| 289 | return 3; |
| 290 | |
| 291 | // Highest Precedence: *, /, %, <<, >> |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 292 | case AsmToken::Star: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 293 | Kind = AsmBinaryExpr::Mul; |
| 294 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 295 | case AsmToken::Slash: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 296 | Kind = AsmBinaryExpr::Div; |
| 297 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 298 | case AsmToken::Percent: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 299 | Kind = AsmBinaryExpr::Mod; |
| 300 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 301 | case AsmToken::LessLess: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 302 | Kind = AsmBinaryExpr::Shl; |
| 303 | return 4; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 304 | case AsmToken::GreaterGreater: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 305 | Kind = AsmBinaryExpr::Shr; |
| 306 | return 4; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 312 | /// Res contains the LHS of the expression on input. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 313 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 314 | while (1) { |
Daniel Dunbar | 5133063 | 2009-06-29 21:14:21 +0000 | [diff] [blame] | 315 | AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 316 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 317 | |
| 318 | // If the next token is lower precedence than we are allowed to eat, return |
| 319 | // successfully with what we ate already. |
| 320 | if (TokPrec < Precedence) |
| 321 | return false; |
| 322 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 323 | Lexer.Lex(); |
| 324 | |
| 325 | // Eat the next primary expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 326 | AsmExpr *RHS; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 327 | if (ParsePrimaryExpr(RHS)) return true; |
| 328 | |
| 329 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 330 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 331 | AsmBinaryExpr::Opcode Dummy; |
| 332 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 333 | if (TokPrec < NextTokPrec) { |
| 334 | if (ParseBinOpRHS(Precedence+1, RHS)) return true; |
| 335 | } |
| 336 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 337 | // Merge LHS and RHS according to operator. |
| 338 | Res = new AsmBinaryExpr(Kind, Res, RHS); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 342 | |
| 343 | |
| 344 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 345 | /// ParseStatement: |
| 346 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 347 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 348 | /// ::= Label* Identifier OperandList* EndOfStatement |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 349 | bool AsmParser::ParseStatement() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 350 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 351 | Lexer.Lex(); |
| 352 | return false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 353 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 354 | |
| 355 | // Statements always start with an identifier. |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 356 | AsmToken ID = Lexer.getTok(); |
| 357 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 358 | StringRef IDVal; |
| 359 | if (ParseIdentifier(IDVal)) |
| 360 | return TokError("unexpected token at start of statement"); |
| 361 | |
| 362 | // FIXME: Recurse on local labels? |
| 363 | |
| 364 | // See what kind of statement we have. |
| 365 | switch (Lexer.getKind()) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 366 | case AsmToken::Colon: { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 367 | // identifier ':' -> Label. |
| 368 | Lexer.Lex(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 369 | |
| 370 | // Diagnose attempt to use a variable as a label. |
| 371 | // |
| 372 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 373 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 374 | // implicitly marked as external. |
| 375 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal); |
| 376 | if (Sym->getSection()) |
| 377 | return Error(IDLoc, "invalid symbol redefinition"); |
| 378 | if (Ctx.GetSymbolValue(Sym)) |
| 379 | return Error(IDLoc, "symbol already used as assembler variable"); |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 380 | |
| 381 | // Since we saw a label, create a symbol and emit it. |
| 382 | // FIXME: If the label starts with L it is an assembler temporary label. |
| 383 | // Why does the client of this api need to know this? |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 384 | Out.EmitLabel(Sym); |
| 385 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 386 | return ParseStatement(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 387 | } |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 389 | case AsmToken::Equal: |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 390 | // identifier '=' ... -> assignment statement |
| 391 | Lexer.Lex(); |
| 392 | |
| 393 | return ParseAssignment(IDVal, false); |
| 394 | |
| 395 | default: // Normal instruction or directive. |
| 396 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | // Otherwise, we have a normal instruction or directive. |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 400 | if (IDVal[0] == '.') { |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 401 | // FIXME: This should be driven based on a hash lookup and callback. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 402 | if (IDVal == ".section") |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 403 | return ParseDirectiveDarwinSection(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 404 | if (IDVal == ".text") |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 405 | // FIXME: This changes behavior based on the -static flag to the |
| 406 | // assembler. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 407 | return ParseDirectiveSectionSwitch("__TEXT", "__text", |
| 408 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 409 | if (IDVal == ".const") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 410 | return ParseDirectiveSectionSwitch("__TEXT", "__const", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 411 | if (IDVal == ".static_const") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 412 | return ParseDirectiveSectionSwitch("__TEXT", "__static_const", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 413 | if (IDVal == ".cstring") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 414 | return ParseDirectiveSectionSwitch("__TEXT","__cstring", |
| 415 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 416 | if (IDVal == ".literal4") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 417 | return ParseDirectiveSectionSwitch("__TEXT", "__literal4", |
| 418 | MCSectionMachO::S_4BYTE_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 419 | if (IDVal == ".literal8") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 420 | return ParseDirectiveSectionSwitch("__TEXT", "__literal8", |
| 421 | MCSectionMachO::S_8BYTE_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 422 | if (IDVal == ".literal16") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 423 | return ParseDirectiveSectionSwitch("__TEXT","__literal16", |
| 424 | MCSectionMachO::S_16BYTE_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 425 | if (IDVal == ".constructor") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 426 | return ParseDirectiveSectionSwitch("__TEXT","__constructor", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 427 | if (IDVal == ".destructor") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 428 | return ParseDirectiveSectionSwitch("__TEXT","__destructor", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 429 | if (IDVal == ".fvmlib_init0") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 430 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 431 | if (IDVal == ".fvmlib_init1") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 432 | return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1", 0); |
| 433 | if (IDVal == ".symbol_stub") |
| 434 | return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub", |
| 435 | MCSectionMachO::S_SYMBOL_STUBS | |
| 436 | MCSectionMachO::S_ATTR_SELF_MODIFYING_CODE | |
| 437 | MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS, |
| 438 | // FIXME: Different on PPC and ARM. |
| 439 | 16); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 440 | // FIXME: .picsymbol_stub on PPC. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 441 | if (IDVal == ".data") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 442 | return ParseDirectiveSectionSwitch("__DATA", "__data", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 443 | if (IDVal == ".static_data") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 444 | return ParseDirectiveSectionSwitch("__DATA", "__static_data", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 445 | if (IDVal == ".non_lazy_symbol_pointer") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 446 | return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_pointer", |
| 447 | MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 448 | if (IDVal == ".lazy_symbol_pointer") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 449 | return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_pointer", |
| 450 | MCSectionMachO::S_LAZY_SYMBOL_POINTERS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 451 | if (IDVal == ".dyld") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 452 | return ParseDirectiveSectionSwitch("__DATA", "__dyld", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 453 | if (IDVal == ".mod_init_func") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 454 | return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func", |
| 455 | MCSectionMachO::S_MOD_INIT_FUNC_POINTERS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 456 | if (IDVal == ".mod_term_func") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 457 | return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func", |
| 458 | MCSectionMachO::S_MOD_TERM_FUNC_POINTERS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 459 | if (IDVal == ".const_data") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 460 | return ParseDirectiveSectionSwitch("__DATA", "__const", 0); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 461 | |
| 462 | |
| 463 | // FIXME: Verify attributes on sections. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 464 | if (IDVal == ".objc_class") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 465 | return ParseDirectiveSectionSwitch("__OBJC", "__class", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 466 | if (IDVal == ".objc_meta_class") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 467 | return ParseDirectiveSectionSwitch("__OBJC", "__meta_class", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 468 | if (IDVal == ".objc_cat_cls_meth") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 469 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 470 | if (IDVal == ".objc_cat_inst_meth") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 471 | return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 472 | if (IDVal == ".objc_protocol") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 473 | return ParseDirectiveSectionSwitch("__OBJC", "__protocol", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 474 | if (IDVal == ".objc_string_object") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 475 | return ParseDirectiveSectionSwitch("__OBJC", "__string_object", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 476 | if (IDVal == ".objc_cls_meth") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 477 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 478 | if (IDVal == ".objc_inst_meth") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 479 | return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 480 | if (IDVal == ".objc_cls_refs") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 481 | return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 482 | if (IDVal == ".objc_message_refs") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 483 | return ParseDirectiveSectionSwitch("__OBJC", "__message_refs", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 484 | if (IDVal == ".objc_symbols") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 485 | return ParseDirectiveSectionSwitch("__OBJC", "__symbols", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 486 | if (IDVal == ".objc_category") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 487 | return ParseDirectiveSectionSwitch("__OBJC", "__category", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 488 | if (IDVal == ".objc_class_vars") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 489 | return ParseDirectiveSectionSwitch("__OBJC", "__class_vars", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 490 | if (IDVal == ".objc_instance_vars") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 491 | return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 492 | if (IDVal == ".objc_module_info") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 493 | return ParseDirectiveSectionSwitch("__OBJC", "__module_info", 0); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 494 | if (IDVal == ".objc_class_names") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 495 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 496 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 497 | if (IDVal == ".objc_meth_var_types") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 498 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 499 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 500 | if (IDVal == ".objc_meth_var_names") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 501 | return ParseDirectiveSectionSwitch("__TEXT", "__cstring", |
| 502 | MCSectionMachO::S_CSTRING_LITERALS); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 503 | if (IDVal == ".objc_selector_strs") |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 504 | return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs", 0); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 505 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 506 | // Assembler features |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 507 | if (IDVal == ".set") |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 508 | return ParseDirectiveSet(); |
| 509 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 510 | // Data directives |
| 511 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 512 | if (IDVal == ".ascii") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 513 | return ParseDirectiveAscii(false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 514 | if (IDVal == ".asciz") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 515 | return ParseDirectiveAscii(true); |
| 516 | |
| 517 | // FIXME: Target hooks for size? Also for "word", "hword". |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 518 | if (IDVal == ".byte") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 519 | return ParseDirectiveValue(1); |
Daniel Dunbar | 1e840b2 | 2009-08-11 04:44:00 +0000 | [diff] [blame] | 520 | if (IDVal == ".short" || IDVal == ".word") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 521 | return ParseDirectiveValue(2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 522 | if (IDVal == ".long") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 523 | return ParseDirectiveValue(4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 524 | if (IDVal == ".quad") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 525 | return ParseDirectiveValue(8); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 526 | |
| 527 | // FIXME: Target hooks for IsPow2. |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 528 | if (IDVal == ".align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 529 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 530 | if (IDVal == ".align32") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 531 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 532 | if (IDVal == ".balign") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 533 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 534 | if (IDVal == ".balignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 535 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 536 | if (IDVal == ".balignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 537 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 538 | if (IDVal == ".p2align") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 539 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 540 | if (IDVal == ".p2alignw") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 541 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 542 | if (IDVal == ".p2alignl") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 543 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
| 544 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 545 | if (IDVal == ".org") |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 546 | return ParseDirectiveOrg(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 547 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 548 | if (IDVal == ".fill") |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 549 | return ParseDirectiveFill(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 550 | if (IDVal == ".space") |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 551 | return ParseDirectiveSpace(); |
| 552 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 553 | // Symbol attribute directives |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 554 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 555 | if (IDVal == ".globl" || IDVal == ".global") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 556 | return ParseDirectiveSymbolAttribute(MCStreamer::Global); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 557 | if (IDVal == ".hidden") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 558 | return ParseDirectiveSymbolAttribute(MCStreamer::Hidden); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 559 | if (IDVal == ".indirect_symbol") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 560 | return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 561 | if (IDVal == ".internal") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 562 | return ParseDirectiveSymbolAttribute(MCStreamer::Internal); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 563 | if (IDVal == ".lazy_reference") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 564 | return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 565 | if (IDVal == ".no_dead_strip") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 566 | return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 567 | if (IDVal == ".private_extern") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 568 | return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 569 | if (IDVal == ".protected") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 570 | return ParseDirectiveSymbolAttribute(MCStreamer::Protected); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 571 | if (IDVal == ".reference") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 572 | return ParseDirectiveSymbolAttribute(MCStreamer::Reference); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 573 | if (IDVal == ".weak") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 574 | return ParseDirectiveSymbolAttribute(MCStreamer::Weak); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 575 | if (IDVal == ".weak_definition") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 576 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 577 | if (IDVal == ".weak_reference") |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 578 | return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference); |
| 579 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 580 | if (IDVal == ".comm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 581 | return ParseDirectiveComm(/*IsLocal=*/false); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 582 | if (IDVal == ".lcomm") |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 583 | return ParseDirectiveComm(/*IsLocal=*/true); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 584 | if (IDVal == ".zerofill") |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 585 | return ParseDirectiveDarwinZerofill(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 586 | if (IDVal == ".desc") |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 587 | return ParseDirectiveDarwinSymbolDesc(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 588 | if (IDVal == ".lsym") |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 589 | return ParseDirectiveDarwinLsym(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 590 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 591 | if (IDVal == ".subsections_via_symbols") |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 592 | return ParseDirectiveDarwinSubsectionsViaSymbols(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 593 | if (IDVal == ".abort") |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 594 | return ParseDirectiveAbort(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 595 | if (IDVal == ".include") |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 596 | return ParseDirectiveInclude(); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 597 | if (IDVal == ".dump") |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 598 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true); |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 599 | if (IDVal == ".load") |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 600 | return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 601 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 602 | // Debugging directives |
| 603 | |
| 604 | if (IDVal == ".file") |
| 605 | return ParseDirectiveFile(IDLoc); |
| 606 | if (IDVal == ".line") |
| 607 | return ParseDirectiveLine(IDLoc); |
| 608 | if (IDVal == ".loc") |
| 609 | return ParseDirectiveLoc(IDLoc); |
| 610 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 611 | Warning(IDLoc, "ignoring directive for now"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 612 | EatToEndOfStatement(); |
| 613 | return false; |
| 614 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 616 | MCInst Inst; |
Daniel Dunbar | 16cdcb3 | 2009-07-28 22:40:46 +0000 | [diff] [blame] | 617 | if (getTargetParser().ParseInstruction(IDVal, Inst)) |
Chris Lattner | 29dfe7c | 2009-06-23 18:41:30 +0000 | [diff] [blame] | 618 | return true; |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 619 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 620 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 621 | return TokError("unexpected token in argument list"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 622 | |
| 623 | // Eat the end of statement marker. |
| 624 | Lexer.Lex(); |
| 625 | |
| 626 | // Instruction is good, process it. |
Daniel Dunbar | 0eebb05 | 2009-07-01 06:35:48 +0000 | [diff] [blame] | 627 | Out.EmitInstruction(Inst); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 628 | |
| 629 | // Skip to end of line for now. |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 632 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 633 | bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 634 | // FIXME: Use better location, we should use proper tokens. |
| 635 | SMLoc EqualLoc = Lexer.getLoc(); |
| 636 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 637 | MCValue Value; |
| 638 | if (ParseRelocatableExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 639 | return true; |
| 640 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 641 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 642 | return TokError("unexpected token in assignment"); |
| 643 | |
| 644 | // Eat the end of statement marker. |
| 645 | Lexer.Lex(); |
| 646 | |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 647 | // Diagnose assignment to a label. |
| 648 | // |
| 649 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 650 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 651 | // implicitly marked as external. |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 652 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 653 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 654 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 655 | if (Sym->getSection()) |
| 656 | return Error(EqualLoc, "invalid assignment to symbol emitted as a label"); |
| 657 | if (Sym->isExternal()) |
| 658 | return Error(EqualLoc, "invalid assignment to external symbol"); |
| 659 | |
| 660 | // Do the assignment. |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 661 | Out.EmitAssignment(Sym, Value, IsDotSet); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 662 | |
| 663 | return false; |
| 664 | } |
| 665 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 666 | /// ParseIdentifier: |
| 667 | /// ::= identifier |
| 668 | /// ::= string |
| 669 | bool AsmParser::ParseIdentifier(StringRef &Res) { |
| 670 | if (Lexer.isNot(AsmToken::Identifier) && |
| 671 | Lexer.isNot(AsmToken::String)) |
| 672 | return true; |
| 673 | |
| 674 | Res = Lexer.getTok().getIdentifier(); |
| 675 | |
| 676 | Lexer.Lex(); // Consume the identifier token. |
| 677 | |
| 678 | return false; |
| 679 | } |
| 680 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 681 | /// ParseDirectiveSet: |
| 682 | /// ::= .set identifier ',' expression |
| 683 | bool AsmParser::ParseDirectiveSet() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 684 | StringRef Name; |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 685 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 686 | if (ParseIdentifier(Name)) |
| 687 | return TokError("expected identifier after '.set' directive"); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 688 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 689 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 690 | return TokError("unexpected token in '.set'"); |
| 691 | Lexer.Lex(); |
| 692 | |
| 693 | return ParseAssignment(Name, true); |
| 694 | } |
| 695 | |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 696 | /// ParseDirectiveSection: |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 697 | /// ::= .section identifier (',' identifier)* |
| 698 | /// FIXME: This should actually parse out the segment, section, attributes and |
| 699 | /// sizeof_stub fields. |
| 700 | bool AsmParser::ParseDirectiveDarwinSection() { |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 701 | SMLoc Loc = Lexer.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 702 | |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 703 | StringRef SectionName; |
| 704 | if (ParseIdentifier(SectionName)) |
| 705 | return Error(Loc, "expected identifier after '.section' directive"); |
| 706 | |
| 707 | // Verify there is a following comma. |
| 708 | if (!Lexer.is(AsmToken::Comma)) |
| 709 | return TokError("unexpected token in '.section' directive"); |
| 710 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 711 | std::string SectionSpec = SectionName; |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 712 | SectionSpec += ","; |
| 713 | |
| 714 | // Add all the tokens until the end of the line, ParseSectionSpecifier will |
| 715 | // handle this. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 716 | StringRef EOL = Lexer.LexUntilEndOfStatement(); |
| 717 | SectionSpec.append(EOL.begin(), EOL.end()); |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 718 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 719 | Lexer.Lex(); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 720 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 721 | return TokError("unexpected token in '.section' directive"); |
| 722 | Lexer.Lex(); |
| 723 | |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 724 | |
| 725 | StringRef Segment, Section; |
| 726 | unsigned TAA, StubSize; |
| 727 | std::string ErrorStr = |
| 728 | MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section, |
| 729 | TAA, StubSize); |
| 730 | |
| 731 | if (!ErrorStr.empty()) |
Daniel Dunbar | ace6312 | 2009-08-11 03:42:33 +0000 | [diff] [blame] | 732 | return Error(Loc, ErrorStr.c_str()); |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 733 | |
| 734 | // FIXME: CACHE THESE. |
| 735 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 736 | // FIXME: Arch specific. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 737 | MCSection *S = 0; //Ctx.GetSection(Section); |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 738 | if (S == 0) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 739 | S = MCSectionMachO::Create(Segment, Section, TAA, StubSize, |
| 740 | SectionKind(), Ctx); |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 741 | |
| 742 | Out.SwitchSection(S); |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 743 | return false; |
| 744 | } |
| 745 | |
Chris Lattner | e15c2d7 | 2009-08-10 18:05:55 +0000 | [diff] [blame] | 746 | /// ParseDirectiveSectionSwitch - |
| 747 | /// |
| 748 | /// FIXME! Many of these directives implicitly cause a ".align" directive to get |
| 749 | /// emitted, we don't do this yet which can lead to subtle miscompiles. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 750 | bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment, |
| 751 | const char *Section, |
| 752 | unsigned TAA, unsigned StubSize) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 753 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 754 | return TokError("unexpected token in section switching directive"); |
| 755 | Lexer.Lex(); |
| 756 | |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 757 | // FIXME: Arch specific. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 758 | // FIXME: Cache this! |
| 759 | MCSection *S = 0; // Ctx.GetSection(Section); |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 760 | if (S == 0) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 761 | S = MCSectionMachO::Create(Segment, Section, TAA, StubSize, |
| 762 | SectionKind(), Ctx); |
Chris Lattner | 56594f9 | 2009-07-31 17:47:16 +0000 | [diff] [blame] | 763 | |
| 764 | Out.SwitchSection(S); |
Chris Lattner | 529fb54 | 2009-06-24 05:13:15 +0000 | [diff] [blame] | 765 | return false; |
| 766 | } |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 767 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame^] | 768 | bool AsmParser::ParseEscapedString(std::string &Data) { |
| 769 | assert(Lexer.is(AsmToken::String) && "Unexpected current token!"); |
| 770 | |
| 771 | Data = ""; |
| 772 | StringRef Str = Lexer.getTok().getStringContents(); |
| 773 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 774 | if (Str[i] != '\\') { |
| 775 | Data += Str[i]; |
| 776 | continue; |
| 777 | } |
| 778 | |
| 779 | // Recognize escaped characters. Note that this escape semantics currently |
| 780 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 781 | ++i; |
| 782 | if (i == e) |
| 783 | return TokError("unexpected backslash at end of string"); |
| 784 | |
| 785 | // Recognize octal sequences. |
| 786 | if ((unsigned) (Str[i] - '0') <= 7) { |
| 787 | // Consume up to three octal characters. |
| 788 | unsigned Value = Str[i] - '0'; |
| 789 | |
| 790 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 791 | ++i; |
| 792 | Value = Value * 8 + (Str[i] - '0'); |
| 793 | |
| 794 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 795 | ++i; |
| 796 | Value = Value * 8 + (Str[i] - '0'); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | if (Value > 255) |
| 801 | return TokError("invalid octal escape sequence (out of range)"); |
| 802 | |
| 803 | Data += (unsigned char) Value; |
| 804 | continue; |
| 805 | } |
| 806 | |
| 807 | // Otherwise recognize individual escapes. |
| 808 | switch (Str[i]) { |
| 809 | default: |
| 810 | // Just reject invalid escape sequences for now. |
| 811 | return TokError("invalid escape sequence (unrecognized character)"); |
| 812 | |
| 813 | case 'b': Data += '\b'; break; |
| 814 | case 'f': Data += '\f'; break; |
| 815 | case 'n': Data += '\n'; break; |
| 816 | case 'r': Data += '\r'; break; |
| 817 | case 't': Data += '\t'; break; |
| 818 | case '"': Data += '"'; break; |
| 819 | case '\\': Data += '\\'; break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | return false; |
| 824 | } |
| 825 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 826 | /// ParseDirectiveAscii: |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 827 | /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ] |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 828 | bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 829 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 830 | for (;;) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 831 | if (Lexer.isNot(AsmToken::String)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 832 | return TokError("expected string in '.ascii' or '.asciz' directive"); |
| 833 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame^] | 834 | std::string Data; |
| 835 | if (ParseEscapedString(Data)) |
| 836 | return true; |
| 837 | |
| 838 | Out.EmitBytes(Data); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 839 | if (ZeroTerminated) |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 840 | Out.EmitBytes(StringRef("\0", 1)); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 841 | |
| 842 | Lexer.Lex(); |
| 843 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 844 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 845 | break; |
| 846 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 847 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 848 | return TokError("unexpected token in '.ascii' or '.asciz' directive"); |
| 849 | Lexer.Lex(); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | Lexer.Lex(); |
| 854 | return false; |
| 855 | } |
| 856 | |
| 857 | /// ParseDirectiveValue |
| 858 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 859 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 860 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 861 | for (;;) { |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 862 | MCValue Expr; |
| 863 | if (ParseRelocatableExpression(Expr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 864 | return true; |
| 865 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 866 | Out.EmitValue(Expr, Size); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 867 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 868 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 869 | break; |
| 870 | |
| 871 | // FIXME: Improve diagnostic. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 872 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 873 | return TokError("unexpected token in directive"); |
| 874 | Lexer.Lex(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | Lexer.Lex(); |
| 879 | return false; |
| 880 | } |
| 881 | |
| 882 | /// ParseDirectiveSpace |
| 883 | /// ::= .space expression [ , expression ] |
| 884 | bool AsmParser::ParseDirectiveSpace() { |
| 885 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 886 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 887 | return true; |
| 888 | |
| 889 | int64_t FillExpr = 0; |
| 890 | bool HasFillExpr = false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 891 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 892 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 893 | return TokError("unexpected token in '.space' directive"); |
| 894 | Lexer.Lex(); |
| 895 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 896 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 897 | return true; |
| 898 | |
| 899 | HasFillExpr = true; |
| 900 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 901 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 902 | return TokError("unexpected token in '.space' directive"); |
| 903 | } |
| 904 | |
| 905 | Lexer.Lex(); |
| 906 | |
| 907 | if (NumBytes <= 0) |
| 908 | return TokError("invalid number of bytes in '.space' directive"); |
| 909 | |
| 910 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
| 911 | for (uint64_t i = 0, e = NumBytes; i != e; ++i) |
| 912 | Out.EmitValue(MCValue::get(FillExpr), 1); |
| 913 | |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | /// ParseDirectiveFill |
| 918 | /// ::= .fill expression , expression , expression |
| 919 | bool AsmParser::ParseDirectiveFill() { |
| 920 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 921 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 922 | return true; |
| 923 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 924 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 925 | return TokError("unexpected token in '.fill' directive"); |
| 926 | Lexer.Lex(); |
| 927 | |
| 928 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 929 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 930 | return true; |
| 931 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 932 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 933 | return TokError("unexpected token in '.fill' directive"); |
| 934 | Lexer.Lex(); |
| 935 | |
| 936 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 937 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 938 | return true; |
| 939 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 940 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 941 | return TokError("unexpected token in '.fill' directive"); |
| 942 | |
| 943 | Lexer.Lex(); |
| 944 | |
| 945 | if (FillSize != 1 && FillSize != 2 && FillSize != 4) |
| 946 | return TokError("invalid '.fill' size, expected 1, 2, or 4"); |
| 947 | |
| 948 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
| 949 | Out.EmitValue(MCValue::get(FillExpr), FillSize); |
| 950 | |
| 951 | return false; |
| 952 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 953 | |
| 954 | /// ParseDirectiveOrg |
| 955 | /// ::= .org expression [ , expression ] |
| 956 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 957 | MCValue Offset; |
| 958 | if (ParseRelocatableExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 959 | return true; |
| 960 | |
| 961 | // Parse optional fill expression. |
| 962 | int64_t FillExpr = 0; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 963 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 964 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 965 | return TokError("unexpected token in '.org' directive"); |
| 966 | Lexer.Lex(); |
| 967 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 968 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 969 | return true; |
| 970 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 971 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 972 | return TokError("unexpected token in '.org' directive"); |
| 973 | } |
| 974 | |
| 975 | Lexer.Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 976 | |
| 977 | // FIXME: Only limited forms of relocatable expressions are accepted here, it |
| 978 | // has to be relative to the current section. |
| 979 | Out.EmitValueToOffset(Offset, FillExpr); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 980 | |
| 981 | return false; |
| 982 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 983 | |
| 984 | /// ParseDirectiveAlign |
| 985 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 986 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
| 987 | int64_t Alignment; |
| 988 | if (ParseAbsoluteExpression(Alignment)) |
| 989 | return true; |
| 990 | |
| 991 | SMLoc MaxBytesLoc; |
| 992 | bool HasFillExpr = false; |
| 993 | int64_t FillExpr = 0; |
| 994 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 995 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 996 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 997 | return TokError("unexpected token in directive"); |
| 998 | Lexer.Lex(); |
| 999 | |
| 1000 | // The fill expression can be omitted while specifying a maximum number of |
| 1001 | // alignment bytes, e.g: |
| 1002 | // .align 3,,4 |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1003 | if (Lexer.isNot(AsmToken::Comma)) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1004 | HasFillExpr = true; |
| 1005 | if (ParseAbsoluteExpression(FillExpr)) |
| 1006 | return true; |
| 1007 | } |
| 1008 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1009 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1010 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1011 | return TokError("unexpected token in directive"); |
| 1012 | Lexer.Lex(); |
| 1013 | |
| 1014 | MaxBytesLoc = Lexer.getLoc(); |
| 1015 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 1016 | return true; |
| 1017 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1018 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1019 | return TokError("unexpected token in directive"); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | Lexer.Lex(); |
| 1024 | |
| 1025 | if (!HasFillExpr) { |
| 1026 | // FIXME: Sometimes fill with nop. |
| 1027 | FillExpr = 0; |
| 1028 | } |
| 1029 | |
| 1030 | // Compute alignment in bytes. |
| 1031 | if (IsPow2) { |
| 1032 | // FIXME: Diagnose overflow. |
Chris Lattner | 3975025 | 2009-07-11 22:32:37 +0000 | [diff] [blame] | 1033 | Alignment = 1LL << Alignment; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | // Diagnose non-sensical max bytes to fill. |
| 1037 | if (MaxBytesLoc.isValid()) { |
| 1038 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1039 | Warning(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 1040 | "many bytes, ignoring"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1041 | return false; |
| 1042 | } |
| 1043 | |
| 1044 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 1045 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 1046 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1047 | MaxBytesToFill = 0; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
| 1052 | Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill); |
| 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1057 | /// ParseDirectiveSymbolAttribute |
| 1058 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
| 1059 | bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1060 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1061 | for (;;) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1062 | StringRef Name; |
| 1063 | |
| 1064 | if (ParseIdentifier(Name)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1065 | return TokError("expected identifier in directive"); |
| 1066 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1067 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1068 | |
| 1069 | // If this is use of an undefined symbol then mark it external. |
| 1070 | if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym)) |
| 1071 | Sym->setExternal(true); |
| 1072 | |
| 1073 | Out.EmitSymbolAttribute(Sym, Attr); |
| 1074 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1075 | if (Lexer.is(AsmToken::EndOfStatement)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1076 | break; |
| 1077 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1078 | if (Lexer.isNot(AsmToken::Comma)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 1079 | return TokError("unexpected token in directive"); |
| 1080 | Lexer.Lex(); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | Lexer.Lex(); |
| 1085 | return false; |
| 1086 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1087 | |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1088 | /// ParseDirectiveDarwinSymbolDesc |
| 1089 | /// ::= .desc identifier , expression |
| 1090 | bool AsmParser::ParseDirectiveDarwinSymbolDesc() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1091 | StringRef Name; |
| 1092 | if (ParseIdentifier(Name)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1093 | return TokError("expected identifier in directive"); |
| 1094 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1095 | // Handle the identifier as the key symbol. |
| 1096 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1097 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1098 | if (Lexer.isNot(AsmToken::Comma)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1099 | return TokError("unexpected token in '.desc' directive"); |
| 1100 | Lexer.Lex(); |
| 1101 | |
| 1102 | SMLoc DescLoc = Lexer.getLoc(); |
| 1103 | int64_t DescValue; |
| 1104 | if (ParseAbsoluteExpression(DescValue)) |
| 1105 | return true; |
| 1106 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1107 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 95cf30c | 2009-07-14 18:17:10 +0000 | [diff] [blame] | 1108 | return TokError("unexpected token in '.desc' directive"); |
| 1109 | |
| 1110 | Lexer.Lex(); |
| 1111 | |
| 1112 | // Set the n_desc field of this Symbol to this DescValue |
| 1113 | Out.EmitSymbolDesc(Sym, DescValue); |
| 1114 | |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1118 | /// ParseDirectiveComm |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1119 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
| 1120 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1121 | SMLoc IDLoc = Lexer.getLoc(); |
| 1122 | StringRef Name; |
| 1123 | if (ParseIdentifier(Name)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1124 | return TokError("expected identifier in directive"); |
| 1125 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1126 | // Handle the identifier as the key symbol. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1127 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1128 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1129 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1130 | return TokError("unexpected token in directive"); |
| 1131 | Lexer.Lex(); |
| 1132 | |
| 1133 | int64_t Size; |
| 1134 | SMLoc SizeLoc = Lexer.getLoc(); |
| 1135 | if (ParseAbsoluteExpression(Size)) |
| 1136 | return true; |
| 1137 | |
| 1138 | int64_t Pow2Alignment = 0; |
| 1139 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1140 | if (Lexer.is(AsmToken::Comma)) { |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1141 | Lexer.Lex(); |
| 1142 | Pow2AlignmentLoc = Lexer.getLoc(); |
| 1143 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1147 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1148 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1149 | |
| 1150 | Lexer.Lex(); |
| 1151 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1152 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 1153 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1154 | if (Size < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1155 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
| 1156 | "be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1157 | |
| 1158 | // NOTE: The alignment in the directive is a power of 2 value, the assember |
| 1159 | // may internally end up wanting an alignment in bytes. |
| 1160 | // FIXME: Diagnose overflow. |
| 1161 | if (Pow2Alignment < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1162 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
| 1163 | "alignment, can't be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1164 | |
| 1165 | // TODO: Symbol must be undefined or it is a error to re-defined the symbol |
| 1166 | if (Sym->getSection() || Ctx.GetSymbolValue(Sym)) |
| 1167 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1168 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 1169 | // Create the Symbol as a common or local common with Size and Pow2Alignment |
| 1170 | Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 1171 | |
| 1172 | return false; |
| 1173 | } |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1174 | |
| 1175 | /// ParseDirectiveDarwinZerofill |
| 1176 | /// ::= .zerofill segname , sectname [, identifier , size_expression [ |
| 1177 | /// , align_expression ]] |
| 1178 | bool AsmParser::ParseDirectiveDarwinZerofill() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1179 | // FIXME: Handle quoted names here. |
| 1180 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1181 | if (Lexer.isNot(AsmToken::Identifier)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1182 | return TokError("expected segment name after '.zerofill' directive"); |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1183 | StringRef Segment = Lexer.getTok().getString(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1184 | Lexer.Lex(); |
| 1185 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1186 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1187 | return TokError("unexpected token in directive"); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1188 | Lexer.Lex(); |
| 1189 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1190 | if (Lexer.isNot(AsmToken::Identifier)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1191 | return TokError("expected section name after comma in '.zerofill' " |
| 1192 | "directive"); |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1193 | StringRef Section = Lexer.getTok().getString(); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1194 | Lexer.Lex(); |
| 1195 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1196 | // If this is the end of the line all that was wanted was to create the |
| 1197 | // the section but with no symbol. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1198 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1199 | // FIXME: CACHE THIS. |
| 1200 | MCSection *S = 0; //Ctx.GetSection(Section); |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1201 | if (S == 0) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1202 | S = MCSectionMachO::Create(Segment, Section, |
| 1203 | MCSectionMachO::S_ZEROFILL, 0, |
| 1204 | SectionKind(), Ctx); |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1205 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1206 | // Create the zerofill section but no symbol |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1207 | Out.EmitZerofill(S); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1208 | return false; |
| 1209 | } |
| 1210 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1211 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1212 | return TokError("unexpected token in directive"); |
| 1213 | Lexer.Lex(); |
| 1214 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1215 | if (Lexer.isNot(AsmToken::Identifier)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1216 | return TokError("expected identifier in directive"); |
| 1217 | |
| 1218 | // handle the identifier as the key symbol. |
| 1219 | SMLoc IDLoc = Lexer.getLoc(); |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1220 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString()); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1221 | Lexer.Lex(); |
| 1222 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1223 | if (Lexer.isNot(AsmToken::Comma)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1224 | return TokError("unexpected token in directive"); |
| 1225 | Lexer.Lex(); |
| 1226 | |
| 1227 | int64_t Size; |
| 1228 | SMLoc SizeLoc = Lexer.getLoc(); |
| 1229 | if (ParseAbsoluteExpression(Size)) |
| 1230 | return true; |
| 1231 | |
| 1232 | int64_t Pow2Alignment = 0; |
| 1233 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1234 | if (Lexer.is(AsmToken::Comma)) { |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1235 | Lexer.Lex(); |
| 1236 | Pow2AlignmentLoc = Lexer.getLoc(); |
| 1237 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 1238 | return true; |
| 1239 | } |
| 1240 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1241 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1242 | return TokError("unexpected token in '.zerofill' directive"); |
| 1243 | |
| 1244 | Lexer.Lex(); |
| 1245 | |
| 1246 | if (Size < 0) |
| 1247 | return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less " |
| 1248 | "than zero"); |
| 1249 | |
| 1250 | // NOTE: The alignment in the directive is a power of 2 value, the assember |
| 1251 | // may internally end up wanting an alignment in bytes. |
| 1252 | // FIXME: Diagnose overflow. |
| 1253 | if (Pow2Alignment < 0) |
| 1254 | return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, " |
| 1255 | "can't be less than zero"); |
| 1256 | |
| 1257 | // TODO: Symbol must be undefined or it is a error to re-defined the symbol |
| 1258 | if (Sym->getSection() || Ctx.GetSymbolValue(Sym)) |
| 1259 | return Error(IDLoc, "invalid symbol redefinition"); |
| 1260 | |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1261 | // FIXME: Arch specific. |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1262 | // FIXME: CACHE. |
| 1263 | MCSection *S = 0; //Ctx.GetSection(Section); |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1264 | if (S == 0) |
Chris Lattner | ff4bc46 | 2009-08-10 01:39:42 +0000 | [diff] [blame] | 1265 | S = MCSectionMachO::Create(Segment, Section, |
| 1266 | MCSectionMachO::S_ZEROFILL, 0, |
| 1267 | SectionKind(), Ctx); |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1269 | // Create the zerofill Symbol with Size and Pow2Alignment |
Chris Lattner | 6bdd74c | 2009-07-31 18:27:48 +0000 | [diff] [blame] | 1270 | Out.EmitZerofill(S, Sym, Size, Pow2Alignment); |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 1271 | |
| 1272 | return false; |
| 1273 | } |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1274 | |
| 1275 | /// ParseDirectiveDarwinSubsectionsViaSymbols |
| 1276 | /// ::= .subsections_via_symbols |
| 1277 | bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1278 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1279 | return TokError("unexpected token in '.subsections_via_symbols' directive"); |
| 1280 | |
| 1281 | Lexer.Lex(); |
| 1282 | |
Kevin Enderby | f96db46 | 2009-07-16 17:56:39 +0000 | [diff] [blame] | 1283 | Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols); |
Kevin Enderby | a5c7832 | 2009-07-13 21:03:15 +0000 | [diff] [blame] | 1284 | |
| 1285 | return false; |
| 1286 | } |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1287 | |
| 1288 | /// ParseDirectiveAbort |
| 1289 | /// ::= .abort [ "abort_string" ] |
| 1290 | bool AsmParser::ParseDirectiveAbort() { |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1291 | // FIXME: Use loc from directive. |
| 1292 | SMLoc Loc = Lexer.getLoc(); |
| 1293 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1294 | StringRef Str = ""; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1295 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1296 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1297 | return TokError("expected string in '.abort' directive"); |
| 1298 | |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1299 | Str = Lexer.getTok().getString(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1300 | |
| 1301 | Lexer.Lex(); |
| 1302 | } |
| 1303 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1304 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1305 | return TokError("unexpected token in '.abort' directive"); |
| 1306 | |
| 1307 | Lexer.Lex(); |
| 1308 | |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 1309 | // FIXME: Handle here. |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 1310 | if (Str.empty()) |
| 1311 | Error(Loc, ".abort detected. Assembly stopping."); |
| 1312 | else |
| 1313 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 1314 | |
| 1315 | return false; |
| 1316 | } |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1317 | |
| 1318 | /// ParseDirectiveLsym |
| 1319 | /// ::= .lsym identifier , expression |
| 1320 | bool AsmParser::ParseDirectiveDarwinLsym() { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1321 | StringRef Name; |
| 1322 | if (ParseIdentifier(Name)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1323 | return TokError("expected identifier in directive"); |
| 1324 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 1325 | // Handle the identifier as the key symbol. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1326 | MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name); |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1327 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1328 | if (Lexer.isNot(AsmToken::Comma)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1329 | return TokError("unexpected token in '.lsym' directive"); |
| 1330 | Lexer.Lex(); |
| 1331 | |
| 1332 | MCValue Expr; |
| 1333 | if (ParseRelocatableExpression(Expr)) |
| 1334 | return true; |
| 1335 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1336 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 1337 | return TokError("unexpected token in '.lsym' directive"); |
| 1338 | |
| 1339 | Lexer.Lex(); |
| 1340 | |
| 1341 | // Create the Sym with the value of the Expr |
| 1342 | Out.EmitLocalSymbol(Sym, Expr); |
| 1343 | |
| 1344 | return false; |
| 1345 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1346 | |
| 1347 | /// ParseDirectiveInclude |
| 1348 | /// ::= .include "filename" |
| 1349 | bool AsmParser::ParseDirectiveInclude() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1350 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1351 | return TokError("expected string in '.include' directive"); |
| 1352 | |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1353 | std::string Filename = Lexer.getTok().getString(); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1354 | SMLoc IncludeLoc = Lexer.getLoc(); |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1355 | Lexer.Lex(); |
| 1356 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1357 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1358 | return TokError("unexpected token in '.include' directive"); |
| 1359 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 1360 | // Strip the quotes. |
| 1361 | Filename = Filename.substr(1, Filename.size()-2); |
| 1362 | |
| 1363 | // Attempt to switch the lexer to the included file before consuming the end |
| 1364 | // of statement to avoid losing it when we switch. |
| 1365 | if (Lexer.EnterIncludeFile(Filename)) { |
| 1366 | Lexer.PrintMessage(IncludeLoc, |
| 1367 | "Could not find include file '" + Filename + "'", |
| 1368 | "error"); |
| 1369 | return true; |
| 1370 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 1371 | |
| 1372 | return false; |
| 1373 | } |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1374 | |
| 1375 | /// ParseDirectiveDarwinDumpOrLoad |
| 1376 | /// ::= ( .dump | .load ) "filename" |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1377 | bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1378 | if (Lexer.isNot(AsmToken::String)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1379 | return TokError("expected string in '.dump' or '.load' directive"); |
| 1380 | |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1381 | Lexer.Lex(); |
| 1382 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1383 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1384 | return TokError("unexpected token in '.dump' or '.load' directive"); |
| 1385 | |
| 1386 | Lexer.Lex(); |
| 1387 | |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1388 | // FIXME: If/when .dump and .load are implemented they will be done in the |
| 1389 | // the assembly parser and not have any need for an MCStreamer API. |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1390 | if (IsDump) |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1391 | Warning(IDLoc, "ignoring directive .dump for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1392 | else |
Kevin Enderby | 5026ae4 | 2009-07-20 20:25:37 +0000 | [diff] [blame] | 1393 | Warning(IDLoc, "ignoring directive .load for now"); |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 1394 | |
| 1395 | return false; |
| 1396 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 1397 | |
| 1398 | /// ParseDirectiveIf |
| 1399 | /// ::= .if expression |
| 1400 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { |
| 1401 | // Consume the identifier that was the .if directive |
| 1402 | Lexer.Lex(); |
| 1403 | |
| 1404 | TheCondStack.push_back(TheCondState); |
| 1405 | TheCondState.TheCond = AsmCond::IfCond; |
| 1406 | if(TheCondState.Ignore) { |
| 1407 | EatToEndOfStatement(); |
| 1408 | } |
| 1409 | else { |
| 1410 | int64_t ExprValue; |
| 1411 | if (ParseAbsoluteExpression(ExprValue)) |
| 1412 | return true; |
| 1413 | |
| 1414 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1415 | return TokError("unexpected token in '.if' directive"); |
| 1416 | |
| 1417 | Lexer.Lex(); |
| 1418 | |
| 1419 | TheCondState.CondMet = ExprValue; |
| 1420 | TheCondState.Ignore = !TheCondState.CondMet; |
| 1421 | } |
| 1422 | |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | /// ParseDirectiveElseIf |
| 1427 | /// ::= .elseif expression |
| 1428 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { |
| 1429 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 1430 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 1431 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 1432 | " an .elseif"); |
| 1433 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 1434 | |
| 1435 | // Consume the identifier that was the .elseif directive |
| 1436 | Lexer.Lex(); |
| 1437 | |
| 1438 | bool LastIgnoreState = false; |
| 1439 | if (!TheCondStack.empty()) |
| 1440 | LastIgnoreState = TheCondStack.back().Ignore; |
| 1441 | if (LastIgnoreState || TheCondState.CondMet) { |
| 1442 | TheCondState.Ignore = true; |
| 1443 | EatToEndOfStatement(); |
| 1444 | } |
| 1445 | else { |
| 1446 | int64_t ExprValue; |
| 1447 | if (ParseAbsoluteExpression(ExprValue)) |
| 1448 | return true; |
| 1449 | |
| 1450 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1451 | return TokError("unexpected token in '.elseif' directive"); |
| 1452 | |
| 1453 | Lexer.Lex(); |
| 1454 | TheCondState.CondMet = ExprValue; |
| 1455 | TheCondState.Ignore = !TheCondState.CondMet; |
| 1456 | } |
| 1457 | |
| 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | /// ParseDirectiveElse |
| 1462 | /// ::= .else |
| 1463 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { |
| 1464 | // Consume the identifier that was the .else directive |
| 1465 | Lexer.Lex(); |
| 1466 | |
| 1467 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1468 | return TokError("unexpected token in '.else' directive"); |
| 1469 | |
| 1470 | Lexer.Lex(); |
| 1471 | |
| 1472 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 1473 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 1474 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 1475 | ".elseif"); |
| 1476 | TheCondState.TheCond = AsmCond::ElseCond; |
| 1477 | bool LastIgnoreState = false; |
| 1478 | if (!TheCondStack.empty()) |
| 1479 | LastIgnoreState = TheCondStack.back().Ignore; |
| 1480 | if (LastIgnoreState || TheCondState.CondMet) |
| 1481 | TheCondState.Ignore = true; |
| 1482 | else |
| 1483 | TheCondState.Ignore = false; |
| 1484 | |
| 1485 | return false; |
| 1486 | } |
| 1487 | |
| 1488 | /// ParseDirectiveEndIf |
| 1489 | /// ::= .endif |
| 1490 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { |
| 1491 | // Consume the identifier that was the .endif directive |
| 1492 | Lexer.Lex(); |
| 1493 | |
| 1494 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1495 | return TokError("unexpected token in '.endif' directive"); |
| 1496 | |
| 1497 | Lexer.Lex(); |
| 1498 | |
| 1499 | if ((TheCondState.TheCond == AsmCond::NoCond) || |
| 1500 | TheCondStack.empty()) |
| 1501 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 1502 | ".else"); |
| 1503 | if (!TheCondStack.empty()) { |
| 1504 | TheCondState = TheCondStack.back(); |
| 1505 | TheCondStack.pop_back(); |
| 1506 | } |
| 1507 | |
| 1508 | return false; |
| 1509 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 1510 | |
| 1511 | /// ParseDirectiveFile |
| 1512 | /// ::= .file [number] string |
| 1513 | bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) { |
| 1514 | // FIXME: I'm not sure what this is. |
| 1515 | int64_t FileNumber = -1; |
| 1516 | if (Lexer.is(AsmToken::Integer)) { |
| 1517 | FileNumber = Lexer.getTok().getIntVal(); |
| 1518 | Lexer.Lex(); |
| 1519 | |
| 1520 | if (FileNumber < 1) |
| 1521 | return TokError("file number less than one"); |
| 1522 | } |
| 1523 | |
| 1524 | if (Lexer.isNot(AsmToken::String)) |
| 1525 | return TokError("unexpected token in '.file' directive"); |
| 1526 | |
| 1527 | StringRef FileName = Lexer.getTok().getString(); |
| 1528 | Lexer.Lex(); |
| 1529 | |
| 1530 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1531 | return TokError("unexpected token in '.file' directive"); |
| 1532 | |
| 1533 | // FIXME: Do something with the .file. |
| 1534 | |
| 1535 | return false; |
| 1536 | } |
| 1537 | |
| 1538 | /// ParseDirectiveLine |
| 1539 | /// ::= .line [number] |
| 1540 | bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) { |
| 1541 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1542 | if (Lexer.isNot(AsmToken::Integer)) |
| 1543 | return TokError("unexpected token in '.line' directive"); |
| 1544 | |
| 1545 | int64_t LineNumber = Lexer.getTok().getIntVal(); |
| 1546 | (void) LineNumber; |
| 1547 | Lexer.Lex(); |
| 1548 | |
| 1549 | // FIXME: Do something with the .line. |
| 1550 | } |
| 1551 | |
| 1552 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1553 | return TokError("unexpected token in '.file' directive"); |
| 1554 | |
| 1555 | return false; |
| 1556 | } |
| 1557 | |
| 1558 | |
| 1559 | /// ParseDirectiveLoc |
| 1560 | /// ::= .loc number [number [number]] |
| 1561 | bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) { |
| 1562 | if (Lexer.isNot(AsmToken::Integer)) |
| 1563 | return TokError("unexpected token in '.loc' directive"); |
| 1564 | |
| 1565 | // FIXME: What are these fields? |
| 1566 | int64_t FileNumber = Lexer.getTok().getIntVal(); |
| 1567 | (void) FileNumber; |
| 1568 | // FIXME: Validate file. |
| 1569 | |
| 1570 | Lexer.Lex(); |
| 1571 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1572 | if (Lexer.isNot(AsmToken::Integer)) |
| 1573 | return TokError("unexpected token in '.loc' directive"); |
| 1574 | |
| 1575 | int64_t Param2 = Lexer.getTok().getIntVal(); |
| 1576 | (void) Param2; |
| 1577 | Lexer.Lex(); |
| 1578 | |
| 1579 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 1580 | if (Lexer.isNot(AsmToken::Integer)) |
| 1581 | return TokError("unexpected token in '.loc' directive"); |
| 1582 | |
| 1583 | int64_t Param3 = Lexer.getTok().getIntVal(); |
| 1584 | (void) Param3; |
| 1585 | Lexer.Lex(); |
| 1586 | |
| 1587 | // FIXME: Do something with the .loc. |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 1592 | return TokError("unexpected token in '.file' directive"); |
| 1593 | |
| 1594 | return false; |
| 1595 | } |
| 1596 | |