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