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