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