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