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