LLVM API Documentation
00001 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This class implements the parser for assembly files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/ADT/APFloat.h" 00015 #include "llvm/ADT/SmallString.h" 00016 #include "llvm/ADT/STLExtras.h" 00017 #include "llvm/ADT/StringMap.h" 00018 #include "llvm/ADT/Twine.h" 00019 #include "llvm/MC/MCAsmInfo.h" 00020 #include "llvm/MC/MCContext.h" 00021 #include "llvm/MC/MCDwarf.h" 00022 #include "llvm/MC/MCExpr.h" 00023 #include "llvm/MC/MCInstPrinter.h" 00024 #include "llvm/MC/MCInstrInfo.h" 00025 #include "llvm/MC/MCParser/AsmCond.h" 00026 #include "llvm/MC/MCParser/AsmLexer.h" 00027 #include "llvm/MC/MCParser/MCAsmParser.h" 00028 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 00029 #include "llvm/MC/MCRegisterInfo.h" 00030 #include "llvm/MC/MCSectionMachO.h" 00031 #include "llvm/MC/MCStreamer.h" 00032 #include "llvm/MC/MCSymbol.h" 00033 #include "llvm/MC/MCTargetAsmParser.h" 00034 #include "llvm/Support/CommandLine.h" 00035 #include "llvm/Support/ErrorHandling.h" 00036 #include "llvm/Support/MathExtras.h" 00037 #include "llvm/Support/MemoryBuffer.h" 00038 #include "llvm/Support/SourceMgr.h" 00039 #include "llvm/Support/raw_ostream.h" 00040 #include <cctype> 00041 #include <set> 00042 #include <string> 00043 #include <vector> 00044 using namespace llvm; 00045 00046 static cl::opt<bool> 00047 FatalAssemblerWarnings("fatal-assembler-warnings", 00048 cl::desc("Consider warnings as error")); 00049 00050 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() {} 00051 00052 namespace { 00053 00054 /// \brief Helper types for tracking macro definitions. 00055 typedef std::vector<AsmToken> MCAsmMacroArgument; 00056 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; 00057 typedef std::pair<StringRef, MCAsmMacroArgument> MCAsmMacroParameter; 00058 typedef std::vector<MCAsmMacroParameter> MCAsmMacroParameters; 00059 00060 struct MCAsmMacro { 00061 StringRef Name; 00062 StringRef Body; 00063 MCAsmMacroParameters Parameters; 00064 00065 public: 00066 MCAsmMacro(StringRef N, StringRef B, const MCAsmMacroParameters &P) : 00067 Name(N), Body(B), Parameters(P) {} 00068 00069 MCAsmMacro(const MCAsmMacro& Other) 00070 : Name(Other.Name), Body(Other.Body), Parameters(Other.Parameters) {} 00071 }; 00072 00073 /// \brief Helper class for storing information about an active macro 00074 /// instantiation. 00075 struct MacroInstantiation { 00076 /// The macro being instantiated. 00077 const MCAsmMacro *TheMacro; 00078 00079 /// The macro instantiation with substitutions. 00080 MemoryBuffer *Instantiation; 00081 00082 /// The location of the instantiation. 00083 SMLoc InstantiationLoc; 00084 00085 /// The buffer where parsing should resume upon instantiation completion. 00086 int ExitBuffer; 00087 00088 /// The location where parsing should resume upon instantiation completion. 00089 SMLoc ExitLoc; 00090 00091 public: 00092 MacroInstantiation(const MCAsmMacro *M, SMLoc IL, int EB, SMLoc EL, 00093 MemoryBuffer *I); 00094 }; 00095 00096 struct ParseStatementInfo { 00097 /// ParsedOperands - The parsed operands from the last parsed statement. 00098 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands; 00099 00100 /// Opcode - The opcode from the last parsed instruction. 00101 unsigned Opcode; 00102 00103 /// Error - Was there an error parsing the inline assembly? 00104 bool ParseError; 00105 00106 SmallVectorImpl<AsmRewrite> *AsmRewrites; 00107 00108 ParseStatementInfo() : Opcode(~0U), ParseError(false), AsmRewrites(0) {} 00109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) 00110 : Opcode(~0), ParseError(false), AsmRewrites(rewrites) {} 00111 00112 ~ParseStatementInfo() { 00113 // Free any parsed operands. 00114 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i) 00115 delete ParsedOperands[i]; 00116 ParsedOperands.clear(); 00117 } 00118 }; 00119 00120 /// \brief The concrete assembly parser instance. 00121 class AsmParser : public MCAsmParser { 00122 AsmParser(const AsmParser &) LLVM_DELETED_FUNCTION; 00123 void operator=(const AsmParser &) LLVM_DELETED_FUNCTION; 00124 private: 00125 AsmLexer Lexer; 00126 MCContext &Ctx; 00127 MCStreamer &Out; 00128 const MCAsmInfo &MAI; 00129 SourceMgr &SrcMgr; 00130 SourceMgr::DiagHandlerTy SavedDiagHandler; 00131 void *SavedDiagContext; 00132 MCAsmParserExtension *PlatformParser; 00133 00134 /// This is the current buffer index we're lexing from as managed by the 00135 /// SourceMgr object. 00136 int CurBuffer; 00137 00138 AsmCond TheCondState; 00139 std::vector<AsmCond> TheCondStack; 00140 00141 /// ExtensionDirectiveMap - maps directive names to handler methods in parser 00142 /// extensions. Extensions register themselves in this map by calling 00143 /// addDirectiveHandler. 00144 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; 00145 00146 /// MacroMap - Map of currently defined macros. 00147 StringMap<MCAsmMacro*> MacroMap; 00148 00149 /// ActiveMacros - Stack of active macro instantiations. 00150 std::vector<MacroInstantiation*> ActiveMacros; 00151 00152 /// Boolean tracking whether macro substitution is enabled. 00153 unsigned MacrosEnabledFlag : 1; 00154 00155 /// Flag tracking whether any errors have been encountered. 00156 unsigned HadError : 1; 00157 00158 /// The values from the last parsed cpp hash file line comment if any. 00159 StringRef CppHashFilename; 00160 int64_t CppHashLineNumber; 00161 SMLoc CppHashLoc; 00162 int CppHashBuf; 00163 00164 /// AssemblerDialect. ~OU means unset value and use value provided by MAI. 00165 unsigned AssemblerDialect; 00166 00167 /// IsDarwin - is Darwin compatibility enabled? 00168 bool IsDarwin; 00169 00170 /// ParsingInlineAsm - Are we parsing ms-style inline assembly? 00171 bool ParsingInlineAsm; 00172 00173 public: 00174 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, 00175 const MCAsmInfo &MAI); 00176 virtual ~AsmParser(); 00177 00178 virtual bool Run(bool NoInitialTextSection, bool NoFinalize = false); 00179 00180 virtual void addDirectiveHandler(StringRef Directive, 00181 ExtensionDirectiveHandler Handler) { 00182 ExtensionDirectiveMap[Directive] = Handler; 00183 } 00184 00185 public: 00186 /// @name MCAsmParser Interface 00187 /// { 00188 00189 virtual SourceMgr &getSourceManager() { return SrcMgr; } 00190 virtual MCAsmLexer &getLexer() { return Lexer; } 00191 virtual MCContext &getContext() { return Ctx; } 00192 virtual MCStreamer &getStreamer() { return Out; } 00193 virtual unsigned getAssemblerDialect() { 00194 if (AssemblerDialect == ~0U) 00195 return MAI.getAssemblerDialect(); 00196 else 00197 return AssemblerDialect; 00198 } 00199 virtual void setAssemblerDialect(unsigned i) { 00200 AssemblerDialect = i; 00201 } 00202 00203 virtual bool Warning(SMLoc L, const Twine &Msg, 00204 ArrayRef<SMRange> Ranges = None); 00205 virtual bool Error(SMLoc L, const Twine &Msg, 00206 ArrayRef<SMRange> Ranges = None); 00207 00208 virtual const AsmToken &Lex(); 00209 00210 void setParsingInlineAsm(bool V) { ParsingInlineAsm = V; } 00211 bool isParsingInlineAsm() { return ParsingInlineAsm; } 00212 00213 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, 00214 unsigned &NumOutputs, unsigned &NumInputs, 00215 SmallVectorImpl<std::pair<void *,bool> > &OpDecls, 00216 SmallVectorImpl<std::string> &Constraints, 00217 SmallVectorImpl<std::string> &Clobbers, 00218 const MCInstrInfo *MII, 00219 const MCInstPrinter *IP, 00220 MCAsmParserSemaCallback &SI); 00221 00222 bool parseExpression(const MCExpr *&Res); 00223 virtual bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc); 00224 virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); 00225 virtual bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc); 00226 virtual bool parseAbsoluteExpression(int64_t &Res); 00227 00228 /// parseIdentifier - Parse an identifier or string (as a quoted identifier) 00229 /// and set \p Res to the identifier contents. 00230 virtual bool parseIdentifier(StringRef &Res); 00231 virtual void eatToEndOfStatement(); 00232 00233 virtual void checkForValidSection(); 00234 /// } 00235 00236 private: 00237 00238 bool ParseStatement(ParseStatementInfo &Info); 00239 void EatToEndOfLine(); 00240 bool ParseCppHashLineFilenameComment(const SMLoc &L); 00241 00242 void CheckForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, 00243 MCAsmMacroParameters Parameters); 00244 bool expandMacro(raw_svector_ostream &OS, StringRef Body, 00245 const MCAsmMacroParameters &Parameters, 00246 const MCAsmMacroArguments &A, 00247 const SMLoc &L); 00248 00249 /// \brief Are macros enabled in the parser? 00250 bool MacrosEnabled() {return MacrosEnabledFlag;} 00251 00252 /// \brief Control a flag in the parser that enables or disables macros. 00253 void SetMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} 00254 00255 /// \brief Lookup a previously defined macro. 00256 /// \param Name Macro name. 00257 /// \returns Pointer to macro. NULL if no such macro was defined. 00258 const MCAsmMacro* LookupMacro(StringRef Name); 00259 00260 /// \brief Define a new macro with the given name and information. 00261 void DefineMacro(StringRef Name, const MCAsmMacro& Macro); 00262 00263 /// \brief Undefine a macro. If no such macro was defined, it's a no-op. 00264 void UndefineMacro(StringRef Name); 00265 00266 /// \brief Are we inside a macro instantiation? 00267 bool InsideMacroInstantiation() {return !ActiveMacros.empty();} 00268 00269 /// \brief Handle entry to macro instantiation. 00270 /// 00271 /// \param M The macro. 00272 /// \param NameLoc Instantiation location. 00273 bool HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); 00274 00275 /// \brief Handle exit from macro instantiation. 00276 void HandleMacroExit(); 00277 00278 /// \brief Extract AsmTokens for a macro argument. If the argument delimiter 00279 /// is initially unknown, set it to AsmToken::Eof. It will be set to the 00280 /// correct delimiter by the method. 00281 bool ParseMacroArgument(MCAsmMacroArgument &MA, 00282 AsmToken::TokenKind &ArgumentDelimiter); 00283 00284 /// \brief Parse all macro arguments for a given macro. 00285 bool ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); 00286 00287 void PrintMacroInstantiations(); 00288 void PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, 00289 ArrayRef<SMRange> Ranges = None) const { 00290 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); 00291 } 00292 static void DiagHandler(const SMDiagnostic &Diag, void *Context); 00293 00294 /// EnterIncludeFile - Enter the specified file. This returns true on failure. 00295 bool EnterIncludeFile(const std::string &Filename); 00296 /// ProcessIncbinFile - Process the specified file for the .incbin directive. 00297 /// This returns true on failure. 00298 bool ProcessIncbinFile(const std::string &Filename); 00299 00300 /// \brief Reset the current lexer position to that given by \p Loc. The 00301 /// current token is not set; clients should ensure Lex() is called 00302 /// subsequently. 00303 /// 00304 /// \param InBuffer If not -1, should be the known buffer id that contains the 00305 /// location. 00306 void JumpToLoc(SMLoc Loc, int InBuffer=-1); 00307 00308 /// \brief Parse up to the end of statement and a return the contents from the 00309 /// current token until the end of the statement; the current token on exit 00310 /// will be either the EndOfStatement or EOF. 00311 virtual StringRef parseStringToEndOfStatement(); 00312 00313 /// \brief Parse until the end of a statement or a comma is encountered, 00314 /// return the contents from the current token up to the end or comma. 00315 StringRef ParseStringToComma(); 00316 00317 bool ParseAssignment(StringRef Name, bool allow_redef, 00318 bool NoDeadStrip = false); 00319 00320 bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc); 00321 bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); 00322 bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); 00323 bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); 00324 00325 bool ParseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); 00326 00327 // Generic (target and platform independent) directive parsing. 00328 enum DirectiveKind { 00329 DK_NO_DIRECTIVE, // Placeholder 00330 DK_SET, DK_EQU, DK_EQUIV, DK_ASCII, DK_ASCIZ, DK_STRING, DK_BYTE, DK_SHORT, 00331 DK_VALUE, DK_2BYTE, DK_LONG, DK_INT, DK_4BYTE, DK_QUAD, DK_8BYTE, DK_SINGLE, 00332 DK_FLOAT, DK_DOUBLE, DK_ALIGN, DK_ALIGN32, DK_BALIGN, DK_BALIGNW, 00333 DK_BALIGNL, DK_P2ALIGN, DK_P2ALIGNW, DK_P2ALIGNL, DK_ORG, DK_FILL, DK_ENDR, 00334 DK_BUNDLE_ALIGN_MODE, DK_BUNDLE_LOCK, DK_BUNDLE_UNLOCK, 00335 DK_ZERO, DK_EXTERN, DK_GLOBL, DK_GLOBAL, DK_INDIRECT_SYMBOL, 00336 DK_LAZY_REFERENCE, DK_NO_DEAD_STRIP, DK_SYMBOL_RESOLVER, DK_PRIVATE_EXTERN, 00337 DK_REFERENCE, DK_WEAK_DEFINITION, DK_WEAK_REFERENCE, 00338 DK_WEAK_DEF_CAN_BE_HIDDEN, DK_COMM, DK_COMMON, DK_LCOMM, DK_ABORT, 00339 DK_INCLUDE, DK_INCBIN, DK_CODE16, DK_CODE16GCC, DK_REPT, DK_IRP, DK_IRPC, 00340 DK_IF, DK_IFB, DK_IFNB, DK_IFC, DK_IFNC, DK_IFDEF, DK_IFNDEF, DK_IFNOTDEF, 00341 DK_ELSEIF, DK_ELSE, DK_ENDIF, 00342 DK_SPACE, DK_SKIP, DK_FILE, DK_LINE, DK_LOC, DK_STABS, 00343 DK_CFI_SECTIONS, DK_CFI_STARTPROC, DK_CFI_ENDPROC, DK_CFI_DEF_CFA, 00344 DK_CFI_DEF_CFA_OFFSET, DK_CFI_ADJUST_CFA_OFFSET, DK_CFI_DEF_CFA_REGISTER, 00345 DK_CFI_OFFSET, DK_CFI_REL_OFFSET, DK_CFI_PERSONALITY, DK_CFI_LSDA, 00346 DK_CFI_REMEMBER_STATE, DK_CFI_RESTORE_STATE, DK_CFI_SAME_VALUE, 00347 DK_CFI_RESTORE, DK_CFI_ESCAPE, DK_CFI_SIGNAL_FRAME, DK_CFI_UNDEFINED, 00348 DK_CFI_REGISTER, 00349 DK_MACROS_ON, DK_MACROS_OFF, DK_MACRO, DK_ENDM, DK_ENDMACRO, DK_PURGEM, 00350 DK_SLEB128, DK_ULEB128 00351 }; 00352 00353 /// DirectiveKindMap - Maps directive name --> DirectiveKind enum, for 00354 /// directives parsed by this class. 00355 StringMap<DirectiveKind> DirectiveKindMap; 00356 00357 // ".ascii", ".asciz", ".string" 00358 bool ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); 00359 bool ParseDirectiveValue(unsigned Size); // ".byte", ".long", ... 00360 bool ParseDirectiveRealValue(const fltSemantics &); // ".single", ... 00361 bool ParseDirectiveFill(); // ".fill" 00362 bool ParseDirectiveZero(); // ".zero" 00363 // ".set", ".equ", ".equiv" 00364 bool ParseDirectiveSet(StringRef IDVal, bool allow_redef); 00365 bool ParseDirectiveOrg(); // ".org" 00366 // ".align{,32}", ".p2align{,w,l}" 00367 bool ParseDirectiveAlign(bool IsPow2, unsigned ValueSize); 00368 00369 // ".file", ".line", ".loc", ".stabs" 00370 bool ParseDirectiveFile(SMLoc DirectiveLoc); 00371 bool ParseDirectiveLine(); 00372 bool ParseDirectiveLoc(); 00373 bool ParseDirectiveStabs(); 00374 00375 // .cfi directives 00376 bool ParseDirectiveCFIRegister(SMLoc DirectiveLoc); 00377 bool ParseDirectiveCFISections(); 00378 bool ParseDirectiveCFIStartProc(); 00379 bool ParseDirectiveCFIEndProc(); 00380 bool ParseDirectiveCFIDefCfaOffset(); 00381 bool ParseDirectiveCFIDefCfa(SMLoc DirectiveLoc); 00382 bool ParseDirectiveCFIAdjustCfaOffset(); 00383 bool ParseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); 00384 bool ParseDirectiveCFIOffset(SMLoc DirectiveLoc); 00385 bool ParseDirectiveCFIRelOffset(SMLoc DirectiveLoc); 00386 bool ParseDirectiveCFIPersonalityOrLsda(bool IsPersonality); 00387 bool ParseDirectiveCFIRememberState(); 00388 bool ParseDirectiveCFIRestoreState(); 00389 bool ParseDirectiveCFISameValue(SMLoc DirectiveLoc); 00390 bool ParseDirectiveCFIRestore(SMLoc DirectiveLoc); 00391 bool ParseDirectiveCFIEscape(); 00392 bool ParseDirectiveCFISignalFrame(); 00393 bool ParseDirectiveCFIUndefined(SMLoc DirectiveLoc); 00394 00395 // macro directives 00396 bool ParseDirectivePurgeMacro(SMLoc DirectiveLoc); 00397 bool ParseDirectiveEndMacro(StringRef Directive); 00398 bool ParseDirectiveMacro(SMLoc DirectiveLoc); 00399 bool ParseDirectiveMacrosOnOff(StringRef Directive); 00400 00401 // ".bundle_align_mode" 00402 bool ParseDirectiveBundleAlignMode(); 00403 // ".bundle_lock" 00404 bool ParseDirectiveBundleLock(); 00405 // ".bundle_unlock" 00406 bool ParseDirectiveBundleUnlock(); 00407 00408 // ".space", ".skip" 00409 bool ParseDirectiveSpace(StringRef IDVal); 00410 00411 // .sleb128 (Signed=true) and .uleb128 (Signed=false) 00412 bool ParseDirectiveLEB128(bool Signed); 00413 00414 /// ParseDirectiveSymbolAttribute - Parse a directive like ".globl" which 00415 /// accepts a single symbol (which should be a label or an external). 00416 bool ParseDirectiveSymbolAttribute(MCSymbolAttr Attr); 00417 00418 bool ParseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" 00419 00420 bool ParseDirectiveAbort(); // ".abort" 00421 bool ParseDirectiveInclude(); // ".include" 00422 bool ParseDirectiveIncbin(); // ".incbin" 00423 00424 bool ParseDirectiveIf(SMLoc DirectiveLoc); // ".if" 00425 // ".ifb" or ".ifnb", depending on ExpectBlank. 00426 bool ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); 00427 // ".ifc" or ".ifnc", depending on ExpectEqual. 00428 bool ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); 00429 // ".ifdef" or ".ifndef", depending on expect_defined 00430 bool ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); 00431 bool ParseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" 00432 bool ParseDirectiveElse(SMLoc DirectiveLoc); // ".else" 00433 bool ParseDirectiveEndIf(SMLoc DirectiveLoc); // .endif 00434 virtual bool parseEscapedString(std::string &Data); 00435 00436 const MCExpr *ApplyModifierToExpr(const MCExpr *E, 00437 MCSymbolRefExpr::VariantKind Variant); 00438 00439 // Macro-like directives 00440 MCAsmMacro *ParseMacroLikeBody(SMLoc DirectiveLoc); 00441 void InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, 00442 raw_svector_ostream &OS); 00443 bool ParseDirectiveRept(SMLoc DirectiveLoc); // ".rept" 00444 bool ParseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" 00445 bool ParseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" 00446 bool ParseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" 00447 00448 // "_emit" or "__emit" 00449 bool ParseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, 00450 size_t Len); 00451 00452 // "align" 00453 bool ParseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); 00454 00455 void initializeDirectiveKindMap(); 00456 }; 00457 } 00458 00459 namespace llvm { 00460 00461 extern MCAsmParserExtension *createDarwinAsmParser(); 00462 extern MCAsmParserExtension *createELFAsmParser(); 00463 extern MCAsmParserExtension *createCOFFAsmParser(); 00464 00465 } 00466 00467 enum { DEFAULT_ADDRSPACE = 0 }; 00468 00469 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, 00470 MCStreamer &_Out, const MCAsmInfo &_MAI) 00471 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), MAI(_MAI), SrcMgr(_SM), 00472 PlatformParser(0), 00473 CurBuffer(0), MacrosEnabledFlag(true), CppHashLineNumber(0), 00474 AssemblerDialect(~0U), IsDarwin(false), ParsingInlineAsm(false) { 00475 // Save the old handler. 00476 SavedDiagHandler = SrcMgr.getDiagHandler(); 00477 SavedDiagContext = SrcMgr.getDiagContext(); 00478 // Set our own handler which calls the saved handler. 00479 SrcMgr.setDiagHandler(DiagHandler, this); 00480 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); 00481 00482 // Initialize the platform / file format parser. 00483 // 00484 // FIXME: This is a hack, we need to (majorly) cleanup how these objects are 00485 // created. 00486 if (_MAI.hasMicrosoftFastStdCallMangling()) { 00487 PlatformParser = createCOFFAsmParser(); 00488 PlatformParser->Initialize(*this); 00489 } else if (_MAI.hasSubsectionsViaSymbols()) { 00490 PlatformParser = createDarwinAsmParser(); 00491 PlatformParser->Initialize(*this); 00492 IsDarwin = true; 00493 } else { 00494 PlatformParser = createELFAsmParser(); 00495 PlatformParser->Initialize(*this); 00496 } 00497 00498 initializeDirectiveKindMap(); 00499 } 00500 00501 AsmParser::~AsmParser() { 00502 assert(ActiveMacros.empty() && "Unexpected active macro instantiation!"); 00503 00504 // Destroy any macros. 00505 for (StringMap<MCAsmMacro*>::iterator it = MacroMap.begin(), 00506 ie = MacroMap.end(); it != ie; ++it) 00507 delete it->getValue(); 00508 00509 delete PlatformParser; 00510 } 00511 00512 void AsmParser::PrintMacroInstantiations() { 00513 // Print the active macro instantiation stack. 00514 for (std::vector<MacroInstantiation*>::const_reverse_iterator 00515 it = ActiveMacros.rbegin(), ie = ActiveMacros.rend(); it != ie; ++it) 00516 PrintMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, 00517 "while in macro instantiation"); 00518 } 00519 00520 bool AsmParser::Warning(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { 00521 if (FatalAssemblerWarnings) 00522 return Error(L, Msg, Ranges); 00523 PrintMessage(L, SourceMgr::DK_Warning, Msg, Ranges); 00524 PrintMacroInstantiations(); 00525 return false; 00526 } 00527 00528 bool AsmParser::Error(SMLoc L, const Twine &Msg, ArrayRef<SMRange> Ranges) { 00529 HadError = true; 00530 PrintMessage(L, SourceMgr::DK_Error, Msg, Ranges); 00531 PrintMacroInstantiations(); 00532 return true; 00533 } 00534 00535 bool AsmParser::EnterIncludeFile(const std::string &Filename) { 00536 std::string IncludedFile; 00537 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); 00538 if (NewBuf == -1) 00539 return true; 00540 00541 CurBuffer = NewBuf; 00542 00543 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); 00544 00545 return false; 00546 } 00547 00548 /// Process the specified .incbin file by searching for it in the include paths 00549 /// then just emitting the byte contents of the file to the streamer. This 00550 /// returns true on failure. 00551 bool AsmParser::ProcessIncbinFile(const std::string &Filename) { 00552 std::string IncludedFile; 00553 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); 00554 if (NewBuf == -1) 00555 return true; 00556 00557 // Pick up the bytes from the file and emit them. 00558 getStreamer().EmitBytes(SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(), 00559 DEFAULT_ADDRSPACE); 00560 return false; 00561 } 00562 00563 void AsmParser::JumpToLoc(SMLoc Loc, int InBuffer) { 00564 if (InBuffer != -1) { 00565 CurBuffer = InBuffer; 00566 } else { 00567 CurBuffer = SrcMgr.FindBufferContainingLoc(Loc); 00568 } 00569 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer), Loc.getPointer()); 00570 } 00571 00572 const AsmToken &AsmParser::Lex() { 00573 const AsmToken *tok = &Lexer.Lex(); 00574 00575 if (tok->is(AsmToken::Eof)) { 00576 // If this is the end of an included file, pop the parent file off the 00577 // include stack. 00578 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); 00579 if (ParentIncludeLoc != SMLoc()) { 00580 JumpToLoc(ParentIncludeLoc); 00581 tok = &Lexer.Lex(); 00582 } 00583 } 00584 00585 if (tok->is(AsmToken::Error)) 00586 Error(Lexer.getErrLoc(), Lexer.getErr()); 00587 00588 return *tok; 00589 } 00590 00591 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { 00592 // Create the initial section, if requested. 00593 if (!NoInitialTextSection) 00594 Out.InitSections(); 00595 00596 // Prime the lexer. 00597 Lex(); 00598 00599 HadError = false; 00600 AsmCond StartingCondState = TheCondState; 00601 00602 // If we are generating dwarf for assembly source files save the initial text 00603 // section and generate a .file directive. 00604 if (getContext().getGenDwarfForAssembly()) { 00605 getContext().setGenDwarfSection(getStreamer().getCurrentSection().first); 00606 MCSymbol *SectionStartSym = getContext().CreateTempSymbol(); 00607 getStreamer().EmitLabel(SectionStartSym); 00608 getContext().setGenDwarfSectionStartSym(SectionStartSym); 00609 getStreamer().EmitDwarfFileDirective(getContext().nextGenDwarfFileNumber(), 00610 StringRef(), 00611 getContext().getMainFileName()); 00612 } 00613 00614 // While we have input, parse each statement. 00615 while (Lexer.isNot(AsmToken::Eof)) { 00616 ParseStatementInfo Info; 00617 if (!ParseStatement(Info)) continue; 00618 00619 // We had an error, validate that one was emitted and recover by skipping to 00620 // the next line. 00621 assert(HadError && "Parse statement returned an error, but none emitted!"); 00622 eatToEndOfStatement(); 00623 } 00624 00625 if (TheCondState.TheCond != StartingCondState.TheCond || 00626 TheCondState.Ignore != StartingCondState.Ignore) 00627 return TokError("unmatched .ifs or .elses"); 00628 00629 // Check to see there are no empty DwarfFile slots. 00630 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles = 00631 getContext().getMCDwarfFiles(); 00632 for (unsigned i = 1; i < MCDwarfFiles.size(); i++) { 00633 if (!MCDwarfFiles[i]) 00634 TokError("unassigned file number: " + Twine(i) + " for .file directives"); 00635 } 00636 00637 // Check to see that all assembler local symbols were actually defined. 00638 // Targets that don't do subsections via symbols may not want this, though, 00639 // so conservatively exclude them. Only do this if we're finalizing, though, 00640 // as otherwise we won't necessarilly have seen everything yet. 00641 if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) { 00642 const MCContext::SymbolTable &Symbols = getContext().getSymbols(); 00643 for (MCContext::SymbolTable::const_iterator i = Symbols.begin(), 00644 e = Symbols.end(); 00645 i != e; ++i) { 00646 MCSymbol *Sym = i->getValue(); 00647 // Variable symbols may not be marked as defined, so check those 00648 // explicitly. If we know it's a variable, we have a definition for 00649 // the purposes of this check. 00650 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) 00651 // FIXME: We would really like to refer back to where the symbol was 00652 // first referenced for a source location. We need to add something 00653 // to track that. Currently, we just point to the end of the file. 00654 PrintMessage(getLexer().getLoc(), SourceMgr::DK_Error, 00655 "assembler local symbol '" + Sym->getName() + 00656 "' not defined"); 00657 } 00658 } 00659 00660 00661 // Finalize the output stream if there are no errors and if the client wants 00662 // us to. 00663 if (!HadError && !NoFinalize) 00664 Out.Finish(); 00665 00666 return HadError; 00667 } 00668 00669 void AsmParser::checkForValidSection() { 00670 if (!ParsingInlineAsm && !getStreamer().getCurrentSection().first) { 00671 TokError("expected section directive before assembly directive"); 00672 Out.InitToTextSection(); 00673 } 00674 } 00675 00676 /// eatToEndOfStatement - Throw away the rest of the line for testing purposes. 00677 void AsmParser::eatToEndOfStatement() { 00678 while (Lexer.isNot(AsmToken::EndOfStatement) && 00679 Lexer.isNot(AsmToken::Eof)) 00680 Lex(); 00681 00682 // Eat EOL. 00683 if (Lexer.is(AsmToken::EndOfStatement)) 00684 Lex(); 00685 } 00686 00687 StringRef AsmParser::parseStringToEndOfStatement() { 00688 const char *Start = getTok().getLoc().getPointer(); 00689 00690 while (Lexer.isNot(AsmToken::EndOfStatement) && 00691 Lexer.isNot(AsmToken::Eof)) 00692 Lex(); 00693 00694 const char *End = getTok().getLoc().getPointer(); 00695 return StringRef(Start, End - Start); 00696 } 00697 00698 StringRef AsmParser::ParseStringToComma() { 00699 const char *Start = getTok().getLoc().getPointer(); 00700 00701 while (Lexer.isNot(AsmToken::EndOfStatement) && 00702 Lexer.isNot(AsmToken::Comma) && 00703 Lexer.isNot(AsmToken::Eof)) 00704 Lex(); 00705 00706 const char *End = getTok().getLoc().getPointer(); 00707 return StringRef(Start, End - Start); 00708 } 00709 00710 /// ParseParenExpr - Parse a paren expression and return it. 00711 /// NOTE: This assumes the leading '(' has already been consumed. 00712 /// 00713 /// parenexpr ::= expr) 00714 /// 00715 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { 00716 if (parseExpression(Res)) return true; 00717 if (Lexer.isNot(AsmToken::RParen)) 00718 return TokError("expected ')' in parentheses expression"); 00719 EndLoc = Lexer.getTok().getEndLoc(); 00720 Lex(); 00721 return false; 00722 } 00723 00724 /// ParseBracketExpr - Parse a bracket expression and return it. 00725 /// NOTE: This assumes the leading '[' has already been consumed. 00726 /// 00727 /// bracketexpr ::= expr] 00728 /// 00729 bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { 00730 if (parseExpression(Res)) return true; 00731 if (Lexer.isNot(AsmToken::RBrac)) 00732 return TokError("expected ']' in brackets expression"); 00733 EndLoc = Lexer.getTok().getEndLoc(); 00734 Lex(); 00735 return false; 00736 } 00737 00738 /// ParsePrimaryExpr - Parse a primary expression and return it. 00739 /// primaryexpr ::= (parenexpr 00740 /// primaryexpr ::= symbol 00741 /// primaryexpr ::= number 00742 /// primaryexpr ::= '.' 00743 /// primaryexpr ::= ~,+,- primaryexpr 00744 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { 00745 SMLoc FirstTokenLoc = getLexer().getLoc(); 00746 AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); 00747 switch (FirstTokenKind) { 00748 default: 00749 return TokError("unknown token in expression"); 00750 // If we have an error assume that we've already handled it. 00751 case AsmToken::Error: 00752 return true; 00753 case AsmToken::Exclaim: 00754 Lex(); // Eat the operator. 00755 if (ParsePrimaryExpr(Res, EndLoc)) 00756 return true; 00757 Res = MCUnaryExpr::CreateLNot(Res, getContext()); 00758 return false; 00759 case AsmToken::Dollar: 00760 case AsmToken::String: 00761 case AsmToken::Identifier: { 00762 StringRef Identifier; 00763 if (parseIdentifier(Identifier)) { 00764 if (FirstTokenKind == AsmToken::Dollar) 00765 return Error(FirstTokenLoc, "invalid token in expression"); 00766 return true; 00767 } 00768 00769 EndLoc = SMLoc::getFromPointer(Identifier.end()); 00770 00771 // This is a symbol reference. 00772 std::pair<StringRef, StringRef> Split = Identifier.split('@'); 00773 MCSymbol *Sym = getContext().GetOrCreateSymbol(Split.first); 00774 00775 // Lookup the symbol variant if used. 00776 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 00777 if (Split.first.size() != Identifier.size()) { 00778 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); 00779 if (Variant == MCSymbolRefExpr::VK_Invalid) { 00780 Variant = MCSymbolRefExpr::VK_None; 00781 return TokError("invalid variant '" + Split.second + "'"); 00782 } 00783 } 00784 00785 // If this is an absolute variable reference, substitute it now to preserve 00786 // semantics in the face of reassignment. 00787 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) { 00788 if (Variant) 00789 return Error(EndLoc, "unexpected modifier on variable reference"); 00790 00791 Res = Sym->getVariableValue(); 00792 return false; 00793 } 00794 00795 // Otherwise create a symbol ref. 00796 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext()); 00797 return false; 00798 } 00799 case AsmToken::Integer: { 00800 SMLoc Loc = getTok().getLoc(); 00801 int64_t IntVal = getTok().getIntVal(); 00802 Res = MCConstantExpr::Create(IntVal, getContext()); 00803 EndLoc = Lexer.getTok().getEndLoc(); 00804 Lex(); // Eat token. 00805 // Look for 'b' or 'f' following an Integer as a directional label 00806 if (Lexer.getKind() == AsmToken::Identifier) { 00807 StringRef IDVal = getTok().getString(); 00808 if (IDVal == "f" || IDVal == "b"){ 00809 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal, 00810 IDVal == "f" ? 1 : 0); 00811 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, 00812 getContext()); 00813 if (IDVal == "b" && Sym->isUndefined()) 00814 return Error(Loc, "invalid reference to undefined symbol"); 00815 EndLoc = Lexer.getTok().getEndLoc(); 00816 Lex(); // Eat identifier. 00817 } 00818 } 00819 return false; 00820 } 00821 case AsmToken::Real: { 00822 APFloat RealVal(APFloat::IEEEdouble, getTok().getString()); 00823 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); 00824 Res = MCConstantExpr::Create(IntVal, getContext()); 00825 EndLoc = Lexer.getTok().getEndLoc(); 00826 Lex(); // Eat token. 00827 return false; 00828 } 00829 case AsmToken::Dot: { 00830 // This is a '.' reference, which references the current PC. Emit a 00831 // temporary label to the streamer and refer to it. 00832 MCSymbol *Sym = Ctx.CreateTempSymbol(); 00833 Out.EmitLabel(Sym); 00834 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext()); 00835 EndLoc = Lexer.getTok().getEndLoc(); 00836 Lex(); // Eat identifier. 00837 return false; 00838 } 00839 case AsmToken::LParen: 00840 Lex(); // Eat the '('. 00841 return ParseParenExpr(Res, EndLoc); 00842 case AsmToken::LBrac: 00843 if (!PlatformParser->HasBracketExpressions()) 00844 return TokError("brackets expression not supported on this target"); 00845 Lex(); // Eat the '['. 00846 return ParseBracketExpr(Res, EndLoc); 00847 case AsmToken::Minus: 00848 Lex(); // Eat the operator. 00849 if (ParsePrimaryExpr(Res, EndLoc)) 00850 return true; 00851 Res = MCUnaryExpr::CreateMinus(Res, getContext()); 00852 return false; 00853 case AsmToken::Plus: 00854 Lex(); // Eat the operator. 00855 if (ParsePrimaryExpr(Res, EndLoc)) 00856 return true; 00857 Res = MCUnaryExpr::CreatePlus(Res, getContext()); 00858 return false; 00859 case AsmToken::Tilde: 00860 Lex(); // Eat the operator. 00861 if (ParsePrimaryExpr(Res, EndLoc)) 00862 return true; 00863 Res = MCUnaryExpr::CreateNot(Res, getContext()); 00864 return false; 00865 } 00866 } 00867 00868 bool AsmParser::parseExpression(const MCExpr *&Res) { 00869 SMLoc EndLoc; 00870 return parseExpression(Res, EndLoc); 00871 } 00872 00873 bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { 00874 return ParsePrimaryExpr(Res, EndLoc); 00875 } 00876 00877 const MCExpr * 00878 AsmParser::ApplyModifierToExpr(const MCExpr *E, 00879 MCSymbolRefExpr::VariantKind Variant) { 00880 // Recurse over the given expression, rebuilding it to apply the given variant 00881 // if there is exactly one symbol. 00882 switch (E->getKind()) { 00883 case MCExpr::Target: 00884 case MCExpr::Constant: 00885 return 0; 00886 00887 case MCExpr::SymbolRef: { 00888 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); 00889 00890 if (SRE->getKind() != MCSymbolRefExpr::VK_None) { 00891 TokError("invalid variant on expression '" + 00892 getTok().getIdentifier() + "' (already modified)"); 00893 return E; 00894 } 00895 00896 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext()); 00897 } 00898 00899 case MCExpr::Unary: { 00900 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); 00901 const MCExpr *Sub = ApplyModifierToExpr(UE->getSubExpr(), Variant); 00902 if (!Sub) 00903 return 0; 00904 return MCUnaryExpr::Create(UE->getOpcode(), Sub, getContext()); 00905 } 00906 00907 case MCExpr::Binary: { 00908 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); 00909 const MCExpr *LHS = ApplyModifierToExpr(BE->getLHS(), Variant); 00910 const MCExpr *RHS = ApplyModifierToExpr(BE->getRHS(), Variant); 00911 00912 if (!LHS && !RHS) 00913 return 0; 00914 00915 if (!LHS) LHS = BE->getLHS(); 00916 if (!RHS) RHS = BE->getRHS(); 00917 00918 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext()); 00919 } 00920 } 00921 00922 llvm_unreachable("Invalid expression kind!"); 00923 } 00924 00925 /// parseExpression - Parse an expression and return it. 00926 /// 00927 /// expr ::= expr &&,|| expr -> lowest. 00928 /// expr ::= expr |,^,&,! expr 00929 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr 00930 /// expr ::= expr <<,>> expr 00931 /// expr ::= expr +,- expr 00932 /// expr ::= expr *,/,% expr -> highest. 00933 /// expr ::= primaryexpr 00934 /// 00935 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { 00936 // Parse the expression. 00937 Res = 0; 00938 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc)) 00939 return true; 00940 00941 // As a special case, we support 'a op b @ modifier' by rewriting the 00942 // expression to include the modifier. This is inefficient, but in general we 00943 // expect users to use 'a@modifier op b'. 00944 if (Lexer.getKind() == AsmToken::At) { 00945 Lex(); 00946 00947 if (Lexer.isNot(AsmToken::Identifier)) 00948 return TokError("unexpected symbol modifier following '@'"); 00949 00950 MCSymbolRefExpr::VariantKind Variant = 00951 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); 00952 if (Variant == MCSymbolRefExpr::VK_Invalid) 00953 return TokError("invalid variant '" + getTok().getIdentifier() + "'"); 00954 00955 const MCExpr *ModifiedRes = ApplyModifierToExpr(Res, Variant); 00956 if (!ModifiedRes) { 00957 return TokError("invalid modifier '" + getTok().getIdentifier() + 00958 "' (no symbols present)"); 00959 } 00960 00961 Res = ModifiedRes; 00962 Lex(); 00963 } 00964 00965 // Try to constant fold it up front, if possible. 00966 int64_t Value; 00967 if (Res->EvaluateAsAbsolute(Value)) 00968 Res = MCConstantExpr::Create(Value, getContext()); 00969 00970 return false; 00971 } 00972 00973 bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { 00974 Res = 0; 00975 return ParseParenExpr(Res, EndLoc) || 00976 ParseBinOpRHS(1, Res, EndLoc); 00977 } 00978 00979 bool AsmParser::parseAbsoluteExpression(int64_t &Res) { 00980 const MCExpr *Expr; 00981 00982 SMLoc StartLoc = Lexer.getLoc(); 00983 if (parseExpression(Expr)) 00984 return true; 00985 00986 if (!Expr->EvaluateAsAbsolute(Res)) 00987 return Error(StartLoc, "expected absolute expression"); 00988 00989 return false; 00990 } 00991 00992 static unsigned getBinOpPrecedence(AsmToken::TokenKind K, 00993 MCBinaryExpr::Opcode &Kind) { 00994 switch (K) { 00995 default: 00996 return 0; // not a binop. 00997 00998 // Lowest Precedence: &&, || 00999 case AsmToken::AmpAmp: 01000 Kind = MCBinaryExpr::LAnd; 01001 return 1; 01002 case AsmToken::PipePipe: 01003 Kind = MCBinaryExpr::LOr; 01004 return 1; 01005 01006 01007 // Low Precedence: |, &, ^ 01008 // 01009 // FIXME: gas seems to support '!' as an infix operator? 01010 case AsmToken::Pipe: 01011 Kind = MCBinaryExpr::Or; 01012 return 2; 01013 case AsmToken::Caret: 01014 Kind = MCBinaryExpr::Xor; 01015 return 2; 01016 case AsmToken::Amp: 01017 Kind = MCBinaryExpr::And; 01018 return 2; 01019 01020 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= 01021 case AsmToken::EqualEqual: 01022 Kind = MCBinaryExpr::EQ; 01023 return 3; 01024 case AsmToken::ExclaimEqual: 01025 case AsmToken::LessGreater: 01026 Kind = MCBinaryExpr::NE; 01027 return 3; 01028 case AsmToken::Less: 01029 Kind = MCBinaryExpr::LT; 01030 return 3; 01031 case AsmToken::LessEqual: 01032 Kind = MCBinaryExpr::LTE; 01033 return 3; 01034 case AsmToken::Greater: 01035 Kind = MCBinaryExpr::GT; 01036 return 3; 01037 case AsmToken::GreaterEqual: 01038 Kind = MCBinaryExpr::GTE; 01039 return 3; 01040 01041 // Intermediate Precedence: <<, >> 01042 case AsmToken::LessLess: 01043 Kind = MCBinaryExpr::Shl; 01044 return 4; 01045 case AsmToken::GreaterGreater: 01046 Kind = MCBinaryExpr::Shr; 01047 return 4; 01048 01049 // High Intermediate Precedence: +, - 01050 case AsmToken::Plus: 01051 Kind = MCBinaryExpr::Add; 01052 return 5; 01053 case AsmToken::Minus: 01054 Kind = MCBinaryExpr::Sub; 01055 return 5; 01056 01057 // Highest Precedence: *, /, % 01058 case AsmToken::Star: 01059 Kind = MCBinaryExpr::Mul; 01060 return 6; 01061 case AsmToken::Slash: 01062 Kind = MCBinaryExpr::Div; 01063 return 6; 01064 case AsmToken::Percent: 01065 Kind = MCBinaryExpr::Mod; 01066 return 6; 01067 } 01068 } 01069 01070 01071 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'. 01072 /// Res contains the LHS of the expression on input. 01073 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, 01074 SMLoc &EndLoc) { 01075 while (1) { 01076 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; 01077 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); 01078 01079 // If the next token is lower precedence than we are allowed to eat, return 01080 // successfully with what we ate already. 01081 if (TokPrec < Precedence) 01082 return false; 01083 01084 Lex(); 01085 01086 // Eat the next primary expression. 01087 const MCExpr *RHS; 01088 if (ParsePrimaryExpr(RHS, EndLoc)) return true; 01089 01090 // If BinOp binds less tightly with RHS than the operator after RHS, let 01091 // the pending operator take RHS as its LHS. 01092 MCBinaryExpr::Opcode Dummy; 01093 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); 01094 if (TokPrec < NextTokPrec) { 01095 if (ParseBinOpRHS(TokPrec+1, RHS, EndLoc)) return true; 01096 } 01097 01098 // Merge LHS and RHS according to operator. 01099 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext()); 01100 } 01101 } 01102 01103 /// ParseStatement: 01104 /// ::= EndOfStatement 01105 /// ::= Label* Directive ...Operands... EndOfStatement 01106 /// ::= Label* Identifier OperandList* EndOfStatement 01107 bool AsmParser::ParseStatement(ParseStatementInfo &Info) { 01108 if (Lexer.is(AsmToken::EndOfStatement)) { 01109 Out.AddBlankLine(); 01110 Lex(); 01111 return false; 01112 } 01113 01114 // Statements always start with an identifier or are a full line comment. 01115 AsmToken ID = getTok(); 01116 SMLoc IDLoc = ID.getLoc(); 01117 StringRef IDVal; 01118 int64_t LocalLabelVal = -1; 01119 // A full line comment is a '#' as the first token. 01120 if (Lexer.is(AsmToken::Hash)) 01121 return ParseCppHashLineFilenameComment(IDLoc); 01122 01123 // Allow an integer followed by a ':' as a directional local label. 01124 if (Lexer.is(AsmToken::Integer)) { 01125 LocalLabelVal = getTok().getIntVal(); 01126 if (LocalLabelVal < 0) { 01127 if (!TheCondState.Ignore) 01128 return TokError("unexpected token at start of statement"); 01129 IDVal = ""; 01130 } else { 01131 IDVal = getTok().getString(); 01132 Lex(); // Consume the integer token to be used as an identifier token. 01133 if (Lexer.getKind() != AsmToken::Colon) { 01134 if (!TheCondState.Ignore) 01135 return TokError("unexpected token at start of statement"); 01136 } 01137 } 01138 } else if (Lexer.is(AsmToken::Dot)) { 01139 // Treat '.' as a valid identifier in this context. 01140 Lex(); 01141 IDVal = "."; 01142 } else if (parseIdentifier(IDVal)) { 01143 if (!TheCondState.Ignore) 01144 return TokError("unexpected token at start of statement"); 01145 IDVal = ""; 01146 } 01147 01148 // Handle conditional assembly here before checking for skipping. We 01149 // have to do this so that .endif isn't skipped in a ".if 0" block for 01150 // example. 01151 StringMap<DirectiveKind>::const_iterator DirKindIt = 01152 DirectiveKindMap.find(IDVal); 01153 DirectiveKind DirKind = 01154 (DirKindIt == DirectiveKindMap.end()) ? DK_NO_DIRECTIVE : 01155 DirKindIt->getValue(); 01156 switch (DirKind) { 01157 default: 01158 break; 01159 case DK_IF: 01160 return ParseDirectiveIf(IDLoc); 01161 case DK_IFB: 01162 return ParseDirectiveIfb(IDLoc, true); 01163 case DK_IFNB: 01164 return ParseDirectiveIfb(IDLoc, false); 01165 case DK_IFC: 01166 return ParseDirectiveIfc(IDLoc, true); 01167 case DK_IFNC: 01168 return ParseDirectiveIfc(IDLoc, false); 01169 case DK_IFDEF: 01170 return ParseDirectiveIfdef(IDLoc, true); 01171 case DK_IFNDEF: 01172 case DK_IFNOTDEF: 01173 return ParseDirectiveIfdef(IDLoc, false); 01174 case DK_ELSEIF: 01175 return ParseDirectiveElseIf(IDLoc); 01176 case DK_ELSE: 01177 return ParseDirectiveElse(IDLoc); 01178 case DK_ENDIF: 01179 return ParseDirectiveEndIf(IDLoc); 01180 } 01181 01182 // Ignore the statement if in the middle of inactive conditional 01183 // (e.g. ".if 0"). 01184 if (TheCondState.Ignore) { 01185 eatToEndOfStatement(); 01186 return false; 01187 } 01188 01189 // FIXME: Recurse on local labels? 01190 01191 // See what kind of statement we have. 01192 switch (Lexer.getKind()) { 01193 case AsmToken::Colon: { 01194 checkForValidSection(); 01195 01196 // identifier ':' -> Label. 01197 Lex(); 01198 01199 // Diagnose attempt to use '.' as a label. 01200 if (IDVal == ".") 01201 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); 01202 01203 // Diagnose attempt to use a variable as a label. 01204 // 01205 // FIXME: Diagnostics. Note the location of the definition as a label. 01206 // FIXME: This doesn't diagnose assignment to a symbol which has been 01207 // implicitly marked as external. 01208 MCSymbol *Sym; 01209 if (LocalLabelVal == -1) 01210 Sym = getContext().GetOrCreateSymbol(IDVal); 01211 else 01212 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal); 01213 if (!Sym->isUndefined() || Sym->isVariable()) 01214 return Error(IDLoc, "invalid symbol redefinition"); 01215 01216 // Emit the label. 01217 if (!ParsingInlineAsm) 01218 Out.EmitLabel(Sym); 01219 01220 // If we are generating dwarf for assembly source files then gather the 01221 // info to make a dwarf label entry for this label if needed. 01222 if (getContext().getGenDwarfForAssembly()) 01223 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), 01224 IDLoc); 01225 01226 // Consume any end of statement token, if present, to avoid spurious 01227 // AddBlankLine calls(). 01228 if (Lexer.is(AsmToken::EndOfStatement)) { 01229 Lex(); 01230 if (Lexer.is(AsmToken::Eof)) 01231 return false; 01232 } 01233 01234 return false; 01235 } 01236 01237 case AsmToken::Equal: 01238 // identifier '=' ... -> assignment statement 01239 Lex(); 01240 01241 return ParseAssignment(IDVal, true); 01242 01243 default: // Normal instruction or directive. 01244 break; 01245 } 01246 01247 // If macros are enabled, check to see if this is a macro instantiation. 01248 if (MacrosEnabled()) 01249 if (const MCAsmMacro *M = LookupMacro(IDVal)) { 01250 return HandleMacroEntry(M, IDLoc); 01251 } 01252 01253 // Otherwise, we have a normal instruction or directive. 01254 01255 // Directives start with "." 01256 if (IDVal[0] == '.' && IDVal != ".") { 01257 // There are several entities interested in parsing directives: 01258 // 01259 // 1. The target-specific assembly parser. Some directives are target 01260 // specific or may potentially behave differently on certain targets. 01261 // 2. Asm parser extensions. For example, platform-specific parsers 01262 // (like the ELF parser) register themselves as extensions. 01263 // 3. The generic directive parser implemented by this class. These are 01264 // all the directives that behave in a target and platform independent 01265 // manner, or at least have a default behavior that's shared between 01266 // all targets and platforms. 01267 01268 // First query the target-specific parser. It will return 'true' if it 01269 // isn't interested in this directive. 01270 if (!getTargetParser().ParseDirective(ID)) 01271 return false; 01272 01273 // Next, check the extention directive map to see if any extension has 01274 // registered itself to parse this directive. 01275 std::pair<MCAsmParserExtension*, DirectiveHandler> Handler = 01276 ExtensionDirectiveMap.lookup(IDVal); 01277 if (Handler.first) 01278 return (*Handler.second)(Handler.first, IDVal, IDLoc); 01279 01280 // Finally, if no one else is interested in this directive, it must be 01281 // generic and familiar to this class. 01282 switch (DirKind) { 01283 default: 01284 break; 01285 case DK_SET: 01286 case DK_EQU: 01287 return ParseDirectiveSet(IDVal, true); 01288 case DK_EQUIV: 01289 return ParseDirectiveSet(IDVal, false); 01290 case DK_ASCII: 01291 return ParseDirectiveAscii(IDVal, false); 01292 case DK_ASCIZ: 01293 case DK_STRING: 01294 return ParseDirectiveAscii(IDVal, true); 01295 case DK_BYTE: 01296 return ParseDirectiveValue(1); 01297 case DK_SHORT: 01298 case DK_VALUE: 01299 case DK_2BYTE: 01300 return ParseDirectiveValue(2); 01301 case DK_LONG: 01302 case DK_INT: 01303 case DK_4BYTE: 01304 return ParseDirectiveValue(4); 01305 case DK_QUAD: 01306 case DK_8BYTE: 01307 return ParseDirectiveValue(8); 01308 case DK_SINGLE: 01309 case DK_FLOAT: 01310 return ParseDirectiveRealValue(APFloat::IEEEsingle); 01311 case DK_DOUBLE: 01312 return ParseDirectiveRealValue(APFloat::IEEEdouble); 01313 case DK_ALIGN: { 01314 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes(); 01315 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/1); 01316 } 01317 case DK_ALIGN32: { 01318 bool IsPow2 = !getContext().getAsmInfo().getAlignmentIsInBytes(); 01319 return ParseDirectiveAlign(IsPow2, /*ExprSize=*/4); 01320 } 01321 case DK_BALIGN: 01322 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); 01323 case DK_BALIGNW: 01324 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); 01325 case DK_BALIGNL: 01326 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); 01327 case DK_P2ALIGN: 01328 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); 01329 case DK_P2ALIGNW: 01330 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); 01331 case DK_P2ALIGNL: 01332 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); 01333 case DK_ORG: 01334 return ParseDirectiveOrg(); 01335 case DK_FILL: 01336 return ParseDirectiveFill(); 01337 case DK_ZERO: 01338 return ParseDirectiveZero(); 01339 case DK_EXTERN: 01340 eatToEndOfStatement(); // .extern is the default, ignore it. 01341 return false; 01342 case DK_GLOBL: 01343 case DK_GLOBAL: 01344 return ParseDirectiveSymbolAttribute(MCSA_Global); 01345 case DK_INDIRECT_SYMBOL: 01346 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol); 01347 case DK_LAZY_REFERENCE: 01348 return ParseDirectiveSymbolAttribute(MCSA_LazyReference); 01349 case DK_NO_DEAD_STRIP: 01350 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip); 01351 case DK_SYMBOL_RESOLVER: 01352 return ParseDirectiveSymbolAttribute(MCSA_SymbolResolver); 01353 case DK_PRIVATE_EXTERN: 01354 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern); 01355 case DK_REFERENCE: 01356 return ParseDirectiveSymbolAttribute(MCSA_Reference); 01357 case DK_WEAK_DEFINITION: 01358 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition); 01359 case DK_WEAK_REFERENCE: 01360 return ParseDirectiveSymbolAttribute(MCSA_WeakReference); 01361 case DK_WEAK_DEF_CAN_BE_HIDDEN: 01362 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); 01363 case DK_COMM: 01364 case DK_COMMON: 01365 return ParseDirectiveComm(/*IsLocal=*/false); 01366 case DK_LCOMM: 01367 return ParseDirectiveComm(/*IsLocal=*/true); 01368 case DK_ABORT: 01369 return ParseDirectiveAbort(); 01370 case DK_INCLUDE: 01371 return ParseDirectiveInclude(); 01372 case DK_INCBIN: 01373 return ParseDirectiveIncbin(); 01374 case DK_CODE16: 01375 case DK_CODE16GCC: 01376 return TokError(Twine(IDVal) + " not supported yet"); 01377 case DK_REPT: 01378 return ParseDirectiveRept(IDLoc); 01379 case DK_IRP: 01380 return ParseDirectiveIrp(IDLoc); 01381 case DK_IRPC: 01382 return ParseDirectiveIrpc(IDLoc); 01383 case DK_ENDR: 01384 return ParseDirectiveEndr(IDLoc); 01385 case DK_BUNDLE_ALIGN_MODE: 01386 return ParseDirectiveBundleAlignMode(); 01387 case DK_BUNDLE_LOCK: 01388 return ParseDirectiveBundleLock(); 01389 case DK_BUNDLE_UNLOCK: 01390 return ParseDirectiveBundleUnlock(); 01391 case DK_SLEB128: 01392 return ParseDirectiveLEB128(true); 01393 case DK_ULEB128: 01394 return ParseDirectiveLEB128(false); 01395 case DK_SPACE: 01396 case DK_SKIP: 01397 return ParseDirectiveSpace(IDVal); 01398 case DK_FILE: 01399 return ParseDirectiveFile(IDLoc); 01400 case DK_LINE: 01401 return ParseDirectiveLine(); 01402 case DK_LOC: 01403 return ParseDirectiveLoc(); 01404 case DK_STABS: 01405 return ParseDirectiveStabs(); 01406 case DK_CFI_SECTIONS: 01407 return ParseDirectiveCFISections(); 01408 case DK_CFI_STARTPROC: 01409 return ParseDirectiveCFIStartProc(); 01410 case DK_CFI_ENDPROC: 01411 return ParseDirectiveCFIEndProc(); 01412 case DK_CFI_DEF_CFA: 01413 return ParseDirectiveCFIDefCfa(IDLoc); 01414 case DK_CFI_DEF_CFA_OFFSET: 01415 return ParseDirectiveCFIDefCfaOffset(); 01416 case DK_CFI_ADJUST_CFA_OFFSET: 01417 return ParseDirectiveCFIAdjustCfaOffset(); 01418 case DK_CFI_DEF_CFA_REGISTER: 01419 return ParseDirectiveCFIDefCfaRegister(IDLoc); 01420 case DK_CFI_OFFSET: 01421 return ParseDirectiveCFIOffset(IDLoc); 01422 case DK_CFI_REL_OFFSET: 01423 return ParseDirectiveCFIRelOffset(IDLoc); 01424 case DK_CFI_PERSONALITY: 01425 return ParseDirectiveCFIPersonalityOrLsda(true); 01426 case DK_CFI_LSDA: 01427 return ParseDirectiveCFIPersonalityOrLsda(false); 01428 case DK_CFI_REMEMBER_STATE: 01429 return ParseDirectiveCFIRememberState(); 01430 case DK_CFI_RESTORE_STATE: 01431 return ParseDirectiveCFIRestoreState(); 01432 case DK_CFI_SAME_VALUE: 01433 return ParseDirectiveCFISameValue(IDLoc); 01434 case DK_CFI_RESTORE: 01435 return ParseDirectiveCFIRestore(IDLoc); 01436 case DK_CFI_ESCAPE: 01437 return ParseDirectiveCFIEscape(); 01438 case DK_CFI_SIGNAL_FRAME: 01439 return ParseDirectiveCFISignalFrame(); 01440 case DK_CFI_UNDEFINED: 01441 return ParseDirectiveCFIUndefined(IDLoc); 01442 case DK_CFI_REGISTER: 01443 return ParseDirectiveCFIRegister(IDLoc); 01444 case DK_MACROS_ON: 01445 case DK_MACROS_OFF: 01446 return ParseDirectiveMacrosOnOff(IDVal); 01447 case DK_MACRO: 01448 return ParseDirectiveMacro(IDLoc); 01449 case DK_ENDM: 01450 case DK_ENDMACRO: 01451 return ParseDirectiveEndMacro(IDVal); 01452 case DK_PURGEM: 01453 return ParseDirectivePurgeMacro(IDLoc); 01454 } 01455 01456 return Error(IDLoc, "unknown directive"); 01457 } 01458 01459 // __asm _emit or __asm __emit 01460 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || 01461 IDVal == "_EMIT" || IDVal == "__EMIT")) 01462 return ParseDirectiveMSEmit(IDLoc, Info, IDVal.size()); 01463 01464 // __asm align 01465 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) 01466 return ParseDirectiveMSAlign(IDLoc, Info); 01467 01468 checkForValidSection(); 01469 01470 // Canonicalize the opcode to lower case. 01471 std::string OpcodeStr = IDVal.lower(); 01472 ParseInstructionInfo IInfo(Info.AsmRewrites); 01473 bool HadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, 01474 IDLoc, Info.ParsedOperands); 01475 Info.ParseError = HadError; 01476 01477 // Dump the parsed representation, if requested. 01478 if (getShowParsedOperands()) { 01479 SmallString<256> Str; 01480 raw_svector_ostream OS(Str); 01481 OS << "parsed instruction: ["; 01482 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { 01483 if (i != 0) 01484 OS << ", "; 01485 Info.ParsedOperands[i]->print(OS); 01486 } 01487 OS << "]"; 01488 01489 PrintMessage(IDLoc, SourceMgr::DK_Note, OS.str()); 01490 } 01491 01492 // If we are generating dwarf for assembly source files and the current 01493 // section is the initial text section then generate a .loc directive for 01494 // the instruction. 01495 if (!HadError && getContext().getGenDwarfForAssembly() && 01496 getContext().getGenDwarfSection() == 01497 getStreamer().getCurrentSection().first) { 01498 01499 unsigned Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); 01500 01501 // If we previously parsed a cpp hash file line comment then make sure the 01502 // current Dwarf File is for the CppHashFilename if not then emit the 01503 // Dwarf File table for it and adjust the line number for the .loc. 01504 const SmallVectorImpl<MCDwarfFile *> &MCDwarfFiles = 01505 getContext().getMCDwarfFiles(); 01506 if (CppHashFilename.size() != 0) { 01507 if (MCDwarfFiles[getContext().getGenDwarfFileNumber()]->getName() != 01508 CppHashFilename) 01509 getStreamer().EmitDwarfFileDirective( 01510 getContext().nextGenDwarfFileNumber(), StringRef(), CppHashFilename); 01511 01512 unsigned CppHashLocLineNo = SrcMgr.FindLineNumber(CppHashLoc,CppHashBuf); 01513 Line = CppHashLineNumber - 1 + (Line - CppHashLocLineNo); 01514 } 01515 01516 getStreamer().EmitDwarfLocDirective(getContext().getGenDwarfFileNumber(), 01517 Line, 0, DWARF2_LINE_DEFAULT_IS_STMT ? 01518 DWARF2_FLAG_IS_STMT : 0, 0, 0, 01519 StringRef()); 01520 } 01521 01522 // If parsing succeeded, match the instruction. 01523 if (!HadError) { 01524 unsigned ErrorInfo; 01525 HadError = getTargetParser().MatchAndEmitInstruction(IDLoc, Info.Opcode, 01526 Info.ParsedOperands, 01527 Out, ErrorInfo, 01528 ParsingInlineAsm); 01529 } 01530 01531 // Don't skip the rest of the line, the instruction parser is responsible for 01532 // that. 01533 return false; 01534 } 01535 01536 /// EatToEndOfLine uses the Lexer to eat the characters to the end of the line 01537 /// since they may not be able to be tokenized to get to the end of line token. 01538 void AsmParser::EatToEndOfLine() { 01539 if (!Lexer.is(AsmToken::EndOfStatement)) 01540 Lexer.LexUntilEndOfLine(); 01541 // Eat EOL. 01542 Lex(); 01543 } 01544 01545 /// ParseCppHashLineFilenameComment as this: 01546 /// ::= # number "filename" 01547 /// or just as a full line comment if it doesn't have a number and a string. 01548 bool AsmParser::ParseCppHashLineFilenameComment(const SMLoc &L) { 01549 Lex(); // Eat the hash token. 01550 01551 if (getLexer().isNot(AsmToken::Integer)) { 01552 // Consume the line since in cases it is not a well-formed line directive, 01553 // as if were simply a full line comment. 01554 EatToEndOfLine(); 01555 return false; 01556 } 01557 01558 int64_t LineNumber = getTok().getIntVal(); 01559 Lex(); 01560 01561 if (getLexer().isNot(AsmToken::String)) { 01562 EatToEndOfLine(); 01563 return false; 01564 } 01565 01566 StringRef Filename = getTok().getString(); 01567 // Get rid of the enclosing quotes. 01568 Filename = Filename.substr(1, Filename.size()-2); 01569 01570 // Save the SMLoc, Filename and LineNumber for later use by diagnostics. 01571 CppHashLoc = L; 01572 CppHashFilename = Filename; 01573 CppHashLineNumber = LineNumber; 01574 CppHashBuf = CurBuffer; 01575 01576 // Ignore any trailing characters, they're just comment. 01577 EatToEndOfLine(); 01578 return false; 01579 } 01580 01581 /// DiagHandler - will use the last parsed cpp hash line filename comment 01582 /// for the Filename and LineNo if any in the diagnostic. 01583 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { 01584 const AsmParser *Parser = static_cast<const AsmParser*>(Context); 01585 raw_ostream &OS = errs(); 01586 01587 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); 01588 const SMLoc &DiagLoc = Diag.getLoc(); 01589 int DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); 01590 int CppHashBuf = Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashLoc); 01591 01592 // Like SourceMgr::PrintMessage() we need to print the include stack if any 01593 // before printing the message. 01594 int DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); 01595 if (!Parser->SavedDiagHandler && DiagCurBuffer > 0) { 01596 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); 01597 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); 01598 } 01599 01600 // If we have not parsed a cpp hash line filename comment or the source 01601 // manager changed or buffer changed (like in a nested include) then just 01602 // print the normal diagnostic using its Filename and LineNo. 01603 if (!Parser->CppHashLineNumber || 01604 &DiagSrcMgr != &Parser->SrcMgr || 01605 DiagBuf != CppHashBuf) { 01606 if (Parser->SavedDiagHandler) 01607 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); 01608 else 01609 Diag.print(0, OS); 01610 return; 01611 } 01612 01613 // Use the CppHashFilename and calculate a line number based on the 01614 // CppHashLoc and CppHashLineNumber relative to this Diag's SMLoc for 01615 // the diagnostic. 01616 const std::string Filename = Parser->CppHashFilename; 01617 01618 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); 01619 int CppHashLocLineNo = 01620 Parser->SrcMgr.FindLineNumber(Parser->CppHashLoc, CppHashBuf); 01621 int LineNo = Parser->CppHashLineNumber - 1 + 01622 (DiagLocLineNo - CppHashLocLineNo); 01623 01624 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), 01625 Filename, LineNo, Diag.getColumnNo(), 01626 Diag.getKind(), Diag.getMessage(), 01627 Diag.getLineContents(), Diag.getRanges()); 01628 01629 if (Parser->SavedDiagHandler) 01630 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); 01631 else 01632 NewDiag.print(0, OS); 01633 } 01634 01635 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The 01636 // difference being that that function accepts '@' as part of identifiers and 01637 // we can't do that. AsmLexer.cpp should probably be changed to handle 01638 // '@' as a special case when needed. 01639 static bool isIdentifierChar(char c) { 01640 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || 01641 c == '.'; 01642 } 01643 01644 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, 01645 const MCAsmMacroParameters &Parameters, 01646 const MCAsmMacroArguments &A, 01647 const SMLoc &L) { 01648 unsigned NParameters = Parameters.size(); 01649 if (NParameters != 0 && NParameters != A.size()) 01650 return Error(L, "Wrong number of arguments"); 01651 01652 // A macro without parameters is handled differently on Darwin: 01653 // gas accepts no arguments and does no substitutions 01654 while (!Body.empty()) { 01655 // Scan for the next substitution. 01656 std::size_t End = Body.size(), Pos = 0; 01657 for (; Pos != End; ++Pos) { 01658 // Check for a substitution or escape. 01659 if (!NParameters) { 01660 // This macro has no parameters, look for $0, $1, etc. 01661 if (Body[Pos] != '$' || Pos + 1 == End) 01662 continue; 01663 01664 char Next = Body[Pos + 1]; 01665 if (Next == '$' || Next == 'n' || 01666 isdigit(static_cast<unsigned char>(Next))) 01667 break; 01668 } else { 01669 // This macro has parameters, look for \foo, \bar, etc. 01670 if (Body[Pos] == '\\' && Pos + 1 != End) 01671 break; 01672 } 01673 } 01674 01675 // Add the prefix. 01676 OS << Body.slice(0, Pos); 01677 01678 // Check if we reached the end. 01679 if (Pos == End) 01680 break; 01681 01682 if (!NParameters) { 01683 switch (Body[Pos+1]) { 01684 // $$ => $ 01685 case '$': 01686 OS << '$'; 01687 break; 01688 01689 // $n => number of arguments 01690 case 'n': 01691 OS << A.size(); 01692 break; 01693 01694 // $[0-9] => argument 01695 default: { 01696 // Missing arguments are ignored. 01697 unsigned Index = Body[Pos+1] - '0'; 01698 if (Index >= A.size()) 01699 break; 01700 01701 // Otherwise substitute with the token values, with spaces eliminated. 01702 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), 01703 ie = A[Index].end(); it != ie; ++it) 01704 OS << it->getString(); 01705 break; 01706 } 01707 } 01708 Pos += 2; 01709 } else { 01710 unsigned I = Pos + 1; 01711 while (isIdentifierChar(Body[I]) && I + 1 != End) 01712 ++I; 01713 01714 const char *Begin = Body.data() + Pos +1; 01715 StringRef Argument(Begin, I - (Pos +1)); 01716 unsigned Index = 0; 01717 for (; Index < NParameters; ++Index) 01718 if (Parameters[Index].first == Argument) 01719 break; 01720 01721 if (Index == NParameters) { 01722 if (Body[Pos+1] == '(' && Body[Pos+2] == ')') 01723 Pos += 3; 01724 else { 01725 OS << '\\' << Argument; 01726 Pos = I; 01727 } 01728 } else { 01729 for (MCAsmMacroArgument::const_iterator it = A[Index].begin(), 01730 ie = A[Index].end(); it != ie; ++it) 01731 if (it->getKind() == AsmToken::String) 01732 OS << it->getStringContents(); 01733 else 01734 OS << it->getString(); 01735 01736 Pos += 1 + Argument.size(); 01737 } 01738 } 01739 // Update the scan point. 01740 Body = Body.substr(Pos); 01741 } 01742 01743 return false; 01744 } 01745 01746 MacroInstantiation::MacroInstantiation(const MCAsmMacro *M, SMLoc IL, 01747 int EB, SMLoc EL, 01748 MemoryBuffer *I) 01749 : TheMacro(M), Instantiation(I), InstantiationLoc(IL), ExitBuffer(EB), 01750 ExitLoc(EL) 01751 { 01752 } 01753 01754 static bool IsOperator(AsmToken::TokenKind kind) 01755 { 01756 switch (kind) 01757 { 01758 default: 01759 return false; 01760 case AsmToken::Plus: 01761 case AsmToken::Minus: 01762 case AsmToken::Tilde: 01763 case AsmToken::Slash: 01764 case AsmToken::Star: 01765 case AsmToken::Dot: 01766 case AsmToken::Equal: 01767 case AsmToken::EqualEqual: 01768 case AsmToken::Pipe: 01769 case AsmToken::PipePipe: 01770 case AsmToken::Caret: 01771 case AsmToken::Amp: 01772 case AsmToken::AmpAmp: 01773 case AsmToken::Exclaim: 01774 case AsmToken::ExclaimEqual: 01775 case AsmToken::Percent: 01776 case AsmToken::Less: 01777 case AsmToken::LessEqual: 01778 case AsmToken::LessLess: 01779 case AsmToken::LessGreater: 01780 case AsmToken::Greater: 01781 case AsmToken::GreaterEqual: 01782 case AsmToken::GreaterGreater: 01783 return true; 01784 } 01785 } 01786 01787 bool AsmParser::ParseMacroArgument(MCAsmMacroArgument &MA, 01788 AsmToken::TokenKind &ArgumentDelimiter) { 01789 unsigned ParenLevel = 0; 01790 unsigned AddTokens = 0; 01791 01792 // gas accepts arguments separated by whitespace, except on Darwin 01793 if (!IsDarwin) 01794 Lexer.setSkipSpace(false); 01795 01796 for (;;) { 01797 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) { 01798 Lexer.setSkipSpace(true); 01799 return TokError("unexpected token in macro instantiation"); 01800 } 01801 01802 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma)) { 01803 // Spaces and commas cannot be mixed to delimit parameters 01804 if (ArgumentDelimiter == AsmToken::Eof) 01805 ArgumentDelimiter = AsmToken::Comma; 01806 else if (ArgumentDelimiter != AsmToken::Comma) { 01807 Lexer.setSkipSpace(true); 01808 return TokError("expected ' ' for macro argument separator"); 01809 } 01810 break; 01811 } 01812 01813 if (Lexer.is(AsmToken::Space)) { 01814 Lex(); // Eat spaces 01815 01816 // Spaces can delimit parameters, but could also be part an expression. 01817 // If the token after a space is an operator, add the token and the next 01818 // one into this argument 01819 if (ArgumentDelimiter == AsmToken::Space || 01820 ArgumentDelimiter == AsmToken::Eof) { 01821 if (IsOperator(Lexer.getKind())) { 01822 // Check to see whether the token is used as an operator, 01823 // or part of an identifier 01824 const char *NextChar = getTok().getEndLoc().getPointer(); 01825 if (*NextChar == ' ') 01826 AddTokens = 2; 01827 } 01828 01829 if (!AddTokens && ParenLevel == 0) { 01830 if (ArgumentDelimiter == AsmToken::Eof && 01831 !IsOperator(Lexer.getKind())) 01832 ArgumentDelimiter = AsmToken::Space; 01833 break; 01834 } 01835 } 01836 } 01837 01838 // HandleMacroEntry relies on not advancing the lexer here 01839 // to be able to fill in the remaining default parameter values 01840 if (Lexer.is(AsmToken::EndOfStatement)) 01841 break; 01842 01843 // Adjust the current parentheses level. 01844 if (Lexer.is(AsmToken::LParen)) 01845 ++ParenLevel; 01846 else if (Lexer.is(AsmToken::RParen) && ParenLevel) 01847 --ParenLevel; 01848 01849 // Append the token to the current argument list. 01850 MA.push_back(getTok()); 01851 if (AddTokens) 01852 AddTokens--; 01853 Lex(); 01854 } 01855 01856 Lexer.setSkipSpace(true); 01857 if (ParenLevel != 0) 01858 return TokError("unbalanced parentheses in macro argument"); 01859 return false; 01860 } 01861 01862 // Parse the macro instantiation arguments. 01863 bool AsmParser::ParseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A) { 01864 const unsigned NParameters = M ? M->Parameters.size() : 0; 01865 // Argument delimiter is initially unknown. It will be set by 01866 // ParseMacroArgument() 01867 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; 01868 01869 // Parse two kinds of macro invocations: 01870 // - macros defined without any parameters accept an arbitrary number of them 01871 // - macros defined with parameters accept at most that many of them 01872 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; 01873 ++Parameter) { 01874 MCAsmMacroArgument MA; 01875 01876 if (ParseMacroArgument(MA, ArgumentDelimiter)) 01877 return true; 01878 01879 if (!MA.empty() || !NParameters) 01880 A.push_back(MA); 01881 else if (NParameters) { 01882 if (!M->Parameters[Parameter].second.empty()) 01883 A.push_back(M->Parameters[Parameter].second); 01884 } 01885 01886 // At the end of the statement, fill in remaining arguments that have 01887 // default values. If there aren't any, then the next argument is 01888 // required but missing 01889 if (Lexer.is(AsmToken::EndOfStatement)) { 01890 if (NParameters && Parameter < NParameters - 1) { 01891 if (M->Parameters[Parameter + 1].second.empty()) 01892 return TokError("macro argument '" + 01893 Twine(M->Parameters[Parameter + 1].first) + 01894 "' is missing"); 01895 else 01896 continue; 01897 } 01898 return false; 01899 } 01900 01901 if (Lexer.is(AsmToken::Comma)) 01902 Lex(); 01903 } 01904 return TokError("Too many arguments"); 01905 } 01906 01907 const MCAsmMacro* AsmParser::LookupMacro(StringRef Name) { 01908 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name); 01909 return (I == MacroMap.end()) ? NULL : I->getValue(); 01910 } 01911 01912 void AsmParser::DefineMacro(StringRef Name, const MCAsmMacro& Macro) { 01913 MacroMap[Name] = new MCAsmMacro(Macro); 01914 } 01915 01916 void AsmParser::UndefineMacro(StringRef Name) { 01917 StringMap<MCAsmMacro*>::iterator I = MacroMap.find(Name); 01918 if (I != MacroMap.end()) { 01919 delete I->getValue(); 01920 MacroMap.erase(I); 01921 } 01922 } 01923 01924 bool AsmParser::HandleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { 01925 // Arbitrarily limit macro nesting depth, to match 'as'. We can eliminate 01926 // this, although we should protect against infinite loops. 01927 if (ActiveMacros.size() == 20) 01928 return TokError("macros cannot be nested more than 20 levels deep"); 01929 01930 MCAsmMacroArguments A; 01931 if (ParseMacroArguments(M, A)) 01932 return true; 01933 01934 // Remove any trailing empty arguments. Do this after-the-fact as we have 01935 // to keep empty arguments in the middle of the list or positionality 01936 // gets off. e.g., "foo 1, , 2" vs. "foo 1, 2," 01937 while (!A.empty() && A.back().empty()) 01938 A.pop_back(); 01939 01940 // Macro instantiation is lexical, unfortunately. We construct a new buffer 01941 // to hold the macro body with substitutions. 01942 SmallString<256> Buf; 01943 StringRef Body = M->Body; 01944 raw_svector_ostream OS(Buf); 01945 01946 if (expandMacro(OS, Body, M->Parameters, A, getTok().getLoc())) 01947 return true; 01948 01949 // We include the .endmacro in the buffer as our cue to exit the macro 01950 // instantiation. 01951 OS << ".endmacro\n"; 01952 01953 MemoryBuffer *Instantiation = 01954 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); 01955 01956 // Create the macro instantiation object and add to the current macro 01957 // instantiation stack. 01958 MacroInstantiation *MI = new MacroInstantiation(M, NameLoc, 01959 CurBuffer, 01960 getTok().getLoc(), 01961 Instantiation); 01962 ActiveMacros.push_back(MI); 01963 01964 // Jump to the macro instantiation and prime the lexer. 01965 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); 01966 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); 01967 Lex(); 01968 01969 return false; 01970 } 01971 01972 void AsmParser::HandleMacroExit() { 01973 // Jump to the EndOfStatement we should return to, and consume it. 01974 JumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); 01975 Lex(); 01976 01977 // Pop the instantiation entry. 01978 delete ActiveMacros.back(); 01979 ActiveMacros.pop_back(); 01980 } 01981 01982 static bool IsUsedIn(const MCSymbol *Sym, const MCExpr *Value) { 01983 switch (Value->getKind()) { 01984 case MCExpr::Binary: { 01985 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr*>(Value); 01986 return IsUsedIn(Sym, BE->getLHS()) || IsUsedIn(Sym, BE->getRHS()); 01987 } 01988 case MCExpr::Target: 01989 case MCExpr::Constant: 01990 return false; 01991 case MCExpr::SymbolRef: { 01992 const MCSymbol &S = static_cast<const MCSymbolRefExpr*>(Value)->getSymbol(); 01993 if (S.isVariable()) 01994 return IsUsedIn(Sym, S.getVariableValue()); 01995 return &S == Sym; 01996 } 01997 case MCExpr::Unary: 01998 return IsUsedIn(Sym, static_cast<const MCUnaryExpr*>(Value)->getSubExpr()); 01999 } 02000 02001 llvm_unreachable("Unknown expr kind!"); 02002 } 02003 02004 bool AsmParser::ParseAssignment(StringRef Name, bool allow_redef, 02005 bool NoDeadStrip) { 02006 // FIXME: Use better location, we should use proper tokens. 02007 SMLoc EqualLoc = Lexer.getLoc(); 02008 02009 const MCExpr *Value; 02010 if (parseExpression(Value)) 02011 return true; 02012 02013 // Note: we don't count b as used in "a = b". This is to allow 02014 // a = b 02015 // b = c 02016 02017 if (Lexer.isNot(AsmToken::EndOfStatement)) 02018 return TokError("unexpected token in assignment"); 02019 02020 // Error on assignment to '.'. 02021 if (Name == ".") { 02022 return Error(EqualLoc, ("assignment to pseudo-symbol '.' is unsupported " 02023 "(use '.space' or '.org').)")); 02024 } 02025 02026 // Eat the end of statement marker. 02027 Lex(); 02028 02029 // Validate that the LHS is allowed to be a variable (either it has not been 02030 // used as a symbol, or it is an absolute symbol). 02031 MCSymbol *Sym = getContext().LookupSymbol(Name); 02032 if (Sym) { 02033 // Diagnose assignment to a label. 02034 // 02035 // FIXME: Diagnostics. Note the location of the definition as a label. 02036 // FIXME: Diagnose assignment to protected identifier (e.g., register name). 02037 if (IsUsedIn(Sym, Value)) 02038 return Error(EqualLoc, "Recursive use of '" + Name + "'"); 02039 else if (Sym->isUndefined() && !Sym->isUsed() && !Sym->isVariable()) 02040 ; // Allow redefinitions of undefined symbols only used in directives. 02041 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) 02042 ; // Allow redefinitions of variables that haven't yet been used. 02043 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) 02044 return Error(EqualLoc, "redefinition of '" + Name + "'"); 02045 else if (!Sym->isVariable()) 02046 return Error(EqualLoc, "invalid assignment to '" + Name + "'"); 02047 else if (!isa<MCConstantExpr>(Sym->getVariableValue())) 02048 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" + 02049 Name + "'"); 02050 02051 // Don't count these checks as uses. 02052 Sym->setUsed(false); 02053 } else 02054 Sym = getContext().GetOrCreateSymbol(Name); 02055 02056 // FIXME: Handle '.'. 02057 02058 // Do the assignment. 02059 Out.EmitAssignment(Sym, Value); 02060 if (NoDeadStrip) 02061 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); 02062 02063 02064 return false; 02065 } 02066 02067 /// parseIdentifier: 02068 /// ::= identifier 02069 /// ::= string 02070 bool AsmParser::parseIdentifier(StringRef &Res) { 02071 // The assembler has relaxed rules for accepting identifiers, in particular we 02072 // allow things like '.globl $foo', which would normally be separate 02073 // tokens. At this level, we have already lexed so we cannot (currently) 02074 // handle this as a context dependent token, instead we detect adjacent tokens 02075 // and return the combined identifier. 02076 if (Lexer.is(AsmToken::Dollar)) { 02077 SMLoc DollarLoc = getLexer().getLoc(); 02078 02079 // Consume the dollar sign, and check for a following identifier. 02080 Lex(); 02081 if (Lexer.isNot(AsmToken::Identifier)) 02082 return true; 02083 02084 // We have a '$' followed by an identifier, make sure they are adjacent. 02085 if (DollarLoc.getPointer() + 1 != getTok().getLoc().getPointer()) 02086 return true; 02087 02088 // Construct the joined identifier and consume the token. 02089 Res = StringRef(DollarLoc.getPointer(), 02090 getTok().getIdentifier().size() + 1); 02091 Lex(); 02092 return false; 02093 } 02094 02095 if (Lexer.isNot(AsmToken::Identifier) && 02096 Lexer.isNot(AsmToken::String)) 02097 return true; 02098 02099 Res = getTok().getIdentifier(); 02100 02101 Lex(); // Consume the identifier token. 02102 02103 return false; 02104 } 02105 02106 /// ParseDirectiveSet: 02107 /// ::= .equ identifier ',' expression 02108 /// ::= .equiv identifier ',' expression 02109 /// ::= .set identifier ',' expression 02110 bool AsmParser::ParseDirectiveSet(StringRef IDVal, bool allow_redef) { 02111 StringRef Name; 02112 02113 if (parseIdentifier(Name)) 02114 return TokError("expected identifier after '" + Twine(IDVal) + "'"); 02115 02116 if (getLexer().isNot(AsmToken::Comma)) 02117 return TokError("unexpected token in '" + Twine(IDVal) + "'"); 02118 Lex(); 02119 02120 return ParseAssignment(Name, allow_redef, true); 02121 } 02122 02123 bool AsmParser::parseEscapedString(std::string &Data) { 02124 assert(getLexer().is(AsmToken::String) && "Unexpected current token!"); 02125 02126 Data = ""; 02127 StringRef Str = getTok().getStringContents(); 02128 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 02129 if (Str[i] != '\\') { 02130 Data += Str[i]; 02131 continue; 02132 } 02133 02134 // Recognize escaped characters. Note that this escape semantics currently 02135 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes. 02136 ++i; 02137 if (i == e) 02138 return TokError("unexpected backslash at end of string"); 02139 02140 // Recognize octal sequences. 02141 if ((unsigned) (Str[i] - '0') <= 7) { 02142 // Consume up to three octal characters. 02143 unsigned Value = Str[i] - '0'; 02144 02145 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { 02146 ++i; 02147 Value = Value * 8 + (Str[i] - '0'); 02148 02149 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) { 02150 ++i; 02151 Value = Value * 8 + (Str[i] - '0'); 02152 } 02153 } 02154 02155 if (Value > 255) 02156 return TokError("invalid octal escape sequence (out of range)"); 02157 02158 Data += (unsigned char) Value; 02159 continue; 02160 } 02161 02162 // Otherwise recognize individual escapes. 02163 switch (Str[i]) { 02164 default: 02165 // Just reject invalid escape sequences for now. 02166 return TokError("invalid escape sequence (unrecognized character)"); 02167 02168 case 'b': Data += '\b'; break; 02169 case 'f': Data += '\f'; break; 02170 case 'n': Data += '\n'; break; 02171 case 'r': Data += '\r'; break; 02172 case 't': Data += '\t'; break; 02173 case '"': Data += '"'; break; 02174 case '\\': Data += '\\'; break; 02175 } 02176 } 02177 02178 return false; 02179 } 02180 02181 /// ParseDirectiveAscii: 02182 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] 02183 bool AsmParser::ParseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { 02184 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02185 checkForValidSection(); 02186 02187 for (;;) { 02188 if (getLexer().isNot(AsmToken::String)) 02189 return TokError("expected string in '" + Twine(IDVal) + "' directive"); 02190 02191 std::string Data; 02192 if (parseEscapedString(Data)) 02193 return true; 02194 02195 getStreamer().EmitBytes(Data, DEFAULT_ADDRSPACE); 02196 if (ZeroTerminated) 02197 getStreamer().EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE); 02198 02199 Lex(); 02200 02201 if (getLexer().is(AsmToken::EndOfStatement)) 02202 break; 02203 02204 if (getLexer().isNot(AsmToken::Comma)) 02205 return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); 02206 Lex(); 02207 } 02208 } 02209 02210 Lex(); 02211 return false; 02212 } 02213 02214 /// ParseDirectiveValue 02215 /// ::= (.byte | .short | ... ) [ expression (, expression)* ] 02216 bool AsmParser::ParseDirectiveValue(unsigned Size) { 02217 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02218 checkForValidSection(); 02219 02220 for (;;) { 02221 const MCExpr *Value; 02222 SMLoc ExprLoc = getLexer().getLoc(); 02223 if (parseExpression(Value)) 02224 return true; 02225 02226 // Special case constant expressions to match code generator. 02227 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 02228 assert(Size <= 8 && "Invalid size"); 02229 uint64_t IntValue = MCE->getValue(); 02230 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) 02231 return Error(ExprLoc, "literal value out of range for directive"); 02232 getStreamer().EmitIntValue(IntValue, Size, DEFAULT_ADDRSPACE); 02233 } else 02234 getStreamer().EmitValue(Value, Size, DEFAULT_ADDRSPACE); 02235 02236 if (getLexer().is(AsmToken::EndOfStatement)) 02237 break; 02238 02239 // FIXME: Improve diagnostic. 02240 if (getLexer().isNot(AsmToken::Comma)) 02241 return TokError("unexpected token in directive"); 02242 Lex(); 02243 } 02244 } 02245 02246 Lex(); 02247 return false; 02248 } 02249 02250 /// ParseDirectiveRealValue 02251 /// ::= (.single | .double) [ expression (, expression)* ] 02252 bool AsmParser::ParseDirectiveRealValue(const fltSemantics &Semantics) { 02253 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02254 checkForValidSection(); 02255 02256 for (;;) { 02257 // We don't truly support arithmetic on floating point expressions, so we 02258 // have to manually parse unary prefixes. 02259 bool IsNeg = false; 02260 if (getLexer().is(AsmToken::Minus)) { 02261 Lex(); 02262 IsNeg = true; 02263 } else if (getLexer().is(AsmToken::Plus)) 02264 Lex(); 02265 02266 if (getLexer().isNot(AsmToken::Integer) && 02267 getLexer().isNot(AsmToken::Real) && 02268 getLexer().isNot(AsmToken::Identifier)) 02269 return TokError("unexpected token in directive"); 02270 02271 // Convert to an APFloat. 02272 APFloat Value(Semantics); 02273 StringRef IDVal = getTok().getString(); 02274 if (getLexer().is(AsmToken::Identifier)) { 02275 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) 02276 Value = APFloat::getInf(Semantics); 02277 else if (!IDVal.compare_lower("nan")) 02278 Value = APFloat::getNaN(Semantics, false, ~0); 02279 else 02280 return TokError("invalid floating point literal"); 02281 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == 02282 APFloat::opInvalidOp) 02283 return TokError("invalid floating point literal"); 02284 if (IsNeg) 02285 Value.changeSign(); 02286 02287 // Consume the numeric token. 02288 Lex(); 02289 02290 // Emit the value as an integer. 02291 APInt AsInt = Value.bitcastToAPInt(); 02292 getStreamer().EmitIntValue(AsInt.getLimitedValue(), 02293 AsInt.getBitWidth() / 8, DEFAULT_ADDRSPACE); 02294 02295 if (getLexer().is(AsmToken::EndOfStatement)) 02296 break; 02297 02298 if (getLexer().isNot(AsmToken::Comma)) 02299 return TokError("unexpected token in directive"); 02300 Lex(); 02301 } 02302 } 02303 02304 Lex(); 02305 return false; 02306 } 02307 02308 /// ParseDirectiveZero 02309 /// ::= .zero expression 02310 bool AsmParser::ParseDirectiveZero() { 02311 checkForValidSection(); 02312 02313 int64_t NumBytes; 02314 if (parseAbsoluteExpression(NumBytes)) 02315 return true; 02316 02317 int64_t Val = 0; 02318 if (getLexer().is(AsmToken::Comma)) { 02319 Lex(); 02320 if (parseAbsoluteExpression(Val)) 02321 return true; 02322 } 02323 02324 if (getLexer().isNot(AsmToken::EndOfStatement)) 02325 return TokError("unexpected token in '.zero' directive"); 02326 02327 Lex(); 02328 02329 getStreamer().EmitFill(NumBytes, Val, DEFAULT_ADDRSPACE); 02330 02331 return false; 02332 } 02333 02334 /// ParseDirectiveFill 02335 /// ::= .fill expression , expression , expression 02336 bool AsmParser::ParseDirectiveFill() { 02337 checkForValidSection(); 02338 02339 int64_t NumValues; 02340 if (parseAbsoluteExpression(NumValues)) 02341 return true; 02342 02343 if (getLexer().isNot(AsmToken::Comma)) 02344 return TokError("unexpected token in '.fill' directive"); 02345 Lex(); 02346 02347 int64_t FillSize; 02348 if (parseAbsoluteExpression(FillSize)) 02349 return true; 02350 02351 if (getLexer().isNot(AsmToken::Comma)) 02352 return TokError("unexpected token in '.fill' directive"); 02353 Lex(); 02354 02355 int64_t FillExpr; 02356 if (parseAbsoluteExpression(FillExpr)) 02357 return true; 02358 02359 if (getLexer().isNot(AsmToken::EndOfStatement)) 02360 return TokError("unexpected token in '.fill' directive"); 02361 02362 Lex(); 02363 02364 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8) 02365 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8"); 02366 02367 for (uint64_t i = 0, e = NumValues; i != e; ++i) 02368 getStreamer().EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE); 02369 02370 return false; 02371 } 02372 02373 /// ParseDirectiveOrg 02374 /// ::= .org expression [ , expression ] 02375 bool AsmParser::ParseDirectiveOrg() { 02376 checkForValidSection(); 02377 02378 const MCExpr *Offset; 02379 SMLoc Loc = getTok().getLoc(); 02380 if (parseExpression(Offset)) 02381 return true; 02382 02383 // Parse optional fill expression. 02384 int64_t FillExpr = 0; 02385 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02386 if (getLexer().isNot(AsmToken::Comma)) 02387 return TokError("unexpected token in '.org' directive"); 02388 Lex(); 02389 02390 if (parseAbsoluteExpression(FillExpr)) 02391 return true; 02392 02393 if (getLexer().isNot(AsmToken::EndOfStatement)) 02394 return TokError("unexpected token in '.org' directive"); 02395 } 02396 02397 Lex(); 02398 02399 // Only limited forms of relocatable expressions are accepted here, it 02400 // has to be relative to the current section. The streamer will return 02401 // 'true' if the expression wasn't evaluatable. 02402 if (getStreamer().EmitValueToOffset(Offset, FillExpr)) 02403 return Error(Loc, "expected assembly-time absolute expression"); 02404 02405 return false; 02406 } 02407 02408 /// ParseDirectiveAlign 02409 /// ::= {.align, ...} expression [ , expression [ , expression ]] 02410 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) { 02411 checkForValidSection(); 02412 02413 SMLoc AlignmentLoc = getLexer().getLoc(); 02414 int64_t Alignment; 02415 if (parseAbsoluteExpression(Alignment)) 02416 return true; 02417 02418 SMLoc MaxBytesLoc; 02419 bool HasFillExpr = false; 02420 int64_t FillExpr = 0; 02421 int64_t MaxBytesToFill = 0; 02422 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02423 if (getLexer().isNot(AsmToken::Comma)) 02424 return TokError("unexpected token in directive"); 02425 Lex(); 02426 02427 // The fill expression can be omitted while specifying a maximum number of 02428 // alignment bytes, e.g: 02429 // .align 3,,4 02430 if (getLexer().isNot(AsmToken::Comma)) { 02431 HasFillExpr = true; 02432 if (parseAbsoluteExpression(FillExpr)) 02433 return true; 02434 } 02435 02436 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02437 if (getLexer().isNot(AsmToken::Comma)) 02438 return TokError("unexpected token in directive"); 02439 Lex(); 02440 02441 MaxBytesLoc = getLexer().getLoc(); 02442 if (parseAbsoluteExpression(MaxBytesToFill)) 02443 return true; 02444 02445 if (getLexer().isNot(AsmToken::EndOfStatement)) 02446 return TokError("unexpected token in directive"); 02447 } 02448 } 02449 02450 Lex(); 02451 02452 if (!HasFillExpr) 02453 FillExpr = 0; 02454 02455 // Compute alignment in bytes. 02456 if (IsPow2) { 02457 // FIXME: Diagnose overflow. 02458 if (Alignment >= 32) { 02459 Error(AlignmentLoc, "invalid alignment value"); 02460 Alignment = 31; 02461 } 02462 02463 Alignment = 1ULL << Alignment; 02464 } else { 02465 // Reject alignments that aren't a power of two, for gas compatibility. 02466 if (!isPowerOf2_64(Alignment)) 02467 Error(AlignmentLoc, "alignment must be a power of 2"); 02468 } 02469 02470 // Diagnose non-sensical max bytes to align. 02471 if (MaxBytesLoc.isValid()) { 02472 if (MaxBytesToFill < 1) { 02473 Error(MaxBytesLoc, "alignment directive can never be satisfied in this " 02474 "many bytes, ignoring maximum bytes expression"); 02475 MaxBytesToFill = 0; 02476 } 02477 02478 if (MaxBytesToFill >= Alignment) { 02479 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " 02480 "has no effect"); 02481 MaxBytesToFill = 0; 02482 } 02483 } 02484 02485 // Check whether we should use optimal code alignment for this .align 02486 // directive. 02487 bool UseCodeAlign = getStreamer().getCurrentSection().first->UseCodeAlign(); 02488 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && 02489 ValueSize == 1 && UseCodeAlign) { 02490 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); 02491 } else { 02492 // FIXME: Target specific behavior about how the "extra" bytes are filled. 02493 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, 02494 MaxBytesToFill); 02495 } 02496 02497 return false; 02498 } 02499 02500 /// ParseDirectiveFile 02501 /// ::= .file [number] filename 02502 /// ::= .file number directory filename 02503 bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) { 02504 // FIXME: I'm not sure what this is. 02505 int64_t FileNumber = -1; 02506 SMLoc FileNumberLoc = getLexer().getLoc(); 02507 if (getLexer().is(AsmToken::Integer)) { 02508 FileNumber = getTok().getIntVal(); 02509 Lex(); 02510 02511 if (FileNumber < 1) 02512 return TokError("file number less than one"); 02513 } 02514 02515 if (getLexer().isNot(AsmToken::String)) 02516 return TokError("unexpected token in '.file' directive"); 02517 02518 // Usually the directory and filename together, otherwise just the directory. 02519 StringRef Path = getTok().getString(); 02520 Path = Path.substr(1, Path.size()-2); 02521 Lex(); 02522 02523 StringRef Directory; 02524 StringRef Filename; 02525 if (getLexer().is(AsmToken::String)) { 02526 if (FileNumber == -1) 02527 return TokError("explicit path specified, but no file number"); 02528 Filename = getTok().getString(); 02529 Filename = Filename.substr(1, Filename.size()-2); 02530 Directory = Path; 02531 Lex(); 02532 } else { 02533 Filename = Path; 02534 } 02535 02536 if (getLexer().isNot(AsmToken::EndOfStatement)) 02537 return TokError("unexpected token in '.file' directive"); 02538 02539 if (FileNumber == -1) 02540 getStreamer().EmitFileDirective(Filename); 02541 else { 02542 if (getContext().getGenDwarfForAssembly() == true) 02543 Error(DirectiveLoc, "input can't have .file dwarf directives when -g is " 02544 "used to generate dwarf debug info for assembly code"); 02545 02546 if (getStreamer().EmitDwarfFileDirective(FileNumber, Directory, Filename)) 02547 Error(FileNumberLoc, "file number already allocated"); 02548 } 02549 02550 return false; 02551 } 02552 02553 /// ParseDirectiveLine 02554 /// ::= .line [number] 02555 bool AsmParser::ParseDirectiveLine() { 02556 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02557 if (getLexer().isNot(AsmToken::Integer)) 02558 return TokError("unexpected token in '.line' directive"); 02559 02560 int64_t LineNumber = getTok().getIntVal(); 02561 (void) LineNumber; 02562 Lex(); 02563 02564 // FIXME: Do something with the .line. 02565 } 02566 02567 if (getLexer().isNot(AsmToken::EndOfStatement)) 02568 return TokError("unexpected token in '.line' directive"); 02569 02570 return false; 02571 } 02572 02573 /// ParseDirectiveLoc 02574 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] 02575 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] 02576 /// The first number is a file number, must have been previously assigned with 02577 /// a .file directive, the second number is the line number and optionally the 02578 /// third number is a column position (zero if not specified). The remaining 02579 /// optional items are .loc sub-directives. 02580 bool AsmParser::ParseDirectiveLoc() { 02581 if (getLexer().isNot(AsmToken::Integer)) 02582 return TokError("unexpected token in '.loc' directive"); 02583 int64_t FileNumber = getTok().getIntVal(); 02584 if (FileNumber < 1) 02585 return TokError("file number less than one in '.loc' directive"); 02586 if (!getContext().isValidDwarfFileNumber(FileNumber)) 02587 return TokError("unassigned file number in '.loc' directive"); 02588 Lex(); 02589 02590 int64_t LineNumber = 0; 02591 if (getLexer().is(AsmToken::Integer)) { 02592 LineNumber = getTok().getIntVal(); 02593 if (LineNumber < 1) 02594 return TokError("line number less than one in '.loc' directive"); 02595 Lex(); 02596 } 02597 02598 int64_t ColumnPos = 0; 02599 if (getLexer().is(AsmToken::Integer)) { 02600 ColumnPos = getTok().getIntVal(); 02601 if (ColumnPos < 0) 02602 return TokError("column position less than zero in '.loc' directive"); 02603 Lex(); 02604 } 02605 02606 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 02607 unsigned Isa = 0; 02608 int64_t Discriminator = 0; 02609 if (getLexer().isNot(AsmToken::EndOfStatement)) { 02610 for (;;) { 02611 if (getLexer().is(AsmToken::EndOfStatement)) 02612 break; 02613 02614 StringRef Name; 02615 SMLoc Loc = getTok().getLoc(); 02616 if (parseIdentifier(Name)) 02617 return TokError("unexpected token in '.loc' directive"); 02618 02619 if (Name == "basic_block") 02620 Flags |= DWARF2_FLAG_BASIC_BLOCK; 02621 else if (Name == "prologue_end") 02622 Flags |= DWARF2_FLAG_PROLOGUE_END; 02623 else if (Name == "epilogue_begin") 02624 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; 02625 else if (Name == "is_stmt") { 02626 Loc = getTok().getLoc(); 02627 const MCExpr *Value; 02628 if (parseExpression(Value)) 02629 return true; 02630 // The expression must be the constant 0 or 1. 02631 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 02632 int Value = MCE->getValue(); 02633 if (Value == 0) 02634 Flags &= ~DWARF2_FLAG_IS_STMT; 02635 else if (Value == 1) 02636 Flags |= DWARF2_FLAG_IS_STMT; 02637 else 02638 return Error(Loc, "is_stmt value not 0 or 1"); 02639 } else { 02640 return Error(Loc, "is_stmt value not the constant value of 0 or 1"); 02641 } 02642 } else if (Name == "isa") { 02643 Loc = getTok().getLoc(); 02644 const MCExpr *Value; 02645 if (parseExpression(Value)) 02646 return true; 02647 // The expression must be a constant greater or equal to 0. 02648 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 02649 int Value = MCE->getValue(); 02650 if (Value < 0) 02651 return Error(Loc, "isa number less than zero"); 02652 Isa = Value; 02653 } else { 02654 return Error(Loc, "isa number not a constant value"); 02655 } 02656 } else if (Name == "discriminator") { 02657 if (parseAbsoluteExpression(Discriminator)) 02658 return true; 02659 } else { 02660 return Error(Loc, "unknown sub-directive in '.loc' directive"); 02661 } 02662 02663 if (getLexer().is(AsmToken::EndOfStatement)) 02664 break; 02665 } 02666 } 02667 02668 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, 02669 Isa, Discriminator, StringRef()); 02670 02671 return false; 02672 } 02673 02674 /// ParseDirectiveStabs 02675 /// ::= .stabs string, number, number, number 02676 bool AsmParser::ParseDirectiveStabs() { 02677 return TokError("unsupported directive '.stabs'"); 02678 } 02679 02680 /// ParseDirectiveCFISections 02681 /// ::= .cfi_sections section [, section] 02682 bool AsmParser::ParseDirectiveCFISections() { 02683 StringRef Name; 02684 bool EH = false; 02685 bool Debug = false; 02686 02687 if (parseIdentifier(Name)) 02688 return TokError("Expected an identifier"); 02689 02690 if (Name == ".eh_frame") 02691 EH = true; 02692 else if (Name == ".debug_frame") 02693 Debug = true; 02694 02695 if (getLexer().is(AsmToken::Comma)) { 02696 Lex(); 02697 02698 if (parseIdentifier(Name)) 02699 return TokError("Expected an identifier"); 02700 02701 if (Name == ".eh_frame") 02702 EH = true; 02703 else if (Name == ".debug_frame") 02704 Debug = true; 02705 } 02706 02707 getStreamer().EmitCFISections(EH, Debug); 02708 return false; 02709 } 02710 02711 /// ParseDirectiveCFIStartProc 02712 /// ::= .cfi_startproc 02713 bool AsmParser::ParseDirectiveCFIStartProc() { 02714 getStreamer().EmitCFIStartProc(); 02715 return false; 02716 } 02717 02718 /// ParseDirectiveCFIEndProc 02719 /// ::= .cfi_endproc 02720 bool AsmParser::ParseDirectiveCFIEndProc() { 02721 getStreamer().EmitCFIEndProc(); 02722 return false; 02723 } 02724 02725 /// ParseRegisterOrRegisterNumber - parse register name or number. 02726 bool AsmParser::ParseRegisterOrRegisterNumber(int64_t &Register, 02727 SMLoc DirectiveLoc) { 02728 unsigned RegNo; 02729 02730 if (getLexer().isNot(AsmToken::Integer)) { 02731 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) 02732 return true; 02733 Register = getContext().getRegisterInfo().getDwarfRegNum(RegNo, true); 02734 } else 02735 return parseAbsoluteExpression(Register); 02736 02737 return false; 02738 } 02739 02740 /// ParseDirectiveCFIDefCfa 02741 /// ::= .cfi_def_cfa register, offset 02742 bool AsmParser::ParseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { 02743 int64_t Register = 0; 02744 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02745 return true; 02746 02747 if (getLexer().isNot(AsmToken::Comma)) 02748 return TokError("unexpected token in directive"); 02749 Lex(); 02750 02751 int64_t Offset = 0; 02752 if (parseAbsoluteExpression(Offset)) 02753 return true; 02754 02755 getStreamer().EmitCFIDefCfa(Register, Offset); 02756 return false; 02757 } 02758 02759 /// ParseDirectiveCFIDefCfaOffset 02760 /// ::= .cfi_def_cfa_offset offset 02761 bool AsmParser::ParseDirectiveCFIDefCfaOffset() { 02762 int64_t Offset = 0; 02763 if (parseAbsoluteExpression(Offset)) 02764 return true; 02765 02766 getStreamer().EmitCFIDefCfaOffset(Offset); 02767 return false; 02768 } 02769 02770 /// ParseDirectiveCFIRegister 02771 /// ::= .cfi_register register, register 02772 bool AsmParser::ParseDirectiveCFIRegister(SMLoc DirectiveLoc) { 02773 int64_t Register1 = 0; 02774 if (ParseRegisterOrRegisterNumber(Register1, DirectiveLoc)) 02775 return true; 02776 02777 if (getLexer().isNot(AsmToken::Comma)) 02778 return TokError("unexpected token in directive"); 02779 Lex(); 02780 02781 int64_t Register2 = 0; 02782 if (ParseRegisterOrRegisterNumber(Register2, DirectiveLoc)) 02783 return true; 02784 02785 getStreamer().EmitCFIRegister(Register1, Register2); 02786 return false; 02787 } 02788 02789 /// ParseDirectiveCFIAdjustCfaOffset 02790 /// ::= .cfi_adjust_cfa_offset adjustment 02791 bool AsmParser::ParseDirectiveCFIAdjustCfaOffset() { 02792 int64_t Adjustment = 0; 02793 if (parseAbsoluteExpression(Adjustment)) 02794 return true; 02795 02796 getStreamer().EmitCFIAdjustCfaOffset(Adjustment); 02797 return false; 02798 } 02799 02800 /// ParseDirectiveCFIDefCfaRegister 02801 /// ::= .cfi_def_cfa_register register 02802 bool AsmParser::ParseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { 02803 int64_t Register = 0; 02804 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02805 return true; 02806 02807 getStreamer().EmitCFIDefCfaRegister(Register); 02808 return false; 02809 } 02810 02811 /// ParseDirectiveCFIOffset 02812 /// ::= .cfi_offset register, offset 02813 bool AsmParser::ParseDirectiveCFIOffset(SMLoc DirectiveLoc) { 02814 int64_t Register = 0; 02815 int64_t Offset = 0; 02816 02817 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02818 return true; 02819 02820 if (getLexer().isNot(AsmToken::Comma)) 02821 return TokError("unexpected token in directive"); 02822 Lex(); 02823 02824 if (parseAbsoluteExpression(Offset)) 02825 return true; 02826 02827 getStreamer().EmitCFIOffset(Register, Offset); 02828 return false; 02829 } 02830 02831 /// ParseDirectiveCFIRelOffset 02832 /// ::= .cfi_rel_offset register, offset 02833 bool AsmParser::ParseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { 02834 int64_t Register = 0; 02835 02836 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02837 return true; 02838 02839 if (getLexer().isNot(AsmToken::Comma)) 02840 return TokError("unexpected token in directive"); 02841 Lex(); 02842 02843 int64_t Offset = 0; 02844 if (parseAbsoluteExpression(Offset)) 02845 return true; 02846 02847 getStreamer().EmitCFIRelOffset(Register, Offset); 02848 return false; 02849 } 02850 02851 static bool isValidEncoding(int64_t Encoding) { 02852 if (Encoding & ~0xff) 02853 return false; 02854 02855 if (Encoding == dwarf::DW_EH_PE_omit) 02856 return true; 02857 02858 const unsigned Format = Encoding & 0xf; 02859 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && 02860 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && 02861 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && 02862 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) 02863 return false; 02864 02865 const unsigned Application = Encoding & 0x70; 02866 if (Application != dwarf::DW_EH_PE_absptr && 02867 Application != dwarf::DW_EH_PE_pcrel) 02868 return false; 02869 02870 return true; 02871 } 02872 02873 /// ParseDirectiveCFIPersonalityOrLsda 02874 /// IsPersonality true for cfi_personality, false for cfi_lsda 02875 /// ::= .cfi_personality encoding, [symbol_name] 02876 /// ::= .cfi_lsda encoding, [symbol_name] 02877 bool AsmParser::ParseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { 02878 int64_t Encoding = 0; 02879 if (parseAbsoluteExpression(Encoding)) 02880 return true; 02881 if (Encoding == dwarf::DW_EH_PE_omit) 02882 return false; 02883 02884 if (!isValidEncoding(Encoding)) 02885 return TokError("unsupported encoding."); 02886 02887 if (getLexer().isNot(AsmToken::Comma)) 02888 return TokError("unexpected token in directive"); 02889 Lex(); 02890 02891 StringRef Name; 02892 if (parseIdentifier(Name)) 02893 return TokError("expected identifier in directive"); 02894 02895 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 02896 02897 if (IsPersonality) 02898 getStreamer().EmitCFIPersonality(Sym, Encoding); 02899 else 02900 getStreamer().EmitCFILsda(Sym, Encoding); 02901 return false; 02902 } 02903 02904 /// ParseDirectiveCFIRememberState 02905 /// ::= .cfi_remember_state 02906 bool AsmParser::ParseDirectiveCFIRememberState() { 02907 getStreamer().EmitCFIRememberState(); 02908 return false; 02909 } 02910 02911 /// ParseDirectiveCFIRestoreState 02912 /// ::= .cfi_remember_state 02913 bool AsmParser::ParseDirectiveCFIRestoreState() { 02914 getStreamer().EmitCFIRestoreState(); 02915 return false; 02916 } 02917 02918 /// ParseDirectiveCFISameValue 02919 /// ::= .cfi_same_value register 02920 bool AsmParser::ParseDirectiveCFISameValue(SMLoc DirectiveLoc) { 02921 int64_t Register = 0; 02922 02923 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02924 return true; 02925 02926 getStreamer().EmitCFISameValue(Register); 02927 return false; 02928 } 02929 02930 /// ParseDirectiveCFIRestore 02931 /// ::= .cfi_restore register 02932 bool AsmParser::ParseDirectiveCFIRestore(SMLoc DirectiveLoc) { 02933 int64_t Register = 0; 02934 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02935 return true; 02936 02937 getStreamer().EmitCFIRestore(Register); 02938 return false; 02939 } 02940 02941 /// ParseDirectiveCFIEscape 02942 /// ::= .cfi_escape expression[,...] 02943 bool AsmParser::ParseDirectiveCFIEscape() { 02944 std::string Values; 02945 int64_t CurrValue; 02946 if (parseAbsoluteExpression(CurrValue)) 02947 return true; 02948 02949 Values.push_back((uint8_t)CurrValue); 02950 02951 while (getLexer().is(AsmToken::Comma)) { 02952 Lex(); 02953 02954 if (parseAbsoluteExpression(CurrValue)) 02955 return true; 02956 02957 Values.push_back((uint8_t)CurrValue); 02958 } 02959 02960 getStreamer().EmitCFIEscape(Values); 02961 return false; 02962 } 02963 02964 /// ParseDirectiveCFISignalFrame 02965 /// ::= .cfi_signal_frame 02966 bool AsmParser::ParseDirectiveCFISignalFrame() { 02967 if (getLexer().isNot(AsmToken::EndOfStatement)) 02968 return Error(getLexer().getLoc(), 02969 "unexpected token in '.cfi_signal_frame'"); 02970 02971 getStreamer().EmitCFISignalFrame(); 02972 return false; 02973 } 02974 02975 /// ParseDirectiveCFIUndefined 02976 /// ::= .cfi_undefined register 02977 bool AsmParser::ParseDirectiveCFIUndefined(SMLoc DirectiveLoc) { 02978 int64_t Register = 0; 02979 02980 if (ParseRegisterOrRegisterNumber(Register, DirectiveLoc)) 02981 return true; 02982 02983 getStreamer().EmitCFIUndefined(Register); 02984 return false; 02985 } 02986 02987 /// ParseDirectiveMacrosOnOff 02988 /// ::= .macros_on 02989 /// ::= .macros_off 02990 bool AsmParser::ParseDirectiveMacrosOnOff(StringRef Directive) { 02991 if (getLexer().isNot(AsmToken::EndOfStatement)) 02992 return Error(getLexer().getLoc(), 02993 "unexpected token in '" + Directive + "' directive"); 02994 02995 SetMacrosEnabled(Directive == ".macros_on"); 02996 return false; 02997 } 02998 02999 /// ParseDirectiveMacro 03000 /// ::= .macro name [parameters] 03001 bool AsmParser::ParseDirectiveMacro(SMLoc DirectiveLoc) { 03002 StringRef Name; 03003 if (parseIdentifier(Name)) 03004 return TokError("expected identifier in '.macro' directive"); 03005 03006 MCAsmMacroParameters Parameters; 03007 // Argument delimiter is initially unknown. It will be set by 03008 // ParseMacroArgument() 03009 AsmToken::TokenKind ArgumentDelimiter = AsmToken::Eof; 03010 if (getLexer().isNot(AsmToken::EndOfStatement)) { 03011 for (;;) { 03012 MCAsmMacroParameter Parameter; 03013 if (parseIdentifier(Parameter.first)) 03014 return TokError("expected identifier in '.macro' directive"); 03015 03016 if (getLexer().is(AsmToken::Equal)) { 03017 Lex(); 03018 if (ParseMacroArgument(Parameter.second, ArgumentDelimiter)) 03019 return true; 03020 } 03021 03022 Parameters.push_back(Parameter); 03023 03024 if (getLexer().is(AsmToken::Comma)) 03025 Lex(); 03026 else if (getLexer().is(AsmToken::EndOfStatement)) 03027 break; 03028 } 03029 } 03030 03031 // Eat the end of statement. 03032 Lex(); 03033 03034 AsmToken EndToken, StartToken = getTok(); 03035 03036 // Lex the macro definition. 03037 for (;;) { 03038 // Check whether we have reached the end of the file. 03039 if (getLexer().is(AsmToken::Eof)) 03040 return Error(DirectiveLoc, "no matching '.endmacro' in definition"); 03041 03042 // Otherwise, check whether we have reach the .endmacro. 03043 if (getLexer().is(AsmToken::Identifier) && 03044 (getTok().getIdentifier() == ".endm" || 03045 getTok().getIdentifier() == ".endmacro")) { 03046 EndToken = getTok(); 03047 Lex(); 03048 if (getLexer().isNot(AsmToken::EndOfStatement)) 03049 return TokError("unexpected token in '" + EndToken.getIdentifier() + 03050 "' directive"); 03051 break; 03052 } 03053 03054 // Otherwise, scan til the end of the statement. 03055 eatToEndOfStatement(); 03056 } 03057 03058 if (LookupMacro(Name)) { 03059 return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); 03060 } 03061 03062 const char *BodyStart = StartToken.getLoc().getPointer(); 03063 const char *BodyEnd = EndToken.getLoc().getPointer(); 03064 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); 03065 CheckForBadMacro(DirectiveLoc, Name, Body, Parameters); 03066 DefineMacro(Name, MCAsmMacro(Name, Body, Parameters)); 03067 return false; 03068 } 03069 03070 /// CheckForBadMacro 03071 /// 03072 /// With the support added for named parameters there may be code out there that 03073 /// is transitioning from positional parameters. In versions of gas that did 03074 /// not support named parameters they would be ignored on the macro defintion. 03075 /// But to support both styles of parameters this is not possible so if a macro 03076 /// defintion has named parameters but does not use them and has what appears 03077 /// to be positional parameters, strings like $1, $2, ... and $n, then issue a 03078 /// warning that the positional parameter found in body which have no effect. 03079 /// Hoping the developer will either remove the named parameters from the macro 03080 /// definiton so the positional parameters get used if that was what was 03081 /// intended or change the macro to use the named parameters. It is possible 03082 /// this warning will trigger when the none of the named parameters are used 03083 /// and the strings like $1 are infact to simply to be passed trough unchanged. 03084 void AsmParser::CheckForBadMacro(SMLoc DirectiveLoc, StringRef Name, 03085 StringRef Body, 03086 MCAsmMacroParameters Parameters) { 03087 // If this macro is not defined with named parameters the warning we are 03088 // checking for here doesn't apply. 03089 unsigned NParameters = Parameters.size(); 03090 if (NParameters == 0) 03091 return; 03092 03093 bool NamedParametersFound = false; 03094 bool PositionalParametersFound = false; 03095 03096 // Look at the body of the macro for use of both the named parameters and what 03097 // are likely to be positional parameters. This is what expandMacro() is 03098 // doing when it finds the parameters in the body. 03099 while (!Body.empty()) { 03100 // Scan for the next possible parameter. 03101 std::size_t End = Body.size(), Pos = 0; 03102 for (; Pos != End; ++Pos) { 03103 // Check for a substitution or escape. 03104 // This macro is defined with parameters, look for \foo, \bar, etc. 03105 if (Body[Pos] == '\\' && Pos + 1 != End) 03106 break; 03107 03108 // This macro should have parameters, but look for $0, $1, ..., $n too. 03109 if (Body[Pos] != '$' || Pos + 1 == End) 03110 continue; 03111 char Next = Body[Pos + 1]; 03112 if (Next == '$' || Next == 'n' || 03113 isdigit(static_cast<unsigned char>(Next))) 03114 break; 03115 } 03116 03117 // Check if we reached the end. 03118 if (Pos == End) 03119 break; 03120 03121 if (Body[Pos] == '$') { 03122 switch (Body[Pos+1]) { 03123 // $$ => $ 03124 case '$': 03125 break; 03126 03127 // $n => number of arguments 03128 case 'n': 03129 PositionalParametersFound = true; 03130 break; 03131 03132 // $[0-9] => argument 03133 default: { 03134 PositionalParametersFound = true; 03135 break; 03136 } 03137 } 03138 Pos += 2; 03139 } else { 03140 unsigned I = Pos + 1; 03141 while (isIdentifierChar(Body[I]) && I + 1 != End) 03142 ++I; 03143 03144 const char *Begin = Body.data() + Pos +1; 03145 StringRef Argument(Begin, I - (Pos +1)); 03146 unsigned Index = 0; 03147 for (; Index < NParameters; ++Index) 03148 if (Parameters[Index].first == Argument) 03149 break; 03150 03151 if (Index == NParameters) { 03152 if (Body[Pos+1] == '(' && Body[Pos+2] == ')') 03153 Pos += 3; 03154 else { 03155 Pos = I; 03156 } 03157 } else { 03158 NamedParametersFound = true; 03159 Pos += 1 + Argument.size(); 03160 } 03161 } 03162 // Update the scan point. 03163 Body = Body.substr(Pos); 03164 } 03165 03166 if (!NamedParametersFound && PositionalParametersFound) 03167 Warning(DirectiveLoc, "macro defined with named parameters which are not " 03168 "used in macro body, possible positional parameter " 03169 "found in body which will have no effect"); 03170 } 03171 03172 /// ParseDirectiveEndMacro 03173 /// ::= .endm 03174 /// ::= .endmacro 03175 bool AsmParser::ParseDirectiveEndMacro(StringRef Directive) { 03176 if (getLexer().isNot(AsmToken::EndOfStatement)) 03177 return TokError("unexpected token in '" + Directive + "' directive"); 03178 03179 // If we are inside a macro instantiation, terminate the current 03180 // instantiation. 03181 if (InsideMacroInstantiation()) { 03182 HandleMacroExit(); 03183 return false; 03184 } 03185 03186 // Otherwise, this .endmacro is a stray entry in the file; well formed 03187 // .endmacro directives are handled during the macro definition parsing. 03188 return TokError("unexpected '" + Directive + "' in file, " 03189 "no current macro definition"); 03190 } 03191 03192 /// ParseDirectivePurgeMacro 03193 /// ::= .purgem 03194 bool AsmParser::ParseDirectivePurgeMacro(SMLoc DirectiveLoc) { 03195 StringRef Name; 03196 if (parseIdentifier(Name)) 03197 return TokError("expected identifier in '.purgem' directive"); 03198 03199 if (getLexer().isNot(AsmToken::EndOfStatement)) 03200 return TokError("unexpected token in '.purgem' directive"); 03201 03202 if (!LookupMacro(Name)) 03203 return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); 03204 03205 UndefineMacro(Name); 03206 return false; 03207 } 03208 03209 /// ParseDirectiveBundleAlignMode 03210 /// ::= {.bundle_align_mode} expression 03211 bool AsmParser::ParseDirectiveBundleAlignMode() { 03212 checkForValidSection(); 03213 03214 // Expect a single argument: an expression that evaluates to a constant 03215 // in the inclusive range 0-30. 03216 SMLoc ExprLoc = getLexer().getLoc(); 03217 int64_t AlignSizePow2; 03218 if (parseAbsoluteExpression(AlignSizePow2)) 03219 return true; 03220 else if (getLexer().isNot(AsmToken::EndOfStatement)) 03221 return TokError("unexpected token after expression in" 03222 " '.bundle_align_mode' directive"); 03223 else if (AlignSizePow2 < 0 || AlignSizePow2 > 30) 03224 return Error(ExprLoc, 03225 "invalid bundle alignment size (expected between 0 and 30)"); 03226 03227 Lex(); 03228 03229 // Because of AlignSizePow2's verified range we can safely truncate it to 03230 // unsigned. 03231 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); 03232 return false; 03233 } 03234 03235 /// ParseDirectiveBundleLock 03236 /// ::= {.bundle_lock} [align_to_end] 03237 bool AsmParser::ParseDirectiveBundleLock() { 03238 checkForValidSection(); 03239 bool AlignToEnd = false; 03240 03241 if (getLexer().isNot(AsmToken::EndOfStatement)) { 03242 StringRef Option; 03243 SMLoc Loc = getTok().getLoc(); 03244 const char *kInvalidOptionError = 03245 "invalid option for '.bundle_lock' directive"; 03246 03247 if (parseIdentifier(Option)) 03248 return Error(Loc, kInvalidOptionError); 03249 03250 if (Option != "align_to_end") 03251 return Error(Loc, kInvalidOptionError); 03252 else if (getLexer().isNot(AsmToken::EndOfStatement)) 03253 return Error(Loc, 03254 "unexpected token after '.bundle_lock' directive option"); 03255 AlignToEnd = true; 03256 } 03257 03258 Lex(); 03259 03260 getStreamer().EmitBundleLock(AlignToEnd); 03261 return false; 03262 } 03263 03264 /// ParseDirectiveBundleLock 03265 /// ::= {.bundle_lock} 03266 bool AsmParser::ParseDirectiveBundleUnlock() { 03267 checkForValidSection(); 03268 03269 if (getLexer().isNot(AsmToken::EndOfStatement)) 03270 return TokError("unexpected token in '.bundle_unlock' directive"); 03271 Lex(); 03272 03273 getStreamer().EmitBundleUnlock(); 03274 return false; 03275 } 03276 03277 /// ParseDirectiveSpace 03278 /// ::= (.skip | .space) expression [ , expression ] 03279 bool AsmParser::ParseDirectiveSpace(StringRef IDVal) { 03280 checkForValidSection(); 03281 03282 int64_t NumBytes; 03283 if (parseAbsoluteExpression(NumBytes)) 03284 return true; 03285 03286 int64_t FillExpr = 0; 03287 if (getLexer().isNot(AsmToken::EndOfStatement)) { 03288 if (getLexer().isNot(AsmToken::Comma)) 03289 return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); 03290 Lex(); 03291 03292 if (parseAbsoluteExpression(FillExpr)) 03293 return true; 03294 03295 if (getLexer().isNot(AsmToken::EndOfStatement)) 03296 return TokError("unexpected token in '" + Twine(IDVal) + "' directive"); 03297 } 03298 03299 Lex(); 03300 03301 if (NumBytes <= 0) 03302 return TokError("invalid number of bytes in '" + 03303 Twine(IDVal) + "' directive"); 03304 03305 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. 03306 getStreamer().EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE); 03307 03308 return false; 03309 } 03310 03311 /// ParseDirectiveLEB128 03312 /// ::= (.sleb128 | .uleb128) expression 03313 bool AsmParser::ParseDirectiveLEB128(bool Signed) { 03314 checkForValidSection(); 03315 const MCExpr *Value; 03316 03317 if (parseExpression(Value)) 03318 return true; 03319 03320 if (getLexer().isNot(AsmToken::EndOfStatement)) 03321 return TokError("unexpected token in directive"); 03322 03323 if (Signed) 03324 getStreamer().EmitSLEB128Value(Value); 03325 else 03326 getStreamer().EmitULEB128Value(Value); 03327 03328 return false; 03329 } 03330 03331 /// ParseDirectiveSymbolAttribute 03332 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] 03333 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) { 03334 if (getLexer().isNot(AsmToken::EndOfStatement)) { 03335 for (;;) { 03336 StringRef Name; 03337 SMLoc Loc = getTok().getLoc(); 03338 03339 if (parseIdentifier(Name)) 03340 return Error(Loc, "expected identifier in directive"); 03341 03342 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 03343 03344 // Assembler local symbols don't make any sense here. Complain loudly. 03345 if (Sym->isTemporary()) 03346 return Error(Loc, "non-local symbol required in directive"); 03347 03348 getStreamer().EmitSymbolAttribute(Sym, Attr); 03349 03350 if (getLexer().is(AsmToken::EndOfStatement)) 03351 break; 03352 03353 if (getLexer().isNot(AsmToken::Comma)) 03354 return TokError("unexpected token in directive"); 03355 Lex(); 03356 } 03357 } 03358 03359 Lex(); 03360 return false; 03361 } 03362 03363 /// ParseDirectiveComm 03364 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] 03365 bool AsmParser::ParseDirectiveComm(bool IsLocal) { 03366 checkForValidSection(); 03367 03368 SMLoc IDLoc = getLexer().getLoc(); 03369 StringRef Name; 03370 if (parseIdentifier(Name)) 03371 return TokError("expected identifier in directive"); 03372 03373 // Handle the identifier as the key symbol. 03374 MCSymbol *Sym = getContext().GetOrCreateSymbol(Name); 03375 03376 if (getLexer().isNot(AsmToken::Comma)) 03377 return TokError("unexpected token in directive"); 03378 Lex(); 03379 03380 int64_t Size; 03381 SMLoc SizeLoc = getLexer().getLoc(); 03382 if (parseAbsoluteExpression(Size)) 03383 return true; 03384 03385 int64_t Pow2Alignment = 0; 03386 SMLoc Pow2AlignmentLoc; 03387 if (getLexer().is(AsmToken::Comma)) { 03388 Lex(); 03389 Pow2AlignmentLoc = getLexer().getLoc(); 03390 if (parseAbsoluteExpression(Pow2Alignment)) 03391 return true; 03392 03393 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); 03394 if (IsLocal && LCOMM == LCOMM::NoAlignment) 03395 return Error(Pow2AlignmentLoc, "alignment not supported on this target"); 03396 03397 // If this target takes alignments in bytes (not log) validate and convert. 03398 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || 03399 (IsLocal && LCOMM == LCOMM::ByteAlignment)) { 03400 if (!isPowerOf2_64(Pow2Alignment)) 03401 return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); 03402 Pow2Alignment = Log2_64(Pow2Alignment); 03403 } 03404 } 03405 03406 if (getLexer().isNot(AsmToken::EndOfStatement)) 03407 return TokError("unexpected token in '.comm' or '.lcomm' directive"); 03408 03409 Lex(); 03410 03411 // NOTE: a size of zero for a .comm should create a undefined symbol 03412 // but a size of .lcomm creates a bss symbol of size zero. 03413 if (Size < 0) 03414 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " 03415 "be less than zero"); 03416 03417 // NOTE: The alignment in the directive is a power of 2 value, the assembler 03418 // may internally end up wanting an alignment in bytes. 03419 // FIXME: Diagnose overflow. 03420 if (Pow2Alignment < 0) 03421 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " 03422 "alignment, can't be less than zero"); 03423 03424 if (!Sym->isUndefined()) 03425 return Error(IDLoc, "invalid symbol redefinition"); 03426 03427 // Create the Symbol as a common or local common with Size and Pow2Alignment 03428 if (IsLocal) { 03429 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); 03430 return false; 03431 } 03432 03433 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); 03434 return false; 03435 } 03436 03437 /// ParseDirectiveAbort 03438 /// ::= .abort [... message ...] 03439 bool AsmParser::ParseDirectiveAbort() { 03440 // FIXME: Use loc from directive. 03441 SMLoc Loc = getLexer().getLoc(); 03442 03443 StringRef Str = parseStringToEndOfStatement(); 03444 if (getLexer().isNot(AsmToken::EndOfStatement)) 03445 return TokError("unexpected token in '.abort' directive"); 03446 03447 Lex(); 03448 03449 if (Str.empty()) 03450 Error(Loc, ".abort detected. Assembly stopping."); 03451 else 03452 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); 03453 // FIXME: Actually abort assembly here. 03454 03455 return false; 03456 } 03457 03458 /// ParseDirectiveInclude 03459 /// ::= .include "filename" 03460 bool AsmParser::ParseDirectiveInclude() { 03461 if (getLexer().isNot(AsmToken::String)) 03462 return TokError("expected string in '.include' directive"); 03463 03464 std::string Filename = getTok().getString(); 03465 SMLoc IncludeLoc = getLexer().getLoc(); 03466 Lex(); 03467 03468 if (getLexer().isNot(AsmToken::EndOfStatement)) 03469 return TokError("unexpected token in '.include' directive"); 03470 03471 // Strip the quotes. 03472 Filename = Filename.substr(1, Filename.size()-2); 03473 03474 // Attempt to switch the lexer to the included file before consuming the end 03475 // of statement to avoid losing it when we switch. 03476 if (EnterIncludeFile(Filename)) { 03477 Error(IncludeLoc, "Could not find include file '" + Filename + "'"); 03478 return true; 03479 } 03480 03481 return false; 03482 } 03483 03484 /// ParseDirectiveIncbin 03485 /// ::= .incbin "filename" 03486 bool AsmParser::ParseDirectiveIncbin() { 03487 if (getLexer().isNot(AsmToken::String)) 03488 return TokError("expected string in '.incbin' directive"); 03489 03490 std::string Filename = getTok().getString(); 03491 SMLoc IncbinLoc = getLexer().getLoc(); 03492 Lex(); 03493 03494 if (getLexer().isNot(AsmToken::EndOfStatement)) 03495 return TokError("unexpected token in '.incbin' directive"); 03496 03497 // Strip the quotes. 03498 Filename = Filename.substr(1, Filename.size()-2); 03499 03500 // Attempt to process the included file. 03501 if (ProcessIncbinFile(Filename)) { 03502 Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); 03503 return true; 03504 } 03505 03506 return false; 03507 } 03508 03509 /// ParseDirectiveIf 03510 /// ::= .if expression 03511 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) { 03512 TheCondStack.push_back(TheCondState); 03513 TheCondState.TheCond = AsmCond::IfCond; 03514 if (TheCondState.Ignore) { 03515 eatToEndOfStatement(); 03516 } else { 03517 int64_t ExprValue; 03518 if (parseAbsoluteExpression(ExprValue)) 03519 return true; 03520 03521 if (getLexer().isNot(AsmToken::EndOfStatement)) 03522 return TokError("unexpected token in '.if' directive"); 03523 03524 Lex(); 03525 03526 TheCondState.CondMet = ExprValue; 03527 TheCondState.Ignore = !TheCondState.CondMet; 03528 } 03529 03530 return false; 03531 } 03532 03533 /// ParseDirectiveIfb 03534 /// ::= .ifb string 03535 bool AsmParser::ParseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { 03536 TheCondStack.push_back(TheCondState); 03537 TheCondState.TheCond = AsmCond::IfCond; 03538 03539 if (TheCondState.Ignore) { 03540 eatToEndOfStatement(); 03541 } else { 03542 StringRef Str = parseStringToEndOfStatement(); 03543 03544 if (getLexer().isNot(AsmToken::EndOfStatement)) 03545 return TokError("unexpected token in '.ifb' directive"); 03546 03547 Lex(); 03548 03549 TheCondState.CondMet = ExpectBlank == Str.empty(); 03550 TheCondState.Ignore = !TheCondState.CondMet; 03551 } 03552 03553 return false; 03554 } 03555 03556 /// ParseDirectiveIfc 03557 /// ::= .ifc string1, string2 03558 bool AsmParser::ParseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { 03559 TheCondStack.push_back(TheCondState); 03560 TheCondState.TheCond = AsmCond::IfCond; 03561 03562 if (TheCondState.Ignore) { 03563 eatToEndOfStatement(); 03564 } else { 03565 StringRef Str1 = ParseStringToComma(); 03566 03567 if (getLexer().isNot(AsmToken::Comma)) 03568 return TokError("unexpected token in '.ifc' directive"); 03569 03570 Lex(); 03571 03572 StringRef Str2 = parseStringToEndOfStatement(); 03573 03574 if (getLexer().isNot(AsmToken::EndOfStatement)) 03575 return TokError("unexpected token in '.ifc' directive"); 03576 03577 Lex(); 03578 03579 TheCondState.CondMet = ExpectEqual == (Str1 == Str2); 03580 TheCondState.Ignore = !TheCondState.CondMet; 03581 } 03582 03583 return false; 03584 } 03585 03586 /// ParseDirectiveIfdef 03587 /// ::= .ifdef symbol 03588 bool AsmParser::ParseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { 03589 StringRef Name; 03590 TheCondStack.push_back(TheCondState); 03591 TheCondState.TheCond = AsmCond::IfCond; 03592 03593 if (TheCondState.Ignore) { 03594 eatToEndOfStatement(); 03595 } else { 03596 if (parseIdentifier(Name)) 03597 return TokError("expected identifier after '.ifdef'"); 03598 03599 Lex(); 03600 03601 MCSymbol *Sym = getContext().LookupSymbol(Name); 03602 03603 if (expect_defined) 03604 TheCondState.CondMet = (Sym != NULL && !Sym->isUndefined()); 03605 else 03606 TheCondState.CondMet = (Sym == NULL || Sym->isUndefined()); 03607 TheCondState.Ignore = !TheCondState.CondMet; 03608 } 03609 03610 return false; 03611 } 03612 03613 /// ParseDirectiveElseIf 03614 /// ::= .elseif expression 03615 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) { 03616 if (TheCondState.TheCond != AsmCond::IfCond && 03617 TheCondState.TheCond != AsmCond::ElseIfCond) 03618 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or " 03619 " an .elseif"); 03620 TheCondState.TheCond = AsmCond::ElseIfCond; 03621 03622 bool LastIgnoreState = false; 03623 if (!TheCondStack.empty()) 03624 LastIgnoreState = TheCondStack.back().Ignore; 03625 if (LastIgnoreState || TheCondState.CondMet) { 03626 TheCondState.Ignore = true; 03627 eatToEndOfStatement(); 03628 } else { 03629 int64_t ExprValue; 03630 if (parseAbsoluteExpression(ExprValue)) 03631 return true; 03632 03633 if (getLexer().isNot(AsmToken::EndOfStatement)) 03634 return TokError("unexpected token in '.elseif' directive"); 03635 03636 Lex(); 03637 TheCondState.CondMet = ExprValue; 03638 TheCondState.Ignore = !TheCondState.CondMet; 03639 } 03640 03641 return false; 03642 } 03643 03644 /// ParseDirectiveElse 03645 /// ::= .else 03646 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) { 03647 if (getLexer().isNot(AsmToken::EndOfStatement)) 03648 return TokError("unexpected token in '.else' directive"); 03649 03650 Lex(); 03651 03652 if (TheCondState.TheCond != AsmCond::IfCond && 03653 TheCondState.TheCond != AsmCond::ElseIfCond) 03654 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an " 03655 ".elseif"); 03656 TheCondState.TheCond = AsmCond::ElseCond; 03657 bool LastIgnoreState = false; 03658 if (!TheCondStack.empty()) 03659 LastIgnoreState = TheCondStack.back().Ignore; 03660 if (LastIgnoreState || TheCondState.CondMet) 03661 TheCondState.Ignore = true; 03662 else 03663 TheCondState.Ignore = false; 03664 03665 return false; 03666 } 03667 03668 /// ParseDirectiveEndIf 03669 /// ::= .endif 03670 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) { 03671 if (getLexer().isNot(AsmToken::EndOfStatement)) 03672 return TokError("unexpected token in '.endif' directive"); 03673 03674 Lex(); 03675 03676 if ((TheCondState.TheCond == AsmCond::NoCond) || 03677 TheCondStack.empty()) 03678 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or " 03679 ".else"); 03680 if (!TheCondStack.empty()) { 03681 TheCondState = TheCondStack.back(); 03682 TheCondStack.pop_back(); 03683 } 03684 03685 return false; 03686 } 03687 03688 void AsmParser::initializeDirectiveKindMap() { 03689 DirectiveKindMap[".set"] = DK_SET; 03690 DirectiveKindMap[".equ"] = DK_EQU; 03691 DirectiveKindMap[".equiv"] = DK_EQUIV; 03692 DirectiveKindMap[".ascii"] = DK_ASCII; 03693 DirectiveKindMap[".asciz"] = DK_ASCIZ; 03694 DirectiveKindMap[".string"] = DK_STRING; 03695 DirectiveKindMap[".byte"] = DK_BYTE; 03696 DirectiveKindMap[".short"] = DK_SHORT; 03697 DirectiveKindMap[".value"] = DK_VALUE; 03698 DirectiveKindMap[".2byte"] = DK_2BYTE; 03699 DirectiveKindMap[".long"] = DK_LONG; 03700 DirectiveKindMap[".int"] = DK_INT; 03701 DirectiveKindMap[".4byte"] = DK_4BYTE; 03702 DirectiveKindMap[".quad"] = DK_QUAD; 03703 DirectiveKindMap[".8byte"] = DK_8BYTE; 03704 DirectiveKindMap[".single"] = DK_SINGLE; 03705 DirectiveKindMap[".float"] = DK_FLOAT; 03706 DirectiveKindMap[".double"] = DK_DOUBLE; 03707 DirectiveKindMap[".align"] = DK_ALIGN; 03708 DirectiveKindMap[".align32"] = DK_ALIGN32; 03709 DirectiveKindMap[".balign"] = DK_BALIGN; 03710 DirectiveKindMap[".balignw"] = DK_BALIGNW; 03711 DirectiveKindMap[".balignl"] = DK_BALIGNL; 03712 DirectiveKindMap[".p2align"] = DK_P2ALIGN; 03713 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; 03714 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; 03715 DirectiveKindMap[".org"] = DK_ORG; 03716 DirectiveKindMap[".fill"] = DK_FILL; 03717 DirectiveKindMap[".zero"] = DK_ZERO; 03718 DirectiveKindMap[".extern"] = DK_EXTERN; 03719 DirectiveKindMap[".globl"] = DK_GLOBL; 03720 DirectiveKindMap[".global"] = DK_GLOBAL; 03721 DirectiveKindMap[".indirect_symbol"] = DK_INDIRECT_SYMBOL; 03722 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; 03723 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; 03724 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; 03725 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; 03726 DirectiveKindMap[".reference"] = DK_REFERENCE; 03727 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; 03728 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; 03729 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; 03730 DirectiveKindMap[".comm"] = DK_COMM; 03731 DirectiveKindMap[".common"] = DK_COMMON; 03732 DirectiveKindMap[".lcomm"] = DK_LCOMM; 03733 DirectiveKindMap[".abort"] = DK_ABORT; 03734 DirectiveKindMap[".include"] = DK_INCLUDE; 03735 DirectiveKindMap[".incbin"] = DK_INCBIN; 03736 DirectiveKindMap[".code16"] = DK_CODE16; 03737 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; 03738 DirectiveKindMap[".rept"] = DK_REPT; 03739 DirectiveKindMap[".irp"] = DK_IRP; 03740 DirectiveKindMap[".irpc"] = DK_IRPC; 03741 DirectiveKindMap[".endr"] = DK_ENDR; 03742 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; 03743 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; 03744 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; 03745 DirectiveKindMap[".if"] = DK_IF; 03746 DirectiveKindMap[".ifb"] = DK_IFB; 03747 DirectiveKindMap[".ifnb"] = DK_IFNB; 03748 DirectiveKindMap[".ifc"] = DK_IFC; 03749 DirectiveKindMap[".ifnc"] = DK_IFNC; 03750 DirectiveKindMap[".ifdef"] = DK_IFDEF; 03751 DirectiveKindMap[".ifndef"] = DK_IFNDEF; 03752 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; 03753 DirectiveKindMap[".elseif"] = DK_ELSEIF; 03754 DirectiveKindMap[".else"] = DK_ELSE; 03755 DirectiveKindMap[".endif"] = DK_ENDIF; 03756 DirectiveKindMap[".skip"] = DK_SKIP; 03757 DirectiveKindMap[".space"] = DK_SPACE; 03758 DirectiveKindMap[".file"] = DK_FILE; 03759 DirectiveKindMap[".line"] = DK_LINE; 03760 DirectiveKindMap[".loc"] = DK_LOC; 03761 DirectiveKindMap[".stabs"] = DK_STABS; 03762 DirectiveKindMap[".sleb128"] = DK_SLEB128; 03763 DirectiveKindMap[".uleb128"] = DK_ULEB128; 03764 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; 03765 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; 03766 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; 03767 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; 03768 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; 03769 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; 03770 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; 03771 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; 03772 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; 03773 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; 03774 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; 03775 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; 03776 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; 03777 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; 03778 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; 03779 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; 03780 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; 03781 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; 03782 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; 03783 DirectiveKindMap[".macros_on"] = DK_MACROS_ON; 03784 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; 03785 DirectiveKindMap[".macro"] = DK_MACRO; 03786 DirectiveKindMap[".endm"] = DK_ENDM; 03787 DirectiveKindMap[".endmacro"] = DK_ENDMACRO; 03788 DirectiveKindMap[".purgem"] = DK_PURGEM; 03789 } 03790 03791 03792 MCAsmMacro *AsmParser::ParseMacroLikeBody(SMLoc DirectiveLoc) { 03793 AsmToken EndToken, StartToken = getTok(); 03794 03795 unsigned NestLevel = 0; 03796 for (;;) { 03797 // Check whether we have reached the end of the file. 03798 if (getLexer().is(AsmToken::Eof)) { 03799 Error(DirectiveLoc, "no matching '.endr' in definition"); 03800 return 0; 03801 } 03802 03803 if (Lexer.is(AsmToken::Identifier) && 03804 (getTok().getIdentifier() == ".rept")) { 03805 ++NestLevel; 03806 } 03807 03808 // Otherwise, check whether we have reached the .endr. 03809 if (Lexer.is(AsmToken::Identifier) && 03810 getTok().getIdentifier() == ".endr") { 03811 if (NestLevel == 0) { 03812 EndToken = getTok(); 03813 Lex(); 03814 if (Lexer.isNot(AsmToken::EndOfStatement)) { 03815 TokError("unexpected token in '.endr' directive"); 03816 return 0; 03817 } 03818 break; 03819 } 03820 --NestLevel; 03821 } 03822 03823 // Otherwise, scan till the end of the statement. 03824 eatToEndOfStatement(); 03825 } 03826 03827 const char *BodyStart = StartToken.getLoc().getPointer(); 03828 const char *BodyEnd = EndToken.getLoc().getPointer(); 03829 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); 03830 03831 // We Are Anonymous. 03832 StringRef Name; 03833 MCAsmMacroParameters Parameters; 03834 return new MCAsmMacro(Name, Body, Parameters); 03835 } 03836 03837 void AsmParser::InstantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, 03838 raw_svector_ostream &OS) { 03839 OS << ".endr\n"; 03840 03841 MemoryBuffer *Instantiation = 03842 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); 03843 03844 // Create the macro instantiation object and add to the current macro 03845 // instantiation stack. 03846 MacroInstantiation *MI = new MacroInstantiation(M, DirectiveLoc, 03847 CurBuffer, 03848 getTok().getLoc(), 03849 Instantiation); 03850 ActiveMacros.push_back(MI); 03851 03852 // Jump to the macro instantiation and prime the lexer. 03853 CurBuffer = SrcMgr.AddNewSourceBuffer(MI->Instantiation, SMLoc()); 03854 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)); 03855 Lex(); 03856 } 03857 03858 bool AsmParser::ParseDirectiveRept(SMLoc DirectiveLoc) { 03859 int64_t Count; 03860 if (parseAbsoluteExpression(Count)) 03861 return TokError("unexpected token in '.rept' directive"); 03862 03863 if (Count < 0) 03864 return TokError("Count is negative"); 03865 03866 if (Lexer.isNot(AsmToken::EndOfStatement)) 03867 return TokError("unexpected token in '.rept' directive"); 03868 03869 // Eat the end of statement. 03870 Lex(); 03871 03872 // Lex the rept definition. 03873 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); 03874 if (!M) 03875 return true; 03876 03877 // Macro instantiation is lexical, unfortunately. We construct a new buffer 03878 // to hold the macro body with substitutions. 03879 SmallString<256> Buf; 03880 MCAsmMacroParameters Parameters; 03881 MCAsmMacroArguments A; 03882 raw_svector_ostream OS(Buf); 03883 while (Count--) { 03884 if (expandMacro(OS, M->Body, Parameters, A, getTok().getLoc())) 03885 return true; 03886 } 03887 InstantiateMacroLikeBody(M, DirectiveLoc, OS); 03888 03889 return false; 03890 } 03891 03892 /// ParseDirectiveIrp 03893 /// ::= .irp symbol,values 03894 bool AsmParser::ParseDirectiveIrp(SMLoc DirectiveLoc) { 03895 MCAsmMacroParameters Parameters; 03896 MCAsmMacroParameter Parameter; 03897 03898 if (parseIdentifier(Parameter.first)) 03899 return TokError("expected identifier in '.irp' directive"); 03900 03901 Parameters.push_back(Parameter); 03902 03903 if (Lexer.isNot(AsmToken::Comma)) 03904 return TokError("expected comma in '.irp' directive"); 03905 03906 Lex(); 03907 03908 MCAsmMacroArguments A; 03909 if (ParseMacroArguments(0, A)) 03910 return true; 03911 03912 // Eat the end of statement. 03913 Lex(); 03914 03915 // Lex the irp definition. 03916 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); 03917 if (!M) 03918 return true; 03919 03920 // Macro instantiation is lexical, unfortunately. We construct a new buffer 03921 // to hold the macro body with substitutions. 03922 SmallString<256> Buf; 03923 raw_svector_ostream OS(Buf); 03924 03925 for (MCAsmMacroArguments::iterator i = A.begin(), e = A.end(); i != e; ++i) { 03926 MCAsmMacroArguments Args; 03927 Args.push_back(*i); 03928 03929 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) 03930 return true; 03931 } 03932 03933 InstantiateMacroLikeBody(M, DirectiveLoc, OS); 03934 03935 return false; 03936 } 03937 03938 /// ParseDirectiveIrpc 03939 /// ::= .irpc symbol,values 03940 bool AsmParser::ParseDirectiveIrpc(SMLoc DirectiveLoc) { 03941 MCAsmMacroParameters Parameters; 03942 MCAsmMacroParameter Parameter; 03943 03944 if (parseIdentifier(Parameter.first)) 03945 return TokError("expected identifier in '.irpc' directive"); 03946 03947 Parameters.push_back(Parameter); 03948 03949 if (Lexer.isNot(AsmToken::Comma)) 03950 return TokError("expected comma in '.irpc' directive"); 03951 03952 Lex(); 03953 03954 MCAsmMacroArguments A; 03955 if (ParseMacroArguments(0, A)) 03956 return true; 03957 03958 if (A.size() != 1 || A.front().size() != 1) 03959 return TokError("unexpected token in '.irpc' directive"); 03960 03961 // Eat the end of statement. 03962 Lex(); 03963 03964 // Lex the irpc definition. 03965 MCAsmMacro *M = ParseMacroLikeBody(DirectiveLoc); 03966 if (!M) 03967 return true; 03968 03969 // Macro instantiation is lexical, unfortunately. We construct a new buffer 03970 // to hold the macro body with substitutions. 03971 SmallString<256> Buf; 03972 raw_svector_ostream OS(Buf); 03973 03974 StringRef Values = A.front().front().getString(); 03975 std::size_t I, End = Values.size(); 03976 for (I = 0; I < End; ++I) { 03977 MCAsmMacroArgument Arg; 03978 Arg.push_back(AsmToken(AsmToken::Identifier, Values.slice(I, I+1))); 03979 03980 MCAsmMacroArguments Args; 03981 Args.push_back(Arg); 03982 03983 if (expandMacro(OS, M->Body, Parameters, Args, getTok().getLoc())) 03984 return true; 03985 } 03986 03987 InstantiateMacroLikeBody(M, DirectiveLoc, OS); 03988 03989 return false; 03990 } 03991 03992 bool AsmParser::ParseDirectiveEndr(SMLoc DirectiveLoc) { 03993 if (ActiveMacros.empty()) 03994 return TokError("unmatched '.endr' directive"); 03995 03996 // The only .repl that should get here are the ones created by 03997 // InstantiateMacroLikeBody. 03998 assert(getLexer().is(AsmToken::EndOfStatement)); 03999 04000 HandleMacroExit(); 04001 return false; 04002 } 04003 04004 bool AsmParser::ParseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, 04005 size_t Len) { 04006 const MCExpr *Value; 04007 SMLoc ExprLoc = getLexer().getLoc(); 04008 if (parseExpression(Value)) 04009 return true; 04010 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); 04011 if (!MCE) 04012 return Error(ExprLoc, "unexpected expression in _emit"); 04013 uint64_t IntValue = MCE->getValue(); 04014 if (!isUIntN(8, IntValue) && !isIntN(8, IntValue)) 04015 return Error(ExprLoc, "literal value out of range for directive"); 04016 04017 Info.AsmRewrites->push_back(AsmRewrite(AOK_Emit, IDLoc, Len)); 04018 return false; 04019 } 04020 04021 bool AsmParser::ParseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { 04022 const MCExpr *Value; 04023 SMLoc ExprLoc = getLexer().getLoc(); 04024 if (parseExpression(Value)) 04025 return true; 04026 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); 04027 if (!MCE) 04028 return Error(ExprLoc, "unexpected expression in align"); 04029 uint64_t IntValue = MCE->getValue(); 04030 if (!isPowerOf2_64(IntValue)) 04031 return Error(ExprLoc, "literal value not a power of two greater then zero"); 04032 04033 Info.AsmRewrites->push_back(AsmRewrite(AOK_Align, IDLoc, 5, 04034 Log2_64(IntValue))); 04035 return false; 04036 } 04037 04038 // We are comparing pointers, but the pointers are relative to a single string. 04039 // Thus, this should always be deterministic. 04040 static int RewritesSort(const void *A, const void *B) { 04041 const AsmRewrite *AsmRewriteA = static_cast<const AsmRewrite *>(A); 04042 const AsmRewrite *AsmRewriteB = static_cast<const AsmRewrite *>(B); 04043 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) 04044 return -1; 04045 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) 04046 return 1; 04047 04048 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output 04049 // rewrite to the same location. Make sure the SizeDirective rewrite is 04050 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This 04051 // ensures the sort algorithm is stable. 04052 if (AsmRewritePrecedence [AsmRewriteA->Kind] > 04053 AsmRewritePrecedence [AsmRewriteB->Kind]) 04054 return -1; 04055 04056 if (AsmRewritePrecedence [AsmRewriteA->Kind] < 04057 AsmRewritePrecedence [AsmRewriteB->Kind]) 04058 return 1; 04059 llvm_unreachable ("Unstable rewrite sort."); 04060 } 04061 04062 bool 04063 AsmParser::parseMSInlineAsm(void *AsmLoc, std::string &AsmString, 04064 unsigned &NumOutputs, unsigned &NumInputs, 04065 SmallVectorImpl<std::pair<void *, bool> > &OpDecls, 04066 SmallVectorImpl<std::string> &Constraints, 04067 SmallVectorImpl<std::string> &Clobbers, 04068 const MCInstrInfo *MII, 04069 const MCInstPrinter *IP, 04070 MCAsmParserSemaCallback &SI) { 04071 SmallVector<void *, 4> InputDecls; 04072 SmallVector<void *, 4> OutputDecls; 04073 SmallVector<bool, 4> InputDeclsAddressOf; 04074 SmallVector<bool, 4> OutputDeclsAddressOf; 04075 SmallVector<std::string, 4> InputConstraints; 04076 SmallVector<std::string, 4> OutputConstraints; 04077 SmallVector<unsigned, 4> ClobberRegs; 04078 04079 SmallVector<AsmRewrite, 4> AsmStrRewrites; 04080 04081 // Prime the lexer. 04082 Lex(); 04083 04084 // While we have input, parse each statement. 04085 unsigned InputIdx = 0; 04086 unsigned OutputIdx = 0; 04087 while (getLexer().isNot(AsmToken::Eof)) { 04088 ParseStatementInfo Info(&AsmStrRewrites); 04089 if (ParseStatement(Info)) 04090 return true; 04091 04092 if (Info.ParseError) 04093 return true; 04094 04095 if (Info.Opcode == ~0U) 04096 continue; 04097 04098 const MCInstrDesc &Desc = MII->get(Info.Opcode); 04099 04100 // Build the list of clobbers, outputs and inputs. 04101 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { 04102 MCParsedAsmOperand *Operand = Info.ParsedOperands[i]; 04103 04104 // Immediate. 04105 if (Operand->isImm()) 04106 continue; 04107 04108 // Register operand. 04109 if (Operand->isReg() && !Operand->needAddressOf()) { 04110 unsigned NumDefs = Desc.getNumDefs(); 04111 // Clobber. 04112 if (NumDefs && Operand->getMCOperandNum() < NumDefs) 04113 ClobberRegs.push_back(Operand->getReg()); 04114 continue; 04115 } 04116 04117 // Expr/Input or Output. 04118 StringRef SymName = Operand->getSymName(); 04119 if (SymName.empty()) 04120 continue; 04121 04122 void *OpDecl = Operand->getOpDecl(); 04123 if (!OpDecl) 04124 continue; 04125 04126 bool isOutput = (i == 1) && Desc.mayStore(); 04127 SMLoc Start = SMLoc::getFromPointer(SymName.data()); 04128 if (isOutput) { 04129 ++InputIdx; 04130 OutputDecls.push_back(OpDecl); 04131 OutputDeclsAddressOf.push_back(Operand->needAddressOf()); 04132 OutputConstraints.push_back('=' + Operand->getConstraint().str()); 04133 AsmStrRewrites.push_back(AsmRewrite(AOK_Output, Start, SymName.size())); 04134 } else { 04135 InputDecls.push_back(OpDecl); 04136 InputDeclsAddressOf.push_back(Operand->needAddressOf()); 04137 InputConstraints.push_back(Operand->getConstraint().str()); 04138 AsmStrRewrites.push_back(AsmRewrite(AOK_Input, Start, SymName.size())); 04139 } 04140 } 04141 } 04142 04143 // Set the number of Outputs and Inputs. 04144 NumOutputs = OutputDecls.size(); 04145 NumInputs = InputDecls.size(); 04146 04147 // Set the unique clobbers. 04148 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); 04149 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), 04150 ClobberRegs.end()); 04151 Clobbers.assign(ClobberRegs.size(), std::string()); 04152 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { 04153 raw_string_ostream OS(Clobbers[I]); 04154 IP->printRegName(OS, ClobberRegs[I]); 04155 } 04156 04157 // Merge the various outputs and inputs. Output are expected first. 04158 if (NumOutputs || NumInputs) { 04159 unsigned NumExprs = NumOutputs + NumInputs; 04160 OpDecls.resize(NumExprs); 04161 Constraints.resize(NumExprs); 04162 for (unsigned i = 0; i < NumOutputs; ++i) { 04163 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); 04164 Constraints[i] = OutputConstraints[i]; 04165 } 04166 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { 04167 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); 04168 Constraints[j] = InputConstraints[i]; 04169 } 04170 } 04171 04172 // Build the IR assembly string. 04173 std::string AsmStringIR; 04174 raw_string_ostream OS(AsmStringIR); 04175 const char *AsmStart = SrcMgr.getMemoryBuffer(0)->getBufferStart(); 04176 const char *AsmEnd = SrcMgr.getMemoryBuffer(0)->getBufferEnd(); 04177 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), RewritesSort); 04178 for (SmallVectorImpl<AsmRewrite>::iterator I = AsmStrRewrites.begin(), 04179 E = AsmStrRewrites.end(); 04180 I != E; ++I) { 04181 AsmRewriteKind Kind = (*I).Kind; 04182 if (Kind == AOK_Delete) 04183 continue; 04184 04185 const char *Loc = (*I).Loc.getPointer(); 04186 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); 04187 04188 // Emit everything up to the immediate/expression. 04189 unsigned Len = Loc - AsmStart; 04190 if (Len) 04191 OS << StringRef(AsmStart, Len); 04192 04193 // Skip the original expression. 04194 if (Kind == AOK_Skip) { 04195 AsmStart = Loc + (*I).Len; 04196 continue; 04197 } 04198 04199 unsigned AdditionalSkip = 0; 04200 // Rewrite expressions in $N notation. 04201 switch (Kind) { 04202 default: break; 04203 case AOK_Imm: 04204 OS << "$$" << (*I).Val; 04205 break; 04206 case AOK_ImmPrefix: 04207 OS << "$$"; 04208 break; 04209 case AOK_Input: 04210 OS << '$' << InputIdx++; 04211 break; 04212 case AOK_Output: 04213 OS << '$' << OutputIdx++; 04214 break; 04215 case AOK_SizeDirective: 04216 switch ((*I).Val) { 04217 default: break; 04218 case 8: OS << "byte ptr "; break; 04219 case 16: OS << "word ptr "; break; 04220 case 32: OS << "dword ptr "; break; 04221 case 64: OS << "qword ptr "; break; 04222 case 80: OS << "xword ptr "; break; 04223 case 128: OS << "xmmword ptr "; break; 04224 case 256: OS << "ymmword ptr "; break; 04225 } 04226 break; 04227 case AOK_Emit: 04228 OS << ".byte"; 04229 break; 04230 case AOK_Align: { 04231 unsigned Val = (*I).Val; 04232 OS << ".align " << Val; 04233 04234 // Skip the original immediate. 04235 assert(Val < 10 && "Expected alignment less then 2^10."); 04236 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; 04237 break; 04238 } 04239 case AOK_DotOperator: 04240 OS << (*I).Val; 04241 break; 04242 } 04243 04244 // Skip the original expression. 04245 AsmStart = Loc + (*I).Len + AdditionalSkip; 04246 } 04247 04248 // Emit the remainder of the asm string. 04249 if (AsmStart != AsmEnd) 04250 OS << StringRef(AsmStart, AsmEnd - AsmStart); 04251 04252 AsmString = OS.str(); 04253 return false; 04254 } 04255 04256 /// \brief Create an MCAsmParser instance. 04257 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, 04258 MCContext &C, MCStreamer &Out, 04259 const MCAsmInfo &MAI) { 04260 return new AsmParser(SM, C, Out, MAI); 04261 }