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