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