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