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