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