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