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