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