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