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