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