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