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