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