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