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