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 | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringMap.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" |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCExpr.h" |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCInstPrinter.h" |
| 23 | #include "llvm/MC/MCInstrInfo.h" |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCParser/AsmCond.h" |
| 25 | #include "llvm/MC/MCParser/AsmLexer.h" |
| 26 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 27 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Evan Cheng | e76a33b | 2011-07-20 05:58:47 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCRegisterInfo.h" |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCTargetAsmParser.h" |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
Benjamin Kramer | 518ff56 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ErrorHandling.h" |
Jim Grosbach | 254cf03 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MathExtras.h" |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 37 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Nick Lewycky | 476b242 | 2010-12-19 20:43:38 +0000 | [diff] [blame] | 39 | #include <cctype> |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 40 | #include <set> |
| 41 | #include <string> |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 42 | #include <vector> |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | FatalAssemblerWarnings("fatal-assembler-warnings", |
| 47 | cl::desc("Consider warnings as error")); |
| 48 | |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 49 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} |
Nick Lewycky | 0d7d11d | 2012-10-19 07:00:09 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | |
Eli Bendersky | f9f40bd | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 53 | /// \brief Helper types for tracking macro definitions. |
| 54 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
| 55 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
| 56 | typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter; |
| 57 | typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; |
| 58 | |
| 59 | struct MCAsmMacro { |
| 60 | StringRef Name; |
| 61 | StringRef Body; |
| 62 | MCAsmMacroParameters Parameters; |
| 63 | |
| 64 | public: |
| 65 | MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) : |
| 66 | Name(N), Body(B), Parameters(P) {} |
| 67 | |
| 68 | MCAsmMacro(const MCAsmMacro& Other) |
| 69 | : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {} |
| 70 | }; |
| 71 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 72 | /// \brief Helper class for storing information about an active macro |
| 73 | /// instantiation. |
| 74 | struct MacroInstantiation { |
| 75 | /// The macro being instantiated. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 76 | const MCAsmMacro *TheMacro; |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 77 | |
| 78 | /// The macro instantiation with substitutions. |
| 79 | MemoryBuffer *Instantiation; |
| 80 | |
| 81 | /// The location of the instantiation. |
| 82 | SMLoc InstantiationLoc; |
| 83 | |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 84 | /// The buffer where parsing should resume upon instantiation completion. |
| 85 | int ExitBuffer; |
| 86 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 87 | /// The location where parsing should resume upon instantiation completion. |
| 88 | SMLoc ExitLoc; |
| 89 | |
| 90 | public: |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 91 | MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL, |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 92 | MemoryBuffer *I); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 95 | struct ParseStatementInfo { |
| 96 | /// ParsedOperands - The parsed operands from the last parsed statement. |
| 97 | SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; |
| 98 | |
| 99 | /// Opcode - The opcode from the last parsed instruction. |
| 100 | unsigned Opcode; |
| 101 | |
Chad Rosier | 5749801 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 102 | /// Error - Was there an error parsing the inline assembly? |
| 103 | bool ParseError; |
| 104 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 105 | SmallVectorImpl<AsmRewrite> *AsmRewrites; |
| 106 | |
Chad Rosier | 5749801 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 107 | ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {} |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 108 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
Chad Rosier | 5749801 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 109 | : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 110 | |
| 111 | ~ParseStatementInfo() { |
| 112 | // Free any parsed operands. |
| 113 | for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) |
| 114 | delete ParsedOperands[i]; |
| 115 | ParsedOperands.clear(); |
| 116 | } |
| 117 | }; |
| 118 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 119 | /// \brief The concrete assembly parser instance. |
| 120 | class AsmParser : public MCAsmParser { |
Craig Topper | 85aadc0 | 2012-09-15 16:23:52 +0000 | [diff] [blame] | 121 | AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION; |
| 122 | void operator=(const AsmParser &) LLVM_DELETED_FUNCTION; |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 123 | private: |
| 124 | AsmLexer Lexer; |
| 125 | MCContext &Ctx; |
| 126 | MCStreamer &Out; |
Jim Grosbach | e82b8ee | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 127 | const MCAsmInfo &MAI; |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 128 | SourceMgr &SrcMgr; |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 129 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
| 130 | void *SavedDiagContext; |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 131 | MCAsmParserExtension *PlatformParser; |
Rafael Espindola | a61842b | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 132 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 133 | /// This is the current buffer index we're lexing from as managed by the |
| 134 | /// SourceMgr object. |
| 135 | int CurBuffer; |
| 136 | |
| 137 | AsmCond TheCondState; |
| 138 | std::vector<AsmCond> TheCondStack; |
| 139 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 140 | /// ExtensionDirectiveMap - maps directive names to handler methods in parser |
| 141 | /// extensions. Extensions register themselves in this map by calling |
| 142 | /// AddDirectiveHandler. |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 143 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
Daniel Dunbar | 3c802de | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 144 | |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 145 | /// MacroMap - Map of currently defined macros. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 146 | StringMap<MCAsmMacro*> MacroMap; |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 147 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 148 | /// ActiveMacros - Stack of active macro instantiations. |
| 149 | std::vector<MacroInstantiation*> ActiveMacros; |
| 150 | |
Daniel Dunbar | 3c802de | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 151 | /// Boolean tracking whether macro substitution is enabled. |
Eli Bendersky | 733c336 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 152 | unsigned MacrosEnabledFlag : 1; |
Daniel Dunbar | 3c802de | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 153 | |
Daniel Dunbar | 93bd4d1 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 154 | /// Flag tracking whether any errors have been encountered. |
| 155 | unsigned HadError : 1; |
| 156 | |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 157 | /// The values from the last parsed cpp hash file line comment if any. |
| 158 | StringRef CppHashFilename; |
| 159 | int64_t CppHashLineNumber; |
| 160 | SMLoc CppHashLoc; |
Kevin Enderby | 32c1a82 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 161 | int CppHashBuf; |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 162 | |
Devang Patel | 0db58bf | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 163 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
| 164 | unsigned AssemblerDialect; |
| 165 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 166 | /// IsDarwin - is Darwin compatibility enabled? |
| 167 | bool IsDarwin; |
| 168 | |
Chad Rosier | 8f138d1 | 2012-10-15 17:19:13 +0000 | [diff] [blame] | 169 | /// ParsingInlineAsm - Are we parsing ms-style inline assembly? |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 170 | bool ParsingInlineAsm; |
| 171 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 172 | public: |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 173 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 174 | const MCAsmInfo &MAI); |
Craig Topper | 345d16d | 2012-08-29 05:48:09 +0000 | [diff] [blame] | 175 | virtual ~AsmParser(); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 176 | |
| 177 | virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false); |
| 178 | |
Eli Bendersky | 171192f | 2013-01-16 00:50:52 +0000 | [diff] [blame] | 179 | virtual void AddDirectiveHandler(StringRef Directive, |
| 180 | ExtensionDirectiveHandler Handler) { |
| 181 | ExtensionDirectiveMap[Directive] = Handler; |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public: |
| 185 | /// @name MCAsmParser Interface |
| 186 | /// { |
| 187 | |
| 188 | virtual SourceMgr &getSourceManager() { return SrcMgr; } |
| 189 | virtual MCAsmLexer &getLexer() { return Lexer; } |
| 190 | virtual MCContext &getContext() { return Ctx; } |
| 191 | virtual MCStreamer &getStreamer() { return Out; } |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 192 | virtual unsigned getAssemblerDialect() { |
Devang Patel | 0db58bf | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 193 | if (AssemblerDialect == ~0U) |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 194 | return MAI.getAssemblerDialect(); |
Devang Patel | 0db58bf | 2012-01-31 18:14:05 +0000 | [diff] [blame] | 195 | else |
| 196 | return AssemblerDialect; |
| 197 | } |
| 198 | virtual void setAssemblerDialect(unsigned i) { |
| 199 | AssemblerDialect = i; |
| 200 | } |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 201 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 202 | virtual bool Warning(SMLoc L, const Twine &Msg, |
| 203 | ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()); |
| 204 | virtual bool Error(SMLoc L, const Twine &Msg, |
| 205 | ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 206 | |
Craig Topper | 345d16d | 2012-08-29 05:48:09 +0000 | [diff] [blame] | 207 | virtual const AsmToken &Lex(); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 208 | |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 209 | void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; } |
Chad Rosier | c5ac87d | 2012-10-16 20:16:20 +0000 | [diff] [blame] | 210 | bool isParsingInlineAsm() { return ParsingInlineAsm; } |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 211 | |
| 212 | bool ParseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
| 213 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 214 | SmallVectorImpl<std::pair<void *,bool> > &OpDecls, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 215 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 216 | SmallVectorImpl<std::string> &Clobbers, |
| 217 | const MCInstrInfo *MII, |
| 218 | const MCInstPrinter *IP, |
| 219 | MCAsmParserSemaCallback &SI); |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 220 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 221 | bool ParseExpression(const MCExpr *&Res); |
| 222 | virtual bool ParseExpression(const MCExpr *&Res, SMLoc &EndLoc); |
| 223 | virtual bool ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc); |
| 224 | virtual bool ParseAbsoluteExpression(int64_t &Res); |
| 225 | |
Eli Bendersky | bf706b3 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 226 | /// ParseIdentifier - Parse an identifier or string (as a quoted identifier) |
| 227 | /// and set \p Res to the identifier contents. |
| 228 | virtual bool ParseIdentifier(StringRef &Res); |
Eli Bendersky | b2f0b59 | 2013-01-12 00:23:24 +0000 | [diff] [blame] | 229 | virtual void EatToEndOfStatement(); |
Eli Bendersky | bf706b3 | 2013-01-12 00:05:00 +0000 | [diff] [blame] | 230 | |
Eli Bendersky | 318cad3 | 2013-01-14 19:15:01 +0000 | [diff] [blame] | 231 | virtual void CheckForValidSection(); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 232 | /// } |
| 233 | |
| 234 | private: |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 235 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 236 | bool ParseStatement(ParseStatementInfo &Info); |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 237 | void EatToEndOfLine(); |
| 238 | bool ParseCppHashLineFilenameComment(const SMLoc &L); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 239 | |
Kevin Enderby | 221514e | 2013-01-22 21:44:53 +0000 | [diff] [blame^] | 240 | void CheckForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, |
| 241 | MCAsmMacroParameters Parameters); |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 242 | bool expandMacro(raw_svector_ostream &OS, StringRef Body, |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 243 | const MCAsmMacroParameters &Parameters, |
| 244 | const MCAsmMacroArguments &A, |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 245 | const SMLoc &L); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 246 | |
Eli Bendersky | f9f40bd | 2013-01-16 18:56:50 +0000 | [diff] [blame] | 247 | /// \brief Are macros enabled in the parser? |
| 248 | bool MacrosEnabled() {return MacrosEnabledFlag;} |
| 249 | |
| 250 | /// \brief Control a flag in the parser that enables or disables macros. |
| 251 | void SetMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} |
| 252 | |
| 253 | /// \brief Lookup a previously defined macro. |
| 254 | /// \param Name Macro name. |
| 255 | /// \returns Pointer to macro. NULL if no such macro was defined. |
| 256 | const MCAsmMacro* LookupMacro(StringRef Name); |
| 257 | |
| 258 | /// \brief Define a new macro with the given name and information. |
| 259 | void DefineMacro(StringRef Name, const MCAsmMacro& Macro); |
| 260 | |
| 261 | /// \brief Undefine a macro. If no such macro was defined, it's a no-op. |
| 262 | void UndefineMacro(StringRef Name); |
| 263 | |
| 264 | /// \brief Are we inside a macro instantiation? |
| 265 | bool InsideMacroInstantiation() {return !ActiveMacros.empty();} |
| 266 | |
| 267 | /// \brief Handle entry to macro instantiation. |
| 268 | /// |
| 269 | /// \param M The macro. |
| 270 | /// \param NameLoc Instantiation location. |
| 271 | bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); |
| 272 | |
| 273 | /// \brief Handle exit from macro instantiation. |
| 274 | void HandleMacroExit(); |
| 275 | |
| 276 | /// \brief Extract AsmTokens for a macro argument. If the argument delimiter |
| 277 | /// is initially unknown, set it to AsmToken::Eof. It will be set to the |
| 278 | /// correct delimiter by the method. |
| 279 | bool ParseMacroArgument(MCAsmMacroArgument &MA, |
| 280 | AsmToken::TokenKind &ArgumentDelimiter); |
| 281 | |
| 282 | /// \brief Parse all macro arguments for a given macro. |
| 283 | bool ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); |
| 284 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 285 | void PrintMacroInstantiations(); |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 286 | void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 287 | ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) const { |
| 288 | SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 289 | } |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 290 | static void DiagHandler(const SMDiagnostic &Diag, void *Context); |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 291 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 292 | /// EnterIncludeFile - Enter the specified file. This returns true on failure. |
| 293 | bool EnterIncludeFile(const std::string &Filename); |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 294 | /// ProcessIncbinFile - Process the specified file for the .incbin directive. |
| 295 | /// This returns true on failure. |
| 296 | bool ProcessIncbinFile(const std::string &Filename); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 297 | |
Dmitri Gribenko | c5252da | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 298 | /// \brief Reset the current lexer position to that given by \p Loc. The |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 299 | /// current token is not set; clients should ensure Lex() is called |
| 300 | /// subsequently. |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 301 | /// |
| 302 | /// \param InBuffer If not -1, should be the known buffer id that contains the |
| 303 | /// location. |
| 304 | void JumpToLoc(SMLoc Loc, int InBuffer=-1); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 305 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 306 | /// \brief Parse up to the end of statement and a return the contents from the |
| 307 | /// current token until the end of the statement; the current token on exit |
| 308 | /// will be either the EndOfStatement or EOF. |
Craig Topper | 345d16d | 2012-08-29 05:48:09 +0000 | [diff] [blame] | 309 | virtual StringRef ParseStringToEndOfStatement(); |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 310 | |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 311 | /// \brief Parse until the end of a statement or a comma is encountered, |
| 312 | /// return the contents from the current token up to the end or comma. |
| 313 | StringRef ParseStringToComma(); |
| 314 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 315 | bool ParseAssignment(StringRef Name, bool allow_redef, |
| 316 | bool NoDeadStrip = false); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 317 | |
| 318 | bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); |
| 319 | bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); |
| 320 | bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 321 | bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 322 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 323 | bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 324 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 325 | // Generic (target and platform independent) directive parsing. |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 326 | enum DirectiveKind { |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 327 | DK_NO_DIRECTIVE, // Placeholder |
| 328 | DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, |
| 329 | DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE, |
| 330 | DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW, |
Eli Bendersky | 9b1bb05 | 2013-01-11 22:55:28 +0000 | [diff] [blame] | 331 | DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR, |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 332 | DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, |
| 333 | DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL, |
| 334 | DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, |
| 335 | DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, |
| 336 | DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, |
| 337 | DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC, |
| 338 | DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF, |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 339 | DK_ELSEIF, DK_ELSE, DK_ENDIF, |
| 340 | DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, |
| 341 | DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, |
| 342 | DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, |
| 343 | DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, |
| 344 | DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, |
| 345 | DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED, |
| 346 | DK_CFI_REGISTER, |
| 347 | DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM, |
| 348 | DK_SLEB128, DK_ULEB128 |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 351 | /// DirectiveKindMap - Maps directive name --> DirectiveKind enum, for |
| 352 | /// directives parsed by this class. |
| 353 | StringMap<DirectiveKind> DirectiveKindMap; |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 354 | |
| 355 | // ".ascii", ".asciz", ".string" |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 356 | bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 357 | bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ... |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 358 | bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ... |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 359 | bool ParseDirectiveFill(); // ".fill" |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 360 | bool ParseDirectiveZero(); // ".zero" |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 361 | // ".set", ".equ", ".equiv" |
| 362 | bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 363 | bool ParseDirectiveOrg(); // ".org" |
| 364 | // ".align{,32}", ".p2align{,w,l}" |
| 365 | bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
| 366 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 367 | // ".file", ".line", ".loc", ".stabs" |
| 368 | bool ParseDirectiveFile(SMLoc DirectiveLoc); |
| 369 | bool ParseDirectiveLine(); |
| 370 | bool ParseDirectiveLoc(); |
| 371 | bool ParseDirectiveStabs(); |
| 372 | |
| 373 | // .cfi directives |
| 374 | bool ParseDirectiveCFIRegister(SMLoc DirectiveLoc); |
| 375 | bool ParseDirectiveCFISections(); |
| 376 | bool ParseDirectiveCFIStartProc(); |
| 377 | bool ParseDirectiveCFIEndProc(); |
| 378 | bool ParseDirectiveCFIDefCfaOffset(); |
| 379 | bool ParseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
| 380 | bool ParseDirectiveCFIAdjustCfaOffset(); |
| 381 | bool ParseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
| 382 | bool ParseDirectiveCFIOffset(SMLoc DirectiveLoc); |
| 383 | bool ParseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
| 384 | bool ParseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
| 385 | bool ParseDirectiveCFIRememberState(); |
| 386 | bool ParseDirectiveCFIRestoreState(); |
| 387 | bool ParseDirectiveCFISameValue(SMLoc DirectiveLoc); |
| 388 | bool ParseDirectiveCFIRestore(SMLoc DirectiveLoc); |
| 389 | bool ParseDirectiveCFIEscape(); |
| 390 | bool ParseDirectiveCFISignalFrame(); |
| 391 | bool ParseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
| 392 | |
| 393 | // macro directives |
| 394 | bool ParseDirectivePurgeMacro(SMLoc DirectiveLoc); |
| 395 | bool ParseDirectiveEndMacro(StringRef Directive); |
| 396 | bool ParseDirectiveMacro(SMLoc DirectiveLoc); |
| 397 | bool ParseDirectiveMacrosOnOff(StringRef Directive); |
| 398 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 399 | // ".bundle_align_mode" |
| 400 | bool ParseDirectiveBundleAlignMode(); |
| 401 | // ".bundle_lock" |
| 402 | bool ParseDirectiveBundleLock(); |
| 403 | // ".bundle_unlock" |
| 404 | bool ParseDirectiveBundleUnlock(); |
| 405 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 406 | // ".space", ".skip" |
| 407 | bool ParseDirectiveSpace(StringRef IDVal); |
| 408 | |
| 409 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
| 410 | bool ParseDirectiveLEB128(bool Signed); |
| 411 | |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 412 | /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which |
| 413 | /// accepts a single symbol (which should be a label or an external). |
| 414 | bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 415 | |
| 416 | bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
| 417 | |
| 418 | bool ParseDirectiveAbort(); // ".abort" |
| 419 | bool ParseDirectiveInclude(); // ".include" |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 420 | bool ParseDirectiveIncbin(); // ".incbin" |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 421 | |
| 422 | bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if" |
Benjamin Kramer | a3dd0eb | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 423 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
| 424 | bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 425 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
| 426 | bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
Benjamin Kramer | 0fd90bc | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 427 | // ".ifdef" or ".ifndef", depending on expect_defined |
| 428 | bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 429 | bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
| 430 | bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
| 431 | bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
Daniel Dunbar | bfdcc70 | 2013-01-18 01:25:33 +0000 | [diff] [blame] | 432 | virtual bool ParseEscapedString(std::string &Data); |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 433 | |
| 434 | const MCExpr *ApplyModifierToExpr(const MCExpr *E, |
| 435 | MCSymbolRefExpr::VariantKind Variant); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 436 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 437 | // Macro-like directives |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 438 | MCAsmMacro *ParseMacroLikeBody(SMLoc DirectiveLoc); |
| 439 | void InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 440 | raw_svector_ostream &OS); |
| 441 | bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept" |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 442 | bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 443 | bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 444 | bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 445 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 446 | // "_emit" |
| 447 | bool ParseDirectiveEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 448 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 449 | void initializeDirectiveKindMap(); |
Daniel Dunbar | aef87e3 | 2010-07-18 18:31:38 +0000 | [diff] [blame] | 450 | }; |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 453 | namespace llvm { |
| 454 | |
| 455 | extern MCAsmParserExtension *createDarwinAsmParser(); |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 456 | extern MCAsmParserExtension *createELFAsmParser(); |
Michael J. Spencer | 7d49004 | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 457 | extern MCAsmParserExtension *createCOFFAsmParser(); |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 458 | |
| 459 | } |
| 460 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 461 | enum { DEFAULT_ADDRSPACE = 0 }; |
| 462 | |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 463 | AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, |
Daniel Dunbar | 9186fa6 | 2010-07-01 20:41:56 +0000 | [diff] [blame] | 464 | MCStreamer &_Out, const MCAsmInfo &_MAI) |
Jim Grosbach | e82b8ee | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 465 | : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM), |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 466 | PlatformParser(0), |
Eli Bendersky | 733c336 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 467 | CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0), |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 468 | AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 469 | // Save the old handler. |
| 470 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
| 471 | SavedDiagContext = SrcMgr.getDiagContext(); |
| 472 | // Set our own handler which calls the saved handler. |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 473 | SrcMgr.setDiagHandler(DiagHandler, this); |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 474 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 475 | |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 476 | // Initialize the platform / file format parser. |
| 477 | // |
| 478 | // FIXME: This is a hack, we need to (majorly) cleanup how these objects are |
| 479 | // created. |
Michael J. Spencer | 7d49004 | 2010-10-09 11:01:07 +0000 | [diff] [blame] | 480 | if (_MAI.hasMicrosoftFastStdCallMangling()) { |
| 481 | PlatformParser = createCOFFAsmParser(); |
| 482 | PlatformParser->Initialize(*this); |
| 483 | } else if (_MAI.hasSubsectionsViaSymbols()) { |
Daniel Dunbar | 9c23d7f | 2010-07-12 20:51:51 +0000 | [diff] [blame] | 484 | PlatformParser = createDarwinAsmParser(); |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 485 | PlatformParser->Initialize(*this); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 486 | IsDarwin = true; |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 487 | } else { |
Daniel Dunbar | 5146a09 | 2010-07-12 21:23:32 +0000 | [diff] [blame] | 488 | PlatformParser = createELFAsmParser(); |
Daniel Dunbar | 7a56fc2 | 2010-07-12 20:08:04 +0000 | [diff] [blame] | 489 | PlatformParser->Initialize(*this); |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 490 | } |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 491 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 492 | initializeDirectiveKindMap(); |
Chris Lattner | ebb89b4 | 2009-09-27 21:16:52 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 495 | AsmParser::~AsmParser() { |
Daniel Dunbar | 5649130 | 2010-07-29 01:51:55 +0000 | [diff] [blame] | 496 | assert(ActiveMacros.empty() && "Unexpected active macro instantiation!"); |
| 497 | |
| 498 | // Destroy any macros. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 499 | for (StringMap<MCAsmMacro*>::iterator it = MacroMap.begin(), |
Daniel Dunbar | 5649130 | 2010-07-29 01:51:55 +0000 | [diff] [blame] | 500 | ie = MacroMap.end(); it != ie; ++it) |
| 501 | delete it->getValue(); |
| 502 | |
Daniel Dunbar | e474970 | 2010-07-12 18:12:02 +0000 | [diff] [blame] | 503 | delete PlatformParser; |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 506 | void AsmParser::PrintMacroInstantiations() { |
| 507 | // Print the active macro instantiation stack. |
| 508 | for (std::vector<MacroInstantiation*>::const_reverse_iterator |
| 509 | it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it) |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 510 | PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, |
| 511 | "while in macro instantiation"); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 514 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 515 | if (FatalAssemblerWarnings) |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 516 | return Error(L, Msg, Ranges); |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 517 | PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 518 | PrintMacroInstantiations(); |
Joerg Sonnenberger | f8cd708 | 2011-05-19 18:00:13 +0000 | [diff] [blame] | 519 | return false; |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 522 | bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { |
Daniel Dunbar | 93bd4d1 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 523 | HadError = true; |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 524 | PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 525 | PrintMacroInstantiations(); |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 526 | return true; |
| 527 | } |
| 528 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 529 | bool AsmParser::EnterIncludeFile(const std::string &Filename) { |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 530 | std::string IncludedFile; |
| 531 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 532 | if (NewBuf == -1) |
| 533 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 534 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 535 | CurBuffer = NewBuf; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 536 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 537 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 538 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 539 | return false; |
| 540 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 541 | |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 542 | /// Process the specified .incbin file by seaching for it in the include paths |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 543 | /// then just emitting the byte contents of the file to the streamer. This |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 544 | /// returns true on failure. |
| 545 | bool AsmParser::ProcessIncbinFile(const std::string &Filename) { |
| 546 | std::string IncludedFile; |
| 547 | int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); |
| 548 | if (NewBuf == -1) |
| 549 | return true; |
| 550 | |
Kevin Enderby | c3fc313 | 2011-12-14 22:34:45 +0000 | [diff] [blame] | 551 | // Pick up the bytes from the file and emit them. |
Kevin Enderby | dac2953 | 2011-12-15 00:00:27 +0000 | [diff] [blame] | 552 | getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(), |
| 553 | DEFAULT_ADDRSPACE); |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 557 | void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) { |
| 558 | if (InBuffer != -1) { |
| 559 | CurBuffer = InBuffer; |
| 560 | } else { |
| 561 | CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); |
| 562 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 563 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer()); |
| 564 | } |
| 565 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 566 | const AsmToken &AsmParser::Lex() { |
| 567 | const AsmToken *tok = &Lexer.Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 568 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 569 | if (tok->is(AsmToken::Eof)) { |
| 570 | // If this is the end of an included file, pop the parent file off the |
| 571 | // include stack. |
| 572 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); |
| 573 | if (ParentIncludeLoc != SMLoc()) { |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 574 | JumpToLoc(ParentIncludeLoc); |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 575 | tok = &Lexer.Lex(); |
| 576 | } |
| 577 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 578 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 579 | if (tok->is(AsmToken::Error)) |
Daniel Dunbar | 275ce39 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 580 | Error(Lexer.getErrLoc(), Lexer.getErr()); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 581 | |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 582 | return *tok; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 585 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 586 | // Create the initial section, if requested. |
Daniel Dunbar | 5e6a7a2 | 2010-03-13 02:20:57 +0000 | [diff] [blame] | 587 | if (!NoInitialTextSection) |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 588 | Out.InitSections(); |
Daniel Dunbar | 7c0a334 | 2009-08-26 22:49:51 +0000 | [diff] [blame] | 589 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 590 | // Prime the lexer. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 591 | Lex(); |
Daniel Dunbar | 93bd4d1 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 592 | |
| 593 | HadError = false; |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 594 | AsmCond StartingCondState = TheCondState; |
| 595 | |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 596 | // If we are generating dwarf for assembly source files save the initial text |
| 597 | // section and generate a .file directive. |
| 598 | if (getContext().getGenDwarfForAssembly()) { |
| 599 | getContext().setGenDwarfSection(getStreamer().getCurrentSection()); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 600 | MCSymbol *SectionStartSym = getContext().CreateTempSymbol(); |
| 601 | getStreamer().EmitLabel(SectionStartSym); |
| 602 | getContext().setGenDwarfSectionStartSym(SectionStartSym); |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 603 | getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(), |
Eric Christopher | 6c58314 | 2012-12-18 00:31:01 +0000 | [diff] [blame] | 604 | StringRef(), |
| 605 | getContext().getMainFileName()); |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 608 | // While we have input, parse each statement. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 609 | while (Lexer.isNot(AsmToken::Eof)) { |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 610 | ParseStatementInfo Info; |
| 611 | if (!ParseStatement(Info)) continue; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 612 | |
Daniel Dunbar | 93bd4d1 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 613 | // We had an error, validate that one was emitted and recover by skipping to |
| 614 | // the next line. |
| 615 | assert(HadError && "Parse statement returned an error, but none emitted!"); |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 616 | EatToEndOfStatement(); |
| 617 | } |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 618 | |
| 619 | if (TheCondState.TheCond != StartingCondState.TheCond || |
| 620 | TheCondState.Ignore != StartingCondState.Ignore) |
| 621 | return TokError("unmatched .ifs or .elses"); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 622 | |
| 623 | // Check to see there are no empty DwarfFile slots. |
| 624 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 625 | getContext().getMCDwarfFiles(); |
| 626 | for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { |
Daniel Dunbar | 93bd4d1 | 2010-09-09 22:42:56 +0000 | [diff] [blame] | 627 | if (!MCDwarfFiles[i]) |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 628 | TokError("unassigned file number: " + Twine(i) + " for .file directives"); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 629 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 630 | |
Jim Grosbach | e82b8ee | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 631 | // Check to see that all assembler local symbols were actually defined. |
| 632 | // Targets that don't do subsections via symbols may not want this, though, |
| 633 | // so conservatively exclude them. Only do this if we're finalizing, though, |
| 634 | // as otherwise we won't necessarilly have seen everything yet. |
| 635 | if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { |
| 636 | const MCContext::SymbolTable &Symbols = getContext().getSymbols(); |
| 637 | for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), |
| 638 | e = Symbols.end(); |
| 639 | i != e; ++i) { |
| 640 | MCSymbol *Sym = i->getValue(); |
| 641 | // Variable symbols may not be marked as defined, so check those |
| 642 | // explicitly. If we know it's a variable, we have a definition for |
| 643 | // the purposes of this check. |
| 644 | if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) |
| 645 | // FIXME: We would really like to refer back to where the symbol was |
| 646 | // first referenced for a source location. We need to add something |
| 647 | // to track that. Currently, we just point to the end of the file. |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 648 | PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error, |
| 649 | "assembler local symbol '" + Sym->getName() + |
| 650 | "' not defined"); |
Jim Grosbach | e82b8ee | 2011-06-15 18:33:28 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | |
Chris Lattner | 79180e2 | 2010-04-05 23:15:42 +0000 | [diff] [blame] | 655 | // Finalize the output stream if there are no errors and if the client wants |
| 656 | // us to. |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 657 | if (!HadError && !NoFinalize) |
Daniel Dunbar | b3f3c03 | 2009-08-21 08:34:18 +0000 | [diff] [blame] | 658 | Out.Finish(); |
| 659 | |
Chris Lattner | b717fb0 | 2009-07-02 21:53:43 +0000 | [diff] [blame] | 660 | return HadError; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 663 | void AsmParser::CheckForValidSection() { |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 664 | if (!ParsingInlineAsm && !getStreamer().getCurrentSection()) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 665 | TokError("expected section directive before assembly directive"); |
Eli Bendersky | 030f63a | 2013-01-14 19:04:57 +0000 | [diff] [blame] | 666 | Out.InitToTextSection(); |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 670 | /// EatToEndOfStatement - Throw away the rest of the line for testing purposes. |
| 671 | void AsmParser::EatToEndOfStatement() { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 672 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 673 | Lexer.isNot(AsmToken::Eof)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 674 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 676 | // Eat EOL. |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 677 | if (Lexer.is(AsmToken::EndOfStatement)) |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 678 | Lex(); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 681 | StringRef AsmParser::ParseStringToEndOfStatement() { |
| 682 | const char *Start = getTok().getLoc().getPointer(); |
| 683 | |
| 684 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 685 | Lexer.isNot(AsmToken::Eof)) |
| 686 | Lex(); |
| 687 | |
| 688 | const char *End = getTok().getLoc().getPointer(); |
| 689 | return StringRef(Start, End - Start); |
| 690 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 691 | |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 692 | StringRef AsmParser::ParseStringToComma() { |
| 693 | const char *Start = getTok().getLoc().getPointer(); |
| 694 | |
| 695 | while (Lexer.isNot(AsmToken::EndOfStatement) && |
| 696 | Lexer.isNot(AsmToken::Comma) && |
| 697 | Lexer.isNot(AsmToken::Eof)) |
| 698 | Lex(); |
| 699 | |
| 700 | const char *End = getTok().getLoc().getPointer(); |
| 701 | return StringRef(Start, End - Start); |
| 702 | } |
| 703 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 704 | /// ParseParenExpr - Parse a paren expression and return it. |
| 705 | /// NOTE: This assumes the leading '(' has already been consumed. |
| 706 | /// |
| 707 | /// parenexpr ::= expr) |
| 708 | /// |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 709 | bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 710 | if (ParseExpression(Res)) return true; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 711 | if (Lexer.isNot(AsmToken::RParen)) |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 712 | return TokError("expected ')' in parentheses expression"); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 713 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 714 | Lex(); |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 715 | return false; |
| 716 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 717 | |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 718 | /// ParseBracketExpr - Parse a bracket expression and return it. |
| 719 | /// NOTE: This assumes the leading '[' has already been consumed. |
| 720 | /// |
| 721 | /// bracketexpr ::= expr] |
| 722 | /// |
| 723 | bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
| 724 | if (ParseExpression(Res)) return true; |
| 725 | if (Lexer.isNot(AsmToken::RBrac)) |
| 726 | return TokError("expected ']' in brackets expression"); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 727 | EndLoc = Lexer.getTok().getEndLoc(); |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 728 | Lex(); |
| 729 | return false; |
| 730 | } |
| 731 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 732 | /// ParsePrimaryExpr - Parse a primary expression and return it. |
| 733 | /// primaryexpr ::= (parenexpr |
| 734 | /// primaryexpr ::= symbol |
| 735 | /// primaryexpr ::= number |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 736 | /// primaryexpr ::= '.' |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 737 | /// primaryexpr ::= ~,+,- primaryexpr |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 738 | bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
Kevin Enderby | 5de048e | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 739 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
| 740 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
| 741 | switch (FirstTokenKind) { |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 742 | default: |
| 743 | return TokError("unknown token in expression"); |
Eric Christopher | f3755b2 | 2011-04-12 00:03:13 +0000 | [diff] [blame] | 744 | // If we have an error assume that we've already handled it. |
| 745 | case AsmToken::Error: |
| 746 | return true; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 747 | case AsmToken::Exclaim: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 748 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 749 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 750 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 751 | Res = MCUnaryExpr::CreateLNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 752 | return false; |
Daniel Dunbar | e17edff | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 753 | case AsmToken::Dollar: |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 754 | case AsmToken::String: |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 755 | case AsmToken::Identifier: { |
Daniel Dunbar | e17edff | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 756 | StringRef Identifier; |
Kevin Enderby | 5de048e | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 757 | if (ParseIdentifier(Identifier)) { |
| 758 | if (FirstTokenKind == AsmToken::Dollar) |
| 759 | return Error(FirstTokenLoc, "invalid token in expression"); |
Jim Grosbach | 95ae09a | 2011-05-23 20:36:04 +0000 | [diff] [blame] | 760 | return true; |
Kevin Enderby | 5de048e | 2013-01-22 21:09:20 +0000 | [diff] [blame] | 761 | } |
Daniel Dunbar | e17edff | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 762 | |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 763 | EndLoc = SMLoc::getFromPointer(Identifier.end()); |
| 764 | |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 765 | // This is a symbol reference. |
Daniel Dunbar | e17edff | 2010-08-24 19:13:42 +0000 | [diff] [blame] | 766 | std::pair<StringRef, StringRef> Split = Identifier.split('@'); |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 767 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first); |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 768 | |
| 769 | // Lookup the symbol variant if used. |
| 770 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 771 | if (Split.first.size() != Identifier.size()) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 772 | Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 773 | if (Variant == MCSymbolRefExpr::VK_Invalid) { |
| 774 | Variant = MCSymbolRefExpr::VK_None; |
Jim Grosbach | 4121e8a | 2011-01-19 23:06:07 +0000 | [diff] [blame] | 775 | return TokError("invalid variant '" + Split.second + "'"); |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 778 | |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 779 | // If this is an absolute variable reference, substitute it now to preserve |
| 780 | // semantics in the face of reassignment. |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 781 | if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 782 | if (Variant) |
Daniel Dunbar | 603abd5 | 2010-11-08 17:53:02 +0000 | [diff] [blame] | 783 | return Error(EndLoc, "unexpected modifier on variable reference"); |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 784 | |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 785 | Res = Sym->getVariableValue(); |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 786 | return false; |
| 787 | } |
| 788 | |
| 789 | // Otherwise create a symbol ref. |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 790 | Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 791 | return false; |
Daniel Dunbar | fffff91 | 2009-10-16 01:34:54 +0000 | [diff] [blame] | 792 | } |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 793 | case AsmToken::Integer: { |
| 794 | SMLoc Loc = getTok().getLoc(); |
| 795 | int64_t IntVal = getTok().getIntVal(); |
| 796 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 797 | EndLoc = Lexer.getTok().getEndLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 798 | Lex(); // Eat token. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 799 | // Look for 'b' or 'f' following an Integer as a directional label |
| 800 | if (Lexer.getKind() == AsmToken::Identifier) { |
| 801 | StringRef IDVal = getTok().getString(); |
| 802 | if (IDVal == "f" || IDVal == "b"){ |
| 803 | MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal, |
| 804 | IDVal == "f" ? 1 : 0); |
| 805 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
| 806 | getContext()); |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 807 | if (IDVal == "b" && Sym->isUndefined()) |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 808 | return Error(Loc, "invalid reference to undefined symbol"); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 809 | EndLoc = Lexer.getTok().getEndLoc(); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 810 | Lex(); // Eat identifier. |
| 811 | } |
| 812 | } |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 813 | return false; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 814 | } |
Bill Wendling | 69c4ef3 | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 815 | case AsmToken::Real: { |
| 816 | APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); |
Bob Wilson | 720b918 | 2011-02-03 23:17:47 +0000 | [diff] [blame] | 817 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
Bill Wendling | 69c4ef3 | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 818 | Res = MCConstantExpr::Create(IntVal, getContext()); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 819 | EndLoc = Lexer.getTok().getEndLoc(); |
Bill Wendling | 69c4ef3 | 2011-01-25 21:26:41 +0000 | [diff] [blame] | 820 | Lex(); // Eat token. |
| 821 | return false; |
| 822 | } |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 823 | case AsmToken::Dot: { |
| 824 | // This is a '.' reference, which references the current PC. Emit a |
| 825 | // temporary label to the streamer and refer to it. |
| 826 | MCSymbol *Sym = Ctx.CreateTempSymbol(); |
| 827 | Out.EmitLabel(Sym); |
| 828 | Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 829 | EndLoc = Lexer.getTok().getEndLoc(); |
Chris Lattner | d305035 | 2010-04-14 04:40:28 +0000 | [diff] [blame] | 830 | Lex(); // Eat identifier. |
| 831 | return false; |
| 832 | } |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 833 | case AsmToken::LParen: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 834 | Lex(); // Eat the '('. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 835 | return ParseParenExpr(Res, EndLoc); |
Joerg Sonnenberger | 93c65e6 | 2011-02-24 21:59:22 +0000 | [diff] [blame] | 836 | case AsmToken::LBrac: |
| 837 | if (!PlatformParser->HasBracketExpressions()) |
| 838 | return TokError("brackets expression not supported on this target"); |
| 839 | Lex(); // Eat the '['. |
| 840 | return ParseBracketExpr(Res, EndLoc); |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 841 | case AsmToken::Minus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 842 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 843 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 844 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 845 | Res = MCUnaryExpr::CreateMinus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 846 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 847 | case AsmToken::Plus: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 848 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 849 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 850 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 851 | Res = MCUnaryExpr::CreatePlus(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 852 | return false; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 853 | case AsmToken::Tilde: |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 854 | Lex(); // Eat the operator. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 855 | if (ParsePrimaryExpr(Res, EndLoc)) |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 856 | return true; |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 857 | Res = MCUnaryExpr::CreateNot(Res, getContext()); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 858 | return false; |
Chris Lattner | c419383 | 2009-06-22 05:51:26 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 861 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 862 | bool AsmParser::ParseExpression(const MCExpr *&Res) { |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 863 | SMLoc EndLoc; |
| 864 | return ParseExpression(Res, EndLoc); |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 867 | const MCExpr * |
| 868 | AsmParser::ApplyModifierToExpr(const MCExpr *E, |
| 869 | MCSymbolRefExpr::VariantKind Variant) { |
| 870 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 871 | // if there is exactly one symbol. |
| 872 | switch (E->getKind()) { |
| 873 | case MCExpr::Target: |
| 874 | case MCExpr::Constant: |
| 875 | return 0; |
| 876 | |
| 877 | case MCExpr::SymbolRef: { |
| 878 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 879 | |
| 880 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) { |
| 881 | TokError("invalid variant on expression '" + |
| 882 | getTok().getIdentifier() + "' (already modified)"); |
| 883 | return E; |
| 884 | } |
| 885 | |
| 886 | return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext()); |
| 887 | } |
| 888 | |
| 889 | case MCExpr::Unary: { |
| 890 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); |
| 891 | const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant); |
| 892 | if (!Sub) |
| 893 | return 0; |
| 894 | return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext()); |
| 895 | } |
| 896 | |
| 897 | case MCExpr::Binary: { |
| 898 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
| 899 | const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant); |
| 900 | const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant); |
| 901 | |
| 902 | if (!LHS && !RHS) |
| 903 | return 0; |
| 904 | |
| 905 | if (!LHS) LHS = BE->getLHS(); |
| 906 | if (!RHS) RHS = BE->getRHS(); |
| 907 | |
| 908 | return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext()); |
| 909 | } |
| 910 | } |
Daniel Dunbar | f3f95c9 | 2010-09-17 16:34:24 +0000 | [diff] [blame] | 911 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 912 | llvm_unreachable("Invalid expression kind!"); |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 915 | /// ParseExpression - Parse an expression and return it. |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 916 | /// |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 917 | /// expr ::= expr &&,|| expr -> lowest. |
| 918 | /// expr ::= expr |,^,&,! expr |
| 919 | /// expr ::= expr ==,!=,<>,<,<=,>,>= expr |
| 920 | /// expr ::= expr <<,>> expr |
| 921 | /// expr ::= expr +,- expr |
| 922 | /// expr ::= expr *,/,% expr -> highest. |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 923 | /// expr ::= primaryexpr |
| 924 | /// |
Chris Lattner | 54482b4 | 2010-01-15 19:39:23 +0000 | [diff] [blame] | 925 | bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 926 | // Parse the expression. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 927 | Res = 0; |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 928 | if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc)) |
| 929 | return true; |
| 930 | |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 931 | // As a special case, we support 'a op b @ modifier' by rewriting the |
| 932 | // expression to include the modifier. This is inefficient, but in general we |
| 933 | // expect users to use 'a@modifier op b'. |
| 934 | if (Lexer.getKind() == AsmToken::At) { |
| 935 | Lex(); |
| 936 | |
| 937 | if (Lexer.isNot(AsmToken::Identifier)) |
| 938 | return TokError("unexpected symbol modifier following '@'"); |
| 939 | |
| 940 | MCSymbolRefExpr::VariantKind Variant = |
| 941 | MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); |
| 942 | if (Variant == MCSymbolRefExpr::VK_Invalid) |
| 943 | return TokError("invalid variant '" + getTok().getIdentifier() + "'"); |
| 944 | |
| 945 | const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant); |
| 946 | if (!ModifiedRes) { |
| 947 | return TokError("invalid modifier '" + getTok().getIdentifier() + |
| 948 | "' (no symbols present)"); |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 949 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 950 | |
Daniel Dunbar | cceba83 | 2010-09-17 02:47:07 +0000 | [diff] [blame] | 951 | Res = ModifiedRes; |
| 952 | Lex(); |
| 953 | } |
| 954 | |
Daniel Dunbar | e9a60eb | 2010-02-13 01:28:07 +0000 | [diff] [blame] | 955 | // Try to constant fold it up front, if possible. |
| 956 | int64_t Value; |
| 957 | if (Res->EvaluateAsAbsolute(Value)) |
| 958 | Res = MCConstantExpr::Create(Value, getContext()); |
| 959 | |
| 960 | return false; |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 961 | } |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 962 | |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 963 | bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
Chris Lattner | 75f265f | 2010-01-24 01:07:33 +0000 | [diff] [blame] | 964 | Res = 0; |
| 965 | return ParseParenExpr(Res, EndLoc) || |
| 966 | ParseBinOpRHS(1, Res, EndLoc); |
Daniel Dunbar | c18274b | 2009-08-31 08:08:17 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 969 | bool AsmParser::ParseAbsoluteExpression(int64_t &Res) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 970 | const MCExpr *Expr; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 971 | |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 972 | SMLoc StartLoc = Lexer.getLoc(); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 973 | if (ParseExpression(Expr)) |
| 974 | return true; |
| 975 | |
Daniel Dunbar | e00b011 | 2009-10-16 01:57:52 +0000 | [diff] [blame] | 976 | if (!Expr->EvaluateAsAbsolute(Res)) |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 977 | return Error(StartLoc, "expected absolute expression"); |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 978 | |
| 979 | return false; |
| 980 | } |
| 981 | |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 982 | static unsigned getBinOpPrecedence(AsmToken::TokenKind K, |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 983 | MCBinaryExpr::Opcode &Kind) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 984 | switch (K) { |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 985 | default: |
| 986 | return 0; // not a binop. |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 987 | |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 988 | // Lowest Precedence: &&, || |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 989 | case AsmToken::AmpAmp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 990 | Kind = MCBinaryExpr::LAnd; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 991 | return 1; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 992 | case AsmToken::PipePipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 993 | Kind = MCBinaryExpr::LOr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 994 | return 1; |
| 995 | |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 996 | |
Chris Lattner | f7d4da0 | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 997 | // Low Precedence: |, &, ^ |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 998 | // |
| 999 | // FIXME: gas seems to support '!' as an infix operator? |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1000 | case AsmToken::Pipe: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1001 | Kind = MCBinaryExpr::Or; |
Chris Lattner | f7d4da0 | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1002 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1003 | case AsmToken::Caret: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1004 | Kind = MCBinaryExpr::Xor; |
Chris Lattner | f7d4da0 | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1005 | return 2; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1006 | case AsmToken::Amp: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1007 | Kind = MCBinaryExpr::And; |
Chris Lattner | f7d4da0 | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1008 | return 2; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1009 | |
Daniel Dunbar | b1e0f76 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1010 | // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= |
Chris Lattner | f7d4da0 | 2010-09-22 05:05:16 +0000 | [diff] [blame] | 1011 | case AsmToken::EqualEqual: |
| 1012 | Kind = MCBinaryExpr::EQ; |
| 1013 | return 3; |
| 1014 | case AsmToken::ExclaimEqual: |
| 1015 | case AsmToken::LessGreater: |
| 1016 | Kind = MCBinaryExpr::NE; |
| 1017 | return 3; |
| 1018 | case AsmToken::Less: |
| 1019 | Kind = MCBinaryExpr::LT; |
| 1020 | return 3; |
| 1021 | case AsmToken::LessEqual: |
| 1022 | Kind = MCBinaryExpr::LTE; |
| 1023 | return 3; |
| 1024 | case AsmToken::Greater: |
| 1025 | Kind = MCBinaryExpr::GT; |
| 1026 | return 3; |
| 1027 | case AsmToken::GreaterEqual: |
| 1028 | Kind = MCBinaryExpr::GTE; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1029 | return 3; |
| 1030 | |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1031 | // Intermediate Precedence: <<, >> |
| 1032 | case AsmToken::LessLess: |
| 1033 | Kind = MCBinaryExpr::Shl; |
| 1034 | return 4; |
| 1035 | case AsmToken::GreaterGreater: |
| 1036 | Kind = MCBinaryExpr::Shr; |
| 1037 | return 4; |
| 1038 | |
Daniel Dunbar | b1e0f76 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1039 | // High Intermediate Precedence: +, - |
| 1040 | case AsmToken::Plus: |
| 1041 | Kind = MCBinaryExpr::Add; |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1042 | return 5; |
Daniel Dunbar | b1e0f76 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1043 | case AsmToken::Minus: |
| 1044 | Kind = MCBinaryExpr::Sub; |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1045 | return 5; |
Daniel Dunbar | b1e0f76 | 2010-10-25 20:18:56 +0000 | [diff] [blame] | 1046 | |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1047 | // Highest Precedence: *, /, % |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1048 | case AsmToken::Star: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1049 | Kind = MCBinaryExpr::Mul; |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1050 | return 6; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1051 | case AsmToken::Slash: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1052 | Kind = MCBinaryExpr::Div; |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1053 | return 6; |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1054 | case AsmToken::Percent: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1055 | Kind = MCBinaryExpr::Mod; |
Jim Grosbach | fbe1681 | 2011-08-20 16:24:13 +0000 | [diff] [blame] | 1056 | return 6; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. |
| 1062 | /// Res contains the LHS of the expression on input. |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1063 | bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, |
| 1064 | SMLoc &EndLoc) { |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1065 | while (1) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1066 | MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1067 | unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1068 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1069 | // If the next token is lower precedence than we are allowed to eat, return |
| 1070 | // successfully with what we ate already. |
| 1071 | if (TokPrec < Precedence) |
| 1072 | return false; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1073 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1074 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1075 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1076 | // Eat the next primary expression. |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 1077 | const MCExpr *RHS; |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1078 | if (ParsePrimaryExpr(RHS, EndLoc)) return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1080 | // If BinOp binds less tightly with RHS than the operator after RHS, let |
| 1081 | // the pending operator take RHS as its LHS. |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1082 | MCBinaryExpr::Opcode Dummy; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1083 | unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1084 | if (TokPrec < NextTokPrec) { |
Chris Lattner | b4307b3 | 2010-01-15 19:28:38 +0000 | [diff] [blame] | 1085 | if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true; |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 1088 | // Merge LHS and RHS according to operator. |
Daniel Dunbar | 6ce004d | 2009-08-31 08:07:44 +0000 | [diff] [blame] | 1089 | Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1093 | /// ParseStatement: |
| 1094 | /// ::= EndOfStatement |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1095 | /// ::= Label* Directive ...Operands... EndOfStatement |
| 1096 | /// ::= Label* Identifier OperandList* EndOfStatement |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1097 | bool AsmParser::ParseStatement(ParseStatementInfo &Info) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1098 | if (Lexer.is(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1099 | Out.AddBlankLine(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1100 | Lex(); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1101 | return false; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1102 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1103 | |
Kevin Enderby | d82ed5b | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1104 | // Statements always start with an identifier or are a full line comment. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1105 | AsmToken ID = getTok(); |
Daniel Dunbar | 419aded | 2009-07-28 16:38:40 +0000 | [diff] [blame] | 1106 | SMLoc IDLoc = ID.getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1107 | StringRef IDVal; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1108 | int64_t LocalLabelVal = -1; |
Kevin Enderby | d82ed5b | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1109 | // A full line comment is a '#' as the first token. |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1110 | if (Lexer.is(AsmToken::Hash)) |
| 1111 | return ParseCppHashLineFilenameComment(IDLoc); |
Daniel Dunbar | 0143ac1 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1112 | |
Kevin Enderby | d82ed5b | 2010-12-24 00:12:02 +0000 | [diff] [blame] | 1113 | // Allow an integer followed by a ':' as a directional local label. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1114 | if (Lexer.is(AsmToken::Integer)) { |
| 1115 | LocalLabelVal = getTok().getIntVal(); |
| 1116 | if (LocalLabelVal < 0) { |
| 1117 | if (!TheCondState.Ignore) |
| 1118 | return TokError("unexpected token at start of statement"); |
| 1119 | IDVal = ""; |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1120 | } else { |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1121 | IDVal = getTok().getString(); |
| 1122 | Lex(); // Consume the integer token to be used as an identifier token. |
| 1123 | if (Lexer.getKind() != AsmToken::Colon) { |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1124 | if (!TheCondState.Ignore) |
| 1125 | return TokError("unexpected token at start of statement"); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1126 | } |
| 1127 | } |
Daniel Dunbar | 8b2b43c | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1128 | } else if (Lexer.is(AsmToken::Dot)) { |
| 1129 | // Treat '.' as a valid identifier in this context. |
| 1130 | Lex(); |
| 1131 | IDVal = "."; |
Daniel Dunbar | 0143ac1 | 2011-03-25 17:47:14 +0000 | [diff] [blame] | 1132 | } else if (ParseIdentifier(IDVal)) { |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1133 | if (!TheCondState.Ignore) |
| 1134 | return TokError("unexpected token at start of statement"); |
| 1135 | IDVal = ""; |
| 1136 | } |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1138 | // Handle conditional assembly here before checking for skipping. We |
| 1139 | // have to do this so that .endif isn't skipped in a ".if 0" block for |
| 1140 | // example. |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1141 | StringMap<DirectiveKind>::const_iterator DirKindIt = |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1142 | DirectiveKindMap.find(IDVal); |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1143 | DirectiveKind DirKind = |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1144 | (DirKindIt == DirectiveKindMap.end()) ? DK_NO_DIRECTIVE : |
| 1145 | DirKindIt->getValue(); |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1146 | switch (DirKind) { |
| 1147 | default: |
| 1148 | break; |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1149 | case DK_IF: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1150 | return ParseDirectiveIf(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1151 | case DK_IFB: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1152 | return ParseDirectiveIfb(IDLoc, true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1153 | case DK_IFNB: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1154 | return ParseDirectiveIfb(IDLoc, false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1155 | case DK_IFC: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1156 | return ParseDirectiveIfc(IDLoc, true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1157 | case DK_IFNC: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1158 | return ParseDirectiveIfc(IDLoc, false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1159 | case DK_IFDEF: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1160 | return ParseDirectiveIfdef(IDLoc, true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1161 | case DK_IFNDEF: |
| 1162 | case DK_IFNOTDEF: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1163 | return ParseDirectiveIfdef(IDLoc, false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1164 | case DK_ELSEIF: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1165 | return ParseDirectiveElseIf(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1166 | case DK_ELSE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1167 | return ParseDirectiveElse(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1168 | case DK_ENDIF: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1169 | return ParseDirectiveEndIf(IDLoc); |
| 1170 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1171 | |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1172 | // Ignore the statement if in the middle of inactive conditional |
| 1173 | // (e.g. ".if 0"). |
Chad Rosier | 17feeec | 2012-10-20 00:47:08 +0000 | [diff] [blame] | 1174 | if (TheCondState.Ignore) { |
Chris Lattner | 7834fac | 2010-04-17 18:14:27 +0000 | [diff] [blame] | 1175 | EatToEndOfStatement(); |
| 1176 | return false; |
| 1177 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1178 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1179 | // FIXME: Recurse on local labels? |
| 1180 | |
| 1181 | // See what kind of statement we have. |
| 1182 | switch (Lexer.getKind()) { |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1183 | case AsmToken::Colon: { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1184 | CheckForValidSection(); |
| 1185 | |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1186 | // identifier ':' -> Label. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1187 | Lex(); |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1188 | |
Daniel Dunbar | 8b2b43c | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1189 | // Diagnose attempt to use '.' as a label. |
| 1190 | if (IDVal == ".") |
| 1191 | return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); |
| 1192 | |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1193 | // Diagnose attempt to use a variable as a label. |
| 1194 | // |
| 1195 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1196 | // FIXME: This doesn't diagnose assignment to a symbol which has been |
| 1197 | // implicitly marked as external. |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1198 | MCSymbol *Sym; |
| 1199 | if (LocalLabelVal == -1) |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1200 | Sym = getContext().GetOrCreateSymbol(IDVal); |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 1201 | else |
| 1202 | Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); |
Daniel Dunbar | c304718 | 2010-05-05 19:01:00 +0000 | [diff] [blame] | 1203 | if (!Sym->isUndefined() || Sym->isVariable()) |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1204 | return Error(IDLoc, "invalid symbol redefinition"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1205 | |
Daniel Dunbar | 959fd88 | 2009-08-26 22:13:22 +0000 | [diff] [blame] | 1206 | // Emit the label. |
Chad Rosier | deb1bab | 2013-01-07 20:34:12 +0000 | [diff] [blame] | 1207 | if (!ParsingInlineAsm) |
| 1208 | Out.EmitLabel(Sym); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1209 | |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1210 | // If we are generating dwarf for assembly source files then gather the |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1211 | // info to make a dwarf label entry for this label if needed. |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1212 | if (getContext().getGenDwarfForAssembly()) |
Kevin Enderby | 11c2def | 2012-01-10 21:12:34 +0000 | [diff] [blame] | 1213 | MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), |
| 1214 | IDLoc); |
Kevin Enderby | 94c2e85 | 2011-12-09 18:09:40 +0000 | [diff] [blame] | 1215 | |
Daniel Dunbar | 01777ff | 2010-05-23 18:36:34 +0000 | [diff] [blame] | 1216 | // Consume any end of statement token, if present, to avoid spurious |
| 1217 | // AddBlankLine calls(). |
| 1218 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1219 | Lex(); |
| 1220 | if (Lexer.is(AsmToken::Eof)) |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1224 | return false; |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1225 | } |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1226 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1227 | case AsmToken::Equal: |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1228 | // identifier '=' ... -> assignment statement |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1229 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1230 | |
Nico Weber | 4c4c732 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 1231 | return ParseAssignment(IDVal, true); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1232 | |
| 1233 | default: // Normal instruction or directive. |
| 1234 | break; |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1235 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1236 | |
| 1237 | // If macros are enabled, check to see if this is a macro instantiation. |
Eli Bendersky | 733c336 | 2013-01-14 18:08:41 +0000 | [diff] [blame] | 1238 | if (MacrosEnabled()) |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1239 | if (const MCAsmMacro *M = LookupMacro(IDVal)) { |
| 1240 | return HandleMacroEntry(M, IDLoc); |
| 1241 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1242 | |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1243 | // Otherwise, we have a normal instruction or directive. |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1244 | |
| 1245 | // Directives start with "." |
Daniel Dunbar | 8b2b43c | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1246 | if (IDVal[0] == '.' && IDVal != ".") { |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1247 | // There are several entities interested in parsing directives: |
| 1248 | // |
| 1249 | // 1. The target-specific assembly parser. Some directives are target |
| 1250 | // specific or may potentially behave differently on certain targets. |
| 1251 | // 2. Asm parser extensions. For example, platform-specific parsers |
| 1252 | // (like the ELF parser) register themselves as extensions. |
| 1253 | // 3. The generic directive parser implemented by this class. These are |
| 1254 | // all the directives that behave in a target and platform independent |
| 1255 | // manner, or at least have a default behavior that's shared between |
| 1256 | // all targets and platforms. |
Akira Hatanaka | 3b02d95 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1257 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1258 | // First query the target-specific parser. It will return 'true' if it |
| 1259 | // isn't interested in this directive. |
Akira Hatanaka | 3b02d95 | 2012-07-05 19:09:33 +0000 | [diff] [blame] | 1260 | if (!getTargetParser().ParseDirective(ID)) |
| 1261 | return false; |
| 1262 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1263 | // Next, check the extention directive map to see if any extension has |
| 1264 | // registered itself to parse this directive. |
| 1265 | std::pair<MCAsmParserExtension*, DirectiveHandler> Handler = |
| 1266 | ExtensionDirectiveMap.lookup(IDVal); |
| 1267 | if (Handler.first) |
| 1268 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
| 1269 | |
| 1270 | // Finally, if no one else is interested in this directive, it must be |
| 1271 | // generic and familiar to this class. |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1272 | switch (DirKind) { |
| 1273 | default: |
| 1274 | break; |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1275 | case DK_SET: |
| 1276 | case DK_EQU: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1277 | return ParseDirectiveSet(IDVal, true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1278 | case DK_EQUIV: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1279 | return ParseDirectiveSet(IDVal, false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1280 | case DK_ASCII: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1281 | return ParseDirectiveAscii(IDVal, false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1282 | case DK_ASCIZ: |
| 1283 | case DK_STRING: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1284 | return ParseDirectiveAscii(IDVal, true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1285 | case DK_BYTE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1286 | return ParseDirectiveValue(1); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1287 | case DK_SHORT: |
| 1288 | case DK_VALUE: |
| 1289 | case DK_2BYTE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1290 | return ParseDirectiveValue(2); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1291 | case DK_LONG: |
| 1292 | case DK_INT: |
| 1293 | case DK_4BYTE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1294 | return ParseDirectiveValue(4); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1295 | case DK_QUAD: |
| 1296 | case DK_8BYTE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1297 | return ParseDirectiveValue(8); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1298 | case DK_SINGLE: |
| 1299 | case DK_FLOAT: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1300 | return ParseDirectiveRealValue(APFloat::IEEEsingle); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1301 | case DK_DOUBLE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1302 | return ParseDirectiveRealValue(APFloat::IEEEdouble); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1303 | case DK_ALIGN: { |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1304 | bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes(); |
| 1305 | return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1); |
| 1306 | } |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1307 | case DK_ALIGN32: { |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1308 | bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes(); |
| 1309 | return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4); |
| 1310 | } |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1311 | case DK_BALIGN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1312 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1313 | case DK_BALIGNW: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1314 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1315 | case DK_BALIGNL: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1316 | return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1317 | case DK_P2ALIGN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1318 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1319 | case DK_P2ALIGNW: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1320 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1321 | case DK_P2ALIGNL: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1322 | return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1323 | case DK_ORG: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1324 | return ParseDirectiveOrg(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1325 | case DK_FILL: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1326 | return ParseDirectiveFill(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1327 | case DK_ZERO: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1328 | return ParseDirectiveZero(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1329 | case DK_EXTERN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1330 | EatToEndOfStatement(); // .extern is the default, ignore it. |
| 1331 | return false; |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1332 | case DK_GLOBL: |
| 1333 | case DK_GLOBAL: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1334 | return ParseDirectiveSymbolAttribute(MCSA_Global); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1335 | case DK_INDIRECT_SYMBOL: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1336 | return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1337 | case DK_LAZY_REFERENCE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1338 | return ParseDirectiveSymbolAttribute(MCSA_LazyReference); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1339 | case DK_NO_DEAD_STRIP: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1340 | return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1341 | case DK_SYMBOL_RESOLVER: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1342 | return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1343 | case DK_PRIVATE_EXTERN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1344 | return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1345 | case DK_REFERENCE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1346 | return ParseDirectiveSymbolAttribute(MCSA_Reference); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1347 | case DK_WEAK_DEFINITION: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1348 | return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1349 | case DK_WEAK_REFERENCE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1350 | return ParseDirectiveSymbolAttribute(MCSA_WeakReference); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1351 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1352 | return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1353 | case DK_COMM: |
| 1354 | case DK_COMMON: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1355 | return ParseDirectiveComm(/*IsLocal=*/false); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1356 | case DK_LCOMM: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1357 | return ParseDirectiveComm(/*IsLocal=*/true); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1358 | case DK_ABORT: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1359 | return ParseDirectiveAbort(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1360 | case DK_INCLUDE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1361 | return ParseDirectiveInclude(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1362 | case DK_INCBIN: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1363 | return ParseDirectiveIncbin(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1364 | case DK_CODE16: |
| 1365 | case DK_CODE16GCC: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1366 | return TokError(Twine(IDVal) + " not supported yet"); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1367 | case DK_REPT: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1368 | return ParseDirectiveRept(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1369 | case DK_IRP: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1370 | return ParseDirectiveIrp(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1371 | case DK_IRPC: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1372 | return ParseDirectiveIrpc(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1373 | case DK_ENDR: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1374 | return ParseDirectiveEndr(IDLoc); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1375 | case DK_BUNDLE_ALIGN_MODE: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1376 | return ParseDirectiveBundleAlignMode(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1377 | case DK_BUNDLE_LOCK: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1378 | return ParseDirectiveBundleLock(); |
Eli Bendersky | 7eef9c1 | 2013-01-10 23:40:56 +0000 | [diff] [blame] | 1379 | case DK_BUNDLE_UNLOCK: |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 1380 | return ParseDirectiveBundleUnlock(); |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 1381 | case DK_SLEB128: |
| 1382 | return ParseDirectiveLEB128(true); |
| 1383 | case DK_ULEB128: |
| 1384 | return ParseDirectiveLEB128(false); |
| 1385 | case DK_SPACE: |
| 1386 | case DK_SKIP: |
| 1387 | return ParseDirectiveSpace(IDVal); |
| 1388 | case DK_FILE: |
| 1389 | return ParseDirectiveFile(IDLoc); |
| 1390 | case DK_LINE: |
| 1391 | return ParseDirectiveLine(); |
| 1392 | case DK_LOC: |
| 1393 | return ParseDirectiveLoc(); |
| 1394 | case DK_STABS: |
| 1395 | return ParseDirectiveStabs(); |
| 1396 | case DK_CFI_SECTIONS: |
| 1397 | return ParseDirectiveCFISections(); |
| 1398 | case DK_CFI_STARTPROC: |
| 1399 | return ParseDirectiveCFIStartProc(); |
| 1400 | case DK_CFI_ENDPROC: |
| 1401 | return ParseDirectiveCFIEndProc(); |
| 1402 | case DK_CFI_DEF_CFA: |
| 1403 | return ParseDirectiveCFIDefCfa(IDLoc); |
| 1404 | case DK_CFI_DEF_CFA_OFFSET: |
| 1405 | return ParseDirectiveCFIDefCfaOffset(); |
| 1406 | case DK_CFI_ADJUST_CFA_OFFSET: |
| 1407 | return ParseDirectiveCFIAdjustCfaOffset(); |
| 1408 | case DK_CFI_DEF_CFA_REGISTER: |
| 1409 | return ParseDirectiveCFIDefCfaRegister(IDLoc); |
| 1410 | case DK_CFI_OFFSET: |
| 1411 | return ParseDirectiveCFIOffset(IDLoc); |
| 1412 | case DK_CFI_REL_OFFSET: |
| 1413 | return ParseDirectiveCFIRelOffset(IDLoc); |
| 1414 | case DK_CFI_PERSONALITY: |
| 1415 | return ParseDirectiveCFIPersonalityOrLsda(true); |
| 1416 | case DK_CFI_LSDA: |
| 1417 | return ParseDirectiveCFIPersonalityOrLsda(false); |
| 1418 | case DK_CFI_REMEMBER_STATE: |
| 1419 | return ParseDirectiveCFIRememberState(); |
| 1420 | case DK_CFI_RESTORE_STATE: |
| 1421 | return ParseDirectiveCFIRestoreState(); |
| 1422 | case DK_CFI_SAME_VALUE: |
| 1423 | return ParseDirectiveCFISameValue(IDLoc); |
| 1424 | case DK_CFI_RESTORE: |
| 1425 | return ParseDirectiveCFIRestore(IDLoc); |
| 1426 | case DK_CFI_ESCAPE: |
| 1427 | return ParseDirectiveCFIEscape(); |
| 1428 | case DK_CFI_SIGNAL_FRAME: |
| 1429 | return ParseDirectiveCFISignalFrame(); |
| 1430 | case DK_CFI_UNDEFINED: |
| 1431 | return ParseDirectiveCFIUndefined(IDLoc); |
| 1432 | case DK_CFI_REGISTER: |
| 1433 | return ParseDirectiveCFIRegister(IDLoc); |
| 1434 | case DK_MACROS_ON: |
| 1435 | case DK_MACROS_OFF: |
| 1436 | return ParseDirectiveMacrosOnOff(IDVal); |
| 1437 | case DK_MACRO: |
| 1438 | return ParseDirectiveMacro(IDLoc); |
| 1439 | case DK_ENDM: |
| 1440 | case DK_ENDMACRO: |
| 1441 | return ParseDirectiveEndMacro(IDVal); |
| 1442 | case DK_PURGEM: |
| 1443 | return ParseDirectivePurgeMacro(IDLoc); |
Eli Friedman | 5d68ec2 | 2010-07-19 04:17:25 +0000 | [diff] [blame] | 1444 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 1445 | |
Jim Grosbach | 686c018 | 2012-05-01 18:38:27 +0000 | [diff] [blame] | 1446 | return Error(IDLoc, "unknown directive"); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1447 | } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 1448 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1449 | // _emit |
| 1450 | if (ParsingInlineAsm && IDVal == "_emit") |
| 1451 | return ParseDirectiveEmit(IDLoc, Info); |
| 1452 | |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 1453 | CheckForValidSection(); |
| 1454 | |
Chris Lattner | a7f1354 | 2010-05-19 23:34:33 +0000 | [diff] [blame] | 1455 | // Canonicalize the opcode to lower case. |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1456 | std::string OpcodeStr = IDVal.lower(); |
Chad Rosier | 6a020a7 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 1457 | ParseInstructionInfo IInfo(Info.AsmRewrites); |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1458 | bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, |
| 1459 | IDLoc, Info.ParsedOperands); |
Chad Rosier | 5749801 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 1460 | Info.ParseError = HadError; |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1461 | |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1462 | // Dump the parsed representation, if requested. |
| 1463 | if (getShowParsedOperands()) { |
| 1464 | SmallString<256> Str; |
| 1465 | raw_svector_ostream OS(Str); |
| 1466 | OS << "parsed instruction: ["; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1467 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1468 | if (i != 0) |
| 1469 | OS << ", "; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1470 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1471 | } |
| 1472 | OS << "]"; |
| 1473 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 1474 | PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1477 | // If we are generating dwarf for assembly source files and the current |
| 1478 | // section is the initial text section then generate a .loc directive for |
| 1479 | // the instruction. |
| 1480 | if (!HadError && getContext().getGenDwarfForAssembly() && |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1481 | getContext().getGenDwarfSection() == getStreamer().getCurrentSection()) { |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1482 | |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1483 | unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1484 | |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1485 | // If we previously parsed a cpp hash file line comment then make sure the |
| 1486 | // current Dwarf File is for the CppHashFilename if not then emit the |
| 1487 | // Dwarf File table for it and adjust the line number for the .loc. |
| 1488 | const std::vector<MCDwarfFile *> &MCDwarfFiles = |
| 1489 | getContext().getMCDwarfFiles(); |
| 1490 | if (CppHashFilename.size() != 0) { |
| 1491 | if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() != |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1492 | CppHashFilename) |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1493 | getStreamer().EmitDwarfFileDirective( |
| 1494 | getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename); |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1495 | |
Kevin Enderby | 32c1a82 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1496 | unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf); |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1497 | Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); |
Eli Bendersky | ed5df01 | 2013-01-16 19:32:36 +0000 | [diff] [blame] | 1498 | } |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1499 | |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1500 | getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(), |
Kevin Enderby | 938482f | 2012-11-01 17:31:35 +0000 | [diff] [blame] | 1501 | Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ? |
Kevin Enderby | dba9a17 | 2011-11-02 17:56:38 +0000 | [diff] [blame] | 1502 | DWARF2_FLAG_IS_STMT : 0, 0, 0, |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1503 | StringRef()); |
| 1504 | } |
| 1505 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1506 | // If parsing succeeded, match the instruction. |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1507 | if (!HadError) { |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1508 | unsigned ErrorInfo; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1509 | HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1510 | Info.ParsedOperands, |
| 1511 | Out, ErrorInfo, |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1512 | ParsingInlineAsm); |
| 1513 | } |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1515 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1516 | // that. |
| 1517 | return false; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1518 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1519 | |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1520 | /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line |
| 1521 | /// since they may not be able to be tokenized to get to the end of line token. |
| 1522 | void AsmParser::EatToEndOfLine() { |
Rafael Espindola | 12ae527 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1523 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1524 | Lexer.LexUntilEndOfLine(); |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1525 | // Eat EOL. |
| 1526 | Lex(); |
| 1527 | } |
| 1528 | |
| 1529 | /// ParseCppHashLineFilenameComment as this: |
| 1530 | /// ::= # number "filename" |
| 1531 | /// or just as a full line comment if it doesn't have a number and a string. |
| 1532 | bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) { |
| 1533 | Lex(); // Eat the hash token. |
| 1534 | |
| 1535 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1536 | // Consume the line since in cases it is not a well-formed line directive, |
| 1537 | // as if were simply a full line comment. |
| 1538 | EatToEndOfLine(); |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
| 1542 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1543 | Lex(); |
| 1544 | |
| 1545 | if (getLexer().isNot(AsmToken::String)) { |
| 1546 | EatToEndOfLine(); |
| 1547 | return false; |
| 1548 | } |
| 1549 | |
| 1550 | StringRef Filename = getTok().getString(); |
| 1551 | // Get rid of the enclosing quotes. |
| 1552 | Filename = Filename.substr(1, Filename.size()-2); |
| 1553 | |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1554 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1555 | CppHashLoc = L; |
| 1556 | CppHashFilename = Filename; |
| 1557 | CppHashLineNumber = LineNumber; |
Kevin Enderby | 32c1a82 | 2012-11-05 21:55:41 +0000 | [diff] [blame] | 1558 | CppHashBuf = CurBuffer; |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1559 | |
| 1560 | // Ignore any trailing characters, they're just comment. |
| 1561 | EatToEndOfLine(); |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 1565 | /// DiagHandler - will use the last parsed cpp hash line filename comment |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1566 | /// for the Filename and LineNo if any in the diagnostic. |
| 1567 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
| 1568 | const AsmParser *Parser = static_cast<const AsmParser*>(Context); |
| 1569 | raw_ostream &OS = errs(); |
| 1570 | |
| 1571 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| 1572 | const SMLoc &DiagLoc = Diag.getLoc(); |
| 1573 | int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1574 | int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
| 1575 | |
| 1576 | // Like SourceMgr::PrintMessage() we need to print the include stack if any |
| 1577 | // before printing the message. |
| 1578 | int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1579 | if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) { |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1580 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1581 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
| 1582 | } |
| 1583 | |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1584 | // If we have not parsed a cpp hash line filename comment or the source |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1585 | // manager changed or buffer changed (like in a nested include) then just |
| 1586 | // print the normal diagnostic using its Filename and LineNo. |
| 1587 | if (!Parser->CppHashLineNumber || |
| 1588 | &DiagSrcMgr != &Parser->SrcMgr || |
| 1589 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1590 | if (Parser->SavedDiagHandler) |
| 1591 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1592 | else |
| 1593 | Diag.print(0, OS); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | } |
| 1596 | |
Eric Christopher | 2318ba1 | 2012-12-18 00:30:54 +0000 | [diff] [blame] | 1597 | // Use the CppHashFilename and calculate a line number based on the |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1598 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1599 | // the diagnostic. |
| 1600 | const std::string Filename = Parser->CppHashFilename; |
| 1601 | |
| 1602 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1603 | int CppHashLocLineNo = |
| 1604 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
| 1605 | int LineNo = Parser->CppHashLineNumber - 1 + |
| 1606 | (DiagLocLineNo - CppHashLocLineNo); |
| 1607 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 1608 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), |
| 1609 | Filename, LineNo, Diag.getColumnNo(), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 1610 | Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1611 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1612 | |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1613 | if (Parser->SavedDiagHandler) |
| 1614 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1615 | else |
| 1616 | NewDiag.print(0, OS); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Rafael Espindola | 799aacf | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1619 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1620 | // difference being that that function accepts '@' as part of identifiers and |
| 1621 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1622 | // '@' as a special case when needed. |
| 1623 | static bool isIdentifierChar(char c) { |
| 1624 | return isalnum(c) || c == '_' || c == '$' || c == '.'; |
| 1625 | } |
| 1626 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1627 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1628 | const MCAsmMacroParameters &Parameters, |
| 1629 | const MCAsmMacroArguments &A, |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1630 | const SMLoc &L) { |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1631 | unsigned NParameters = Parameters.size(); |
| 1632 | if (NParameters != 0 && NParameters != A.size()) |
| 1633 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1634 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1635 | // A macro without parameters is handled differently on Darwin: |
| 1636 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1637 | while (!Body.empty()) { |
| 1638 | // Scan for the next substitution. |
| 1639 | std::size_t End = Body.size(), Pos = 0; |
| 1640 | for (; Pos != End; ++Pos) { |
| 1641 | // Check for a substitution or escape. |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1642 | if (!NParameters) { |
| 1643 | // This macro has no parameters, look for $0, $1, etc. |
| 1644 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1645 | continue; |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1646 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1647 | char Next = Body[Pos + 1]; |
| 1648 | if (Next == '$' || Next == 'n' || isdigit(Next)) |
| 1649 | break; |
| 1650 | } else { |
| 1651 | // This macro has parameters, look for \foo, \bar, etc. |
| 1652 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1653 | break; |
| 1654 | } |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | // Add the prefix. |
| 1658 | OS << Body.slice(0, Pos); |
| 1659 | |
| 1660 | // Check if we reached the end. |
| 1661 | if (Pos == End) |
| 1662 | break; |
| 1663 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1664 | if (!NParameters) { |
| 1665 | switch (Body[Pos+1]) { |
| 1666 | // $$ => $ |
| 1667 | case '$': |
| 1668 | OS << '$'; |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1669 | break; |
| 1670 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1671 | // $n => number of arguments |
| 1672 | case 'n': |
| 1673 | OS << A.size(); |
| 1674 | break; |
| 1675 | |
| 1676 | // $[0-9] => argument |
| 1677 | default: { |
| 1678 | // Missing arguments are ignored. |
| 1679 | unsigned Index = Body[Pos+1] - '0'; |
| 1680 | if (Index >= A.size()) |
| 1681 | break; |
| 1682 | |
| 1683 | // Otherwise substitute with the token values, with spaces eliminated. |
Eli Bendersky | 9bac6b2 | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1684 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1685 | ie = A[Index].end(); it != ie; ++it) |
| 1686 | OS << it->getString(); |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | Pos += 2; |
| 1691 | } else { |
| 1692 | unsigned I = Pos + 1; |
Rafael Espindola | 799aacf | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1693 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1694 | ++I; |
| 1695 | |
| 1696 | const char *Begin = Body.data() + Pos +1; |
| 1697 | StringRef Argument(Begin, I - (Pos +1)); |
| 1698 | unsigned Index = 0; |
| 1699 | for (; Index < NParameters; ++Index) |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1700 | if (Parameters[Index].first == Argument) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1701 | break; |
| 1702 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1703 | if (Index == NParameters) { |
| 1704 | if (Body[Pos+1] == '(' && Body[Pos+2] == ')') |
| 1705 | Pos += 3; |
| 1706 | else { |
| 1707 | OS << '\\' << Argument; |
| 1708 | Pos = I; |
| 1709 | } |
| 1710 | } else { |
Eli Bendersky | 9bac6b2 | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1711 | for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1712 | ie = A[Index].end(); it != ie; ++it) |
| 1713 | if (it->getKind() == AsmToken::String) |
| 1714 | OS << it->getStringContents(); |
| 1715 | else |
| 1716 | OS << it->getString(); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1717 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1718 | Pos += 1 + Argument.size(); |
| 1719 | } |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1720 | } |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1721 | // Update the scan point. |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1722 | Body = Body.substr(Pos); |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1723 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1724 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1725 | return false; |
| 1726 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1727 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1728 | MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 1729 | int EB, SMLoc EL, |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1730 | MemoryBuffer *I) |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 1731 | : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB), |
| 1732 | ExitLoc(EL) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1733 | { |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1736 | static bool IsOperator(AsmToken::TokenKind kind) |
| 1737 | { |
| 1738 | switch (kind) |
| 1739 | { |
| 1740 | default: |
| 1741 | return false; |
| 1742 | case AsmToken::Plus: |
| 1743 | case AsmToken::Minus: |
| 1744 | case AsmToken::Tilde: |
| 1745 | case AsmToken::Slash: |
| 1746 | case AsmToken::Star: |
| 1747 | case AsmToken::Dot: |
| 1748 | case AsmToken::Equal: |
| 1749 | case AsmToken::EqualEqual: |
| 1750 | case AsmToken::Pipe: |
| 1751 | case AsmToken::PipePipe: |
| 1752 | case AsmToken::Caret: |
| 1753 | case AsmToken::Amp: |
| 1754 | case AsmToken::AmpAmp: |
| 1755 | case AsmToken::Exclaim: |
| 1756 | case AsmToken::ExclaimEqual: |
| 1757 | case AsmToken::Percent: |
| 1758 | case AsmToken::Less: |
| 1759 | case AsmToken::LessEqual: |
| 1760 | case AsmToken::LessLess: |
| 1761 | case AsmToken::LessGreater: |
| 1762 | case AsmToken::Greater: |
| 1763 | case AsmToken::GreaterEqual: |
| 1764 | case AsmToken::GreaterGreater: |
| 1765 | return true; |
| 1766 | } |
| 1767 | } |
| 1768 | |
Eli Bendersky | 9bac6b2 | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1769 | bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA, |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1770 | AsmToken::TokenKind &ArgumentDelimiter) { |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1771 | unsigned ParenLevel = 0; |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1772 | unsigned AddTokens = 0; |
| 1773 | |
| 1774 | // gas accepts arguments separated by whitespace, except on Darwin |
| 1775 | if (!IsDarwin) |
| 1776 | Lexer.setSkipSpace(false); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1777 | |
| 1778 | for (;;) { |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1779 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) { |
| 1780 | Lexer.setSkipSpace(true); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1781 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) { |
| 1785 | // Spaces and commas cannot be mixed to delimit parameters |
| 1786 | if (ArgumentDelimiter == AsmToken::Eof) |
| 1787 | ArgumentDelimiter = AsmToken::Comma; |
| 1788 | else if (ArgumentDelimiter != AsmToken::Comma) { |
| 1789 | Lexer.setSkipSpace(true); |
| 1790 | return TokError("expected ' ' for macro argument separator"); |
| 1791 | } |
| 1792 | break; |
| 1793 | } |
| 1794 | |
| 1795 | if (Lexer.is(AsmToken::Space)) { |
| 1796 | Lex(); // Eat spaces |
| 1797 | |
| 1798 | // Spaces can delimit parameters, but could also be part an expression. |
| 1799 | // If the token after a space is an operator, add the token and the next |
| 1800 | // one into this argument |
| 1801 | if (ArgumentDelimiter == AsmToken::Space || |
| 1802 | ArgumentDelimiter == AsmToken::Eof) { |
| 1803 | if (IsOperator(Lexer.getKind())) { |
| 1804 | // Check to see whether the token is used as an operator, |
| 1805 | // or part of an identifier |
Jordan Rose | 3ebe59c | 2013-01-07 19:00:49 +0000 | [diff] [blame] | 1806 | const char *NextChar = getTok().getEndLoc().getPointer(); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1807 | if (*NextChar == ' ') |
| 1808 | AddTokens = 2; |
| 1809 | } |
| 1810 | |
| 1811 | if (!AddTokens && ParenLevel == 0) { |
| 1812 | if (ArgumentDelimiter == AsmToken::Eof && |
| 1813 | !IsOperator(Lexer.getKind())) |
| 1814 | ArgumentDelimiter = AsmToken::Space; |
| 1815 | break; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1819 | |
| 1820 | // HandleMacroEntry relies on not advancing the lexer here |
| 1821 | // to be able to fill in the remaining default parameter values |
| 1822 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 1823 | break; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1824 | |
| 1825 | // Adjust the current parentheses level. |
| 1826 | if (Lexer.is(AsmToken::LParen)) |
| 1827 | ++ParenLevel; |
| 1828 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 1829 | --ParenLevel; |
| 1830 | |
| 1831 | // Append the token to the current argument list. |
| 1832 | MA.push_back(getTok()); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1833 | if (AddTokens) |
| 1834 | AddTokens--; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1835 | Lex(); |
| 1836 | } |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1837 | |
| 1838 | Lexer.setSkipSpace(true); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1839 | if (ParenLevel != 0) |
Rafael Espindola | 76ac200 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 1840 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1841 | return false; |
| 1842 | } |
| 1843 | |
| 1844 | // Parse the macro instantiation arguments. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1845 | bool AsmParser::ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A) { |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1846 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1847 | // Argument delimiter is initially unknown. It will be set by |
| 1848 | // ParseMacroArgument() |
| 1849 | AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Parse two kinds of macro invocations: |
| 1852 | // - macros defined without any parameters accept an arbitrary number of them |
| 1853 | // - macros defined with parameters accept at most that many of them |
| 1854 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 1855 | ++Parameter) { |
Eli Bendersky | 9bac6b2 | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 1856 | MCAsmMacroArgument MA; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1857 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1858 | if (ParseMacroArgument(MA, ArgumentDelimiter)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1859 | return true; |
| 1860 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1861 | if (!MA.empty() || !NParameters) |
| 1862 | A.push_back(MA); |
| 1863 | else if (NParameters) { |
| 1864 | if (!M->Parameters[Parameter].second.empty()) |
| 1865 | A.push_back(M->Parameters[Parameter].second); |
| 1866 | } |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1867 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1868 | // At the end of the statement, fill in remaining arguments that have |
| 1869 | // default values. If there aren't any, then the next argument is |
| 1870 | // required but missing |
| 1871 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1872 | if (NParameters && Parameter < NParameters - 1) { |
| 1873 | if (M->Parameters[Parameter + 1].second.empty()) |
| 1874 | return TokError("macro argument '" + |
| 1875 | Twine(M->Parameters[Parameter + 1].first) + |
| 1876 | "' is missing"); |
| 1877 | else |
| 1878 | continue; |
| 1879 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1880 | return false; |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1881 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1882 | |
| 1883 | if (Lexer.is(AsmToken::Comma)) |
| 1884 | Lex(); |
| 1885 | } |
| 1886 | return TokError("Too many arguments"); |
| 1887 | } |
| 1888 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1889 | const MCAsmMacro* AsmParser::LookupMacro(StringRef Name) { |
| 1890 | StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name); |
| 1891 | return (I == MacroMap.end()) ? NULL : I->getValue(); |
| 1892 | } |
| 1893 | |
| 1894 | void AsmParser::DefineMacro(StringRef Name, const MCAsmMacro& Macro) { |
| 1895 | MacroMap[Name] = new MCAsmMacro(Macro); |
| 1896 | } |
| 1897 | |
| 1898 | void AsmParser::UndefineMacro(StringRef Name) { |
| 1899 | StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name); |
| 1900 | if (I != MacroMap.end()) { |
| 1901 | delete I->getValue(); |
| 1902 | MacroMap.erase(I); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | bool AsmParser::HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1907 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 1908 | // this, although we should protect against infinite loops. |
| 1909 | if (ActiveMacros.size() == 20) |
| 1910 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 1911 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1912 | MCAsmMacroArguments A; |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1913 | if (ParseMacroArguments(M, A)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1914 | return true; |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1915 | |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1916 | // Remove any trailing empty arguments. Do this after-the-fact as we have |
| 1917 | // to keep empty arguments in the middle of the list or positionality |
| 1918 | // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2," |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1919 | while (!A.empty() && A.back().empty()) |
| 1920 | A.pop_back(); |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1921 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1922 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 1923 | // to hold the macro body with substitutions. |
| 1924 | SmallString<256> Buf; |
| 1925 | StringRef Body = M->Body; |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1926 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1927 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1928 | if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc())) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1929 | return true; |
| 1930 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 1931 | // We include the .endmacro in the buffer as our cue to exit the macro |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1932 | // instantiation. |
| 1933 | OS << ".endmacro\n"; |
| 1934 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1935 | MemoryBuffer *Instantiation = |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1936 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1937 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1938 | // Create the macro instantiation object and add to the current macro |
| 1939 | // instantiation stack. |
| 1940 | MacroInstantiation *MI = new MacroInstantiation(M, NameLoc, |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 1941 | CurBuffer, |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1942 | getTok().getLoc(), |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1943 | Instantiation); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1944 | ActiveMacros.push_back(MI); |
| 1945 | |
| 1946 | // Jump to the macro instantiation and prime the lexer. |
| 1947 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 1948 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 1949 | Lex(); |
| 1950 | |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
| 1954 | void AsmParser::HandleMacroExit() { |
| 1955 | // Jump to the EndOfStatement we should return to, and consume it. |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 1956 | JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1957 | Lex(); |
| 1958 | |
| 1959 | // Pop the instantiation entry. |
| 1960 | delete ActiveMacros.back(); |
| 1961 | ActiveMacros.pop_back(); |
| 1962 | } |
| 1963 | |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1964 | static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) { |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1965 | switch (Value->getKind()) { |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1966 | case MCExpr::Binary: { |
| 1967 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value); |
| 1968 | return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS()); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1969 | break; |
| 1970 | } |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1971 | case MCExpr::Target: |
| 1972 | case MCExpr::Constant: |
| 1973 | return false; |
| 1974 | case MCExpr::SymbolRef: { |
| 1975 | const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol(); |
Rafael Espindola | 8b01c82 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 1976 | if (S.isVariable()) |
| 1977 | return IsUsedIn(Sym, S.getVariableValue()); |
| 1978 | return &S == Sym; |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1979 | } |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1980 | case MCExpr::Unary: |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1981 | return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr()); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1982 | } |
Benjamin Kramer | 518ff56 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 1983 | |
| 1984 | llvm_unreachable("Unknown expr kind!"); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 1987 | bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef, |
| 1988 | bool NoDeadStrip) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1989 | // FIXME: Use better location, we should use proper tokens. |
| 1990 | SMLoc EqualLoc = Lexer.getLoc(); |
| 1991 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1992 | const MCExpr *Value; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1993 | if (ParseExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1994 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1995 | |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1996 | // Note: we don't count b as used in "a = b". This is to allow |
| 1997 | // a = b |
| 1998 | // b = c |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1999 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 2000 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2001 | return TokError("unexpected token in assignment"); |
| 2002 | |
Daniel Dunbar | 8b2b43c | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 2003 | // Error on assignment to '.'. |
| 2004 | if (Name == ".") { |
| 2005 | return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported " |
| 2006 | "(use '.space' or '.org').)")); |
| 2007 | } |
| 2008 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2009 | // Eat the end of statement marker. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2010 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2011 | |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2012 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 2013 | // used as a symbol, or it is an absolute symbol). |
| 2014 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 2015 | if (Sym) { |
| 2016 | // Diagnose assignment to a label. |
| 2017 | // |
| 2018 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 2019 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 2020 | if (IsUsedIn(Sym, Value)) |
| 2021 | return Error(EqualLoc, "Recursive use of '" + Name + "'"); |
| 2022 | else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 2023 | ; // Allow redefinitions of undefined symbols only used in directives. |
Jim Grosbach | 48c9533 | 2012-03-20 21:33:21 +0000 | [diff] [blame] | 2024 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 2025 | ; // Allow redefinitions of variables that haven't yet been used. |
Daniel Dunbar | 6db7fe8 | 2011-04-29 17:53:11 +0000 | [diff] [blame] | 2026 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2027 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 2028 | else if (!Sym->isVariable()) |
| 2029 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 2030 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2031 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
| 2032 | Name + "'"); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 2033 | |
| 2034 | // Don't count these checks as uses. |
| 2035 | Sym->setUsed(false); |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2036 | } else |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 2037 | Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 2038 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2039 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 2040 | |
| 2041 | // Do the assignment. |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 2042 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2043 | if (NoDeadStrip) |
| 2044 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 2045 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2046 | |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2050 | /// ParseIdentifier: |
| 2051 | /// ::= identifier |
| 2052 | /// ::= string |
| 2053 | bool AsmParser::ParseIdentifier(StringRef &Res) { |
Daniel Dunbar | 1f1b865 | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 2054 | // The assembler has relaxed rules for accepting identifiers, in particular we |
| 2055 | // allow things like '.globl $foo', which would normally be separate |
| 2056 | // tokens. At this level, we have already lexed so we cannot (currently) |
| 2057 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 2058 | // and return the combined identifier. |
| 2059 | if (Lexer.is(AsmToken::Dollar)) { |
| 2060 | SMLoc DollarLoc = getLexer().getLoc(); |
| 2061 | |
| 2062 | // Consume the dollar sign, and check for a following identifier. |
| 2063 | Lex(); |
| 2064 | if (Lexer.isNot(AsmToken::Identifier)) |
| 2065 | return true; |
| 2066 | |
| 2067 | // We have a '$' followed by an identifier, make sure they are adjacent. |
| 2068 | if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
| 2069 | return true; |
| 2070 | |
| 2071 | // Construct the joined identifier and consume the token. |
| 2072 | Res = StringRef(DollarLoc.getPointer(), |
| 2073 | getTok().getIdentifier().size() + 1); |
| 2074 | Lex(); |
| 2075 | return false; |
| 2076 | } |
| 2077 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2078 | if (Lexer.isNot(AsmToken::Identifier) && |
| 2079 | Lexer.isNot(AsmToken::String)) |
| 2080 | return true; |
| 2081 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2082 | Res = getTok().getIdentifier(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2083 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2084 | Lex(); // Consume the identifier token. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2085 | |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2089 | /// ParseDirectiveSet: |
Nico Weber | 4c4c732 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2090 | /// ::= .equ identifier ',' expression |
| 2091 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2092 | /// ::= .set identifier ',' expression |
Nico Weber | 4c4c732 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 2093 | bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2094 | StringRef Name; |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2095 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2096 | if (ParseIdentifier(Name)) |
Roman Divacky | f9d1752 | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2097 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2098 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2099 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | f9d1752 | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 2100 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2101 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2102 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 2103 | return ParseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2106 | bool AsmParser::ParseEscapedString(std::string &Data) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2107 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2108 | |
| 2109 | Data = ""; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2110 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2111 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 2112 | if (Str[i] != '\\') { |
| 2113 | Data += Str[i]; |
| 2114 | continue; |
| 2115 | } |
| 2116 | |
| 2117 | // Recognize escaped characters. Note that this escape semantics currently |
| 2118 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 2119 | ++i; |
| 2120 | if (i == e) |
| 2121 | return TokError("unexpected backslash at end of string"); |
| 2122 | |
| 2123 | // Recognize octal sequences. |
| 2124 | if ((unsigned) (Str[i] - '0') <= 7) { |
| 2125 | // Consume up to three octal characters. |
| 2126 | unsigned Value = Str[i] - '0'; |
| 2127 | |
| 2128 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 2129 | ++i; |
| 2130 | Value = Value * 8 + (Str[i] - '0'); |
| 2131 | |
| 2132 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 2133 | ++i; |
| 2134 | Value = Value * 8 + (Str[i] - '0'); |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | if (Value > 255) |
| 2139 | return TokError("invalid octal escape sequence (out of range)"); |
| 2140 | |
| 2141 | Data += (unsigned char) Value; |
| 2142 | continue; |
| 2143 | } |
| 2144 | |
| 2145 | // Otherwise recognize individual escapes. |
| 2146 | switch (Str[i]) { |
| 2147 | default: |
| 2148 | // Just reject invalid escape sequences for now. |
| 2149 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2150 | |
| 2151 | case 'b': Data += '\b'; break; |
| 2152 | case 'f': Data += '\f'; break; |
| 2153 | case 'n': Data += '\n'; break; |
| 2154 | case 'r': Data += '\r'; break; |
| 2155 | case 't': Data += '\t'; break; |
| 2156 | case '"': Data += '"'; break; |
| 2157 | case '\\': Data += '\\'; break; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | return false; |
| 2162 | } |
| 2163 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2164 | /// ParseDirectiveAscii: |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2165 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
| 2166 | bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2167 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2168 | CheckForValidSection(); |
| 2169 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2170 | for (;;) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2171 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2172 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2173 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2174 | std::string Data; |
| 2175 | if (ParseEscapedString(Data)) |
| 2176 | return true; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2177 | |
| 2178 | getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2179 | if (ZeroTerminated) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2180 | getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); |
| 2181 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2182 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2183 | |
| 2184 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2187 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2188 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2189 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2190 | } |
| 2191 | } |
| 2192 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2193 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2194 | return false; |
| 2195 | } |
| 2196 | |
| 2197 | /// ParseDirectiveValue |
| 2198 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 2199 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2200 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2201 | CheckForValidSection(); |
| 2202 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2203 | for (;;) { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2204 | const MCExpr *Value; |
Jim Grosbach | 254cf03 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2205 | SMLoc ExprLoc = getLexer().getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2206 | if (ParseExpression(Value)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2207 | return true; |
| 2208 | |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2209 | // Special case constant expressions to match code generator. |
Jim Grosbach | 254cf03 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2210 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2211 | assert(Size <= 8 && "Invalid size"); |
| 2212 | uint64_t IntValue = MCE->getValue(); |
| 2213 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2214 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2215 | getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE); |
| 2216 | } else |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2217 | getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2218 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2219 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2220 | break; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2221 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2222 | // FIXME: Improve diagnostic. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2223 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2224 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2225 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2229 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2230 | return false; |
| 2231 | } |
| 2232 | |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2233 | /// ParseDirectiveRealValue |
| 2234 | /// ::= (.single | .double) [ expression (, expression)* ] |
| 2235 | bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) { |
| 2236 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2237 | CheckForValidSection(); |
| 2238 | |
| 2239 | for (;;) { |
| 2240 | // We don't truly support arithmetic on floating point expressions, so we |
| 2241 | // have to manually parse unary prefixes. |
| 2242 | bool IsNeg = false; |
| 2243 | if (getLexer().is(AsmToken::Minus)) { |
| 2244 | Lex(); |
| 2245 | IsNeg = true; |
| 2246 | } else if (getLexer().is(AsmToken::Plus)) |
| 2247 | Lex(); |
| 2248 | |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2249 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 360d8d7 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2250 | getLexer().isNot(AsmToken::Real) && |
| 2251 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2252 | return TokError("unexpected token in directive"); |
| 2253 | |
| 2254 | // Convert to an APFloat. |
| 2255 | APFloat Value(Semantics); |
Kevin Enderby | 360d8d7 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2256 | StringRef IDVal = getTok().getString(); |
| 2257 | if (getLexer().is(AsmToken::Identifier)) { |
| 2258 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2259 | Value = APFloat::getInf(Semantics); |
| 2260 | else if (!IDVal.compare_lower("nan")) |
| 2261 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2262 | else |
| 2263 | return TokError("invalid floating point literal"); |
| 2264 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2265 | APFloat::opInvalidOp) |
| 2266 | return TokError("invalid floating point literal"); |
| 2267 | if (IsNeg) |
| 2268 | Value.changeSign(); |
| 2269 | |
| 2270 | // Consume the numeric token. |
| 2271 | Lex(); |
| 2272 | |
| 2273 | // Emit the value as an integer. |
| 2274 | APInt AsInt = Value.bitcastToAPInt(); |
| 2275 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 2276 | AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE); |
| 2277 | |
| 2278 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2279 | break; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2280 | |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2281 | if (getLexer().isNot(AsmToken::Comma)) |
| 2282 | return TokError("unexpected token in directive"); |
| 2283 | Lex(); |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | Lex(); |
| 2288 | return false; |
| 2289 | } |
| 2290 | |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2291 | /// ParseDirectiveZero |
| 2292 | /// ::= .zero expression |
| 2293 | bool AsmParser::ParseDirectiveZero() { |
| 2294 | CheckForValidSection(); |
| 2295 | |
| 2296 | int64_t NumBytes; |
| 2297 | if (ParseAbsoluteExpression(NumBytes)) |
| 2298 | return true; |
| 2299 | |
Rafael Espindola | e452b17 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2300 | int64_t Val = 0; |
| 2301 | if (getLexer().is(AsmToken::Comma)) { |
| 2302 | Lex(); |
| 2303 | if (ParseAbsoluteExpression(Val)) |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2307 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2308 | return TokError("unexpected token in '.zero' directive"); |
| 2309 | |
| 2310 | Lex(); |
| 2311 | |
Rafael Espindola | e452b17 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2312 | getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE); |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2313 | |
| 2314 | return false; |
| 2315 | } |
| 2316 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2317 | /// ParseDirectiveFill |
| 2318 | /// ::= .fill expression , expression , expression |
| 2319 | bool AsmParser::ParseDirectiveFill() { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2320 | CheckForValidSection(); |
| 2321 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2322 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2323 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2324 | return true; |
| 2325 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2326 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2327 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2328 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2329 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2330 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2331 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2332 | return true; |
| 2333 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2334 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2335 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2336 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2337 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2338 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2339 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2340 | return true; |
| 2341 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2342 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2343 | return TokError("unexpected token in '.fill' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2344 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2345 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2346 | |
Daniel Dunbar | bc38ca7 | 2009-08-21 15:43:35 +0000 | [diff] [blame] | 2347 | if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) |
| 2348 | return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2349 | |
| 2350 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2351 | getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2352 | |
| 2353 | return false; |
| 2354 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2355 | |
| 2356 | /// ParseDirectiveOrg |
| 2357 | /// ::= .org expression [ , expression ] |
| 2358 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2359 | CheckForValidSection(); |
| 2360 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2361 | const MCExpr *Offset; |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2362 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2363 | if (ParseExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2364 | return true; |
| 2365 | |
| 2366 | // Parse optional fill expression. |
| 2367 | int64_t FillExpr = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2368 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2369 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2370 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2371 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2372 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2373 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2374 | return true; |
| 2375 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2376 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2377 | return TokError("unexpected token in '.org' directive"); |
| 2378 | } |
| 2379 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2380 | Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 2381 | |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2382 | // Only limited forms of relocatable expressions are accepted here, it |
| 2383 | // has to be relative to the current section. The streamer will return |
| 2384 | // 'true' if the expression wasn't evaluatable. |
| 2385 | if (getStreamer().EmitValueToOffset(Offset, FillExpr)) |
| 2386 | return Error(Loc, "expected assembly-time absolute expression"); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2387 | |
| 2388 | return false; |
| 2389 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2390 | |
| 2391 | /// ParseDirectiveAlign |
| 2392 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 2393 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2394 | CheckForValidSection(); |
| 2395 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2396 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2397 | int64_t Alignment; |
| 2398 | if (ParseAbsoluteExpression(Alignment)) |
| 2399 | return true; |
| 2400 | |
| 2401 | SMLoc MaxBytesLoc; |
| 2402 | bool HasFillExpr = false; |
| 2403 | int64_t FillExpr = 0; |
| 2404 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2405 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2406 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2407 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2408 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2409 | |
| 2410 | // The fill expression can be omitted while specifying a maximum number of |
| 2411 | // alignment bytes, e.g: |
| 2412 | // .align 3,,4 |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2413 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2414 | HasFillExpr = true; |
| 2415 | if (ParseAbsoluteExpression(FillExpr)) |
| 2416 | return true; |
| 2417 | } |
| 2418 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2419 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2420 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2421 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2422 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2423 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2424 | MaxBytesLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2425 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 2426 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2427 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2428 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2429 | return TokError("unexpected token in directive"); |
| 2430 | } |
| 2431 | } |
| 2432 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2433 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2434 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2435 | if (!HasFillExpr) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2436 | FillExpr = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2437 | |
| 2438 | // Compute alignment in bytes. |
| 2439 | if (IsPow2) { |
| 2440 | // FIXME: Diagnose overflow. |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2441 | if (Alignment >= 32) { |
| 2442 | Error(AlignmentLoc, "invalid alignment value"); |
| 2443 | Alignment = 31; |
| 2444 | } |
| 2445 | |
Benjamin Kramer | 12fd767 | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2446 | Alignment = 1ULL << Alignment; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2449 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2450 | if (MaxBytesLoc.isValid()) { |
| 2451 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2452 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 2453 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 0afb9f5 | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2454 | MaxBytesToFill = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2458 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 2459 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2460 | MaxBytesToFill = 0; |
| 2461 | } |
| 2462 | } |
| 2463 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2464 | // Check whether we should use optimal code alignment for this .align |
| 2465 | // directive. |
Jan Wen Voung | 083cf15 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 2466 | bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign(); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2467 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2468 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2469 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2470 | } else { |
Kevin Enderby | d74acb0 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2471 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | a955853 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2472 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2473 | MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2474 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2475 | |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 2479 | /// ParseDirectiveFile |
| 2480 | /// ::= .file [number] filename |
| 2481 | /// ::= .file number directory filename |
| 2482 | bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) { |
| 2483 | // FIXME: I'm not sure what this is. |
| 2484 | int64_t FileNumber = -1; |
| 2485 | SMLoc FileNumberLoc = getLexer().getLoc(); |
| 2486 | if (getLexer().is(AsmToken::Integer)) { |
| 2487 | FileNumber = getTok().getIntVal(); |
| 2488 | Lex(); |
| 2489 | |
| 2490 | if (FileNumber < 1) |
| 2491 | return TokError("file number less than one"); |
| 2492 | } |
| 2493 | |
| 2494 | if (getLexer().isNot(AsmToken::String)) |
| 2495 | return TokError("unexpected token in '.file' directive"); |
| 2496 | |
| 2497 | // Usually the directory and filename together, otherwise just the directory. |
| 2498 | StringRef Path = getTok().getString(); |
| 2499 | Path = Path.substr(1, Path.size()-2); |
| 2500 | Lex(); |
| 2501 | |
| 2502 | StringRef Directory; |
| 2503 | StringRef Filename; |
| 2504 | if (getLexer().is(AsmToken::String)) { |
| 2505 | if (FileNumber == -1) |
| 2506 | return TokError("explicit path specified, but no file number"); |
| 2507 | Filename = getTok().getString(); |
| 2508 | Filename = Filename.substr(1, Filename.size()-2); |
| 2509 | Directory = Path; |
| 2510 | Lex(); |
| 2511 | } else { |
| 2512 | Filename = Path; |
| 2513 | } |
| 2514 | |
| 2515 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2516 | return TokError("unexpected token in '.file' directive"); |
| 2517 | |
| 2518 | if (FileNumber == -1) |
| 2519 | getStreamer().EmitFileDirective(Filename); |
| 2520 | else { |
| 2521 | if (getContext().getGenDwarfForAssembly() == true) |
| 2522 | Error(DirectiveLoc, "input can't have .file dwarf directives when -g is " |
| 2523 | "used to generate dwarf debug info for assembly code"); |
| 2524 | |
| 2525 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename)) |
| 2526 | Error(FileNumberLoc, "file number already allocated"); |
| 2527 | } |
| 2528 | |
| 2529 | return false; |
| 2530 | } |
| 2531 | |
| 2532 | /// ParseDirectiveLine |
| 2533 | /// ::= .line [number] |
| 2534 | bool AsmParser::ParseDirectiveLine() { |
| 2535 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2536 | if (getLexer().isNot(AsmToken::Integer)) |
| 2537 | return TokError("unexpected token in '.line' directive"); |
| 2538 | |
| 2539 | int64_t LineNumber = getTok().getIntVal(); |
| 2540 | (void) LineNumber; |
| 2541 | Lex(); |
| 2542 | |
| 2543 | // FIXME: Do something with the .line. |
| 2544 | } |
| 2545 | |
| 2546 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2547 | return TokError("unexpected token in '.line' directive"); |
| 2548 | |
| 2549 | return false; |
| 2550 | } |
| 2551 | |
| 2552 | /// ParseDirectiveLoc |
| 2553 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
| 2554 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2555 | /// The first number is a file number, must have been previously assigned with |
| 2556 | /// a .file directive, the second number is the line number and optionally the |
| 2557 | /// third number is a column position (zero if not specified). The remaining |
| 2558 | /// optional items are .loc sub-directives. |
| 2559 | bool AsmParser::ParseDirectiveLoc() { |
| 2560 | if (getLexer().isNot(AsmToken::Integer)) |
| 2561 | return TokError("unexpected token in '.loc' directive"); |
| 2562 | int64_t FileNumber = getTok().getIntVal(); |
| 2563 | if (FileNumber < 1) |
| 2564 | return TokError("file number less than one in '.loc' directive"); |
| 2565 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
| 2566 | return TokError("unassigned file number in '.loc' directive"); |
| 2567 | Lex(); |
| 2568 | |
| 2569 | int64_t LineNumber = 0; |
| 2570 | if (getLexer().is(AsmToken::Integer)) { |
| 2571 | LineNumber = getTok().getIntVal(); |
| 2572 | if (LineNumber < 1) |
| 2573 | return TokError("line number less than one in '.loc' directive"); |
| 2574 | Lex(); |
| 2575 | } |
| 2576 | |
| 2577 | int64_t ColumnPos = 0; |
| 2578 | if (getLexer().is(AsmToken::Integer)) { |
| 2579 | ColumnPos = getTok().getIntVal(); |
| 2580 | if (ColumnPos < 0) |
| 2581 | return TokError("column position less than zero in '.loc' directive"); |
| 2582 | Lex(); |
| 2583 | } |
| 2584 | |
| 2585 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
| 2586 | unsigned Isa = 0; |
| 2587 | int64_t Discriminator = 0; |
| 2588 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2589 | for (;;) { |
| 2590 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2591 | break; |
| 2592 | |
| 2593 | StringRef Name; |
| 2594 | SMLoc Loc = getTok().getLoc(); |
| 2595 | if (ParseIdentifier(Name)) |
| 2596 | return TokError("unexpected token in '.loc' directive"); |
| 2597 | |
| 2598 | if (Name == "basic_block") |
| 2599 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2600 | else if (Name == "prologue_end") |
| 2601 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2602 | else if (Name == "epilogue_begin") |
| 2603 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2604 | else if (Name == "is_stmt") { |
| 2605 | Loc = getTok().getLoc(); |
| 2606 | const MCExpr *Value; |
| 2607 | if (ParseExpression(Value)) |
| 2608 | return true; |
| 2609 | // The expression must be the constant 0 or 1. |
| 2610 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2611 | int Value = MCE->getValue(); |
| 2612 | if (Value == 0) |
| 2613 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2614 | else if (Value == 1) |
| 2615 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2616 | else |
| 2617 | return Error(Loc, "is_stmt value not 0 or 1"); |
| 2618 | } |
| 2619 | else { |
| 2620 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2621 | } |
| 2622 | } |
| 2623 | else if (Name == "isa") { |
| 2624 | Loc = getTok().getLoc(); |
| 2625 | const MCExpr *Value; |
| 2626 | if (ParseExpression(Value)) |
| 2627 | return true; |
| 2628 | // The expression must be a constant greater or equal to 0. |
| 2629 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2630 | int Value = MCE->getValue(); |
| 2631 | if (Value < 0) |
| 2632 | return Error(Loc, "isa number less than zero"); |
| 2633 | Isa = Value; |
| 2634 | } |
| 2635 | else { |
| 2636 | return Error(Loc, "isa number not a constant value"); |
| 2637 | } |
| 2638 | } |
| 2639 | else if (Name == "discriminator") { |
| 2640 | if (ParseAbsoluteExpression(Discriminator)) |
| 2641 | return true; |
| 2642 | } |
| 2643 | else { |
| 2644 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2645 | } |
| 2646 | |
| 2647 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2648 | break; |
| 2649 | } |
| 2650 | } |
| 2651 | |
| 2652 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
| 2653 | Isa, Discriminator, StringRef()); |
| 2654 | |
| 2655 | return false; |
| 2656 | } |
| 2657 | |
| 2658 | /// ParseDirectiveStabs |
| 2659 | /// ::= .stabs string, number, number, number |
| 2660 | bool AsmParser::ParseDirectiveStabs() { |
| 2661 | return TokError("unsupported directive '.stabs'"); |
| 2662 | } |
| 2663 | |
| 2664 | /// ParseDirectiveCFISections |
| 2665 | /// ::= .cfi_sections section [, section] |
| 2666 | bool AsmParser::ParseDirectiveCFISections() { |
| 2667 | StringRef Name; |
| 2668 | bool EH = false; |
| 2669 | bool Debug = false; |
| 2670 | |
| 2671 | if (ParseIdentifier(Name)) |
| 2672 | return TokError("Expected an identifier"); |
| 2673 | |
| 2674 | if (Name == ".eh_frame") |
| 2675 | EH = true; |
| 2676 | else if (Name == ".debug_frame") |
| 2677 | Debug = true; |
| 2678 | |
| 2679 | if (getLexer().is(AsmToken::Comma)) { |
| 2680 | Lex(); |
| 2681 | |
| 2682 | if (ParseIdentifier(Name)) |
| 2683 | return TokError("Expected an identifier"); |
| 2684 | |
| 2685 | if (Name == ".eh_frame") |
| 2686 | EH = true; |
| 2687 | else if (Name == ".debug_frame") |
| 2688 | Debug = true; |
| 2689 | } |
| 2690 | |
| 2691 | getStreamer().EmitCFISections(EH, Debug); |
| 2692 | return false; |
| 2693 | } |
| 2694 | |
| 2695 | /// ParseDirectiveCFIStartProc |
| 2696 | /// ::= .cfi_startproc |
| 2697 | bool AsmParser::ParseDirectiveCFIStartProc() { |
| 2698 | getStreamer().EmitCFIStartProc(); |
| 2699 | return false; |
| 2700 | } |
| 2701 | |
| 2702 | /// ParseDirectiveCFIEndProc |
| 2703 | /// ::= .cfi_endproc |
| 2704 | bool AsmParser::ParseDirectiveCFIEndProc() { |
| 2705 | getStreamer().EmitCFIEndProc(); |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
| 2709 | /// ParseRegisterOrRegisterNumber - parse register name or number. |
| 2710 | bool AsmParser::ParseRegisterOrRegisterNumber(int64_t &Register, |
| 2711 | SMLoc DirectiveLoc) { |
| 2712 | unsigned RegNo; |
| 2713 | |
| 2714 | if (getLexer().isNot(AsmToken::Integer)) { |
| 2715 | if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) |
| 2716 | return true; |
| 2717 | Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true); |
| 2718 | } else |
| 2719 | return ParseAbsoluteExpression(Register); |
| 2720 | |
| 2721 | return false; |
| 2722 | } |
| 2723 | |
| 2724 | /// ParseDirectiveCFIDefCfa |
| 2725 | /// ::= .cfi_def_cfa register, offset |
| 2726 | bool AsmParser::ParseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
| 2727 | int64_t Register = 0; |
| 2728 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2729 | return true; |
| 2730 | |
| 2731 | if (getLexer().isNot(AsmToken::Comma)) |
| 2732 | return TokError("unexpected token in directive"); |
| 2733 | Lex(); |
| 2734 | |
| 2735 | int64_t Offset = 0; |
| 2736 | if (ParseAbsoluteExpression(Offset)) |
| 2737 | return true; |
| 2738 | |
| 2739 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 2740 | return false; |
| 2741 | } |
| 2742 | |
| 2743 | /// ParseDirectiveCFIDefCfaOffset |
| 2744 | /// ::= .cfi_def_cfa_offset offset |
| 2745 | bool AsmParser::ParseDirectiveCFIDefCfaOffset() { |
| 2746 | int64_t Offset = 0; |
| 2747 | if (ParseAbsoluteExpression(Offset)) |
| 2748 | return true; |
| 2749 | |
| 2750 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 2751 | return false; |
| 2752 | } |
| 2753 | |
| 2754 | /// ParseDirectiveCFIRegister |
| 2755 | /// ::= .cfi_register register, register |
| 2756 | bool AsmParser::ParseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
| 2757 | int64_t Register1 = 0; |
| 2758 | if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc)) |
| 2759 | return true; |
| 2760 | |
| 2761 | if (getLexer().isNot(AsmToken::Comma)) |
| 2762 | return TokError("unexpected token in directive"); |
| 2763 | Lex(); |
| 2764 | |
| 2765 | int64_t Register2 = 0; |
| 2766 | if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc)) |
| 2767 | return true; |
| 2768 | |
| 2769 | getStreamer().EmitCFIRegister(Register1, Register2); |
| 2770 | return false; |
| 2771 | } |
| 2772 | |
| 2773 | /// ParseDirectiveCFIAdjustCfaOffset |
| 2774 | /// ::= .cfi_adjust_cfa_offset adjustment |
| 2775 | bool AsmParser::ParseDirectiveCFIAdjustCfaOffset() { |
| 2776 | int64_t Adjustment = 0; |
| 2777 | if (ParseAbsoluteExpression(Adjustment)) |
| 2778 | return true; |
| 2779 | |
| 2780 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 2781 | return false; |
| 2782 | } |
| 2783 | |
| 2784 | /// ParseDirectiveCFIDefCfaRegister |
| 2785 | /// ::= .cfi_def_cfa_register register |
| 2786 | bool AsmParser::ParseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
| 2787 | int64_t Register = 0; |
| 2788 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2789 | return true; |
| 2790 | |
| 2791 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 2792 | return false; |
| 2793 | } |
| 2794 | |
| 2795 | /// ParseDirectiveCFIOffset |
| 2796 | /// ::= .cfi_offset register, offset |
| 2797 | bool AsmParser::ParseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
| 2798 | int64_t Register = 0; |
| 2799 | int64_t Offset = 0; |
| 2800 | |
| 2801 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2802 | return true; |
| 2803 | |
| 2804 | if (getLexer().isNot(AsmToken::Comma)) |
| 2805 | return TokError("unexpected token in directive"); |
| 2806 | Lex(); |
| 2807 | |
| 2808 | if (ParseAbsoluteExpression(Offset)) |
| 2809 | return true; |
| 2810 | |
| 2811 | getStreamer().EmitCFIOffset(Register, Offset); |
| 2812 | return false; |
| 2813 | } |
| 2814 | |
| 2815 | /// ParseDirectiveCFIRelOffset |
| 2816 | /// ::= .cfi_rel_offset register, offset |
| 2817 | bool AsmParser::ParseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
| 2818 | int64_t Register = 0; |
| 2819 | |
| 2820 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2821 | return true; |
| 2822 | |
| 2823 | if (getLexer().isNot(AsmToken::Comma)) |
| 2824 | return TokError("unexpected token in directive"); |
| 2825 | Lex(); |
| 2826 | |
| 2827 | int64_t Offset = 0; |
| 2828 | if (ParseAbsoluteExpression(Offset)) |
| 2829 | return true; |
| 2830 | |
| 2831 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 2832 | return false; |
| 2833 | } |
| 2834 | |
| 2835 | static bool isValidEncoding(int64_t Encoding) { |
| 2836 | if (Encoding & ~0xff) |
| 2837 | return false; |
| 2838 | |
| 2839 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 2840 | return true; |
| 2841 | |
| 2842 | const unsigned Format = Encoding & 0xf; |
| 2843 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 2844 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 2845 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 2846 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 2847 | return false; |
| 2848 | |
| 2849 | const unsigned Application = Encoding & 0x70; |
| 2850 | if (Application != dwarf::DW_EH_PE_absptr && |
| 2851 | Application != dwarf::DW_EH_PE_pcrel) |
| 2852 | return false; |
| 2853 | |
| 2854 | return true; |
| 2855 | } |
| 2856 | |
| 2857 | /// ParseDirectiveCFIPersonalityOrLsda |
| 2858 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
| 2859 | /// ::= .cfi_personality encoding, [symbol_name] |
| 2860 | /// ::= .cfi_lsda encoding, [symbol_name] |
| 2861 | bool AsmParser::ParseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
| 2862 | int64_t Encoding = 0; |
| 2863 | if (ParseAbsoluteExpression(Encoding)) |
| 2864 | return true; |
| 2865 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 2866 | return false; |
| 2867 | |
| 2868 | if (!isValidEncoding(Encoding)) |
| 2869 | return TokError("unsupported encoding."); |
| 2870 | |
| 2871 | if (getLexer().isNot(AsmToken::Comma)) |
| 2872 | return TokError("unexpected token in directive"); |
| 2873 | Lex(); |
| 2874 | |
| 2875 | StringRef Name; |
| 2876 | if (ParseIdentifier(Name)) |
| 2877 | return TokError("expected identifier in directive"); |
| 2878 | |
| 2879 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 2880 | |
| 2881 | if (IsPersonality) |
| 2882 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
| 2883 | else |
| 2884 | getStreamer().EmitCFILsda(Sym, Encoding); |
| 2885 | return false; |
| 2886 | } |
| 2887 | |
| 2888 | /// ParseDirectiveCFIRememberState |
| 2889 | /// ::= .cfi_remember_state |
| 2890 | bool AsmParser::ParseDirectiveCFIRememberState() { |
| 2891 | getStreamer().EmitCFIRememberState(); |
| 2892 | return false; |
| 2893 | } |
| 2894 | |
| 2895 | /// ParseDirectiveCFIRestoreState |
| 2896 | /// ::= .cfi_remember_state |
| 2897 | bool AsmParser::ParseDirectiveCFIRestoreState() { |
| 2898 | getStreamer().EmitCFIRestoreState(); |
| 2899 | return false; |
| 2900 | } |
| 2901 | |
| 2902 | /// ParseDirectiveCFISameValue |
| 2903 | /// ::= .cfi_same_value register |
| 2904 | bool AsmParser::ParseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
| 2905 | int64_t Register = 0; |
| 2906 | |
| 2907 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2908 | return true; |
| 2909 | |
| 2910 | getStreamer().EmitCFISameValue(Register); |
| 2911 | return false; |
| 2912 | } |
| 2913 | |
| 2914 | /// ParseDirectiveCFIRestore |
| 2915 | /// ::= .cfi_restore register |
| 2916 | bool AsmParser::ParseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
| 2917 | int64_t Register = 0; |
| 2918 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2919 | return true; |
| 2920 | |
| 2921 | getStreamer().EmitCFIRestore(Register); |
| 2922 | return false; |
| 2923 | } |
| 2924 | |
| 2925 | /// ParseDirectiveCFIEscape |
| 2926 | /// ::= .cfi_escape expression[,...] |
| 2927 | bool AsmParser::ParseDirectiveCFIEscape() { |
| 2928 | std::string Values; |
| 2929 | int64_t CurrValue; |
| 2930 | if (ParseAbsoluteExpression(CurrValue)) |
| 2931 | return true; |
| 2932 | |
| 2933 | Values.push_back((uint8_t)CurrValue); |
| 2934 | |
| 2935 | while (getLexer().is(AsmToken::Comma)) { |
| 2936 | Lex(); |
| 2937 | |
| 2938 | if (ParseAbsoluteExpression(CurrValue)) |
| 2939 | return true; |
| 2940 | |
| 2941 | Values.push_back((uint8_t)CurrValue); |
| 2942 | } |
| 2943 | |
| 2944 | getStreamer().EmitCFIEscape(Values); |
| 2945 | return false; |
| 2946 | } |
| 2947 | |
| 2948 | /// ParseDirectiveCFISignalFrame |
| 2949 | /// ::= .cfi_signal_frame |
| 2950 | bool AsmParser::ParseDirectiveCFISignalFrame() { |
| 2951 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2952 | return Error(getLexer().getLoc(), |
| 2953 | "unexpected token in '.cfi_signal_frame'"); |
| 2954 | |
| 2955 | getStreamer().EmitCFISignalFrame(); |
| 2956 | return false; |
| 2957 | } |
| 2958 | |
| 2959 | /// ParseDirectiveCFIUndefined |
| 2960 | /// ::= .cfi_undefined register |
| 2961 | bool AsmParser::ParseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
| 2962 | int64_t Register = 0; |
| 2963 | |
| 2964 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 2965 | return true; |
| 2966 | |
| 2967 | getStreamer().EmitCFIUndefined(Register); |
| 2968 | return false; |
| 2969 | } |
| 2970 | |
| 2971 | /// ParseDirectiveMacrosOnOff |
| 2972 | /// ::= .macros_on |
| 2973 | /// ::= .macros_off |
| 2974 | bool AsmParser::ParseDirectiveMacrosOnOff(StringRef Directive) { |
| 2975 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2976 | return Error(getLexer().getLoc(), |
| 2977 | "unexpected token in '" + Directive + "' directive"); |
| 2978 | |
| 2979 | SetMacrosEnabled(Directive == ".macros_on"); |
| 2980 | return false; |
| 2981 | } |
| 2982 | |
| 2983 | /// ParseDirectiveMacro |
| 2984 | /// ::= .macro name [parameters] |
| 2985 | bool AsmParser::ParseDirectiveMacro(SMLoc DirectiveLoc) { |
| 2986 | StringRef Name; |
| 2987 | if (ParseIdentifier(Name)) |
| 2988 | return TokError("expected identifier in '.macro' directive"); |
| 2989 | |
| 2990 | MCAsmMacroParameters Parameters; |
| 2991 | // Argument delimiter is initially unknown. It will be set by |
| 2992 | // ParseMacroArgument() |
| 2993 | AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; |
| 2994 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2995 | for (;;) { |
| 2996 | MCAsmMacroParameter Parameter; |
| 2997 | if (ParseIdentifier(Parameter.first)) |
| 2998 | return TokError("expected identifier in '.macro' directive"); |
| 2999 | |
| 3000 | if (getLexer().is(AsmToken::Equal)) { |
| 3001 | Lex(); |
| 3002 | if (ParseMacroArgument(Parameter.second, ArgumentDelimiter)) |
| 3003 | return true; |
| 3004 | } |
| 3005 | |
| 3006 | Parameters.push_back(Parameter); |
| 3007 | |
| 3008 | if (getLexer().is(AsmToken::Comma)) |
| 3009 | Lex(); |
| 3010 | else if (getLexer().is(AsmToken::EndOfStatement)) |
| 3011 | break; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | // Eat the end of statement. |
| 3016 | Lex(); |
| 3017 | |
| 3018 | AsmToken EndToken, StartToken = getTok(); |
| 3019 | |
| 3020 | // Lex the macro definition. |
| 3021 | for (;;) { |
| 3022 | // Check whether we have reached the end of the file. |
| 3023 | if (getLexer().is(AsmToken::Eof)) |
| 3024 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3025 | |
| 3026 | // Otherwise, check whether we have reach the .endmacro. |
| 3027 | if (getLexer().is(AsmToken::Identifier) && |
| 3028 | (getTok().getIdentifier() == ".endm" || |
| 3029 | getTok().getIdentifier() == ".endmacro")) { |
| 3030 | EndToken = getTok(); |
| 3031 | Lex(); |
| 3032 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3033 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3034 | "' directive"); |
| 3035 | break; |
| 3036 | } |
| 3037 | |
| 3038 | // Otherwise, scan til the end of the statement. |
| 3039 | EatToEndOfStatement(); |
| 3040 | } |
| 3041 | |
| 3042 | if (LookupMacro(Name)) { |
| 3043 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3044 | } |
| 3045 | |
| 3046 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3047 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3048 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Kevin Enderby | 221514e | 2013-01-22 21:44:53 +0000 | [diff] [blame^] | 3049 | CheckForBadMacro(DirectiveLoc, Name, Body, Parameters); |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3050 | DefineMacro(Name, MCAsmMacro(Name, Body, Parameters)); |
| 3051 | return false; |
| 3052 | } |
| 3053 | |
Kevin Enderby | 221514e | 2013-01-22 21:44:53 +0000 | [diff] [blame^] | 3054 | /// CheckForBadMacro |
| 3055 | /// |
| 3056 | /// With the support added for named parameters there may be code out there that |
| 3057 | /// is transitioning from positional parameters. In versions of gas that did |
| 3058 | /// not support named parameters they would be ignored on the macro defintion. |
| 3059 | /// But to support both styles of parameters this is not possible so if a macro |
| 3060 | /// defintion has named parameters but does not use them and has what appears |
| 3061 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
| 3062 | /// warning that the positional parameter found in body which have no effect. |
| 3063 | /// Hoping the developer will either remove the named parameters from the macro |
| 3064 | /// definiton so the positional parameters get used if that was what was |
| 3065 | /// intended or change the macro to use the named parameters. It is possible |
| 3066 | /// this warning will trigger when the none of the named parameters are used |
| 3067 | /// and the strings like $1 are infact to simply to be passed trough unchanged. |
| 3068 | void AsmParser::CheckForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
| 3069 | StringRef Body, |
| 3070 | MCAsmMacroParameters Parameters) { |
| 3071 | // If this macro is not defined with named parameters the warning we are |
| 3072 | // checking for here doesn't apply. |
| 3073 | unsigned NParameters = Parameters.size(); |
| 3074 | if (NParameters == 0) |
| 3075 | return; |
| 3076 | |
| 3077 | bool NamedParametersFound = false; |
| 3078 | bool PositionalParametersFound = false; |
| 3079 | |
| 3080 | // Look at the body of the macro for use of both the named parameters and what |
| 3081 | // are likely to be positional parameters. This is what expandMacro() is |
| 3082 | // doing when it finds the parameters in the body. |
| 3083 | while (!Body.empty()) { |
| 3084 | // Scan for the next possible parameter. |
| 3085 | std::size_t End = Body.size(), Pos = 0; |
| 3086 | for (; Pos != End; ++Pos) { |
| 3087 | // Check for a substitution or escape. |
| 3088 | // This macro is defined with parameters, look for \foo, \bar, etc. |
| 3089 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 3090 | break; |
| 3091 | |
| 3092 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
| 3093 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 3094 | continue; |
| 3095 | char Next = Body[Pos + 1]; |
| 3096 | if (Next == '$' || Next == 'n' || isdigit(Next)) |
| 3097 | break; |
| 3098 | } |
| 3099 | |
| 3100 | // Check if we reached the end. |
| 3101 | if (Pos == End) |
| 3102 | break; |
| 3103 | |
| 3104 | if (Body[Pos] == '$') { |
| 3105 | switch (Body[Pos+1]) { |
| 3106 | // $$ => $ |
| 3107 | case '$': |
| 3108 | break; |
| 3109 | |
| 3110 | // $n => number of arguments |
| 3111 | case 'n': |
| 3112 | PositionalParametersFound = true; |
| 3113 | break; |
| 3114 | |
| 3115 | // $[0-9] => argument |
| 3116 | default: { |
| 3117 | PositionalParametersFound = true; |
| 3118 | break; |
| 3119 | } |
| 3120 | } |
| 3121 | Pos += 2; |
| 3122 | } else { |
| 3123 | unsigned I = Pos + 1; |
| 3124 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
| 3125 | ++I; |
| 3126 | |
| 3127 | const char *Begin = Body.data() + Pos +1; |
| 3128 | StringRef Argument(Begin, I - (Pos +1)); |
| 3129 | unsigned Index = 0; |
| 3130 | for (; Index < NParameters; ++Index) |
| 3131 | if (Parameters[Index].first == Argument) |
| 3132 | break; |
| 3133 | |
| 3134 | if (Index == NParameters) { |
| 3135 | if (Body[Pos+1] == '(' && Body[Pos+2] == ')') |
| 3136 | Pos += 3; |
| 3137 | else { |
| 3138 | Pos = I; |
| 3139 | } |
| 3140 | } else { |
| 3141 | NamedParametersFound = true; |
| 3142 | Pos += 1 + Argument.size(); |
| 3143 | } |
| 3144 | } |
| 3145 | // Update the scan point. |
| 3146 | Body = Body.substr(Pos); |
| 3147 | } |
| 3148 | |
| 3149 | if (!NamedParametersFound && PositionalParametersFound) |
| 3150 | Warning(DirectiveLoc, "macro defined with named parameters which are not " |
| 3151 | "used in macro body, possible positional parameter " |
| 3152 | "found in body which will have no effect"); |
| 3153 | } |
| 3154 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3155 | /// ParseDirectiveEndMacro |
| 3156 | /// ::= .endm |
| 3157 | /// ::= .endmacro |
| 3158 | bool AsmParser::ParseDirectiveEndMacro(StringRef Directive) { |
| 3159 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3160 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3161 | |
| 3162 | // If we are inside a macro instantiation, terminate the current |
| 3163 | // instantiation. |
| 3164 | if (InsideMacroInstantiation()) { |
| 3165 | HandleMacroExit(); |
| 3166 | return false; |
| 3167 | } |
| 3168 | |
| 3169 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3170 | // .endmacro directives are handled during the macro definition parsing. |
| 3171 | return TokError("unexpected '" + Directive + "' in file, " |
| 3172 | "no current macro definition"); |
| 3173 | } |
| 3174 | |
| 3175 | /// ParseDirectivePurgeMacro |
| 3176 | /// ::= .purgem |
| 3177 | bool AsmParser::ParseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
| 3178 | StringRef Name; |
| 3179 | if (ParseIdentifier(Name)) |
| 3180 | return TokError("expected identifier in '.purgem' directive"); |
| 3181 | |
| 3182 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3183 | return TokError("unexpected token in '.purgem' directive"); |
| 3184 | |
| 3185 | if (!LookupMacro(Name)) |
| 3186 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3187 | |
| 3188 | UndefineMacro(Name); |
| 3189 | return false; |
| 3190 | } |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3191 | |
| 3192 | /// ParseDirectiveBundleAlignMode |
| 3193 | /// ::= {.bundle_align_mode} expression |
| 3194 | bool AsmParser::ParseDirectiveBundleAlignMode() { |
| 3195 | CheckForValidSection(); |
| 3196 | |
| 3197 | // Expect a single argument: an expression that evaluates to a constant |
| 3198 | // in the inclusive range 0-30. |
| 3199 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3200 | int64_t AlignSizePow2; |
| 3201 | if (ParseAbsoluteExpression(AlignSizePow2)) |
| 3202 | return true; |
| 3203 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3204 | return TokError("unexpected token after expression in" |
| 3205 | " '.bundle_align_mode' directive"); |
| 3206 | else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) |
| 3207 | return Error(ExprLoc, |
| 3208 | "invalid bundle alignment size (expected between 0 and 30)"); |
| 3209 | |
| 3210 | Lex(); |
| 3211 | |
| 3212 | // Because of AlignSizePow2's verified range we can safely truncate it to |
| 3213 | // unsigned. |
| 3214 | getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); |
| 3215 | return false; |
| 3216 | } |
| 3217 | |
| 3218 | /// ParseDirectiveBundleLock |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3219 | /// ::= {.bundle_lock} [align_to_end] |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3220 | bool AsmParser::ParseDirectiveBundleLock() { |
| 3221 | CheckForValidSection(); |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3222 | bool AlignToEnd = false; |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3223 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3224 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3225 | StringRef Option; |
| 3226 | SMLoc Loc = getTok().getLoc(); |
| 3227 | const char *kInvalidOptionError = |
| 3228 | "invalid option for '.bundle_lock' directive"; |
| 3229 | |
| 3230 | if (ParseIdentifier(Option)) |
| 3231 | return Error(Loc, kInvalidOptionError); |
| 3232 | |
| 3233 | if (Option != "align_to_end") |
| 3234 | return Error(Loc, kInvalidOptionError); |
| 3235 | else if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3236 | return Error(Loc, |
| 3237 | "unexpected token after '.bundle_lock' directive option"); |
| 3238 | AlignToEnd = true; |
| 3239 | } |
| 3240 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3241 | Lex(); |
| 3242 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 3243 | getStreamer().EmitBundleLock(AlignToEnd); |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 3244 | return false; |
| 3245 | } |
| 3246 | |
| 3247 | /// ParseDirectiveBundleLock |
| 3248 | /// ::= {.bundle_lock} |
| 3249 | bool AsmParser::ParseDirectiveBundleUnlock() { |
| 3250 | CheckForValidSection(); |
| 3251 | |
| 3252 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3253 | return TokError("unexpected token in '.bundle_unlock' directive"); |
| 3254 | Lex(); |
| 3255 | |
| 3256 | getStreamer().EmitBundleUnlock(); |
| 3257 | return false; |
| 3258 | } |
| 3259 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3260 | /// ParseDirectiveSpace |
| 3261 | /// ::= (.skip | .space) expression [ , expression ] |
| 3262 | bool AsmParser::ParseDirectiveSpace(StringRef IDVal) { |
| 3263 | CheckForValidSection(); |
| 3264 | |
| 3265 | int64_t NumBytes; |
| 3266 | if (ParseAbsoluteExpression(NumBytes)) |
| 3267 | return true; |
| 3268 | |
| 3269 | int64_t FillExpr = 0; |
| 3270 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 3271 | if (getLexer().isNot(AsmToken::Comma)) |
| 3272 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3273 | Lex(); |
| 3274 | |
| 3275 | if (ParseAbsoluteExpression(FillExpr)) |
| 3276 | return true; |
| 3277 | |
| 3278 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3279 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
| 3280 | } |
| 3281 | |
| 3282 | Lex(); |
| 3283 | |
| 3284 | if (NumBytes <= 0) |
| 3285 | return TokError("invalid number of bytes in '" + |
| 3286 | Twine(IDVal) + "' directive"); |
| 3287 | |
| 3288 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
| 3289 | getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE); |
| 3290 | |
| 3291 | return false; |
| 3292 | } |
| 3293 | |
| 3294 | /// ParseDirectiveLEB128 |
| 3295 | /// ::= (.sleb128 | .uleb128) expression |
| 3296 | bool AsmParser::ParseDirectiveLEB128(bool Signed) { |
| 3297 | CheckForValidSection(); |
| 3298 | const MCExpr *Value; |
| 3299 | |
| 3300 | if (ParseExpression(Value)) |
| 3301 | return true; |
| 3302 | |
| 3303 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3304 | return TokError("unexpected token in directive"); |
| 3305 | |
| 3306 | if (Signed) |
| 3307 | getStreamer().EmitSLEB128Value(Value); |
| 3308 | else |
| 3309 | getStreamer().EmitULEB128Value(Value); |
| 3310 | |
| 3311 | return false; |
| 3312 | } |
| 3313 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3314 | /// ParseDirectiveSymbolAttribute |
| 3315 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 3316 | bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3317 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3318 | for (;;) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3319 | StringRef Name; |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3320 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3321 | |
| 3322 | if (ParseIdentifier(Name)) |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3323 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3324 | |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 3325 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3326 | |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 3327 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 3328 | if (Sym->isTemporary()) |
| 3329 | return Error(Loc, "non-local symbol required in directive"); |
| 3330 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3331 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3332 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3333 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3334 | break; |
| 3335 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3336 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3337 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3338 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3339 | } |
| 3340 | } |
| 3341 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3342 | Lex(); |
Jan Wen Voung | a854a4b | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 3343 | return false; |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 3344 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3345 | |
| 3346 | /// ParseDirectiveComm |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3347 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
| 3348 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 3349 | CheckForValidSection(); |
| 3350 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3351 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 3352 | StringRef Name; |
| 3353 | if (ParseIdentifier(Name)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3354 | return TokError("expected identifier in directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3355 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 3356 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 3357 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3358 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3359 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3360 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3361 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3362 | |
| 3363 | int64_t Size; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3364 | SMLoc SizeLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3365 | if (ParseAbsoluteExpression(Size)) |
| 3366 | return true; |
| 3367 | |
| 3368 | int64_t Pow2Alignment = 0; |
| 3369 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3370 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3371 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3372 | Pow2AlignmentLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3373 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 3374 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3375 | |
Benjamin Kramer | a9e37c5 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3376 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 3377 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 39646d9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3378 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 3379 | |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3380 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | a9e37c5 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 3381 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 3382 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 3383 | if (!isPowerOf2_64(Pow2Alignment)) |
| 3384 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 3385 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 3386 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3387 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3388 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3389 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3390 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3391 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3392 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3393 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3394 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 3395 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3396 | if (Size < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3397 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
| 3398 | "be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3399 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 3400 | // 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] | 3401 | // may internally end up wanting an alignment in bytes. |
| 3402 | // FIXME: Diagnose overflow. |
| 3403 | if (Pow2Alignment < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3404 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
| 3405 | "alignment, can't be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3406 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 3407 | if (!Sym->isUndefined()) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3408 | return Error(IDLoc, "invalid symbol redefinition"); |
| 3409 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 3410 | // 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] | 3411 | if (IsLocal) { |
Benjamin Kramer | 39646d9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 3412 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 3413 | return false; |
| 3414 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3415 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3416 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 3417 | return false; |
| 3418 | } |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 3419 | |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3420 | /// ParseDirectiveAbort |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3421 | /// ::= .abort [... message ...] |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3422 | bool AsmParser::ParseDirectiveAbort() { |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3423 | // FIXME: Use loc from directive. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3424 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3425 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3426 | StringRef Str = ParseStringToEndOfStatement(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3427 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3428 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3429 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3430 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3431 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 3432 | if (Str.empty()) |
| 3433 | Error(Loc, ".abort detected. Assembly stopping."); |
| 3434 | else |
| 3435 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 3436 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 3437 | |
| 3438 | return false; |
| 3439 | } |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 3440 | |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3441 | /// ParseDirectiveInclude |
| 3442 | /// ::= .include "filename" |
| 3443 | bool AsmParser::ParseDirectiveInclude() { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3444 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3445 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3446 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 3447 | std::string Filename = getTok().getString(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3448 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3449 | Lex(); |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3450 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3451 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3452 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3453 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3454 | // Strip the quotes. |
| 3455 | Filename = Filename.substr(1, Filename.size()-2); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3456 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3457 | // Attempt to switch the lexer to the included file before consuming the end |
| 3458 | // of statement to avoid losing it when we switch. |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 3459 | if (EnterIncludeFile(Filename)) { |
Daniel Dunbar | 275ce39 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 3460 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 3461 | return true; |
| 3462 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 3463 | |
| 3464 | return false; |
| 3465 | } |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 3466 | |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 3467 | /// ParseDirectiveIncbin |
| 3468 | /// ::= .incbin "filename" |
| 3469 | bool AsmParser::ParseDirectiveIncbin() { |
| 3470 | if (getLexer().isNot(AsmToken::String)) |
| 3471 | return TokError("expected string in '.incbin' directive"); |
| 3472 | |
| 3473 | std::string Filename = getTok().getString(); |
| 3474 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 3475 | Lex(); |
| 3476 | |
| 3477 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3478 | return TokError("unexpected token in '.incbin' directive"); |
| 3479 | |
| 3480 | // Strip the quotes. |
| 3481 | Filename = Filename.substr(1, Filename.size()-2); |
| 3482 | |
| 3483 | // Attempt to process the included file. |
| 3484 | if (ProcessIncbinFile(Filename)) { |
| 3485 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 3486 | return true; |
| 3487 | } |
| 3488 | |
| 3489 | return false; |
| 3490 | } |
| 3491 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3492 | /// ParseDirectiveIf |
| 3493 | /// ::= .if expression |
| 3494 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3495 | TheCondStack.push_back(TheCondState); |
| 3496 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3497 | if (TheCondState.Ignore) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3498 | EatToEndOfStatement(); |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3499 | } else { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3500 | int64_t ExprValue; |
| 3501 | if (ParseAbsoluteExpression(ExprValue)) |
| 3502 | return true; |
| 3503 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3504 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3505 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3506 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3507 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3508 | |
| 3509 | TheCondState.CondMet = ExprValue; |
| 3510 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3511 | } |
| 3512 | |
| 3513 | return false; |
| 3514 | } |
| 3515 | |
Benjamin Kramer | a3dd0eb | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3516 | /// ParseDirectiveIfb |
| 3517 | /// ::= .ifb string |
| 3518 | bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
| 3519 | TheCondStack.push_back(TheCondState); |
| 3520 | TheCondState.TheCond = AsmCond::IfCond; |
| 3521 | |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3522 | if (TheCondState.Ignore) { |
Benjamin Kramer | a3dd0eb | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 3523 | EatToEndOfStatement(); |
| 3524 | } else { |
| 3525 | StringRef Str = ParseStringToEndOfStatement(); |
| 3526 | |
| 3527 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3528 | return TokError("unexpected token in '.ifb' directive"); |
| 3529 | |
| 3530 | Lex(); |
| 3531 | |
| 3532 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 3533 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3534 | } |
| 3535 | |
| 3536 | return false; |
| 3537 | } |
| 3538 | |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3539 | /// ParseDirectiveIfc |
| 3540 | /// ::= .ifc string1, string2 |
| 3541 | bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
| 3542 | TheCondStack.push_back(TheCondState); |
| 3543 | TheCondState.TheCond = AsmCond::IfCond; |
| 3544 | |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 3545 | if (TheCondState.Ignore) { |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 3546 | EatToEndOfStatement(); |
| 3547 | } else { |
| 3548 | StringRef Str1 = ParseStringToComma(); |
| 3549 | |
| 3550 | if (getLexer().isNot(AsmToken::Comma)) |
| 3551 | return TokError("unexpected token in '.ifc' directive"); |
| 3552 | |
| 3553 | Lex(); |
| 3554 | |
| 3555 | StringRef Str2 = ParseStringToEndOfStatement(); |
| 3556 | |
| 3557 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3558 | return TokError("unexpected token in '.ifc' directive"); |
| 3559 | |
| 3560 | Lex(); |
| 3561 | |
| 3562 | TheCondState.CondMet = ExpectEqual == (Str1 == Str2); |
| 3563 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3564 | } |
| 3565 | |
| 3566 | return false; |
| 3567 | } |
| 3568 | |
| 3569 | /// ParseDirectiveIfdef |
| 3570 | /// ::= .ifdef symbol |
Benjamin Kramer | 0fd90bc | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 3571 | bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
| 3572 | StringRef Name; |
| 3573 | TheCondStack.push_back(TheCondState); |
| 3574 | TheCondState.TheCond = AsmCond::IfCond; |
| 3575 | |
| 3576 | if (TheCondState.Ignore) { |
| 3577 | EatToEndOfStatement(); |
| 3578 | } else { |
| 3579 | if (ParseIdentifier(Name)) |
| 3580 | return TokError("expected identifier after '.ifdef'"); |
| 3581 | |
| 3582 | Lex(); |
| 3583 | |
| 3584 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 3585 | |
| 3586 | if (expect_defined) |
| 3587 | TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined()); |
| 3588 | else |
| 3589 | TheCondState.CondMet = (Sym == NULL || Sym->isUndefined()); |
| 3590 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3591 | } |
| 3592 | |
| 3593 | return false; |
| 3594 | } |
| 3595 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3596 | /// ParseDirectiveElseIf |
| 3597 | /// ::= .elseif expression |
| 3598 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { |
| 3599 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 3600 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 3601 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 3602 | " an .elseif"); |
| 3603 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 3604 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3605 | bool LastIgnoreState = false; |
| 3606 | if (!TheCondStack.empty()) |
| 3607 | LastIgnoreState = TheCondStack.back().Ignore; |
| 3608 | if (LastIgnoreState || TheCondState.CondMet) { |
| 3609 | TheCondState.Ignore = true; |
| 3610 | EatToEndOfStatement(); |
| 3611 | } |
| 3612 | else { |
| 3613 | int64_t ExprValue; |
| 3614 | if (ParseAbsoluteExpression(ExprValue)) |
| 3615 | return true; |
| 3616 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3617 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3618 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3619 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3620 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3621 | TheCondState.CondMet = ExprValue; |
| 3622 | TheCondState.Ignore = !TheCondState.CondMet; |
| 3623 | } |
| 3624 | |
| 3625 | return false; |
| 3626 | } |
| 3627 | |
| 3628 | /// ParseDirectiveElse |
| 3629 | /// ::= .else |
| 3630 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3631 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3632 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3633 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3634 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3635 | |
| 3636 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 3637 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 3638 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 3639 | ".elseif"); |
| 3640 | TheCondState.TheCond = AsmCond::ElseCond; |
| 3641 | bool LastIgnoreState = false; |
| 3642 | if (!TheCondStack.empty()) |
| 3643 | LastIgnoreState = TheCondStack.back().Ignore; |
| 3644 | if (LastIgnoreState || TheCondState.CondMet) |
| 3645 | TheCondState.Ignore = true; |
| 3646 | else |
| 3647 | TheCondState.Ignore = false; |
| 3648 | |
| 3649 | return false; |
| 3650 | } |
| 3651 | |
| 3652 | /// ParseDirectiveEndIf |
| 3653 | /// ::= .endif |
| 3654 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 3655 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3656 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 3657 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 3658 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 3659 | |
| 3660 | if ((TheCondState.TheCond == AsmCond::NoCond) || |
| 3661 | TheCondStack.empty()) |
| 3662 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 3663 | ".else"); |
| 3664 | if (!TheCondStack.empty()) { |
| 3665 | TheCondState = TheCondStack.back(); |
| 3666 | TheCondStack.pop_back(); |
| 3667 | } |
| 3668 | |
| 3669 | return false; |
| 3670 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 3671 | |
Eli Bendersky | 6ee1308 | 2013-01-15 22:59:42 +0000 | [diff] [blame] | 3672 | void AsmParser::initializeDirectiveKindMap() { |
| 3673 | DirectiveKindMap[".set"] = DK_SET; |
| 3674 | DirectiveKindMap[".equ"] = DK_EQU; |
| 3675 | DirectiveKindMap[".equiv"] = DK_EQUIV; |
| 3676 | DirectiveKindMap[".ascii"] = DK_ASCII; |
| 3677 | DirectiveKindMap[".asciz"] = DK_ASCIZ; |
| 3678 | DirectiveKindMap[".string"] = DK_STRING; |
| 3679 | DirectiveKindMap[".byte"] = DK_BYTE; |
| 3680 | DirectiveKindMap[".short"] = DK_SHORT; |
| 3681 | DirectiveKindMap[".value"] = DK_VALUE; |
| 3682 | DirectiveKindMap[".2byte"] = DK_2BYTE; |
| 3683 | DirectiveKindMap[".long"] = DK_LONG; |
| 3684 | DirectiveKindMap[".int"] = DK_INT; |
| 3685 | DirectiveKindMap[".4byte"] = DK_4BYTE; |
| 3686 | DirectiveKindMap[".quad"] = DK_QUAD; |
| 3687 | DirectiveKindMap[".8byte"] = DK_8BYTE; |
| 3688 | DirectiveKindMap[".single"] = DK_SINGLE; |
| 3689 | DirectiveKindMap[".float"] = DK_FLOAT; |
| 3690 | DirectiveKindMap[".double"] = DK_DOUBLE; |
| 3691 | DirectiveKindMap[".align"] = DK_ALIGN; |
| 3692 | DirectiveKindMap[".align32"] = DK_ALIGN32; |
| 3693 | DirectiveKindMap[".balign"] = DK_BALIGN; |
| 3694 | DirectiveKindMap[".balignw"] = DK_BALIGNW; |
| 3695 | DirectiveKindMap[".balignl"] = DK_BALIGNL; |
| 3696 | DirectiveKindMap[".p2align"] = DK_P2ALIGN; |
| 3697 | DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; |
| 3698 | DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; |
| 3699 | DirectiveKindMap[".org"] = DK_ORG; |
| 3700 | DirectiveKindMap[".fill"] = DK_FILL; |
| 3701 | DirectiveKindMap[".zero"] = DK_ZERO; |
| 3702 | DirectiveKindMap[".extern"] = DK_EXTERN; |
| 3703 | DirectiveKindMap[".globl"] = DK_GLOBL; |
| 3704 | DirectiveKindMap[".global"] = DK_GLOBAL; |
| 3705 | DirectiveKindMap[".indirect_symbol"] = DK_INDIRECT_SYMBOL; |
| 3706 | DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; |
| 3707 | DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; |
| 3708 | DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; |
| 3709 | DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; |
| 3710 | DirectiveKindMap[".reference"] = DK_REFERENCE; |
| 3711 | DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; |
| 3712 | DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; |
| 3713 | DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
| 3714 | DirectiveKindMap[".comm"] = DK_COMM; |
| 3715 | DirectiveKindMap[".common"] = DK_COMMON; |
| 3716 | DirectiveKindMap[".lcomm"] = DK_LCOMM; |
| 3717 | DirectiveKindMap[".abort"] = DK_ABORT; |
| 3718 | DirectiveKindMap[".include"] = DK_INCLUDE; |
| 3719 | DirectiveKindMap[".incbin"] = DK_INCBIN; |
| 3720 | DirectiveKindMap[".code16"] = DK_CODE16; |
| 3721 | DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; |
| 3722 | DirectiveKindMap[".rept"] = DK_REPT; |
| 3723 | DirectiveKindMap[".irp"] = DK_IRP; |
| 3724 | DirectiveKindMap[".irpc"] = DK_IRPC; |
| 3725 | DirectiveKindMap[".endr"] = DK_ENDR; |
| 3726 | DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; |
| 3727 | DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; |
| 3728 | DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; |
| 3729 | DirectiveKindMap[".if"] = DK_IF; |
| 3730 | DirectiveKindMap[".ifb"] = DK_IFB; |
| 3731 | DirectiveKindMap[".ifnb"] = DK_IFNB; |
| 3732 | DirectiveKindMap[".ifc"] = DK_IFC; |
| 3733 | DirectiveKindMap[".ifnc"] = DK_IFNC; |
| 3734 | DirectiveKindMap[".ifdef"] = DK_IFDEF; |
| 3735 | DirectiveKindMap[".ifndef"] = DK_IFNDEF; |
| 3736 | DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; |
| 3737 | DirectiveKindMap[".elseif"] = DK_ELSEIF; |
| 3738 | DirectiveKindMap[".else"] = DK_ELSE; |
| 3739 | DirectiveKindMap[".endif"] = DK_ENDIF; |
| 3740 | DirectiveKindMap[".skip"] = DK_SKIP; |
| 3741 | DirectiveKindMap[".space"] = DK_SPACE; |
| 3742 | DirectiveKindMap[".file"] = DK_FILE; |
| 3743 | DirectiveKindMap[".line"] = DK_LINE; |
| 3744 | DirectiveKindMap[".loc"] = DK_LOC; |
| 3745 | DirectiveKindMap[".stabs"] = DK_STABS; |
| 3746 | DirectiveKindMap[".sleb128"] = DK_SLEB128; |
| 3747 | DirectiveKindMap[".uleb128"] = DK_ULEB128; |
| 3748 | DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; |
| 3749 | DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; |
| 3750 | DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; |
| 3751 | DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; |
| 3752 | DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; |
| 3753 | DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; |
| 3754 | DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; |
| 3755 | DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; |
| 3756 | DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; |
| 3757 | DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; |
| 3758 | DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; |
| 3759 | DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; |
| 3760 | DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; |
| 3761 | DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; |
| 3762 | DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; |
| 3763 | DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; |
| 3764 | DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; |
| 3765 | DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; |
| 3766 | DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; |
| 3767 | DirectiveKindMap[".macros_on"] = DK_MACROS_ON; |
| 3768 | DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; |
| 3769 | DirectiveKindMap[".macro"] = DK_MACRO; |
| 3770 | DirectiveKindMap[".endm"] = DK_ENDM; |
| 3771 | DirectiveKindMap[".endmacro"] = DK_ENDMACRO; |
| 3772 | DirectiveKindMap[".purgem"] = DK_PURGEM; |
Eli Bendersky | 5d0f061 | 2013-01-10 22:44:57 +0000 | [diff] [blame] | 3773 | } |
| 3774 | |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3775 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3776 | MCAsmMacro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3777 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3778 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3779 | unsigned NestLevel = 0; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3780 | for (;;) { |
| 3781 | // Check whether we have reached the end of the file. |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3782 | if (getLexer().is(AsmToken::Eof)) { |
| 3783 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
| 3784 | return 0; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3787 | if (Lexer.is(AsmToken::Identifier) && |
| 3788 | (getTok().getIdentifier() == ".rept")) { |
| 3789 | ++NestLevel; |
| 3790 | } |
| 3791 | |
| 3792 | // Otherwise, check whether we have reached the .endr. |
| 3793 | if (Lexer.is(AsmToken::Identifier) && |
| 3794 | getTok().getIdentifier() == ".endr") { |
| 3795 | if (NestLevel == 0) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3796 | EndToken = getTok(); |
| 3797 | Lex(); |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3798 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 3799 | TokError("unexpected token in '.endr' directive"); |
| 3800 | return 0; |
| 3801 | } |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3802 | break; |
| 3803 | } |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3804 | --NestLevel; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3805 | } |
| 3806 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3807 | // Otherwise, scan till the end of the statement. |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3808 | EatToEndOfStatement(); |
| 3809 | } |
| 3810 | |
| 3811 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3812 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3813 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 3814 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3815 | // We Are Anonymous. |
| 3816 | StringRef Name; |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3817 | MCAsmMacroParameters Parameters; |
| 3818 | return new MCAsmMacro(Name, Body, Parameters); |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3821 | void AsmParser::InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3822 | raw_svector_ostream &OS) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3823 | OS << ".endr\n"; |
| 3824 | |
| 3825 | MemoryBuffer *Instantiation = |
| 3826 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
| 3827 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3828 | // Create the macro instantiation object and add to the current macro |
| 3829 | // instantiation stack. |
| 3830 | MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc, |
Daniel Dunbar | 4259a1a | 2012-12-01 01:38:48 +0000 | [diff] [blame] | 3831 | CurBuffer, |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3832 | getTok().getLoc(), |
| 3833 | Instantiation); |
| 3834 | ActiveMacros.push_back(MI); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3835 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3836 | // Jump to the macro instantiation and prime the lexer. |
| 3837 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 3838 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 3839 | Lex(); |
| 3840 | } |
| 3841 | |
| 3842 | bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) { |
| 3843 | int64_t Count; |
| 3844 | if (ParseAbsoluteExpression(Count)) |
| 3845 | return TokError("unexpected token in '.rept' directive"); |
| 3846 | |
| 3847 | if (Count < 0) |
| 3848 | return TokError("Count is negative"); |
| 3849 | |
| 3850 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 3851 | return TokError("unexpected token in '.rept' directive"); |
| 3852 | |
| 3853 | // Eat the end of statement. |
| 3854 | Lex(); |
| 3855 | |
| 3856 | // Lex the rept definition. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3857 | MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3858 | if (!M) |
| 3859 | return true; |
| 3860 | |
| 3861 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3862 | // to hold the macro body with substitutions. |
| 3863 | SmallString<256> Buf; |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3864 | MCAsmMacroParameters Parameters; |
| 3865 | MCAsmMacroArguments A; |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3866 | raw_svector_ostream OS(Buf); |
| 3867 | while (Count--) { |
| 3868 | if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc())) |
| 3869 | return true; |
| 3870 | } |
| 3871 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3872 | |
| 3873 | return false; |
| 3874 | } |
| 3875 | |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3876 | /// ParseDirectiveIrp |
| 3877 | /// ::= .irp symbol,values |
| 3878 | bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) { |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3879 | MCAsmMacroParameters Parameters; |
| 3880 | MCAsmMacroParameter Parameter; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3881 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3882 | if (ParseIdentifier(Parameter.first)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3883 | return TokError("expected identifier in '.irp' directive"); |
| 3884 | |
| 3885 | Parameters.push_back(Parameter); |
| 3886 | |
| 3887 | if (Lexer.isNot(AsmToken::Comma)) |
| 3888 | return TokError("expected comma in '.irp' directive"); |
| 3889 | |
| 3890 | Lex(); |
| 3891 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3892 | MCAsmMacroArguments A; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3893 | if (ParseMacroArguments(0, A)) |
| 3894 | return true; |
| 3895 | |
| 3896 | // Eat the end of statement. |
| 3897 | Lex(); |
| 3898 | |
| 3899 | // Lex the irp definition. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3900 | MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3901 | if (!M) |
| 3902 | return true; |
| 3903 | |
| 3904 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3905 | // to hold the macro body with substitutions. |
| 3906 | SmallString<256> Buf; |
| 3907 | raw_svector_ostream OS(Buf); |
| 3908 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3909 | for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { |
| 3910 | MCAsmMacroArguments Args; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3911 | Args.push_back(*i); |
| 3912 | |
| 3913 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 3914 | return true; |
| 3915 | } |
| 3916 | |
| 3917 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
| 3918 | |
| 3919 | return false; |
| 3920 | } |
| 3921 | |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3922 | /// ParseDirectiveIrpc |
| 3923 | /// ::= .irpc symbol,values |
| 3924 | bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) { |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3925 | MCAsmMacroParameters Parameters; |
| 3926 | MCAsmMacroParameter Parameter; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3927 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3928 | if (ParseIdentifier(Parameter.first)) |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3929 | return TokError("expected identifier in '.irpc' directive"); |
| 3930 | |
| 3931 | Parameters.push_back(Parameter); |
| 3932 | |
| 3933 | if (Lexer.isNot(AsmToken::Comma)) |
| 3934 | return TokError("expected comma in '.irpc' directive"); |
| 3935 | |
| 3936 | Lex(); |
| 3937 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3938 | MCAsmMacroArguments A; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3939 | if (ParseMacroArguments(0, A)) |
| 3940 | return true; |
| 3941 | |
| 3942 | if (A.size() != 1 || A.front().size() != 1) |
| 3943 | return TokError("unexpected token in '.irpc' directive"); |
| 3944 | |
| 3945 | // Eat the end of statement. |
| 3946 | Lex(); |
| 3947 | |
| 3948 | // Lex the irpc definition. |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3949 | MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3950 | if (!M) |
| 3951 | return true; |
| 3952 | |
| 3953 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3954 | // to hold the macro body with substitutions. |
| 3955 | SmallString<256> Buf; |
| 3956 | raw_svector_ostream OS(Buf); |
| 3957 | |
| 3958 | StringRef Values = A.front().front().getString(); |
| 3959 | std::size_t I, End = Values.size(); |
| 3960 | for (I = 0; I < End; ++I) { |
Eli Bendersky | 9bac6b2 | 2013-01-14 19:00:26 +0000 | [diff] [blame] | 3961 | MCAsmMacroArgument Arg; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3962 | Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1))); |
| 3963 | |
Eli Bendersky | c0c67b0 | 2013-01-14 23:22:36 +0000 | [diff] [blame] | 3964 | MCAsmMacroArguments Args; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3965 | Args.push_back(Arg); |
| 3966 | |
| 3967 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 3968 | return true; |
| 3969 | } |
| 3970 | |
| 3971 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
| 3972 | |
| 3973 | return false; |
| 3974 | } |
| 3975 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3976 | bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) { |
| 3977 | if (ActiveMacros.empty()) |
Preston Gurd | 6579eea | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 3978 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3979 | |
| 3980 | // The only .repl that should get here are the ones created by |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3981 | // InstantiateMacroLikeBody. |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3982 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 3983 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3984 | HandleMacroExit(); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3985 | return false; |
| 3986 | } |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3987 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3988 | bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) { |
| 3989 | const MCExpr *Value; |
| 3990 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3991 | if (ParseExpression(Value)) |
| 3992 | return true; |
| 3993 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 3994 | if (!MCE) |
| 3995 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 3996 | uint64_t IntValue = MCE->getValue(); |
| 3997 | if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) |
| 3998 | return Error(ExprLoc, "literal value out of range for directive"); |
| 3999 | |
| 4000 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5)); |
| 4001 | return false; |
| 4002 | } |
| 4003 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4004 | bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
| 4005 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4006 | SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4007 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4008 | SmallVectorImpl<std::string> &Clobbers, |
| 4009 | const MCInstrInfo *MII, |
| 4010 | const MCInstPrinter *IP, |
| 4011 | MCAsmParserSemaCallback &SI) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4012 | SmallVector<void *, 4> InputDecls; |
| 4013 | SmallVector<void *, 4> OutputDecls; |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4014 | SmallVector<bool, 4> InputDeclsAddressOf; |
| 4015 | SmallVector<bool, 4> OutputDeclsAddressOf; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4016 | SmallVector<std::string, 4> InputConstraints; |
| 4017 | SmallVector<std::string, 4> OutputConstraints; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4018 | std::set<std::string> ClobberRegs; |
| 4019 | |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4020 | SmallVector<struct AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4021 | |
| 4022 | // Prime the lexer. |
| 4023 | Lex(); |
| 4024 | |
| 4025 | // While we have input, parse each statement. |
| 4026 | unsigned InputIdx = 0; |
| 4027 | unsigned OutputIdx = 0; |
| 4028 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4029 | ParseStatementInfo Info(&AsmStrRewrites); |
| 4030 | if (ParseStatement(Info)) |
Chad Rosier | ab450e4 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 4031 | return true; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4032 | |
Chad Rosier | 5749801 | 2012-12-12 22:45:52 +0000 | [diff] [blame] | 4033 | if (Info.ParseError) |
| 4034 | return true; |
| 4035 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4036 | if (Info.Opcode != ~0U) { |
| 4037 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4038 | |
| 4039 | // Build the list of clobbers, outputs and inputs. |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4040 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
| 4041 | MCParsedAsmOperand *Operand = Info.ParsedOperands[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4042 | |
| 4043 | // Immediate. |
| 4044 | if (Operand->isImm()) { |
Chad Rosier | efcb3d9 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4045 | if (Operand->needAsmRewrite()) |
| 4046 | AsmStrRewrites.push_back(AsmRewrite(AOK_ImmPrefix, |
| 4047 | Operand->getStartLoc())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4048 | continue; |
| 4049 | } |
| 4050 | |
| 4051 | // Register operand. |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4052 | if (Operand->isReg() && !Operand->needAddressOf()) { |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4053 | unsigned NumDefs = Desc.getNumDefs(); |
| 4054 | // Clobber. |
| 4055 | if (NumDefs && Operand->getMCOperandNum() < NumDefs) { |
| 4056 | std::string Reg; |
| 4057 | raw_string_ostream OS(Reg); |
| 4058 | IP->printRegName(OS, Operand->getReg()); |
| 4059 | ClobberRegs.insert(StringRef(OS.str())); |
| 4060 | } |
| 4061 | continue; |
| 4062 | } |
| 4063 | |
| 4064 | // Expr/Input or Output. |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4065 | bool IsVarDecl; |
Chad Rosier | 505bca3 | 2013-01-17 19:21:48 +0000 | [diff] [blame] | 4066 | unsigned Length, Size, Type; |
Chad Rosier | 3298959 | 2012-10-18 20:27:15 +0000 | [diff] [blame] | 4067 | void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc, |
Chad Rosier | 505bca3 | 2013-01-17 19:21:48 +0000 | [diff] [blame] | 4068 | Length, Size, Type, IsVarDecl); |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4069 | if (OpDecl) { |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4070 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4071 | if (Operand->isMem() && Operand->needSizeDirective()) |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4072 | AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective, |
Chad Rosier | efcb3d9 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4073 | Operand->getStartLoc(), |
| 4074 | /*Len*/0, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4075 | Operand->getMemSize())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4076 | if (isOutput) { |
| 4077 | std::string Constraint = "="; |
| 4078 | ++InputIdx; |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4079 | OutputDecls.push_back(OpDecl); |
NAKAMURA Takumi | b956ec1 | 2013-01-11 02:50:09 +0000 | [diff] [blame] | 4080 | OutputDeclsAddressOf.push_back(Operand->needAddressOf()); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4081 | Constraint += Operand->getConstraint().str(); |
| 4082 | OutputConstraints.push_back(Constraint); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4083 | AsmStrRewrites.push_back(AsmRewrite(AOK_Output, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4084 | Operand->getStartLoc(), |
| 4085 | Operand->getNameLen())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4086 | } else { |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4087 | InputDecls.push_back(OpDecl); |
NAKAMURA Takumi | b956ec1 | 2013-01-11 02:50:09 +0000 | [diff] [blame] | 4088 | InputDeclsAddressOf.push_back(Operand->needAddressOf()); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4089 | InputConstraints.push_back(Operand->getConstraint().str()); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4090 | AsmStrRewrites.push_back(AsmRewrite(AOK_Input, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4091 | Operand->getStartLoc(), |
| 4092 | Operand->getNameLen())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4093 | } |
| 4094 | } |
| 4095 | } |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4096 | } |
| 4097 | } |
| 4098 | |
| 4099 | // Set the number of Outputs and Inputs. |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4100 | NumOutputs = OutputDecls.size(); |
| 4101 | NumInputs = InputDecls.size(); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4102 | |
| 4103 | // Set the unique clobbers. |
| 4104 | for (std::set<std::string>::iterator I = ClobberRegs.begin(), |
| 4105 | E = ClobberRegs.end(); I != E; ++I) |
| 4106 | Clobbers.push_back(*I); |
| 4107 | |
| 4108 | // Merge the various outputs and inputs. Output are expected first. |
| 4109 | if (NumOutputs || NumInputs) { |
| 4110 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 4111 | OpDecls.resize(NumExprs); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4112 | Constraints.resize(NumExprs); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4113 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4114 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); |
Chad Rosier | 1c99a7f | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4115 | Constraints[i] = OutputConstraints[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4116 | } |
| 4117 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | c1ec207 | 2013-01-10 22:10:27 +0000 | [diff] [blame] | 4118 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); |
Chad Rosier | 1c99a7f | 2013-01-15 23:07:53 +0000 | [diff] [blame] | 4119 | Constraints[j] = InputConstraints[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | // Build the IR assembly string. |
| 4124 | std::string AsmStringIR; |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4125 | AsmRewriteKind PrevKind = AOK_Imm; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4126 | raw_string_ostream OS(AsmStringIR); |
| 4127 | const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart(); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4128 | for (SmallVectorImpl<struct AsmRewrite>::iterator |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4129 | I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) { |
| 4130 | const char *Loc = (*I).Loc.getPointer(); |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4131 | |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 4132 | AsmRewriteKind Kind = (*I).Kind; |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4133 | |
| 4134 | // Emit everything up to the immediate/expression. If the previous rewrite |
| 4135 | // was a size directive, then this has already been done. |
| 4136 | if (PrevKind != AOK_SizeDirective) |
| 4137 | OS << StringRef(Start, Loc - Start); |
| 4138 | PrevKind = Kind; |
| 4139 | |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4140 | // Skip the original expression. |
| 4141 | if (Kind == AOK_Skip) { |
| 4142 | Start = Loc + (*I).Len; |
| 4143 | continue; |
| 4144 | } |
| 4145 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4146 | // Rewrite expressions in $N notation. |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4147 | switch (Kind) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 4148 | default: break; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4149 | case AOK_Imm: |
Chad Rosier | efcb3d9 | 2012-10-26 18:04:20 +0000 | [diff] [blame] | 4150 | OS << Twine("$$"); |
| 4151 | OS << (*I).Val; |
| 4152 | break; |
| 4153 | case AOK_ImmPrefix: |
| 4154 | OS << Twine("$$"); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4155 | break; |
| 4156 | case AOK_Input: |
| 4157 | OS << '$'; |
| 4158 | OS << InputIdx++; |
| 4159 | break; |
| 4160 | case AOK_Output: |
| 4161 | OS << '$'; |
| 4162 | OS << OutputIdx++; |
| 4163 | break; |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4164 | case AOK_SizeDirective: |
Chad Rosier | 6a020a7 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4165 | switch((*I).Val) { |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4166 | default: break; |
| 4167 | case 8: OS << "byte ptr "; break; |
| 4168 | case 16: OS << "word ptr "; break; |
| 4169 | case 32: OS << "dword ptr "; break; |
| 4170 | case 64: OS << "qword ptr "; break; |
| 4171 | case 80: OS << "xword ptr "; break; |
| 4172 | case 128: OS << "xmmword ptr "; break; |
| 4173 | case 256: OS << "ymmword ptr "; break; |
| 4174 | } |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 4175 | break; |
| 4176 | case AOK_Emit: |
| 4177 | OS << ".byte"; |
| 4178 | break; |
Chad Rosier | 6a020a7 | 2012-10-25 20:41:34 +0000 | [diff] [blame] | 4179 | case AOK_DotOperator: |
| 4180 | OS << (*I).Val; |
| 4181 | break; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4182 | } |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4183 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4184 | // Skip the original expression. |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 4185 | if (Kind != AOK_SizeDirective) |
| 4186 | Start = Loc + (*I).Len; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| 4189 | // Emit the remainder of the asm string. |
| 4190 | const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd(); |
| 4191 | if (Start != AsmEnd) |
| 4192 | OS << StringRef(Start, AsmEnd - Start); |
| 4193 | |
| 4194 | AsmString = OS.str(); |
| 4195 | return false; |
| 4196 | } |
| 4197 | |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4198 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4199 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4200 | MCContext &C, MCStreamer &Out, |
| 4201 | const MCAsmInfo &MAI) { |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 4202 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 4203 | } |