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