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 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 89 | struct AsmRewrite; |
| 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 | 8f138d1 | 2012-10-15 17:19:13 +0000 | [diff] [blame] | 1368 | bool HadError = getTargetParser().ParseInstruction(OpcodeStr.str(), IDLoc, |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1369 | Info.ParsedOperands); |
Chris Lattner | 2cf5f14 | 2009-06-22 01:29:09 +0000 | [diff] [blame] | 1370 | |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1371 | // Dump the parsed representation, if requested. |
| 1372 | if (getShowParsedOperands()) { |
| 1373 | SmallString<256> Str; |
| 1374 | raw_svector_ostream OS(Str); |
| 1375 | OS << "parsed instruction: ["; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1376 | for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1377 | if (i != 0) |
| 1378 | OS << ", "; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1379 | Info.ParsedOperands[i]->print(OS); |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1380 | } |
| 1381 | OS << "]"; |
| 1382 | |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 1383 | PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str()); |
Daniel Dunbar | 3c14ca4 | 2010-08-11 06:37:09 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1386 | // If we are generating dwarf for assembly source files and the current |
| 1387 | // section is the initial text section then generate a .loc directive for |
| 1388 | // the instruction. |
| 1389 | if (!HadError && getContext().getGenDwarfForAssembly() && |
| 1390 | getContext().getGenDwarfSection() == getStreamer().getCurrentSection() ) { |
| 1391 | getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(), |
| 1392 | SrcMgr.FindLineNumber(IDLoc, CurBuffer), |
| 1393 | 0, DWARF2_LINE_DEFAULT_IS_STMT ? |
Kevin Enderby | dba9a17 | 2011-11-02 17:56:38 +0000 | [diff] [blame] | 1394 | DWARF2_FLAG_IS_STMT : 0, 0, 0, |
Kevin Enderby | 613b757 | 2011-11-01 22:27:22 +0000 | [diff] [blame] | 1395 | StringRef()); |
| 1396 | } |
| 1397 | |
Daniel Dunbar | 31e8e1d | 2010-05-04 00:33:07 +0000 | [diff] [blame] | 1398 | // If parsing succeeded, match the instruction. |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1399 | if (!HadError) { |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1400 | unsigned ErrorInfo; |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 1401 | HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, |
| 1402 | Info.ParsedOperands, |
| 1403 | Out, ErrorInfo, |
Chad Rosier | 84125ca | 2012-10-13 00:26:04 +0000 | [diff] [blame] | 1404 | ParsingInlineAsm); |
| 1405 | } |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1406 | |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1407 | // Don't skip the rest of the line, the instruction parser is responsible for |
| 1408 | // that. |
| 1409 | return false; |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1410 | } |
Chris Lattner | 9a023f7 | 2009-06-24 04:43:34 +0000 | [diff] [blame] | 1411 | |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1412 | /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line |
| 1413 | /// since they may not be able to be tokenized to get to the end of line token. |
| 1414 | void AsmParser::EatToEndOfLine() { |
Rafael Espindola | 12ae527 | 2011-10-19 18:48:52 +0000 | [diff] [blame] | 1415 | if (!Lexer.is(AsmToken::EndOfStatement)) |
| 1416 | Lexer.LexUntilEndOfLine(); |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1417 | // Eat EOL. |
| 1418 | Lex(); |
| 1419 | } |
| 1420 | |
| 1421 | /// ParseCppHashLineFilenameComment as this: |
| 1422 | /// ::= # number "filename" |
| 1423 | /// or just as a full line comment if it doesn't have a number and a string. |
| 1424 | bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) { |
| 1425 | Lex(); // Eat the hash token. |
| 1426 | |
| 1427 | if (getLexer().isNot(AsmToken::Integer)) { |
| 1428 | // Consume the line since in cases it is not a well-formed line directive, |
| 1429 | // as if were simply a full line comment. |
| 1430 | EatToEndOfLine(); |
| 1431 | return false; |
| 1432 | } |
| 1433 | |
| 1434 | int64_t LineNumber = getTok().getIntVal(); |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1435 | Lex(); |
| 1436 | |
| 1437 | if (getLexer().isNot(AsmToken::String)) { |
| 1438 | EatToEndOfLine(); |
| 1439 | return false; |
| 1440 | } |
| 1441 | |
| 1442 | StringRef Filename = getTok().getString(); |
| 1443 | // Get rid of the enclosing quotes. |
| 1444 | Filename = Filename.substr(1, Filename.size()-2); |
| 1445 | |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1446 | // Save the SMLoc, Filename and LineNumber for later use by diagnostics. |
| 1447 | CppHashLoc = L; |
| 1448 | CppHashFilename = Filename; |
| 1449 | CppHashLineNumber = LineNumber; |
Kevin Enderby | f1c21a8 | 2011-09-13 23:45:18 +0000 | [diff] [blame] | 1450 | |
| 1451 | // Ignore any trailing characters, they're just comment. |
| 1452 | EatToEndOfLine(); |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 1456 | /// DiagHandler - will use the last parsed cpp hash line filename comment |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1457 | /// for the Filename and LineNo if any in the diagnostic. |
| 1458 | void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { |
| 1459 | const AsmParser *Parser = static_cast<const AsmParser*>(Context); |
| 1460 | raw_ostream &OS = errs(); |
| 1461 | |
| 1462 | const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); |
| 1463 | const SMLoc &DiagLoc = Diag.getLoc(); |
| 1464 | int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
| 1465 | int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); |
| 1466 | |
| 1467 | // Like SourceMgr::PrintMessage() we need to print the include stack if any |
| 1468 | // before printing the message. |
| 1469 | int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1470 | if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) { |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1471 | SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); |
| 1472 | DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); |
| 1473 | } |
| 1474 | |
| 1475 | // If we have not parsed a cpp hash line filename comment or the source |
| 1476 | // manager changed or buffer changed (like in a nested include) then just |
| 1477 | // print the normal diagnostic using its Filename and LineNo. |
| 1478 | if (!Parser->CppHashLineNumber || |
| 1479 | &DiagSrcMgr != &Parser->SrcMgr || |
| 1480 | DiagBuf != CppHashBuf) { |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1481 | if (Parser->SavedDiagHandler) |
| 1482 | Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); |
| 1483 | else |
| 1484 | Diag.print(0, OS); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1485 | return; |
| 1486 | } |
| 1487 | |
| 1488 | // Use the CppHashFilename and calculate a line number based on the |
| 1489 | // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for |
| 1490 | // the diagnostic. |
| 1491 | const std::string Filename = Parser->CppHashFilename; |
| 1492 | |
| 1493 | int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); |
| 1494 | int CppHashLocLineNo = |
| 1495 | Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); |
| 1496 | int LineNo = Parser->CppHashLineNumber - 1 + |
| 1497 | (DiagLocLineNo - CppHashLocLineNo); |
| 1498 | |
Chris Lattner | d8b7aa2 | 2011-10-16 04:47:35 +0000 | [diff] [blame] | 1499 | SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), |
| 1500 | Filename, LineNo, Diag.getColumnNo(), |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 1501 | Diag.getKind(), Diag.getMessage(), |
Chris Lattner | 462b43c | 2011-10-16 05:47:55 +0000 | [diff] [blame] | 1502 | Diag.getLineContents(), Diag.getRanges()); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1503 | |
Benjamin Kramer | 04a0426 | 2011-10-16 10:48:29 +0000 | [diff] [blame] | 1504 | if (Parser->SavedDiagHandler) |
| 1505 | Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); |
| 1506 | else |
| 1507 | NewDiag.print(0, OS); |
Kevin Enderby | acbaecd | 2011-10-12 21:38:39 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Rafael Espindola | 799aacf | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1510 | // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The |
| 1511 | // difference being that that function accepts '@' as part of identifiers and |
| 1512 | // we can't do that. AsmLexer.cpp should probably be changed to handle |
| 1513 | // '@' as a special case when needed. |
| 1514 | static bool isIdentifierChar(char c) { |
| 1515 | return isalnum(c) || c == '_' || c == '$' || c == '.'; |
| 1516 | } |
| 1517 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1518 | bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1519 | const MacroParameters &Parameters, |
| 1520 | const MacroArguments &A, |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1521 | const SMLoc &L) { |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1522 | unsigned NParameters = Parameters.size(); |
| 1523 | if (NParameters != 0 && NParameters != A.size()) |
| 1524 | return Error(L, "Wrong number of arguments"); |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1525 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1526 | // A macro without parameters is handled differently on Darwin: |
| 1527 | // gas accepts no arguments and does no substitutions |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1528 | while (!Body.empty()) { |
| 1529 | // Scan for the next substitution. |
| 1530 | std::size_t End = Body.size(), Pos = 0; |
| 1531 | for (; Pos != End; ++Pos) { |
| 1532 | // Check for a substitution or escape. |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1533 | if (!NParameters) { |
| 1534 | // This macro has no parameters, look for $0, $1, etc. |
| 1535 | if (Body[Pos] != '$' || Pos + 1 == End) |
| 1536 | continue; |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1537 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1538 | char Next = Body[Pos + 1]; |
| 1539 | if (Next == '$' || Next == 'n' || isdigit(Next)) |
| 1540 | break; |
| 1541 | } else { |
| 1542 | // This macro has parameters, look for \foo, \bar, etc. |
| 1543 | if (Body[Pos] == '\\' && Pos + 1 != End) |
| 1544 | break; |
| 1545 | } |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | // Add the prefix. |
| 1549 | OS << Body.slice(0, Pos); |
| 1550 | |
| 1551 | // Check if we reached the end. |
| 1552 | if (Pos == End) |
| 1553 | break; |
| 1554 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1555 | if (!NParameters) { |
| 1556 | switch (Body[Pos+1]) { |
| 1557 | // $$ => $ |
| 1558 | case '$': |
| 1559 | OS << '$'; |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1560 | break; |
| 1561 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1562 | // $n => number of arguments |
| 1563 | case 'n': |
| 1564 | OS << A.size(); |
| 1565 | break; |
| 1566 | |
| 1567 | // $[0-9] => argument |
| 1568 | default: { |
| 1569 | // Missing arguments are ignored. |
| 1570 | unsigned Index = Body[Pos+1] - '0'; |
| 1571 | if (Index >= A.size()) |
| 1572 | break; |
| 1573 | |
| 1574 | // Otherwise substitute with the token values, with spaces eliminated. |
Rafael Espindola | 28c1f666 | 2012-06-03 22:41:23 +0000 | [diff] [blame] | 1575 | for (MacroArgument::const_iterator it = A[Index].begin(), |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1576 | ie = A[Index].end(); it != ie; ++it) |
| 1577 | OS << it->getString(); |
| 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | Pos += 2; |
| 1582 | } else { |
| 1583 | unsigned I = Pos + 1; |
Rafael Espindola | 799aacf | 2012-08-21 18:29:30 +0000 | [diff] [blame] | 1584 | while (isIdentifierChar(Body[I]) && I + 1 != End) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1585 | ++I; |
| 1586 | |
| 1587 | const char *Begin = Body.data() + Pos +1; |
| 1588 | StringRef Argument(Begin, I - (Pos +1)); |
| 1589 | unsigned Index = 0; |
| 1590 | for (; Index < NParameters; ++Index) |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1591 | if (Parameters[Index].first == Argument) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1592 | break; |
| 1593 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1594 | if (Index == NParameters) { |
| 1595 | if (Body[Pos+1] == '(' && Body[Pos+2] == ')') |
| 1596 | Pos += 3; |
| 1597 | else { |
| 1598 | OS << '\\' << Argument; |
| 1599 | Pos = I; |
| 1600 | } |
| 1601 | } else { |
| 1602 | for (MacroArgument::const_iterator it = A[Index].begin(), |
| 1603 | ie = A[Index].end(); it != ie; ++it) |
| 1604 | if (it->getKind() == AsmToken::String) |
| 1605 | OS << it->getStringContents(); |
| 1606 | else |
| 1607 | OS << it->getString(); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1608 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1609 | Pos += 1 + Argument.size(); |
| 1610 | } |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1611 | } |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1612 | // Update the scan point. |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1613 | Body = Body.substr(Pos); |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1614 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1615 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1618 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1619 | MacroInstantiation::MacroInstantiation(const Macro *M, SMLoc IL, SMLoc EL, |
| 1620 | MemoryBuffer *I) |
| 1621 | : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitLoc(EL) |
| 1622 | { |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1625 | static bool IsOperator(AsmToken::TokenKind kind) |
| 1626 | { |
| 1627 | switch (kind) |
| 1628 | { |
| 1629 | default: |
| 1630 | return false; |
| 1631 | case AsmToken::Plus: |
| 1632 | case AsmToken::Minus: |
| 1633 | case AsmToken::Tilde: |
| 1634 | case AsmToken::Slash: |
| 1635 | case AsmToken::Star: |
| 1636 | case AsmToken::Dot: |
| 1637 | case AsmToken::Equal: |
| 1638 | case AsmToken::EqualEqual: |
| 1639 | case AsmToken::Pipe: |
| 1640 | case AsmToken::PipePipe: |
| 1641 | case AsmToken::Caret: |
| 1642 | case AsmToken::Amp: |
| 1643 | case AsmToken::AmpAmp: |
| 1644 | case AsmToken::Exclaim: |
| 1645 | case AsmToken::ExclaimEqual: |
| 1646 | case AsmToken::Percent: |
| 1647 | case AsmToken::Less: |
| 1648 | case AsmToken::LessEqual: |
| 1649 | case AsmToken::LessLess: |
| 1650 | case AsmToken::LessGreater: |
| 1651 | case AsmToken::Greater: |
| 1652 | case AsmToken::GreaterEqual: |
| 1653 | case AsmToken::GreaterGreater: |
| 1654 | return true; |
| 1655 | } |
| 1656 | } |
| 1657 | |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1658 | /// ParseMacroArgument - Extract AsmTokens for a macro argument. |
| 1659 | /// This is used for both default macro parameter values and the |
| 1660 | /// arguments in macro invocations |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1661 | bool AsmParser::ParseMacroArgument(MacroArgument &MA, |
| 1662 | AsmToken::TokenKind &ArgumentDelimiter) { |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1663 | unsigned ParenLevel = 0; |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1664 | unsigned AddTokens = 0; |
| 1665 | |
| 1666 | // gas accepts arguments separated by whitespace, except on Darwin |
| 1667 | if (!IsDarwin) |
| 1668 | Lexer.setSkipSpace(false); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1669 | |
| 1670 | for (;;) { |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1671 | if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) { |
| 1672 | Lexer.setSkipSpace(true); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1673 | return TokError("unexpected token in macro instantiation"); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) { |
| 1677 | // Spaces and commas cannot be mixed to delimit parameters |
| 1678 | if (ArgumentDelimiter == AsmToken::Eof) |
| 1679 | ArgumentDelimiter = AsmToken::Comma; |
| 1680 | else if (ArgumentDelimiter != AsmToken::Comma) { |
| 1681 | Lexer.setSkipSpace(true); |
| 1682 | return TokError("expected ' ' for macro argument separator"); |
| 1683 | } |
| 1684 | break; |
| 1685 | } |
| 1686 | |
| 1687 | if (Lexer.is(AsmToken::Space)) { |
| 1688 | Lex(); // Eat spaces |
| 1689 | |
| 1690 | // Spaces can delimit parameters, but could also be part an expression. |
| 1691 | // If the token after a space is an operator, add the token and the next |
| 1692 | // one into this argument |
| 1693 | if (ArgumentDelimiter == AsmToken::Space || |
| 1694 | ArgumentDelimiter == AsmToken::Eof) { |
| 1695 | if (IsOperator(Lexer.getKind())) { |
| 1696 | // Check to see whether the token is used as an operator, |
| 1697 | // or part of an identifier |
| 1698 | const char *NextChar = getTok().getEndLoc().getPointer() + 1; |
| 1699 | if (*NextChar == ' ') |
| 1700 | AddTokens = 2; |
| 1701 | } |
| 1702 | |
| 1703 | if (!AddTokens && ParenLevel == 0) { |
| 1704 | if (ArgumentDelimiter == AsmToken::Eof && |
| 1705 | !IsOperator(Lexer.getKind())) |
| 1706 | ArgumentDelimiter = AsmToken::Space; |
| 1707 | break; |
| 1708 | } |
| 1709 | } |
| 1710 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1711 | |
| 1712 | // HandleMacroEntry relies on not advancing the lexer here |
| 1713 | // to be able to fill in the remaining default parameter values |
| 1714 | if (Lexer.is(AsmToken::EndOfStatement)) |
| 1715 | break; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Adjust the current parentheses level. |
| 1718 | if (Lexer.is(AsmToken::LParen)) |
| 1719 | ++ParenLevel; |
| 1720 | else if (Lexer.is(AsmToken::RParen) && ParenLevel) |
| 1721 | --ParenLevel; |
| 1722 | |
| 1723 | // Append the token to the current argument list. |
| 1724 | MA.push_back(getTok()); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1725 | if (AddTokens) |
| 1726 | AddTokens--; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1727 | Lex(); |
| 1728 | } |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1729 | |
| 1730 | Lexer.setSkipSpace(true); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1731 | if (ParenLevel != 0) |
Rafael Espindola | 76ac200 | 2012-08-21 15:55:04 +0000 | [diff] [blame] | 1732 | return TokError("unbalanced parentheses in macro argument"); |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1733 | return false; |
| 1734 | } |
| 1735 | |
| 1736 | // Parse the macro instantiation arguments. |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1737 | bool AsmParser::ParseMacroArguments(const Macro *M, MacroArguments &A) { |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1738 | const unsigned NParameters = M ? M->Parameters.size() : 0; |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1739 | // Argument delimiter is initially unknown. It will be set by |
| 1740 | // ParseMacroArgument() |
| 1741 | AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1742 | |
| 1743 | // Parse two kinds of macro invocations: |
| 1744 | // - macros defined without any parameters accept an arbitrary number of them |
| 1745 | // - macros defined with parameters accept at most that many of them |
| 1746 | for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; |
| 1747 | ++Parameter) { |
| 1748 | MacroArgument MA; |
| 1749 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 1750 | if (ParseMacroArgument(MA, ArgumentDelimiter)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1751 | return true; |
| 1752 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1753 | if (!MA.empty() || !NParameters) |
| 1754 | A.push_back(MA); |
| 1755 | else if (NParameters) { |
| 1756 | if (!M->Parameters[Parameter].second.empty()) |
| 1757 | A.push_back(M->Parameters[Parameter].second); |
| 1758 | } |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1759 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1760 | // At the end of the statement, fill in remaining arguments that have |
| 1761 | // default values. If there aren't any, then the next argument is |
| 1762 | // required but missing |
| 1763 | if (Lexer.is(AsmToken::EndOfStatement)) { |
| 1764 | if (NParameters && Parameter < NParameters - 1) { |
| 1765 | if (M->Parameters[Parameter + 1].second.empty()) |
| 1766 | return TokError("macro argument '" + |
| 1767 | Twine(M->Parameters[Parameter + 1].first) + |
| 1768 | "' is missing"); |
| 1769 | else |
| 1770 | continue; |
| 1771 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1772 | return false; |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 1773 | } |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1774 | |
| 1775 | if (Lexer.is(AsmToken::Comma)) |
| 1776 | Lex(); |
| 1777 | } |
| 1778 | return TokError("Too many arguments"); |
| 1779 | } |
| 1780 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1781 | bool AsmParser::HandleMacroEntry(StringRef Name, SMLoc NameLoc, |
| 1782 | const Macro *M) { |
| 1783 | // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate |
| 1784 | // this, although we should protect against infinite loops. |
| 1785 | if (ActiveMacros.size() == 20) |
| 1786 | return TokError("macros cannot be nested more than 20 levels deep"); |
| 1787 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1788 | MacroArguments A; |
| 1789 | if (ParseMacroArguments(M, A)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 1790 | return true; |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1791 | |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1792 | // Remove any trailing empty arguments. Do this after-the-fact as we have |
| 1793 | // to keep empty arguments in the middle of the list or positionality |
| 1794 | // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2," |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1795 | while (!A.empty() && A.back().empty()) |
| 1796 | A.pop_back(); |
Jim Grosbach | 9714644 | 2012-07-30 22:44:17 +0000 | [diff] [blame] | 1797 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1798 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 1799 | // to hold the macro body with substitutions. |
| 1800 | SmallString<256> Buf; |
| 1801 | StringRef Body = M->Body; |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1802 | raw_svector_ostream OS(Buf); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1803 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 1804 | if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc())) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1805 | return true; |
| 1806 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1807 | // We include the .endmacro in the buffer as our queue to exit the macro |
| 1808 | // instantiation. |
| 1809 | OS << ".endmacro\n"; |
| 1810 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1811 | MemoryBuffer *Instantiation = |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 1812 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1813 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1814 | // Create the macro instantiation object and add to the current macro |
| 1815 | // instantiation stack. |
| 1816 | MacroInstantiation *MI = new MacroInstantiation(M, NameLoc, |
Daniel Dunbar | 7a570d0 | 2010-07-18 19:00:10 +0000 | [diff] [blame] | 1817 | getTok().getLoc(), |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 1818 | Instantiation); |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 1819 | ActiveMacros.push_back(MI); |
| 1820 | |
| 1821 | // Jump to the macro instantiation and prime the lexer. |
| 1822 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 1823 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 1824 | Lex(); |
| 1825 | |
| 1826 | return false; |
| 1827 | } |
| 1828 | |
| 1829 | void AsmParser::HandleMacroExit() { |
| 1830 | // Jump to the EndOfStatement we should return to, and consume it. |
| 1831 | JumpToLoc(ActiveMacros.back()->ExitLoc); |
| 1832 | Lex(); |
| 1833 | |
| 1834 | // Pop the instantiation entry. |
| 1835 | delete ActiveMacros.back(); |
| 1836 | ActiveMacros.pop_back(); |
| 1837 | } |
| 1838 | |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1839 | static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) { |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1840 | switch (Value->getKind()) { |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1841 | case MCExpr::Binary: { |
| 1842 | const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value); |
| 1843 | return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS()); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | } |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1846 | case MCExpr::Target: |
| 1847 | case MCExpr::Constant: |
| 1848 | return false; |
| 1849 | case MCExpr::SymbolRef: { |
| 1850 | const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol(); |
Rafael Espindola | 8b01c82 | 2012-01-28 06:22:14 +0000 | [diff] [blame] | 1851 | if (S.isVariable()) |
| 1852 | return IsUsedIn(Sym, S.getVariableValue()); |
| 1853 | return &S == Sym; |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1854 | } |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1855 | case MCExpr::Unary: |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1856 | return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr()); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1857 | } |
Benjamin Kramer | 518ff56 | 2012-01-28 15:28:41 +0000 | [diff] [blame] | 1858 | |
| 1859 | llvm_unreachable("Unknown expr kind!"); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 1862 | bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef, |
| 1863 | bool NoDeadStrip) { |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1864 | // FIXME: Use better location, we should use proper tokens. |
| 1865 | SMLoc EqualLoc = Lexer.getLoc(); |
| 1866 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1867 | const MCExpr *Value; |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 1868 | if (ParseExpression(Value)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1869 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1870 | |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1871 | // Note: we don't count b as used in "a = b". This is to allow |
| 1872 | // a = b |
| 1873 | // b = c |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1874 | |
Daniel Dunbar | 3f87233 | 2009-07-28 16:08:33 +0000 | [diff] [blame] | 1875 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1876 | return TokError("unexpected token in assignment"); |
| 1877 | |
Daniel Dunbar | 8b2b43c | 2011-03-25 17:47:17 +0000 | [diff] [blame] | 1878 | // Error on assignment to '.'. |
| 1879 | if (Name == ".") { |
| 1880 | return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported " |
| 1881 | "(use '.space' or '.org').)")); |
| 1882 | } |
| 1883 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1884 | // Eat the end of statement marker. |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1885 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1886 | |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1887 | // Validate that the LHS is allowed to be a variable (either it has not been |
| 1888 | // used as a symbol, or it is an absolute symbol). |
| 1889 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 1890 | if (Sym) { |
| 1891 | // Diagnose assignment to a label. |
| 1892 | // |
| 1893 | // FIXME: Diagnostics. Note the location of the definition as a label. |
| 1894 | // FIXME: Diagnose assignment to protected identifier (e.g., register name). |
Rafael Espindola | e71cc86 | 2012-01-28 05:57:00 +0000 | [diff] [blame] | 1895 | if (IsUsedIn(Sym, Value)) |
| 1896 | return Error(EqualLoc, "Recursive use of '" + Name + "'"); |
| 1897 | else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) |
Daniel Dunbar | 525a3a6 | 2010-05-17 17:46:23 +0000 | [diff] [blame] | 1898 | ; // Allow redefinitions of undefined symbols only used in directives. |
Jim Grosbach | 48c9533 | 2012-03-20 21:33:21 +0000 | [diff] [blame] | 1899 | else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) |
| 1900 | ; // Allow redefinitions of variables that haven't yet been used. |
Daniel Dunbar | 6db7fe8 | 2011-04-29 17:53:11 +0000 | [diff] [blame] | 1901 | else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1902 | return Error(EqualLoc, "redefinition of '" + Name + "'"); |
| 1903 | else if (!Sym->isVariable()) |
| 1904 | return Error(EqualLoc, "invalid assignment to '" + Name + "'"); |
Daniel Dunbar | 08a408a | 2010-05-05 17:41:00 +0000 | [diff] [blame] | 1905 | else if (!isa<MCConstantExpr>(Sym->getVariableValue())) |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1906 | return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + |
| 1907 | Name + "'"); |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 1908 | |
| 1909 | // Don't count these checks as uses. |
| 1910 | Sym->setUsed(false); |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1911 | } else |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 1912 | Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | 75773ff | 2009-10-16 01:57:39 +0000 | [diff] [blame] | 1913 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1914 | // FIXME: Handle '.'. |
Daniel Dunbar | dce0f3c | 2009-06-29 23:43:14 +0000 | [diff] [blame] | 1915 | |
| 1916 | // Do the assignment. |
Daniel Dunbar | e2ace50 | 2009-08-31 08:09:09 +0000 | [diff] [blame] | 1917 | Out.EmitAssignment(Sym, Value); |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 1918 | if (NoDeadStrip) |
| 1919 | Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); |
| 1920 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1921 | |
| 1922 | return false; |
| 1923 | } |
| 1924 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1925 | /// ParseIdentifier: |
| 1926 | /// ::= identifier |
| 1927 | /// ::= string |
| 1928 | bool AsmParser::ParseIdentifier(StringRef &Res) { |
Daniel Dunbar | 1f1b865 | 2010-08-24 18:12:12 +0000 | [diff] [blame] | 1929 | // The assembler has relaxed rules for accepting identifiers, in particular we |
| 1930 | // allow things like '.globl $foo', which would normally be separate |
| 1931 | // tokens. At this level, we have already lexed so we cannot (currently) |
| 1932 | // handle this as a context dependent token, instead we detect adjacent tokens |
| 1933 | // and return the combined identifier. |
| 1934 | if (Lexer.is(AsmToken::Dollar)) { |
| 1935 | SMLoc DollarLoc = getLexer().getLoc(); |
| 1936 | |
| 1937 | // Consume the dollar sign, and check for a following identifier. |
| 1938 | Lex(); |
| 1939 | if (Lexer.isNot(AsmToken::Identifier)) |
| 1940 | return true; |
| 1941 | |
| 1942 | // We have a '$' followed by an identifier, make sure they are adjacent. |
| 1943 | if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer()) |
| 1944 | return true; |
| 1945 | |
| 1946 | // Construct the joined identifier and consume the token. |
| 1947 | Res = StringRef(DollarLoc.getPointer(), |
| 1948 | getTok().getIdentifier().size() + 1); |
| 1949 | Lex(); |
| 1950 | return false; |
| 1951 | } |
| 1952 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1953 | if (Lexer.isNot(AsmToken::Identifier) && |
| 1954 | Lexer.isNot(AsmToken::String)) |
| 1955 | return true; |
| 1956 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1957 | Res = getTok().getIdentifier(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1958 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1959 | Lex(); // Consume the identifier token. |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1960 | |
| 1961 | return false; |
| 1962 | } |
| 1963 | |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1964 | /// ParseDirectiveSet: |
Nico Weber | 4c4c732 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 1965 | /// ::= .equ identifier ',' expression |
| 1966 | /// ::= .equiv identifier ',' expression |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1967 | /// ::= .set identifier ',' expression |
Nico Weber | 4c4c732 | 2011-01-28 03:04:41 +0000 | [diff] [blame] | 1968 | bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1969 | StringRef Name; |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1970 | |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 1971 | if (ParseIdentifier(Name)) |
Roman Divacky | f9d1752 | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 1972 | return TokError("expected identifier after '" + Twine(IDVal) + "'"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 1973 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1974 | if (getLexer().isNot(AsmToken::Comma)) |
Roman Divacky | f9d1752 | 2010-10-28 16:57:58 +0000 | [diff] [blame] | 1975 | return TokError("unexpected token in '" + Twine(IDVal) + "'"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 1976 | Lex(); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1977 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 1978 | return ParseAssignment(Name, allow_redef, true); |
Daniel Dunbar | 8f780cd | 2009-06-25 21:56:11 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1981 | bool AsmParser::ParseEscapedString(std::string &Data) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 1982 | assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1983 | |
| 1984 | Data = ""; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1985 | StringRef Str = getTok().getStringContents(); |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 1986 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 1987 | if (Str[i] != '\\') { |
| 1988 | Data += Str[i]; |
| 1989 | continue; |
| 1990 | } |
| 1991 | |
| 1992 | // Recognize escaped characters. Note that this escape semantics currently |
| 1993 | // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. |
| 1994 | ++i; |
| 1995 | if (i == e) |
| 1996 | return TokError("unexpected backslash at end of string"); |
| 1997 | |
| 1998 | // Recognize octal sequences. |
| 1999 | if ((unsigned) (Str[i] - '0') <= 7) { |
| 2000 | // Consume up to three octal characters. |
| 2001 | unsigned Value = Str[i] - '0'; |
| 2002 | |
| 2003 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 2004 | ++i; |
| 2005 | Value = Value * 8 + (Str[i] - '0'); |
| 2006 | |
| 2007 | if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { |
| 2008 | ++i; |
| 2009 | Value = Value * 8 + (Str[i] - '0'); |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | if (Value > 255) |
| 2014 | return TokError("invalid octal escape sequence (out of range)"); |
| 2015 | |
| 2016 | Data += (unsigned char) Value; |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
| 2020 | // Otherwise recognize individual escapes. |
| 2021 | switch (Str[i]) { |
| 2022 | default: |
| 2023 | // Just reject invalid escape sequences for now. |
| 2024 | return TokError("invalid escape sequence (unrecognized character)"); |
| 2025 | |
| 2026 | case 'b': Data += '\b'; break; |
| 2027 | case 'f': Data += '\f'; break; |
| 2028 | case 'n': Data += '\n'; break; |
| 2029 | case 'r': Data += '\r'; break; |
| 2030 | case 't': Data += '\t'; break; |
| 2031 | case '"': Data += '"'; break; |
| 2032 | case '\\': Data += '\\'; break; |
| 2033 | } |
| 2034 | } |
| 2035 | |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2039 | /// ParseDirectiveAscii: |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2040 | /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] |
| 2041 | bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2042 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2043 | CheckForValidSection(); |
| 2044 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2045 | for (;;) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2046 | if (getLexer().isNot(AsmToken::String)) |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2047 | return TokError("expected string in '" + Twine(IDVal) + "' directive"); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2048 | |
Daniel Dunbar | 1ab7594 | 2009-08-14 18:19:52 +0000 | [diff] [blame] | 2049 | std::string Data; |
| 2050 | if (ParseEscapedString(Data)) |
| 2051 | return true; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2052 | |
| 2053 | getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2054 | if (ZeroTerminated) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2055 | getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); |
| 2056 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2057 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2058 | |
| 2059 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2060 | break; |
| 2061 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2062 | if (getLexer().isNot(AsmToken::Comma)) |
Rafael Espindola | 787c337 | 2010-10-28 20:02:27 +0000 | [diff] [blame] | 2063 | return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2064 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2068 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2069 | return false; |
| 2070 | } |
| 2071 | |
| 2072 | /// ParseDirectiveValue |
| 2073 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
| 2074 | bool AsmParser::ParseDirectiveValue(unsigned Size) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2075 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2076 | CheckForValidSection(); |
| 2077 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2078 | for (;;) { |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2079 | const MCExpr *Value; |
Jim Grosbach | 254cf03 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2080 | SMLoc ExprLoc = getLexer().getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2081 | if (ParseExpression(Value)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2082 | return true; |
| 2083 | |
Daniel Dunbar | 414c0c4 | 2010-05-23 18:36:38 +0000 | [diff] [blame] | 2084 | // Special case constant expressions to match code generator. |
Jim Grosbach | 254cf03 | 2011-06-29 16:05:14 +0000 | [diff] [blame] | 2085 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2086 | assert(Size <= 8 && "Invalid size"); |
| 2087 | uint64_t IntValue = MCE->getValue(); |
| 2088 | if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) |
| 2089 | return Error(ExprLoc, "literal value out of range for directive"); |
| 2090 | getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE); |
| 2091 | } else |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2092 | getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2093 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2094 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2095 | break; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2096 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2097 | // FIXME: Improve diagnostic. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2098 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2099 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2100 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2104 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2105 | return false; |
| 2106 | } |
| 2107 | |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2108 | /// ParseDirectiveRealValue |
| 2109 | /// ::= (.single | .double) [ expression (, expression)* ] |
| 2110 | bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) { |
| 2111 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2112 | CheckForValidSection(); |
| 2113 | |
| 2114 | for (;;) { |
| 2115 | // We don't truly support arithmetic on floating point expressions, so we |
| 2116 | // have to manually parse unary prefixes. |
| 2117 | bool IsNeg = false; |
| 2118 | if (getLexer().is(AsmToken::Minus)) { |
| 2119 | Lex(); |
| 2120 | IsNeg = true; |
| 2121 | } else if (getLexer().is(AsmToken::Plus)) |
| 2122 | Lex(); |
| 2123 | |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2124 | if (getLexer().isNot(AsmToken::Integer) && |
Kevin Enderby | 360d8d7 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2125 | getLexer().isNot(AsmToken::Real) && |
| 2126 | getLexer().isNot(AsmToken::Identifier)) |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2127 | return TokError("unexpected token in directive"); |
| 2128 | |
| 2129 | // Convert to an APFloat. |
| 2130 | APFloat Value(Semantics); |
Kevin Enderby | 360d8d7 | 2011-03-29 21:11:52 +0000 | [diff] [blame] | 2131 | StringRef IDVal = getTok().getString(); |
| 2132 | if (getLexer().is(AsmToken::Identifier)) { |
| 2133 | if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) |
| 2134 | Value = APFloat::getInf(Semantics); |
| 2135 | else if (!IDVal.compare_lower("nan")) |
| 2136 | Value = APFloat::getNaN(Semantics, false, ~0); |
| 2137 | else |
| 2138 | return TokError("invalid floating point literal"); |
| 2139 | } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2140 | APFloat::opInvalidOp) |
| 2141 | return TokError("invalid floating point literal"); |
| 2142 | if (IsNeg) |
| 2143 | Value.changeSign(); |
| 2144 | |
| 2145 | // Consume the numeric token. |
| 2146 | Lex(); |
| 2147 | |
| 2148 | // Emit the value as an integer. |
| 2149 | APInt AsInt = Value.bitcastToAPInt(); |
| 2150 | getStreamer().EmitIntValue(AsInt.getLimitedValue(), |
| 2151 | AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE); |
| 2152 | |
| 2153 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2154 | break; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2155 | |
Daniel Dunbar | b95a079 | 2010-09-24 01:59:56 +0000 | [diff] [blame] | 2156 | if (getLexer().isNot(AsmToken::Comma)) |
| 2157 | return TokError("unexpected token in directive"); |
| 2158 | Lex(); |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | Lex(); |
| 2163 | return false; |
| 2164 | } |
| 2165 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2166 | /// ParseDirectiveSpace |
| 2167 | /// ::= .space expression [ , expression ] |
| 2168 | bool AsmParser::ParseDirectiveSpace() { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2169 | CheckForValidSection(); |
| 2170 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2171 | int64_t NumBytes; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2172 | if (ParseAbsoluteExpression(NumBytes)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2173 | return true; |
| 2174 | |
| 2175 | int64_t FillExpr = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2176 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2177 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2178 | return TokError("unexpected token in '.space' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2179 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2180 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2181 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2182 | return true; |
| 2183 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2184 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2185 | return TokError("unexpected token in '.space' directive"); |
| 2186 | } |
| 2187 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2188 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2189 | |
| 2190 | if (NumBytes <= 0) |
| 2191 | return TokError("invalid number of bytes in '.space' directive"); |
| 2192 | |
| 2193 | // 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] | 2194 | getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2195 | |
| 2196 | return false; |
| 2197 | } |
| 2198 | |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2199 | /// ParseDirectiveZero |
| 2200 | /// ::= .zero expression |
| 2201 | bool AsmParser::ParseDirectiveZero() { |
| 2202 | CheckForValidSection(); |
| 2203 | |
| 2204 | int64_t NumBytes; |
| 2205 | if (ParseAbsoluteExpression(NumBytes)) |
| 2206 | return true; |
| 2207 | |
Rafael Espindola | e452b17 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2208 | int64_t Val = 0; |
| 2209 | if (getLexer().is(AsmToken::Comma)) { |
| 2210 | Lex(); |
| 2211 | if (ParseAbsoluteExpression(Val)) |
| 2212 | return true; |
| 2213 | } |
| 2214 | |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2215 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2216 | return TokError("unexpected token in '.zero' directive"); |
| 2217 | |
| 2218 | Lex(); |
| 2219 | |
Rafael Espindola | e452b17 | 2010-10-05 19:42:57 +0000 | [diff] [blame] | 2220 | getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE); |
Rafael Espindola | 2ea2ac7 | 2010-09-16 15:03:59 +0000 | [diff] [blame] | 2221 | |
| 2222 | return false; |
| 2223 | } |
| 2224 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2225 | /// ParseDirectiveFill |
| 2226 | /// ::= .fill expression , expression , expression |
| 2227 | bool AsmParser::ParseDirectiveFill() { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2228 | CheckForValidSection(); |
| 2229 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2230 | int64_t NumValues; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2231 | if (ParseAbsoluteExpression(NumValues)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2232 | return true; |
| 2233 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2234 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2235 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2236 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2237 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2238 | int64_t FillSize; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2239 | if (ParseAbsoluteExpression(FillSize)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2240 | return true; |
| 2241 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2242 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2243 | return TokError("unexpected token in '.fill' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2244 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2245 | |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2246 | int64_t FillExpr; |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2247 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2248 | return true; |
| 2249 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2250 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2251 | return TokError("unexpected token in '.fill' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2252 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2253 | Lex(); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2254 | |
Daniel Dunbar | bc38ca7 | 2009-08-21 15:43:35 +0000 | [diff] [blame] | 2255 | if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) |
| 2256 | return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2257 | |
| 2258 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2259 | getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE); |
Daniel Dunbar | a0d1426 | 2009-06-24 23:30:00 +0000 | [diff] [blame] | 2260 | |
| 2261 | return false; |
| 2262 | } |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2263 | |
| 2264 | /// ParseDirectiveOrg |
| 2265 | /// ::= .org expression [ , expression ] |
| 2266 | bool AsmParser::ParseDirectiveOrg() { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2267 | CheckForValidSection(); |
| 2268 | |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2269 | const MCExpr *Offset; |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2270 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | 821e333 | 2009-08-31 08:09:28 +0000 | [diff] [blame] | 2271 | if (ParseExpression(Offset)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2272 | return true; |
| 2273 | |
| 2274 | // Parse optional fill expression. |
| 2275 | int64_t FillExpr = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2276 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2277 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2278 | return TokError("unexpected token in '.org' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2279 | Lex(); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2280 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 2281 | if (ParseAbsoluteExpression(FillExpr)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2282 | return true; |
| 2283 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2284 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2285 | return TokError("unexpected token in '.org' directive"); |
| 2286 | } |
| 2287 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2288 | Lex(); |
Daniel Dunbar | f4b830f | 2009-06-30 02:10:03 +0000 | [diff] [blame] | 2289 | |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 2290 | // Only limited forms of relocatable expressions are accepted here, it |
| 2291 | // has to be relative to the current section. The streamer will return |
| 2292 | // 'true' if the expression wasn't evaluatable. |
| 2293 | if (getStreamer().EmitValueToOffset(Offset, FillExpr)) |
| 2294 | return Error(Loc, "expected assembly-time absolute expression"); |
Daniel Dunbar | c238b58 | 2009-06-25 22:44:51 +0000 | [diff] [blame] | 2295 | |
| 2296 | return false; |
| 2297 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2298 | |
| 2299 | /// ParseDirectiveAlign |
| 2300 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
| 2301 | bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2302 | CheckForValidSection(); |
| 2303 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2304 | SMLoc AlignmentLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2305 | int64_t Alignment; |
| 2306 | if (ParseAbsoluteExpression(Alignment)) |
| 2307 | return true; |
| 2308 | |
| 2309 | SMLoc MaxBytesLoc; |
| 2310 | bool HasFillExpr = false; |
| 2311 | int64_t FillExpr = 0; |
| 2312 | int64_t MaxBytesToFill = 0; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2313 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2314 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2315 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2316 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2317 | |
| 2318 | // The fill expression can be omitted while specifying a maximum number of |
| 2319 | // alignment bytes, e.g: |
| 2320 | // .align 3,,4 |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2321 | if (getLexer().isNot(AsmToken::Comma)) { |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2322 | HasFillExpr = true; |
| 2323 | if (ParseAbsoluteExpression(FillExpr)) |
| 2324 | return true; |
| 2325 | } |
| 2326 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2327 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2328 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2329 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2330 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2331 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2332 | MaxBytesLoc = getLexer().getLoc(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2333 | if (ParseAbsoluteExpression(MaxBytesToFill)) |
| 2334 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2335 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2336 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2337 | return TokError("unexpected token in directive"); |
| 2338 | } |
| 2339 | } |
| 2340 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2341 | Lex(); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2342 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2343 | if (!HasFillExpr) |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2344 | FillExpr = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2345 | |
| 2346 | // Compute alignment in bytes. |
| 2347 | if (IsPow2) { |
| 2348 | // FIXME: Diagnose overflow. |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2349 | if (Alignment >= 32) { |
| 2350 | Error(AlignmentLoc, "invalid alignment value"); |
| 2351 | Alignment = 31; |
| 2352 | } |
| 2353 | |
Benjamin Kramer | 12fd767 | 2009-09-06 09:35:10 +0000 | [diff] [blame] | 2354 | Alignment = 1ULL << Alignment; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2357 | // Diagnose non-sensical max bytes to align. |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2358 | if (MaxBytesLoc.isValid()) { |
| 2359 | if (MaxBytesToFill < 1) { |
Daniel Dunbar | b58a804 | 2009-08-26 09:16:34 +0000 | [diff] [blame] | 2360 | Error(MaxBytesLoc, "alignment directive can never be satisfied in this " |
| 2361 | "many bytes, ignoring maximum bytes expression"); |
Daniel Dunbar | 0afb9f5 | 2009-08-21 23:01:53 +0000 | [diff] [blame] | 2362 | MaxBytesToFill = 0; |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | if (MaxBytesToFill >= Alignment) { |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 2366 | Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " |
| 2367 | "has no effect"); |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2368 | MaxBytesToFill = 0; |
| 2369 | } |
| 2370 | } |
| 2371 | |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2372 | // Check whether we should use optimal code alignment for this .align |
| 2373 | // directive. |
Jan Wen Voung | 083cf15 | 2010-10-04 17:32:41 +0000 | [diff] [blame] | 2374 | bool UseCodeAlign = getStreamer().getCurrentSection()->UseCodeAlign(); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2375 | if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && |
| 2376 | ValueSize == 1 && UseCodeAlign) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2377 | getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2378 | } else { |
Kevin Enderby | d74acb0 | 2010-02-25 18:46:04 +0000 | [diff] [blame] | 2379 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
Chris Lattner | a955853 | 2010-07-15 21:19:31 +0000 | [diff] [blame] | 2380 | getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, |
| 2381 | MaxBytesToFill); |
Daniel Dunbar | 648ac51 | 2010-05-17 21:54:30 +0000 | [diff] [blame] | 2382 | } |
Daniel Dunbar | c29dfa7 | 2009-06-29 23:46:59 +0000 | [diff] [blame] | 2383 | |
| 2384 | return false; |
| 2385 | } |
| 2386 | |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2387 | /// ParseDirectiveSymbolAttribute |
| 2388 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
Chris Lattner | a5ad93a | 2010-01-23 06:39:22 +0000 | [diff] [blame] | 2389 | bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2390 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2391 | for (;;) { |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2392 | StringRef Name; |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 2393 | SMLoc Loc = getTok().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2394 | |
| 2395 | if (ParseIdentifier(Name)) |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 2396 | return Error(Loc, "expected identifier in directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2397 | |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 2398 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2399 | |
Jim Grosbach | 10ec650 | 2011-09-15 17:56:49 +0000 | [diff] [blame] | 2400 | // Assembler local symbols don't make any sense here. Complain loudly. |
| 2401 | if (Sym->isTemporary()) |
| 2402 | return Error(Loc, "non-local symbol required in directive"); |
| 2403 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2404 | getStreamer().EmitSymbolAttribute(Sym, Attr); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2405 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2406 | if (getLexer().is(AsmToken::EndOfStatement)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2407 | break; |
| 2408 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2409 | if (getLexer().isNot(AsmToken::Comma)) |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2410 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2411 | Lex(); |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2415 | Lex(); |
Jan Wen Voung | a854a4b | 2010-09-30 01:09:20 +0000 | [diff] [blame] | 2416 | return false; |
Daniel Dunbar | d7b267b | 2009-06-30 00:33:19 +0000 | [diff] [blame] | 2417 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2418 | |
| 2419 | /// ParseDirectiveComm |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2420 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
| 2421 | bool AsmParser::ParseDirectiveComm(bool IsLocal) { |
Daniel Dunbar | 1ab6f2f | 2010-09-09 22:42:59 +0000 | [diff] [blame] | 2422 | CheckForValidSection(); |
| 2423 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2424 | SMLoc IDLoc = getLexer().getLoc(); |
Daniel Dunbar | a6b3c5d | 2009-08-01 00:48:30 +0000 | [diff] [blame] | 2425 | StringRef Name; |
| 2426 | if (ParseIdentifier(Name)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2427 | return TokError("expected identifier in directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2428 | |
Daniel Dunbar | 76c4d76 | 2009-07-31 21:55:09 +0000 | [diff] [blame] | 2429 | // Handle the identifier as the key symbol. |
Daniel Dunbar | 4c7c08b | 2010-07-12 19:52:10 +0000 | [diff] [blame] | 2430 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2431 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2432 | if (getLexer().isNot(AsmToken::Comma)) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2433 | return TokError("unexpected token in directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2434 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2435 | |
| 2436 | int64_t Size; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2437 | SMLoc SizeLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2438 | if (ParseAbsoluteExpression(Size)) |
| 2439 | return true; |
| 2440 | |
| 2441 | int64_t Pow2Alignment = 0; |
| 2442 | SMLoc Pow2AlignmentLoc; |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2443 | if (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2444 | Lex(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2445 | Pow2AlignmentLoc = getLexer().getLoc(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2446 | if (ParseAbsoluteExpression(Pow2Alignment)) |
| 2447 | return true; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2448 | |
Benjamin Kramer | a9e37c5 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 2449 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
| 2450 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
Benjamin Kramer | 39646d9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 2451 | return Error(Pow2AlignmentLoc, "alignment not supported on this target"); |
| 2452 | |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 2453 | // If this target takes alignments in bytes (not log) validate and convert. |
Benjamin Kramer | a9e37c5 | 2012-09-07 21:08:01 +0000 | [diff] [blame] | 2454 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
| 2455 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
Chris Lattner | 258281d | 2010-01-19 06:22:22 +0000 | [diff] [blame] | 2456 | if (!isPowerOf2_64(Pow2Alignment)) |
| 2457 | return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); |
| 2458 | Pow2Alignment = Log2_64(Pow2Alignment); |
| 2459 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2460 | } |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2461 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2462 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2463 | return TokError("unexpected token in '.comm' or '.lcomm' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2464 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2465 | Lex(); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2466 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2467 | // NOTE: a size of zero for a .comm should create a undefined symbol |
| 2468 | // but a size of .lcomm creates a bss symbol of size zero. |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2469 | if (Size < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2470 | return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " |
| 2471 | "be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2472 | |
Eric Christopher | c260a3e | 2010-05-14 01:38:54 +0000 | [diff] [blame] | 2473 | // 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] | 2474 | // may internally end up wanting an alignment in bytes. |
| 2475 | // FIXME: Diagnose overflow. |
| 2476 | if (Pow2Alignment < 0) |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2477 | return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " |
| 2478 | "alignment, can't be less than zero"); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2479 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 2480 | if (!Sym->isUndefined()) |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2481 | return Error(IDLoc, "invalid symbol redefinition"); |
| 2482 | |
Chris Lattner | 1fc3d75 | 2009-07-09 17:25:12 +0000 | [diff] [blame] | 2483 | // 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] | 2484 | if (IsLocal) { |
Benjamin Kramer | 39646d9 | 2012-09-07 17:25:13 +0000 | [diff] [blame] | 2485 | getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Daniel Dunbar | 7092c7e | 2009-08-30 06:17:16 +0000 | [diff] [blame] | 2486 | return false; |
| 2487 | } |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2488 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2489 | getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); |
Chris Lattner | 4e4db7a | 2009-07-07 20:30:46 +0000 | [diff] [blame] | 2490 | return false; |
| 2491 | } |
Chris Lattner | 9be3fee | 2009-07-10 22:20:30 +0000 | [diff] [blame] | 2492 | |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 2493 | /// ParseDirectiveAbort |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 2494 | /// ::= .abort [... message ...] |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 2495 | bool AsmParser::ParseDirectiveAbort() { |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 2496 | // FIXME: Use loc from directive. |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2497 | SMLoc Loc = getLexer().getLoc(); |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 2498 | |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 2499 | StringRef Str = ParseStringToEndOfStatement(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2500 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 2501 | return TokError("unexpected token in '.abort' directive"); |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 2502 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2503 | Lex(); |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 2504 | |
Daniel Dunbar | f9507ff | 2009-07-27 23:20:52 +0000 | [diff] [blame] | 2505 | if (Str.empty()) |
| 2506 | Error(Loc, ".abort detected. Assembly stopping."); |
| 2507 | else |
| 2508 | Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); |
Daniel Dunbar | 6a46d57 | 2010-07-18 20:15:59 +0000 | [diff] [blame] | 2509 | // FIXME: Actually abort assembly here. |
Kevin Enderby | 5f1f0b8 | 2009-07-13 23:15:14 +0000 | [diff] [blame] | 2510 | |
| 2511 | return false; |
| 2512 | } |
Kevin Enderby | 7114824 | 2009-07-14 21:35:03 +0000 | [diff] [blame] | 2513 | |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 2514 | /// ParseDirectiveInclude |
| 2515 | /// ::= .include "filename" |
| 2516 | bool AsmParser::ParseDirectiveInclude() { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2517 | if (getLexer().isNot(AsmToken::String)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 2518 | return TokError("expected string in '.include' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2519 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2520 | std::string Filename = getTok().getString(); |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2521 | SMLoc IncludeLoc = getLexer().getLoc(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2522 | Lex(); |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 2523 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2524 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 2525 | return TokError("unexpected token in '.include' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2526 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 2527 | // Strip the quotes. |
| 2528 | Filename = Filename.substr(1, Filename.size()-2); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2529 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 2530 | // Attempt to switch the lexer to the included file before consuming the end |
| 2531 | // of statement to avoid losing it when we switch. |
Sean Callanan | fd0b028 | 2010-01-21 00:19:58 +0000 | [diff] [blame] | 2532 | if (EnterIncludeFile(Filename)) { |
Daniel Dunbar | 275ce39 | 2010-07-18 18:31:45 +0000 | [diff] [blame] | 2533 | Error(IncludeLoc, "Could not find include file '" + Filename + "'"); |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 2534 | return true; |
| 2535 | } |
Kevin Enderby | 1f049b2 | 2009-07-14 23:21:55 +0000 | [diff] [blame] | 2536 | |
| 2537 | return false; |
| 2538 | } |
Kevin Enderby | 6e68cd9 | 2009-07-15 15:30:11 +0000 | [diff] [blame] | 2539 | |
Kevin Enderby | c55acca | 2011-12-14 21:47:48 +0000 | [diff] [blame] | 2540 | /// ParseDirectiveIncbin |
| 2541 | /// ::= .incbin "filename" |
| 2542 | bool AsmParser::ParseDirectiveIncbin() { |
| 2543 | if (getLexer().isNot(AsmToken::String)) |
| 2544 | return TokError("expected string in '.incbin' directive"); |
| 2545 | |
| 2546 | std::string Filename = getTok().getString(); |
| 2547 | SMLoc IncbinLoc = getLexer().getLoc(); |
| 2548 | Lex(); |
| 2549 | |
| 2550 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2551 | return TokError("unexpected token in '.incbin' directive"); |
| 2552 | |
| 2553 | // Strip the quotes. |
| 2554 | Filename = Filename.substr(1, Filename.size()-2); |
| 2555 | |
| 2556 | // Attempt to process the included file. |
| 2557 | if (ProcessIncbinFile(Filename)) { |
| 2558 | Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); |
| 2559 | return true; |
| 2560 | } |
| 2561 | |
| 2562 | return false; |
| 2563 | } |
| 2564 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2565 | /// ParseDirectiveIf |
| 2566 | /// ::= .if expression |
| 2567 | bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2568 | TheCondStack.push_back(TheCondState); |
| 2569 | TheCondState.TheCond = AsmCond::IfCond; |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 2570 | if (TheCondState.Ignore) { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2571 | EatToEndOfStatement(); |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 2572 | } else { |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2573 | int64_t ExprValue; |
| 2574 | if (ParseAbsoluteExpression(ExprValue)) |
| 2575 | return true; |
| 2576 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2577 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2578 | return TokError("unexpected token in '.if' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2579 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2580 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2581 | |
| 2582 | TheCondState.CondMet = ExprValue; |
| 2583 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2584 | } |
| 2585 | |
| 2586 | return false; |
| 2587 | } |
| 2588 | |
Benjamin Kramer | a3dd0eb | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 2589 | /// ParseDirectiveIfb |
| 2590 | /// ::= .ifb string |
| 2591 | bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
| 2592 | TheCondStack.push_back(TheCondState); |
| 2593 | TheCondState.TheCond = AsmCond::IfCond; |
| 2594 | |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 2595 | if (TheCondState.Ignore) { |
Benjamin Kramer | a3dd0eb | 2012-05-12 11:18:42 +0000 | [diff] [blame] | 2596 | EatToEndOfStatement(); |
| 2597 | } else { |
| 2598 | StringRef Str = ParseStringToEndOfStatement(); |
| 2599 | |
| 2600 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2601 | return TokError("unexpected token in '.ifb' directive"); |
| 2602 | |
| 2603 | Lex(); |
| 2604 | |
| 2605 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
| 2606 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2607 | } |
| 2608 | |
| 2609 | return false; |
| 2610 | } |
| 2611 | |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 2612 | /// ParseDirectiveIfc |
| 2613 | /// ::= .ifc string1, string2 |
| 2614 | bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
| 2615 | TheCondStack.push_back(TheCondState); |
| 2616 | TheCondState.TheCond = AsmCond::IfCond; |
| 2617 | |
Benjamin Kramer | 29739e7 | 2012-05-12 16:52:21 +0000 | [diff] [blame] | 2618 | if (TheCondState.Ignore) { |
Benjamin Kramer | dec06ef | 2012-05-12 11:18:51 +0000 | [diff] [blame] | 2619 | EatToEndOfStatement(); |
| 2620 | } else { |
| 2621 | StringRef Str1 = ParseStringToComma(); |
| 2622 | |
| 2623 | if (getLexer().isNot(AsmToken::Comma)) |
| 2624 | return TokError("unexpected token in '.ifc' directive"); |
| 2625 | |
| 2626 | Lex(); |
| 2627 | |
| 2628 | StringRef Str2 = ParseStringToEndOfStatement(); |
| 2629 | |
| 2630 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 2631 | return TokError("unexpected token in '.ifc' directive"); |
| 2632 | |
| 2633 | Lex(); |
| 2634 | |
| 2635 | TheCondState.CondMet = ExpectEqual == (Str1 == Str2); |
| 2636 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2637 | } |
| 2638 | |
| 2639 | return false; |
| 2640 | } |
| 2641 | |
| 2642 | /// ParseDirectiveIfdef |
| 2643 | /// ::= .ifdef symbol |
Benjamin Kramer | 0fd90bc | 2011-02-08 22:29:56 +0000 | [diff] [blame] | 2644 | bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
| 2645 | StringRef Name; |
| 2646 | TheCondStack.push_back(TheCondState); |
| 2647 | TheCondState.TheCond = AsmCond::IfCond; |
| 2648 | |
| 2649 | if (TheCondState.Ignore) { |
| 2650 | EatToEndOfStatement(); |
| 2651 | } else { |
| 2652 | if (ParseIdentifier(Name)) |
| 2653 | return TokError("expected identifier after '.ifdef'"); |
| 2654 | |
| 2655 | Lex(); |
| 2656 | |
| 2657 | MCSymbol *Sym = getContext().LookupSymbol(Name); |
| 2658 | |
| 2659 | if (expect_defined) |
| 2660 | TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined()); |
| 2661 | else |
| 2662 | TheCondState.CondMet = (Sym == NULL || Sym->isUndefined()); |
| 2663 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2664 | } |
| 2665 | |
| 2666 | return false; |
| 2667 | } |
| 2668 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2669 | /// ParseDirectiveElseIf |
| 2670 | /// ::= .elseif expression |
| 2671 | bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { |
| 2672 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 2673 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 2674 | Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " |
| 2675 | " an .elseif"); |
| 2676 | TheCondState.TheCond = AsmCond::ElseIfCond; |
| 2677 | |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2678 | bool LastIgnoreState = false; |
| 2679 | if (!TheCondStack.empty()) |
| 2680 | LastIgnoreState = TheCondStack.back().Ignore; |
| 2681 | if (LastIgnoreState || TheCondState.CondMet) { |
| 2682 | TheCondState.Ignore = true; |
| 2683 | EatToEndOfStatement(); |
| 2684 | } |
| 2685 | else { |
| 2686 | int64_t ExprValue; |
| 2687 | if (ParseAbsoluteExpression(ExprValue)) |
| 2688 | return true; |
| 2689 | |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2690 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2691 | return TokError("unexpected token in '.elseif' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2692 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2693 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2694 | TheCondState.CondMet = ExprValue; |
| 2695 | TheCondState.Ignore = !TheCondState.CondMet; |
| 2696 | } |
| 2697 | |
| 2698 | return false; |
| 2699 | } |
| 2700 | |
| 2701 | /// ParseDirectiveElse |
| 2702 | /// ::= .else |
| 2703 | bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2704 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2705 | return TokError("unexpected token in '.else' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2706 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2707 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2708 | |
| 2709 | if (TheCondState.TheCond != AsmCond::IfCond && |
| 2710 | TheCondState.TheCond != AsmCond::ElseIfCond) |
| 2711 | Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " |
| 2712 | ".elseif"); |
| 2713 | TheCondState.TheCond = AsmCond::ElseCond; |
| 2714 | bool LastIgnoreState = false; |
| 2715 | if (!TheCondStack.empty()) |
| 2716 | LastIgnoreState = TheCondStack.back().Ignore; |
| 2717 | if (LastIgnoreState || TheCondState.CondMet) |
| 2718 | TheCondState.Ignore = true; |
| 2719 | else |
| 2720 | TheCondState.Ignore = false; |
| 2721 | |
| 2722 | return false; |
| 2723 | } |
| 2724 | |
| 2725 | /// ParseDirectiveEndIf |
| 2726 | /// ::= .endif |
| 2727 | bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { |
Daniel Dunbar | 8f34bea | 2010-07-12 18:03:11 +0000 | [diff] [blame] | 2728 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2729 | return TokError("unexpected token in '.endif' directive"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2730 | |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2731 | Lex(); |
Kevin Enderby | c114ed7 | 2009-08-07 22:46:00 +0000 | [diff] [blame] | 2732 | |
| 2733 | if ((TheCondState.TheCond == AsmCond::NoCond) || |
| 2734 | TheCondStack.empty()) |
| 2735 | Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " |
| 2736 | ".else"); |
| 2737 | if (!TheCondStack.empty()) { |
| 2738 | TheCondState = TheCondStack.back(); |
| 2739 | TheCondStack.pop_back(); |
| 2740 | } |
| 2741 | |
| 2742 | return false; |
| 2743 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2744 | |
| 2745 | /// ParseDirectiveFile |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 2746 | /// ::= .file [number] filename |
| 2747 | /// ::= .file number directory filename |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2748 | bool GenericAsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2749 | // FIXME: I'm not sure what this is. |
| 2750 | int64_t FileNumber = -1; |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 2751 | SMLoc FileNumberLoc = getLexer().getLoc(); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2752 | if (getLexer().is(AsmToken::Integer)) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2753 | FileNumber = getTok().getIntVal(); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2754 | Lex(); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2755 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2756 | if (FileNumber < 1) |
| 2757 | return TokError("file number less than one"); |
| 2758 | } |
| 2759 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2760 | if (getLexer().isNot(AsmToken::String)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2761 | return TokError("unexpected token in '.file' directive"); |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2762 | |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 2763 | // Usually the directory and filename together, otherwise just the directory. |
| 2764 | StringRef Path = getTok().getString(); |
| 2765 | Path = Path.substr(1, Path.size()-2); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2766 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2767 | |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 2768 | StringRef Directory; |
| 2769 | StringRef Filename; |
| 2770 | if (getLexer().is(AsmToken::String)) { |
| 2771 | if (FileNumber == -1) |
| 2772 | return TokError("explicit path specified, but no file number"); |
| 2773 | Filename = getTok().getString(); |
| 2774 | Filename = Filename.substr(1, Filename.size()-2); |
| 2775 | Directory = Path; |
| 2776 | Lex(); |
| 2777 | } else { |
| 2778 | Filename = Path; |
| 2779 | } |
| 2780 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2781 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2782 | return TokError("unexpected token in '.file' directive"); |
| 2783 | |
Chris Lattner | d32e803 | 2010-01-25 19:02:58 +0000 | [diff] [blame] | 2784 | if (FileNumber == -1) |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2785 | getStreamer().EmitFileDirective(Filename); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 2786 | else { |
Kevin Enderby | 8704b78 | 2012-01-11 18:04:47 +0000 | [diff] [blame] | 2787 | if (getContext().getGenDwarfForAssembly() == true) |
| 2788 | Error(DirectiveLoc, "input can't have .file dwarf directives when -g is " |
| 2789 | "used to generate dwarf debug info for assembly code"); |
| 2790 | |
Nick Lewycky | 44d798d | 2011-10-17 23:05:28 +0000 | [diff] [blame] | 2791 | if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename)) |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2792 | Error(FileNumberLoc, "file number already allocated"); |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 2793 | } |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2794 | |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2795 | return false; |
| 2796 | } |
| 2797 | |
| 2798 | /// ParseDirectiveLine |
| 2799 | /// ::= .line [number] |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2800 | bool GenericAsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) { |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2801 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2802 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2803 | return TokError("unexpected token in '.line' directive"); |
| 2804 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2805 | int64_t LineNumber = getTok().getIntVal(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2806 | (void) LineNumber; |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2807 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2808 | |
| 2809 | // FIXME: Do something with the .line. |
| 2810 | } |
| 2811 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2812 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Daniel Dunbar | 839348a | 2010-07-01 20:20:01 +0000 | [diff] [blame] | 2813 | return TokError("unexpected token in '.line' directive"); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2814 | |
| 2815 | return false; |
| 2816 | } |
| 2817 | |
| 2818 | |
| 2819 | /// ParseDirectiveLoc |
Kevin Enderby | 6d8f1a9 | 2010-08-24 21:14:47 +0000 | [diff] [blame] | 2820 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2821 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
| 2822 | /// The first number is a file number, must have been previously assigned with |
| 2823 | /// a .file directive, the second number is the line number and optionally the |
| 2824 | /// third number is a column position (zero if not specified). The remaining |
| 2825 | /// optional items are .loc sub-directives. |
Daniel Dunbar | 81ea00f | 2010-07-12 17:54:38 +0000 | [diff] [blame] | 2826 | bool GenericAsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) { |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2827 | |
Daniel Dunbar | eceec05 | 2010-07-12 17:45:27 +0000 | [diff] [blame] | 2828 | if (getLexer().isNot(AsmToken::Integer)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2829 | return TokError("unexpected token in '.loc' directive"); |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 2830 | int64_t FileNumber = getTok().getIntVal(); |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2831 | if (FileNumber < 1) |
| 2832 | return TokError("file number less than one in '.loc' directive"); |
Kevin Enderby | 3f55c24 | 2010-10-04 20:17:24 +0000 | [diff] [blame] | 2833 | if (!getContext().isValidDwarfFileNumber(FileNumber)) |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2834 | return TokError("unassigned file number in '.loc' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2835 | Lex(); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2836 | |
Kevin Enderby | 6d8f1a9 | 2010-08-24 21:14:47 +0000 | [diff] [blame] | 2837 | int64_t LineNumber = 0; |
| 2838 | if (getLexer().is(AsmToken::Integer)) { |
| 2839 | LineNumber = getTok().getIntVal(); |
| 2840 | if (LineNumber < 1) |
| 2841 | return TokError("line number less than one in '.loc' directive"); |
| 2842 | Lex(); |
| 2843 | } |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2844 | |
| 2845 | int64_t ColumnPos = 0; |
| 2846 | if (getLexer().is(AsmToken::Integer)) { |
| 2847 | ColumnPos = getTok().getIntVal(); |
| 2848 | if (ColumnPos < 0) |
| 2849 | return TokError("column position less than zero in '.loc' directive"); |
Sean Callanan | 79ed1a8 | 2010-01-19 20:22:31 +0000 | [diff] [blame] | 2850 | Lex(); |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2851 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2852 | |
Kevin Enderby | c095793 | 2010-09-30 16:52:03 +0000 | [diff] [blame] | 2853 | unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2854 | unsigned Isa = 0; |
Rafael Espindola | c50a0fd | 2010-11-13 03:18:27 +0000 | [diff] [blame] | 2855 | int64_t Discriminator = 0; |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2856 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 2857 | for (;;) { |
| 2858 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2859 | break; |
| 2860 | |
| 2861 | StringRef Name; |
| 2862 | SMLoc Loc = getTok().getLoc(); |
| 2863 | if (getParser().ParseIdentifier(Name)) |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2864 | return TokError("unexpected token in '.loc' directive"); |
| 2865 | |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2866 | if (Name == "basic_block") |
| 2867 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
| 2868 | else if (Name == "prologue_end") |
| 2869 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
| 2870 | else if (Name == "epilogue_begin") |
| 2871 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
| 2872 | else if (Name == "is_stmt") { |
| 2873 | SMLoc Loc = getTok().getLoc(); |
| 2874 | const MCExpr *Value; |
| 2875 | if (getParser().ParseExpression(Value)) |
| 2876 | return true; |
| 2877 | // The expression must be the constant 0 or 1. |
| 2878 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2879 | int Value = MCE->getValue(); |
| 2880 | if (Value == 0) |
| 2881 | Flags &= ~DWARF2_FLAG_IS_STMT; |
| 2882 | else if (Value == 1) |
| 2883 | Flags |= DWARF2_FLAG_IS_STMT; |
| 2884 | else |
| 2885 | return Error(Loc, "is_stmt value not 0 or 1"); |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2886 | } |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2887 | else { |
| 2888 | return Error(Loc, "is_stmt value not the constant value of 0 or 1"); |
| 2889 | } |
| 2890 | } |
| 2891 | else if (Name == "isa") { |
| 2892 | SMLoc Loc = getTok().getLoc(); |
| 2893 | const MCExpr *Value; |
| 2894 | if (getParser().ParseExpression(Value)) |
| 2895 | return true; |
| 2896 | // The expression must be a constant greater or equal to 0. |
| 2897 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { |
| 2898 | int Value = MCE->getValue(); |
| 2899 | if (Value < 0) |
| 2900 | return Error(Loc, "isa number less than zero"); |
| 2901 | Isa = Value; |
Michael J. Spencer | c0c8df3 | 2010-10-09 11:00:50 +0000 | [diff] [blame] | 2902 | } |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2903 | else { |
| 2904 | return Error(Loc, "isa number not a constant value"); |
| 2905 | } |
| 2906 | } |
Rafael Espindola | c50a0fd | 2010-11-13 03:18:27 +0000 | [diff] [blame] | 2907 | else if (Name == "discriminator") { |
| 2908 | if (getParser().ParseAbsoluteExpression(Discriminator)) |
| 2909 | return true; |
| 2910 | } |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2911 | else { |
| 2912 | return Error(Loc, "unknown sub-directive in '.loc' directive"); |
| 2913 | } |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2914 | |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame] | 2915 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 2916 | break; |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | |
Rafael Espindola | af6b5808 | 2010-11-16 21:20:32 +0000 | [diff] [blame] | 2920 | getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, |
Devang Patel | 3f3bf93 | 2011-04-18 20:26:49 +0000 | [diff] [blame] | 2921 | Isa, Discriminator, StringRef()); |
Daniel Dunbar | d0c14d6 | 2009-08-11 04:24:50 +0000 | [diff] [blame] | 2922 | |
| 2923 | return false; |
| 2924 | } |
| 2925 | |
Daniel Dunbar | 138abae | 2010-10-16 04:56:42 +0000 | [diff] [blame] | 2926 | /// ParseDirectiveStabs |
| 2927 | /// ::= .stabs string, number, number, number |
| 2928 | bool GenericAsmParser::ParseDirectiveStabs(StringRef Directive, |
| 2929 | SMLoc DirectiveLoc) { |
| 2930 | return TokError("unsupported directive '" + Directive + "'"); |
| 2931 | } |
| 2932 | |
Rafael Espindola | f9efd83 | 2011-05-10 01:10:18 +0000 | [diff] [blame] | 2933 | /// ParseDirectiveCFISections |
| 2934 | /// ::= .cfi_sections section [, section] |
| 2935 | bool GenericAsmParser::ParseDirectiveCFISections(StringRef, |
| 2936 | SMLoc DirectiveLoc) { |
| 2937 | StringRef Name; |
| 2938 | bool EH = false; |
| 2939 | bool Debug = false; |
| 2940 | |
| 2941 | if (getParser().ParseIdentifier(Name)) |
| 2942 | return TokError("Expected an identifier"); |
| 2943 | |
| 2944 | if (Name == ".eh_frame") |
| 2945 | EH = true; |
| 2946 | else if (Name == ".debug_frame") |
| 2947 | Debug = true; |
| 2948 | |
| 2949 | if (getLexer().is(AsmToken::Comma)) { |
| 2950 | Lex(); |
| 2951 | |
| 2952 | if (getParser().ParseIdentifier(Name)) |
| 2953 | return TokError("Expected an identifier"); |
| 2954 | |
| 2955 | if (Name == ".eh_frame") |
| 2956 | EH = true; |
| 2957 | else if (Name == ".debug_frame") |
| 2958 | Debug = true; |
| 2959 | } |
| 2960 | |
| 2961 | getStreamer().EmitCFISections(EH, Debug); |
| 2962 | |
| 2963 | return false; |
| 2964 | } |
| 2965 | |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 2966 | /// ParseDirectiveCFIStartProc |
| 2967 | /// ::= .cfi_startproc |
| 2968 | bool GenericAsmParser::ParseDirectiveCFIStartProc(StringRef, |
| 2969 | SMLoc DirectiveLoc) { |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 2970 | getStreamer().EmitCFIStartProc(); |
| 2971 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /// ParseDirectiveCFIEndProc |
| 2975 | /// ::= .cfi_endproc |
| 2976 | bool GenericAsmParser::ParseDirectiveCFIEndProc(StringRef, SMLoc DirectiveLoc) { |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 2977 | getStreamer().EmitCFIEndProc(); |
| 2978 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 2981 | /// ParseRegisterOrRegisterNumber - parse register name or number. |
| 2982 | bool GenericAsmParser::ParseRegisterOrRegisterNumber(int64_t &Register, |
| 2983 | SMLoc DirectiveLoc) { |
| 2984 | unsigned RegNo; |
| 2985 | |
Jim Grosbach | 6f888a8 | 2011-06-02 17:14:04 +0000 | [diff] [blame] | 2986 | if (getLexer().isNot(AsmToken::Integer)) { |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 2987 | if (getParser().getTargetParser().ParseRegister(RegNo, DirectiveLoc, |
| 2988 | DirectiveLoc)) |
| 2989 | return true; |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 2990 | Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true); |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 2991 | } else |
| 2992 | return getParser().ParseAbsoluteExpression(Register); |
Jim Grosbach | de2f5f4 | 2011-02-11 19:05:56 +0000 | [diff] [blame] | 2993 | |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 2994 | return false; |
| 2995 | } |
| 2996 | |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 2997 | /// ParseDirectiveCFIDefCfa |
| 2998 | /// ::= .cfi_def_cfa register, offset |
| 2999 | bool GenericAsmParser::ParseDirectiveCFIDefCfa(StringRef, |
| 3000 | SMLoc DirectiveLoc) { |
| 3001 | int64_t Register = 0; |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 3002 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 3003 | return true; |
| 3004 | |
| 3005 | if (getLexer().isNot(AsmToken::Comma)) |
| 3006 | return TokError("unexpected token in directive"); |
| 3007 | Lex(); |
| 3008 | |
| 3009 | int64_t Offset = 0; |
| 3010 | if (getParser().ParseAbsoluteExpression(Offset)) |
| 3011 | return true; |
| 3012 | |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3013 | getStreamer().EmitCFIDefCfa(Register, Offset); |
| 3014 | return false; |
Rafael Espindola | b40a71f | 2010-12-29 01:42:56 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3017 | /// ParseDirectiveCFIDefCfaOffset |
| 3018 | /// ::= .cfi_def_cfa_offset offset |
| 3019 | bool GenericAsmParser::ParseDirectiveCFIDefCfaOffset(StringRef, |
| 3020 | SMLoc DirectiveLoc) { |
| 3021 | int64_t Offset = 0; |
| 3022 | if (getParser().ParseAbsoluteExpression(Offset)) |
| 3023 | return true; |
| 3024 | |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3025 | getStreamer().EmitCFIDefCfaOffset(Offset); |
| 3026 | return false; |
Rafael Espindola | 53abbe5 | 2011-04-11 20:29:16 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | /// ParseDirectiveCFIAdjustCfaOffset |
| 3030 | /// ::= .cfi_adjust_cfa_offset adjustment |
| 3031 | bool GenericAsmParser::ParseDirectiveCFIAdjustCfaOffset(StringRef, |
| 3032 | SMLoc DirectiveLoc) { |
| 3033 | int64_t Adjustment = 0; |
| 3034 | if (getParser().ParseAbsoluteExpression(Adjustment)) |
| 3035 | return true; |
| 3036 | |
Rafael Espindola | 5d7dcd3 | 2011-04-12 18:53:30 +0000 | [diff] [blame] | 3037 | getStreamer().EmitCFIAdjustCfaOffset(Adjustment); |
| 3038 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | /// ParseDirectiveCFIDefCfaRegister |
| 3042 | /// ::= .cfi_def_cfa_register register |
| 3043 | bool GenericAsmParser::ParseDirectiveCFIDefCfaRegister(StringRef, |
| 3044 | SMLoc DirectiveLoc) { |
| 3045 | int64_t Register = 0; |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 3046 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3047 | return true; |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3048 | |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3049 | getStreamer().EmitCFIDefCfaRegister(Register); |
| 3050 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | /// ParseDirectiveCFIOffset |
Rafael Espindola | a61842b | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 3054 | /// ::= .cfi_offset register, offset |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3055 | bool GenericAsmParser::ParseDirectiveCFIOffset(StringRef, SMLoc DirectiveLoc) { |
| 3056 | int64_t Register = 0; |
| 3057 | int64_t Offset = 0; |
Roman Divacky | 54b0f4f | 2011-01-27 17:16:37 +0000 | [diff] [blame] | 3058 | |
| 3059 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3060 | return true; |
| 3061 | |
| 3062 | if (getLexer().isNot(AsmToken::Comma)) |
| 3063 | return TokError("unexpected token in directive"); |
| 3064 | Lex(); |
| 3065 | |
| 3066 | if (getParser().ParseAbsoluteExpression(Offset)) |
| 3067 | return true; |
| 3068 | |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3069 | getStreamer().EmitCFIOffset(Register, Offset); |
| 3070 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Rafael Espindola | a61842b | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 3073 | /// ParseDirectiveCFIRelOffset |
| 3074 | /// ::= .cfi_rel_offset register, offset |
| 3075 | bool GenericAsmParser::ParseDirectiveCFIRelOffset(StringRef, |
| 3076 | SMLoc DirectiveLoc) { |
| 3077 | int64_t Register = 0; |
| 3078 | |
| 3079 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 3080 | return true; |
| 3081 | |
| 3082 | if (getLexer().isNot(AsmToken::Comma)) |
| 3083 | return TokError("unexpected token in directive"); |
| 3084 | Lex(); |
| 3085 | |
| 3086 | int64_t Offset = 0; |
| 3087 | if (getParser().ParseAbsoluteExpression(Offset)) |
| 3088 | return true; |
| 3089 | |
Rafael Espindola | 25f492e | 2011-04-12 16:12:03 +0000 | [diff] [blame] | 3090 | getStreamer().EmitCFIRelOffset(Register, Offset); |
| 3091 | return false; |
Rafael Espindola | a61842b | 2011-04-11 21:49:50 +0000 | [diff] [blame] | 3092 | } |
| 3093 | |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 3094 | static bool isValidEncoding(int64_t Encoding) { |
| 3095 | if (Encoding & ~0xff) |
| 3096 | return false; |
| 3097 | |
| 3098 | if (Encoding == dwarf::DW_EH_PE_omit) |
| 3099 | return true; |
| 3100 | |
| 3101 | const unsigned Format = Encoding & 0xf; |
| 3102 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
| 3103 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
| 3104 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
| 3105 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
| 3106 | return false; |
| 3107 | |
Rafael Espindola | caf1158 | 2010-12-29 04:31:26 +0000 | [diff] [blame] | 3108 | const unsigned Application = Encoding & 0x70; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 3109 | if (Application != dwarf::DW_EH_PE_absptr && |
Rafael Espindola | caf1158 | 2010-12-29 04:31:26 +0000 | [diff] [blame] | 3110 | Application != dwarf::DW_EH_PE_pcrel) |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 3111 | return false; |
| 3112 | |
| 3113 | return true; |
| 3114 | } |
| 3115 | |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3116 | /// ParseDirectiveCFIPersonalityOrLsda |
| 3117 | /// ::= .cfi_personality encoding, [symbol_name] |
| 3118 | /// ::= .cfi_lsda encoding, [symbol_name] |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3119 | bool GenericAsmParser::ParseDirectiveCFIPersonalityOrLsda(StringRef IDVal, |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3120 | SMLoc DirectiveLoc) { |
| 3121 | int64_t Encoding = 0; |
| 3122 | if (getParser().ParseAbsoluteExpression(Encoding)) |
| 3123 | return true; |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 3124 | if (Encoding == dwarf::DW_EH_PE_omit) |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3125 | return false; |
| 3126 | |
Rafael Espindola | d7c8cca | 2010-12-26 20:20:31 +0000 | [diff] [blame] | 3127 | if (!isValidEncoding(Encoding)) |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3128 | return TokError("unsupported encoding."); |
| 3129 | |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3130 | if (getLexer().isNot(AsmToken::Comma)) |
| 3131 | return TokError("unexpected token in directive"); |
| 3132 | Lex(); |
| 3133 | |
| 3134 | StringRef Name; |
| 3135 | if (getParser().ParseIdentifier(Name)) |
| 3136 | return TokError("expected identifier in directive"); |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3137 | |
| 3138 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); |
| 3139 | |
| 3140 | if (IDVal == ".cfi_personality") |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3141 | getStreamer().EmitCFIPersonality(Sym, Encoding); |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3142 | else { |
| 3143 | assert(IDVal == ".cfi_lsda"); |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3144 | getStreamer().EmitCFILsda(Sym, Encoding); |
Rafael Espindola | cdfecc8 | 2010-11-22 14:27:24 +0000 | [diff] [blame] | 3145 | } |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3146 | return false; |
Rafael Espindola | 1fdfbc4 | 2010-11-16 18:34:07 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 3149 | /// ParseDirectiveCFIRememberState |
| 3150 | /// ::= .cfi_remember_state |
| 3151 | bool GenericAsmParser::ParseDirectiveCFIRememberState(StringRef IDVal, |
| 3152 | SMLoc DirectiveLoc) { |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3153 | getStreamer().EmitCFIRememberState(); |
| 3154 | return false; |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
| 3157 | /// ParseDirectiveCFIRestoreState |
| 3158 | /// ::= .cfi_remember_state |
| 3159 | bool GenericAsmParser::ParseDirectiveCFIRestoreState(StringRef IDVal, |
| 3160 | SMLoc DirectiveLoc) { |
Rafael Espindola | 066c2f4 | 2011-04-12 23:59:07 +0000 | [diff] [blame] | 3161 | getStreamer().EmitCFIRestoreState(); |
| 3162 | return false; |
Rafael Espindola | fe024d0 | 2010-12-28 18:36:23 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
Rafael Espindola | c575439 | 2011-04-12 15:31:05 +0000 | [diff] [blame] | 3165 | /// ParseDirectiveCFISameValue |
| 3166 | /// ::= .cfi_same_value register |
| 3167 | bool GenericAsmParser::ParseDirectiveCFISameValue(StringRef IDVal, |
| 3168 | SMLoc DirectiveLoc) { |
| 3169 | int64_t Register = 0; |
| 3170 | |
| 3171 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 3172 | return true; |
| 3173 | |
| 3174 | getStreamer().EmitCFISameValue(Register); |
| 3175 | |
| 3176 | return false; |
| 3177 | } |
| 3178 | |
Rafael Espindola | ed23bdb | 2011-12-29 21:43:03 +0000 | [diff] [blame] | 3179 | /// ParseDirectiveCFIRestore |
| 3180 | /// ::= .cfi_restore register |
| 3181 | bool GenericAsmParser::ParseDirectiveCFIRestore(StringRef IDVal, |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 3182 | SMLoc DirectiveLoc) { |
Rafael Espindola | ed23bdb | 2011-12-29 21:43:03 +0000 | [diff] [blame] | 3183 | int64_t Register = 0; |
| 3184 | if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) |
| 3185 | return true; |
| 3186 | |
| 3187 | getStreamer().EmitCFIRestore(Register); |
| 3188 | |
| 3189 | return false; |
| 3190 | } |
| 3191 | |
Rafael Espindola | 6f0b181 | 2011-12-29 20:24:47 +0000 | [diff] [blame] | 3192 | /// ParseDirectiveCFIEscape |
| 3193 | /// ::= .cfi_escape expression[,...] |
| 3194 | bool GenericAsmParser::ParseDirectiveCFIEscape(StringRef IDVal, |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 3195 | SMLoc DirectiveLoc) { |
Rafael Espindola | 6f0b181 | 2011-12-29 20:24:47 +0000 | [diff] [blame] | 3196 | std::string Values; |
| 3197 | int64_t CurrValue; |
| 3198 | if (getParser().ParseAbsoluteExpression(CurrValue)) |
| 3199 | return true; |
| 3200 | |
| 3201 | Values.push_back((uint8_t)CurrValue); |
| 3202 | |
| 3203 | while (getLexer().is(AsmToken::Comma)) { |
| 3204 | Lex(); |
| 3205 | |
| 3206 | if (getParser().ParseAbsoluteExpression(CurrValue)) |
| 3207 | return true; |
| 3208 | |
| 3209 | Values.push_back((uint8_t)CurrValue); |
| 3210 | } |
| 3211 | |
| 3212 | getStreamer().EmitCFIEscape(Values); |
| 3213 | return false; |
| 3214 | } |
| 3215 | |
Rafael Espindola | 16d7d43 | 2012-01-23 21:51:52 +0000 | [diff] [blame] | 3216 | /// ParseDirectiveCFISignalFrame |
| 3217 | /// ::= .cfi_signal_frame |
| 3218 | bool GenericAsmParser::ParseDirectiveCFISignalFrame(StringRef Directive, |
| 3219 | SMLoc DirectiveLoc) { |
| 3220 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3221 | return Error(getLexer().getLoc(), |
| 3222 | "unexpected token in '" + Directive + "' directive"); |
| 3223 | |
| 3224 | getStreamer().EmitCFISignalFrame(); |
| 3225 | |
| 3226 | return false; |
| 3227 | } |
| 3228 | |
Daniel Dunbar | 3c802de | 2010-07-18 18:38:02 +0000 | [diff] [blame] | 3229 | /// ParseDirectiveMacrosOnOff |
| 3230 | /// ::= .macros_on |
| 3231 | /// ::= .macros_off |
| 3232 | bool GenericAsmParser::ParseDirectiveMacrosOnOff(StringRef Directive, |
| 3233 | SMLoc DirectiveLoc) { |
| 3234 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3235 | return Error(getLexer().getLoc(), |
| 3236 | "unexpected token in '" + Directive + "' directive"); |
| 3237 | |
| 3238 | getParser().MacrosEnabled = Directive == ".macros_on"; |
| 3239 | |
| 3240 | return false; |
| 3241 | } |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 3242 | |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3243 | /// ParseDirectiveMacro |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3244 | /// ::= .macro name [parameters] |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3245 | bool GenericAsmParser::ParseDirectiveMacro(StringRef Directive, |
| 3246 | SMLoc DirectiveLoc) { |
| 3247 | StringRef Name; |
| 3248 | if (getParser().ParseIdentifier(Name)) |
Rafael Espindola | d7ae0f1 | 2012-08-21 17:12:05 +0000 | [diff] [blame] | 3249 | return TokError("expected identifier in '.macro' directive"); |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3250 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3251 | MacroParameters Parameters; |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 3252 | // Argument delimiter is initially unknown. It will be set by |
| 3253 | // ParseMacroArgument() |
| 3254 | AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3255 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Rafael Espindola | 7996d04 | 2012-08-21 16:06:48 +0000 | [diff] [blame] | 3256 | for (;;) { |
| 3257 | MacroParameter Parameter; |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3258 | if (getParser().ParseIdentifier(Parameter.first)) |
Rafael Espindola | d7ae0f1 | 2012-08-21 17:12:05 +0000 | [diff] [blame] | 3259 | return TokError("expected identifier in '.macro' directive"); |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3260 | |
| 3261 | if (getLexer().is(AsmToken::Equal)) { |
| 3262 | Lex(); |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 3263 | if (getParser().ParseMacroArgument(Parameter.second, ArgumentDelimiter)) |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3264 | return true; |
| 3265 | } |
| 3266 | |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3267 | Parameters.push_back(Parameter); |
| 3268 | |
Preston Gurd | 7b6f203 | 2012-09-19 20:36:12 +0000 | [diff] [blame] | 3269 | if (getLexer().is(AsmToken::Comma)) |
| 3270 | Lex(); |
| 3271 | else if (getLexer().is(AsmToken::EndOfStatement)) |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3272 | break; |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3273 | } |
| 3274 | } |
| 3275 | |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3276 | // Eat the end of statement. |
| 3277 | Lex(); |
| 3278 | |
| 3279 | AsmToken EndToken, StartToken = getTok(); |
| 3280 | |
| 3281 | // Lex the macro definition. |
| 3282 | for (;;) { |
| 3283 | // Check whether we have reached the end of the file. |
| 3284 | if (getLexer().is(AsmToken::Eof)) |
| 3285 | return Error(DirectiveLoc, "no matching '.endmacro' in definition"); |
| 3286 | |
| 3287 | // Otherwise, check whether we have reach the .endmacro. |
| 3288 | if (getLexer().is(AsmToken::Identifier) && |
| 3289 | (getTok().getIdentifier() == ".endm" || |
| 3290 | getTok().getIdentifier() == ".endmacro")) { |
| 3291 | EndToken = getTok(); |
| 3292 | Lex(); |
| 3293 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3294 | return TokError("unexpected token in '" + EndToken.getIdentifier() + |
| 3295 | "' directive"); |
| 3296 | break; |
| 3297 | } |
| 3298 | |
| 3299 | // Otherwise, scan til the end of the statement. |
| 3300 | getParser().EatToEndOfStatement(); |
| 3301 | } |
| 3302 | |
| 3303 | if (getParser().MacroMap.lookup(Name)) { |
| 3304 | return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); |
| 3305 | } |
| 3306 | |
| 3307 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3308 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3309 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
Rafael Espindola | 6536644 | 2011-06-05 02:43:45 +0000 | [diff] [blame] | 3310 | getParser().MacroMap[Name] = new Macro(Name, Body, Parameters); |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3311 | return false; |
| 3312 | } |
| 3313 | |
| 3314 | /// ParseDirectiveEndMacro |
| 3315 | /// ::= .endm |
| 3316 | /// ::= .endmacro |
| 3317 | bool GenericAsmParser::ParseDirectiveEndMacro(StringRef Directive, |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3318 | SMLoc DirectiveLoc) { |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3319 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3320 | return TokError("unexpected token in '" + Directive + "' directive"); |
| 3321 | |
Daniel Dunbar | c64a0d7 | 2010-07-18 18:54:11 +0000 | [diff] [blame] | 3322 | // If we are inside a macro instantiation, terminate the current |
| 3323 | // instantiation. |
| 3324 | if (!getParser().ActiveMacros.empty()) { |
| 3325 | getParser().HandleMacroExit(); |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
| 3329 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
| 3330 | // .endmacro directives are handled during the macro definition parsing. |
Daniel Dunbar | 6d8cf08 | 2010-07-18 18:47:21 +0000 | [diff] [blame] | 3331 | return TokError("unexpected '" + Directive + "' in file, " |
| 3332 | "no current macro definition"); |
| 3333 | } |
| 3334 | |
Benjamin Kramer | bc3b27c | 2012-05-12 11:21:46 +0000 | [diff] [blame] | 3335 | /// ParseDirectivePurgeMacro |
| 3336 | /// ::= .purgem |
| 3337 | bool GenericAsmParser::ParseDirectivePurgeMacro(StringRef Directive, |
| 3338 | SMLoc DirectiveLoc) { |
| 3339 | StringRef Name; |
| 3340 | if (getParser().ParseIdentifier(Name)) |
| 3341 | return TokError("expected identifier in '.purgem' directive"); |
| 3342 | |
| 3343 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3344 | return TokError("unexpected token in '.purgem' directive"); |
| 3345 | |
| 3346 | StringMap<Macro*>::iterator I = getParser().MacroMap.find(Name); |
| 3347 | if (I == getParser().MacroMap.end()) |
| 3348 | return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); |
| 3349 | |
| 3350 | // Undefine the macro. |
| 3351 | delete I->getValue(); |
| 3352 | getParser().MacroMap.erase(I); |
| 3353 | return false; |
| 3354 | } |
| 3355 | |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3356 | bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) { |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 3357 | getParser().CheckForValidSection(); |
| 3358 | |
| 3359 | const MCExpr *Value; |
| 3360 | |
| 3361 | if (getParser().ParseExpression(Value)) |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3362 | return true; |
| 3363 | |
| 3364 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 3365 | return TokError("unexpected token in directive"); |
| 3366 | |
| 3367 | if (DirName[1] == 's') |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 3368 | getStreamer().EmitSLEB128Value(Value); |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3369 | else |
Rafael Espindola | 3ff5709 | 2010-11-02 17:22:24 +0000 | [diff] [blame] | 3370 | getStreamer().EmitULEB128Value(Value); |
| 3371 | |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3372 | return false; |
| 3373 | } |
| 3374 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3375 | Macro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3376 | AsmToken EndToken, StartToken = getTok(); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3377 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3378 | unsigned NestLevel = 0; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3379 | for (;;) { |
| 3380 | // Check whether we have reached the end of the file. |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3381 | if (getLexer().is(AsmToken::Eof)) { |
| 3382 | Error(DirectiveLoc, "no matching '.endr' in definition"); |
| 3383 | return 0; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3386 | if (Lexer.is(AsmToken::Identifier) && |
| 3387 | (getTok().getIdentifier() == ".rept")) { |
| 3388 | ++NestLevel; |
| 3389 | } |
| 3390 | |
| 3391 | // Otherwise, check whether we have reached the .endr. |
| 3392 | if (Lexer.is(AsmToken::Identifier) && |
| 3393 | getTok().getIdentifier() == ".endr") { |
| 3394 | if (NestLevel == 0) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3395 | EndToken = getTok(); |
| 3396 | Lex(); |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3397 | if (Lexer.isNot(AsmToken::EndOfStatement)) { |
| 3398 | TokError("unexpected token in '.endr' directive"); |
| 3399 | return 0; |
| 3400 | } |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | } |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3403 | --NestLevel; |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3406 | // Otherwise, scan till the end of the statement. |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3407 | EatToEndOfStatement(); |
| 3408 | } |
| 3409 | |
| 3410 | const char *BodyStart = StartToken.getLoc().getPointer(); |
| 3411 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
| 3412 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
| 3413 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3414 | // We Are Anonymous. |
| 3415 | StringRef Name; |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3416 | MacroParameters Parameters; |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3417 | return new Macro(Name, Body, Parameters); |
| 3418 | } |
| 3419 | |
| 3420 | void AsmParser::InstantiateMacroLikeBody(Macro *M, SMLoc DirectiveLoc, |
| 3421 | raw_svector_ostream &OS) { |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3422 | OS << ".endr\n"; |
| 3423 | |
| 3424 | MemoryBuffer *Instantiation = |
| 3425 | MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); |
| 3426 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3427 | // Create the macro instantiation object and add to the current macro |
| 3428 | // instantiation stack. |
| 3429 | MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc, |
| 3430 | getTok().getLoc(), |
| 3431 | Instantiation); |
| 3432 | ActiveMacros.push_back(MI); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3433 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3434 | // Jump to the macro instantiation and prime the lexer. |
| 3435 | CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); |
| 3436 | Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); |
| 3437 | Lex(); |
| 3438 | } |
| 3439 | |
| 3440 | bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) { |
| 3441 | int64_t Count; |
| 3442 | if (ParseAbsoluteExpression(Count)) |
| 3443 | return TokError("unexpected token in '.rept' directive"); |
| 3444 | |
| 3445 | if (Count < 0) |
| 3446 | return TokError("Count is negative"); |
| 3447 | |
| 3448 | if (Lexer.isNot(AsmToken::EndOfStatement)) |
| 3449 | return TokError("unexpected token in '.rept' directive"); |
| 3450 | |
| 3451 | // Eat the end of statement. |
| 3452 | Lex(); |
| 3453 | |
| 3454 | // Lex the rept definition. |
| 3455 | Macro *M = ParseMacroLikeBody(DirectiveLoc); |
| 3456 | if (!M) |
| 3457 | return true; |
| 3458 | |
| 3459 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3460 | // to hold the macro body with substitutions. |
| 3461 | SmallString<256> Buf; |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3462 | MacroParameters Parameters; |
| 3463 | MacroArguments A; |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3464 | raw_svector_ostream OS(Buf); |
| 3465 | while (Count--) { |
| 3466 | if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc())) |
| 3467 | return true; |
| 3468 | } |
| 3469 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3470 | |
| 3471 | return false; |
| 3472 | } |
| 3473 | |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3474 | /// ParseDirectiveIrp |
| 3475 | /// ::= .irp symbol,values |
| 3476 | bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) { |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3477 | MacroParameters Parameters; |
| 3478 | MacroParameter Parameter; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3479 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3480 | if (ParseIdentifier(Parameter.first)) |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3481 | return TokError("expected identifier in '.irp' directive"); |
| 3482 | |
| 3483 | Parameters.push_back(Parameter); |
| 3484 | |
| 3485 | if (Lexer.isNot(AsmToken::Comma)) |
| 3486 | return TokError("expected comma in '.irp' directive"); |
| 3487 | |
| 3488 | Lex(); |
| 3489 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3490 | MacroArguments A; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3491 | if (ParseMacroArguments(0, A)) |
| 3492 | return true; |
| 3493 | |
| 3494 | // Eat the end of statement. |
| 3495 | Lex(); |
| 3496 | |
| 3497 | // Lex the irp definition. |
| 3498 | Macro *M = ParseMacroLikeBody(DirectiveLoc); |
| 3499 | if (!M) |
| 3500 | return true; |
| 3501 | |
| 3502 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3503 | // to hold the macro body with substitutions. |
| 3504 | SmallString<256> Buf; |
| 3505 | raw_svector_ostream OS(Buf); |
| 3506 | |
Rafael Espindola | 7996d04 | 2012-08-21 16:06:48 +0000 | [diff] [blame] | 3507 | for (MacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { |
| 3508 | MacroArguments Args; |
Rafael Espindola | aa7a2f2 | 2012-06-15 14:02:34 +0000 | [diff] [blame] | 3509 | Args.push_back(*i); |
| 3510 | |
| 3511 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 3512 | return true; |
| 3513 | } |
| 3514 | |
| 3515 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
| 3516 | |
| 3517 | return false; |
| 3518 | } |
| 3519 | |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3520 | /// ParseDirectiveIrpc |
| 3521 | /// ::= .irpc symbol,values |
| 3522 | bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) { |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3523 | MacroParameters Parameters; |
| 3524 | MacroParameter Parameter; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3525 | |
Preston Gurd | 6c9176a | 2012-09-19 20:29:04 +0000 | [diff] [blame] | 3526 | if (ParseIdentifier(Parameter.first)) |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3527 | return TokError("expected identifier in '.irpc' directive"); |
| 3528 | |
| 3529 | Parameters.push_back(Parameter); |
| 3530 | |
| 3531 | if (Lexer.isNot(AsmToken::Comma)) |
| 3532 | return TokError("expected comma in '.irpc' directive"); |
| 3533 | |
| 3534 | Lex(); |
| 3535 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3536 | MacroArguments A; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3537 | if (ParseMacroArguments(0, A)) |
| 3538 | return true; |
| 3539 | |
| 3540 | if (A.size() != 1 || A.front().size() != 1) |
| 3541 | return TokError("unexpected token in '.irpc' directive"); |
| 3542 | |
| 3543 | // Eat the end of statement. |
| 3544 | Lex(); |
| 3545 | |
| 3546 | // Lex the irpc definition. |
| 3547 | Macro *M = ParseMacroLikeBody(DirectiveLoc); |
| 3548 | if (!M) |
| 3549 | return true; |
| 3550 | |
| 3551 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
| 3552 | // to hold the macro body with substitutions. |
| 3553 | SmallString<256> Buf; |
| 3554 | raw_svector_ostream OS(Buf); |
| 3555 | |
| 3556 | StringRef Values = A.front().front().getString(); |
| 3557 | std::size_t I, End = Values.size(); |
| 3558 | for (I = 0; I < End; ++I) { |
| 3559 | MacroArgument Arg; |
| 3560 | Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1))); |
| 3561 | |
Rafael Espindola | 8a403d3 | 2012-08-08 14:51:03 +0000 | [diff] [blame] | 3562 | MacroArguments Args; |
Rafael Espindola | fc9216e | 2012-06-16 18:03:25 +0000 | [diff] [blame] | 3563 | Args.push_back(Arg); |
| 3564 | |
| 3565 | if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) |
| 3566 | return true; |
| 3567 | } |
| 3568 | |
| 3569 | InstantiateMacroLikeBody(M, DirectiveLoc, OS); |
| 3570 | |
| 3571 | return false; |
| 3572 | } |
| 3573 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3574 | bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) { |
| 3575 | if (ActiveMacros.empty()) |
Preston Gurd | 6579eea | 2012-09-19 20:23:43 +0000 | [diff] [blame] | 3576 | return TokError("unmatched '.endr' directive"); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3577 | |
| 3578 | // The only .repl that should get here are the ones created by |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3579 | // InstantiateMacroLikeBody. |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3580 | assert(getLexer().is(AsmToken::EndOfStatement)); |
| 3581 | |
Rafael Espindola | 761cb06 | 2012-06-03 23:57:14 +0000 | [diff] [blame] | 3582 | HandleMacroExit(); |
Rafael Espindola | 2ec304c | 2012-05-12 16:31:10 +0000 | [diff] [blame] | 3583 | return false; |
| 3584 | } |
Rafael Espindola | b98ac2a | 2010-09-11 16:45:15 +0000 | [diff] [blame] | 3585 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3586 | namespace { |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3587 | enum AsmRewriteKind { |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3588 | AOK_Imm, |
| 3589 | AOK_Input, |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3590 | AOK_Output, |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3591 | AOK_SizeDirective, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3592 | AOK_Emit, |
| 3593 | AOK_Skip |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3594 | }; |
| 3595 | |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3596 | struct AsmRewrite { |
| 3597 | AsmRewriteKind Kind; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3598 | SMLoc Loc; |
| 3599 | unsigned Len; |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3600 | unsigned Size; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3601 | public: |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3602 | AsmRewrite(AsmRewriteKind kind, SMLoc loc, unsigned len, unsigned size = 0) |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3603 | : Kind(kind), Loc(loc), Len(len), Size(size) { } |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3604 | }; |
| 3605 | } |
| 3606 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3607 | bool AsmParser::ParseDirectiveEmit(SMLoc IDLoc, ParseStatementInfo &Info) { |
| 3608 | const MCExpr *Value; |
| 3609 | SMLoc ExprLoc = getLexer().getLoc(); |
| 3610 | if (ParseExpression(Value)) |
| 3611 | return true; |
| 3612 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); |
| 3613 | if (!MCE) |
| 3614 | return Error(ExprLoc, "unexpected expression in _emit"); |
| 3615 | uint64_t IntValue = MCE->getValue(); |
| 3616 | if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) |
| 3617 | return Error(ExprLoc, "literal value out of range for directive"); |
| 3618 | |
| 3619 | Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, 5)); |
| 3620 | return false; |
| 3621 | } |
| 3622 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3623 | bool AsmParser::ParseMSInlineAsm(void *AsmLoc, std::string &AsmString, |
| 3624 | unsigned &NumOutputs, unsigned &NumInputs, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3625 | SmallVectorImpl<std::pair<void *, bool> > &OpDecls, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3626 | SmallVectorImpl<std::string> &Constraints, |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3627 | SmallVectorImpl<std::string> &Clobbers, |
| 3628 | const MCInstrInfo *MII, |
| 3629 | const MCInstPrinter *IP, |
| 3630 | MCAsmParserSemaCallback &SI) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3631 | SmallVector<void *, 4> InputDecls; |
| 3632 | SmallVector<void *, 4> OutputDecls; |
| 3633 | SmallVector<bool, 4> InputDeclsOffsetOf; |
| 3634 | SmallVector<bool, 4> OutputDeclsOffsetOf; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3635 | SmallVector<std::string, 4> InputConstraints; |
| 3636 | SmallVector<std::string, 4> OutputConstraints; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3637 | std::set<std::string> ClobberRegs; |
| 3638 | |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3639 | SmallVector<struct AsmRewrite, 4> AsmStrRewrites; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3640 | |
| 3641 | // Prime the lexer. |
| 3642 | Lex(); |
| 3643 | |
| 3644 | // While we have input, parse each statement. |
| 3645 | unsigned InputIdx = 0; |
| 3646 | unsigned OutputIdx = 0; |
| 3647 | while (getLexer().isNot(AsmToken::Eof)) { |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3648 | ParseStatementInfo Info(&AsmStrRewrites); |
| 3649 | if (ParseStatement(Info)) |
Chad Rosier | ab450e4 | 2012-10-19 22:57:33 +0000 | [diff] [blame] | 3650 | return true; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3651 | |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3652 | if (Info.Opcode != ~0U) { |
| 3653 | const MCInstrDesc &Desc = MII->get(Info.Opcode); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3654 | |
| 3655 | // Build the list of clobbers, outputs and inputs. |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3656 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
| 3657 | MCParsedAsmOperand *Operand = Info.ParsedOperands[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3658 | |
| 3659 | // Immediate. |
| 3660 | if (Operand->isImm()) { |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3661 | AsmStrRewrites.push_back(AsmRewrite(AOK_Imm, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3662 | Operand->getStartLoc(), |
| 3663 | Operand->getNameLen())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3664 | continue; |
| 3665 | } |
| 3666 | |
| 3667 | // Register operand. |
Chad Rosier | c0a14b8 | 2012-10-24 17:22:29 +0000 | [diff] [blame] | 3668 | if (Operand->isReg() && !Operand->isOffsetOf()) { |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3669 | unsigned NumDefs = Desc.getNumDefs(); |
| 3670 | // Clobber. |
| 3671 | if (NumDefs && Operand->getMCOperandNum() < NumDefs) { |
| 3672 | std::string Reg; |
| 3673 | raw_string_ostream OS(Reg); |
| 3674 | IP->printRegName(OS, Operand->getReg()); |
| 3675 | ClobberRegs.insert(StringRef(OS.str())); |
| 3676 | } |
| 3677 | continue; |
| 3678 | } |
| 3679 | |
| 3680 | // Expr/Input or Output. |
Chad Rosier | 3298959 | 2012-10-18 20:27:15 +0000 | [diff] [blame] | 3681 | unsigned Size; |
| 3682 | void *OpDecl = SI.LookupInlineAsmIdentifier(Operand->getName(), AsmLoc, |
| 3683 | Size); |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 3684 | if (OpDecl) { |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3685 | bool isOutput = (i == 1) && Desc.mayStore(); |
Chad Rosier | c0a14b8 | 2012-10-24 17:22:29 +0000 | [diff] [blame] | 3686 | if (!Operand->isOffsetOf() && Operand->needSizeDirective()) |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3687 | AsmStrRewrites.push_back(AsmRewrite(AOK_SizeDirective, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3688 | Operand->getStartLoc(), 0, |
| 3689 | Operand->getMemSize())); |
| 3690 | |
| 3691 | // Don't emit the offset directive. |
| 3692 | if (Operand->isOffsetOf()) |
| 3693 | AsmStrRewrites.push_back(AsmRewrite(AOK_Skip, |
| 3694 | Operand->getOffsetOfLoc(), 7)); |
| 3695 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3696 | if (isOutput) { |
| 3697 | std::string Constraint = "="; |
| 3698 | ++InputIdx; |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 3699 | OutputDecls.push_back(OpDecl); |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3700 | OutputDeclsOffsetOf.push_back(Operand->isOffsetOf()); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3701 | Constraint += Operand->getConstraint().str(); |
| 3702 | OutputConstraints.push_back(Constraint); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3703 | AsmStrRewrites.push_back(AsmRewrite(AOK_Output, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3704 | Operand->getStartLoc(), |
| 3705 | Operand->getNameLen())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3706 | } else { |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 3707 | InputDecls.push_back(OpDecl); |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3708 | InputDeclsOffsetOf.push_back(Operand->isOffsetOf()); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3709 | InputConstraints.push_back(Operand->getConstraint().str()); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3710 | AsmStrRewrites.push_back(AsmRewrite(AOK_Input, |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3711 | Operand->getStartLoc(), |
| 3712 | Operand->getNameLen())); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3713 | } |
| 3714 | } |
| 3715 | } |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3716 | } |
| 3717 | } |
| 3718 | |
| 3719 | // Set the number of Outputs and Inputs. |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 3720 | NumOutputs = OutputDecls.size(); |
| 3721 | NumInputs = InputDecls.size(); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3722 | |
| 3723 | // Set the unique clobbers. |
| 3724 | for (std::set<std::string>::iterator I = ClobberRegs.begin(), |
| 3725 | E = ClobberRegs.end(); I != E; ++I) |
| 3726 | Clobbers.push_back(*I); |
| 3727 | |
| 3728 | // Merge the various outputs and inputs. Output are expected first. |
| 3729 | if (NumOutputs || NumInputs) { |
| 3730 | unsigned NumExprs = NumOutputs + NumInputs; |
Chad Rosier | c8dd27e | 2012-10-18 19:39:30 +0000 | [diff] [blame] | 3731 | OpDecls.resize(NumExprs); |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3732 | Constraints.resize(NumExprs); |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3733 | // FIXME: Constraints are hard coded to 'm', but we need an 'r' |
| 3734 | // constraint for offsetof. This needs to be cleaned up! |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3735 | for (unsigned i = 0; i < NumOutputs; ++i) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3736 | OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsOffsetOf[i]); |
| 3737 | Constraints[i] = OutputDeclsOffsetOf[i] ? "=r" : OutputConstraints[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3738 | } |
| 3739 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3740 | OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsOffsetOf[i]); |
| 3741 | Constraints[j] = InputDeclsOffsetOf[i] ? "r" : InputConstraints[i]; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | // Build the IR assembly string. |
| 3746 | std::string AsmStringIR; |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3747 | AsmRewriteKind PrevKind = AOK_Imm; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3748 | raw_string_ostream OS(AsmStringIR); |
| 3749 | const char *Start = SrcMgr.getMemoryBuffer(0)->getBufferStart(); |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3750 | for (SmallVectorImpl<struct AsmRewrite>::iterator |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3751 | I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) { |
| 3752 | const char *Loc = (*I).Loc.getPointer(); |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3753 | |
Chad Rosier | 4e472d2 | 2012-10-20 01:02:45 +0000 | [diff] [blame] | 3754 | AsmRewriteKind Kind = (*I).Kind; |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3755 | |
| 3756 | // Emit everything up to the immediate/expression. If the previous rewrite |
| 3757 | // was a size directive, then this has already been done. |
| 3758 | if (PrevKind != AOK_SizeDirective) |
| 3759 | OS << StringRef(Start, Loc - Start); |
| 3760 | PrevKind = Kind; |
| 3761 | |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3762 | // Skip the original expression. |
| 3763 | if (Kind == AOK_Skip) { |
| 3764 | Start = Loc + (*I).Len; |
| 3765 | continue; |
| 3766 | } |
| 3767 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3768 | // Rewrite expressions in $N notation. |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3769 | switch (Kind) { |
Chad Rosier | 5a719fc | 2012-10-23 17:43:43 +0000 | [diff] [blame] | 3770 | default: break; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3771 | case AOK_Imm: |
| 3772 | OS << Twine("$$") + StringRef(Loc, (*I).Len); |
| 3773 | break; |
| 3774 | case AOK_Input: |
| 3775 | OS << '$'; |
| 3776 | OS << InputIdx++; |
| 3777 | break; |
| 3778 | case AOK_Output: |
| 3779 | OS << '$'; |
| 3780 | OS << OutputIdx++; |
| 3781 | break; |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3782 | case AOK_SizeDirective: |
| 3783 | switch((*I).Size) { |
| 3784 | default: break; |
| 3785 | case 8: OS << "byte ptr "; break; |
| 3786 | case 16: OS << "word ptr "; break; |
| 3787 | case 32: OS << "dword ptr "; break; |
| 3788 | case 64: OS << "qword ptr "; break; |
| 3789 | case 80: OS << "xword ptr "; break; |
| 3790 | case 128: OS << "xmmword ptr "; break; |
| 3791 | case 256: OS << "ymmword ptr "; break; |
| 3792 | } |
Eli Friedman | 2128aae | 2012-10-22 23:58:19 +0000 | [diff] [blame] | 3793 | break; |
| 3794 | case AOK_Emit: |
| 3795 | OS << ".byte"; |
| 3796 | break; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3797 | } |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3798 | |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3799 | // Skip the original expression. |
Chad Rosier | 96d58e6 | 2012-10-19 20:57:14 +0000 | [diff] [blame] | 3800 | if (Kind != AOK_SizeDirective) |
| 3801 | Start = Loc + (*I).Len; |
Chad Rosier | b1f8c13 | 2012-10-18 15:49:34 +0000 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | // Emit the remainder of the asm string. |
| 3805 | const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd(); |
| 3806 | if (Start != AsmEnd) |
| 3807 | OS << StringRef(Start, AsmEnd - Start); |
| 3808 | |
| 3809 | AsmString = OS.str(); |
| 3810 | return false; |
| 3811 | } |
| 3812 | |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 3813 | /// \brief Create an MCAsmParser instance. |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 3814 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 3815 | MCContext &C, MCStreamer &Out, |
| 3816 | const MCAsmInfo &MAI) { |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 3817 | return new AsmParser(SM, C, Out, MAI); |
Daniel Dunbar | d1e3b44 | 2010-07-17 02:26:10 +0000 | [diff] [blame] | 3818 | } |