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