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