LLVM API Documentation
00001 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===// 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 #ifndef LLVM_MC_MCEXPR_H 00011 #define LLVM_MC_MCEXPR_H 00012 00013 #include "llvm/ADT/DenseMap.h" 00014 #include "llvm/Support/Casting.h" 00015 #include "llvm/Support/DataTypes.h" 00016 00017 namespace llvm { 00018 class MCAsmInfo; 00019 class MCAsmLayout; 00020 class MCAssembler; 00021 class MCContext; 00022 class MCFixup; 00023 class MCSection; 00024 class MCSectionData; 00025 class MCStreamer; 00026 class MCSymbol; 00027 class MCValue; 00028 class raw_ostream; 00029 class StringRef; 00030 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap; 00031 00032 /// MCExpr - Base class for the full range of assembler expressions which are 00033 /// needed for parsing. 00034 class MCExpr { 00035 public: 00036 enum ExprKind { 00037 Binary, ///< Binary expressions. 00038 Constant, ///< Constant expressions. 00039 SymbolRef, ///< References to labels and assigned expressions. 00040 Unary, ///< Unary expressions. 00041 Target ///< Target specific expression. 00042 }; 00043 00044 private: 00045 ExprKind Kind; 00046 00047 MCExpr(const MCExpr&) LLVM_DELETED_FUNCTION; 00048 void operator=(const MCExpr&) LLVM_DELETED_FUNCTION; 00049 00050 bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 00051 const MCAsmLayout *Layout, 00052 const SectionAddrMap *Addrs) const; 00053 00054 bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm, 00055 const MCAsmLayout *Layout, 00056 const SectionAddrMap *Addrs, bool InSet) const; 00057 00058 protected: 00059 explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {} 00060 00061 bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, 00062 const MCAsmLayout *Layout, 00063 const MCFixup *Fixup, 00064 const SectionAddrMap *Addrs, bool InSet, 00065 bool ForceVarExpansion) const; 00066 00067 public: 00068 /// @name Accessors 00069 /// @{ 00070 00071 ExprKind getKind() const { return Kind; } 00072 00073 /// @} 00074 /// @name Utility Methods 00075 /// @{ 00076 00077 void print(raw_ostream &OS) const; 00078 void dump() const; 00079 00080 /// @} 00081 /// @name Expression Evaluation 00082 /// @{ 00083 00084 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value. 00085 /// 00086 /// @param Res - The absolute value, if evaluation succeeds. 00087 /// @param Layout - The assembler layout object to use for evaluating symbol 00088 /// values. If not given, then only non-symbolic expressions will be 00089 /// evaluated. 00090 /// @result - True on success. 00091 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout, 00092 const SectionAddrMap &Addrs) const; 00093 bool EvaluateAsAbsolute(int64_t &Res) const; 00094 bool EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const; 00095 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const; 00096 00097 int64_t evaluateKnownAbsolute(const MCAsmLayout &Layout) const; 00098 00099 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable 00100 /// value, i.e. an expression of the fixed form (a - b + constant). 00101 /// 00102 /// @param Res - The relocatable value, if evaluation succeeds. 00103 /// @param Layout - The assembler layout object to use for evaluating values. 00104 /// @param Fixup - The Fixup object if available. 00105 /// @result - True on success. 00106 bool EvaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, 00107 const MCFixup *Fixup) const; 00108 00109 /// \brief Try to evaluate the expression to the form (a - b + constant) where 00110 /// neither a nor b are variables. 00111 /// 00112 /// This is a more aggressive variant of EvaluateAsRelocatable. The intended 00113 /// use is for when relocations are not available, like the symbol value in 00114 /// the symbol table. 00115 bool EvaluateAsValue(MCValue &Res, const MCAsmLayout *Layout, 00116 const MCFixup *Fixup) const; 00117 00118 /// FindAssociatedSection - Find the "associated section" for this expression, 00119 /// which is currently defined as the absolute section for constants, or 00120 /// otherwise the section associated with the first defined symbol in the 00121 /// expression. 00122 const MCSection *FindAssociatedSection() const; 00123 00124 /// @} 00125 }; 00126 00127 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) { 00128 E.print(OS); 00129 return OS; 00130 } 00131 00132 //// MCConstantExpr - Represent a constant integer expression. 00133 class MCConstantExpr : public MCExpr { 00134 int64_t Value; 00135 00136 explicit MCConstantExpr(int64_t _Value) 00137 : MCExpr(MCExpr::Constant), Value(_Value) {} 00138 00139 public: 00140 /// @name Construction 00141 /// @{ 00142 00143 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx); 00144 00145 /// @} 00146 /// @name Accessors 00147 /// @{ 00148 00149 int64_t getValue() const { return Value; } 00150 00151 /// @} 00152 00153 static bool classof(const MCExpr *E) { 00154 return E->getKind() == MCExpr::Constant; 00155 } 00156 }; 00157 00158 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an 00159 /// expression. 00160 /// 00161 /// A symbol reference in an expression may be a use of a label, a use of an 00162 /// assembler variable (defined constant), or constitute an implicit definition 00163 /// of the symbol as external. 00164 class MCSymbolRefExpr : public MCExpr { 00165 public: 00166 enum VariantKind { 00167 VK_None, 00168 VK_Invalid, 00169 00170 VK_GOT, 00171 VK_GOTOFF, 00172 VK_GOTPCREL, 00173 VK_GOTTPOFF, 00174 VK_INDNTPOFF, 00175 VK_NTPOFF, 00176 VK_GOTNTPOFF, 00177 VK_PLT, 00178 VK_TLSGD, 00179 VK_TLSLD, 00180 VK_TLSLDM, 00181 VK_TPOFF, 00182 VK_DTPOFF, 00183 VK_TLVP, // Mach-O thread local variable relocations 00184 VK_TLVPPAGE, 00185 VK_TLVPPAGEOFF, 00186 VK_PAGE, 00187 VK_PAGEOFF, 00188 VK_GOTPAGE, 00189 VK_GOTPAGEOFF, 00190 VK_SECREL, 00191 VK_WEAKREF, // The link between the symbols in .weakref foo, bar 00192 00193 VK_ARM_NONE, 00194 VK_ARM_TARGET1, 00195 VK_ARM_TARGET2, 00196 VK_ARM_PREL31, 00197 VK_ARM_TLSLDO, // symbol(tlsldo) 00198 VK_ARM_TLSCALL, // symbol(tlscall) 00199 VK_ARM_TLSDESC, // symbol(tlsdesc) 00200 VK_ARM_TLSDESCSEQ, 00201 00202 VK_PPC_LO, // symbol@l 00203 VK_PPC_HI, // symbol@h 00204 VK_PPC_HA, // symbol@ha 00205 VK_PPC_HIGHER, // symbol@higher 00206 VK_PPC_HIGHERA, // symbol@highera 00207 VK_PPC_HIGHEST, // symbol@highest 00208 VK_PPC_HIGHESTA, // symbol@highesta 00209 VK_PPC_GOT_LO, // symbol@got@l 00210 VK_PPC_GOT_HI, // symbol@got@h 00211 VK_PPC_GOT_HA, // symbol@got@ha 00212 VK_PPC_TOCBASE, // symbol@tocbase 00213 VK_PPC_TOC, // symbol@toc 00214 VK_PPC_TOC_LO, // symbol@toc@l 00215 VK_PPC_TOC_HI, // symbol@toc@h 00216 VK_PPC_TOC_HA, // symbol@toc@ha 00217 VK_PPC_DTPMOD, // symbol@dtpmod 00218 VK_PPC_TPREL, // symbol@tprel 00219 VK_PPC_TPREL_LO, // symbol@tprel@l 00220 VK_PPC_TPREL_HI, // symbol@tprel@h 00221 VK_PPC_TPREL_HA, // symbol@tprel@ha 00222 VK_PPC_TPREL_HIGHER, // symbol@tprel@higher 00223 VK_PPC_TPREL_HIGHERA, // symbol@tprel@highera 00224 VK_PPC_TPREL_HIGHEST, // symbol@tprel@highest 00225 VK_PPC_TPREL_HIGHESTA, // symbol@tprel@highesta 00226 VK_PPC_DTPREL, // symbol@dtprel 00227 VK_PPC_DTPREL_LO, // symbol@dtprel@l 00228 VK_PPC_DTPREL_HI, // symbol@dtprel@h 00229 VK_PPC_DTPREL_HA, // symbol@dtprel@ha 00230 VK_PPC_DTPREL_HIGHER, // symbol@dtprel@higher 00231 VK_PPC_DTPREL_HIGHERA, // symbol@dtprel@highera 00232 VK_PPC_DTPREL_HIGHEST, // symbol@dtprel@highest 00233 VK_PPC_DTPREL_HIGHESTA,// symbol@dtprel@highesta 00234 VK_PPC_GOT_TPREL, // symbol@got@tprel 00235 VK_PPC_GOT_TPREL_LO, // symbol@got@tprel@l 00236 VK_PPC_GOT_TPREL_HI, // symbol@got@tprel@h 00237 VK_PPC_GOT_TPREL_HA, // symbol@got@tprel@ha 00238 VK_PPC_GOT_DTPREL, // symbol@got@dtprel 00239 VK_PPC_GOT_DTPREL_LO, // symbol@got@dtprel@l 00240 VK_PPC_GOT_DTPREL_HI, // symbol@got@dtprel@h 00241 VK_PPC_GOT_DTPREL_HA, // symbol@got@dtprel@ha 00242 VK_PPC_TLS, // symbol@tls 00243 VK_PPC_GOT_TLSGD, // symbol@got@tlsgd 00244 VK_PPC_GOT_TLSGD_LO, // symbol@got@tlsgd@l 00245 VK_PPC_GOT_TLSGD_HI, // symbol@got@tlsgd@h 00246 VK_PPC_GOT_TLSGD_HA, // symbol@got@tlsgd@ha 00247 VK_PPC_TLSGD, // symbol@tlsgd 00248 VK_PPC_GOT_TLSLD, // symbol@got@tlsld 00249 VK_PPC_GOT_TLSLD_LO, // symbol@got@tlsld@l 00250 VK_PPC_GOT_TLSLD_HI, // symbol@got@tlsld@h 00251 VK_PPC_GOT_TLSLD_HA, // symbol@got@tlsld@ha 00252 VK_PPC_TLSLD, // symbol@tlsld 00253 00254 VK_Mips_GPREL, 00255 VK_Mips_GOT_CALL, 00256 VK_Mips_GOT16, 00257 VK_Mips_GOT, 00258 VK_Mips_ABS_HI, 00259 VK_Mips_ABS_LO, 00260 VK_Mips_TLSGD, 00261 VK_Mips_TLSLDM, 00262 VK_Mips_DTPREL_HI, 00263 VK_Mips_DTPREL_LO, 00264 VK_Mips_GOTTPREL, 00265 VK_Mips_TPREL_HI, 00266 VK_Mips_TPREL_LO, 00267 VK_Mips_GPOFF_HI, 00268 VK_Mips_GPOFF_LO, 00269 VK_Mips_GOT_DISP, 00270 VK_Mips_GOT_PAGE, 00271 VK_Mips_GOT_OFST, 00272 VK_Mips_HIGHER, 00273 VK_Mips_HIGHEST, 00274 VK_Mips_GOT_HI16, 00275 VK_Mips_GOT_LO16, 00276 VK_Mips_CALL_HI16, 00277 VK_Mips_CALL_LO16, 00278 VK_Mips_PCREL_HI16, 00279 VK_Mips_PCREL_LO16, 00280 00281 VK_COFF_IMGREL32 // symbol@imgrel (image-relative) 00282 }; 00283 00284 private: 00285 /// The symbol being referenced. 00286 const MCSymbol *Symbol; 00287 00288 /// The symbol reference modifier. 00289 const VariantKind Kind; 00290 00291 /// MCAsmInfo that is used to print symbol variants correctly. 00292 const MCAsmInfo *MAI; 00293 00294 explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind, 00295 const MCAsmInfo *_MAI) 00296 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind), MAI(_MAI) { 00297 assert(Symbol); 00298 assert(MAI); 00299 } 00300 00301 public: 00302 /// @name Construction 00303 /// @{ 00304 00305 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) { 00306 return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx); 00307 } 00308 00309 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind, 00310 MCContext &Ctx); 00311 static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind, 00312 MCContext &Ctx); 00313 00314 /// @} 00315 /// @name Accessors 00316 /// @{ 00317 00318 const MCSymbol &getSymbol() const { return *Symbol; } 00319 const MCAsmInfo &getMCAsmInfo() const { return *MAI; } 00320 00321 VariantKind getKind() const { return Kind; } 00322 00323 /// @} 00324 /// @name Static Utility Functions 00325 /// @{ 00326 00327 static StringRef getVariantKindName(VariantKind Kind); 00328 00329 static VariantKind getVariantKindForName(StringRef Name); 00330 00331 /// @} 00332 00333 static bool classof(const MCExpr *E) { 00334 return E->getKind() == MCExpr::SymbolRef; 00335 } 00336 }; 00337 00338 /// MCUnaryExpr - Unary assembler expressions. 00339 class MCUnaryExpr : public MCExpr { 00340 public: 00341 enum Opcode { 00342 LNot, ///< Logical negation. 00343 Minus, ///< Unary minus. 00344 Not, ///< Bitwise negation. 00345 Plus ///< Unary plus. 00346 }; 00347 00348 private: 00349 Opcode Op; 00350 const MCExpr *Expr; 00351 00352 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr) 00353 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {} 00354 00355 public: 00356 /// @name Construction 00357 /// @{ 00358 00359 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr, 00360 MCContext &Ctx); 00361 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) { 00362 return Create(LNot, Expr, Ctx); 00363 } 00364 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) { 00365 return Create(Minus, Expr, Ctx); 00366 } 00367 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) { 00368 return Create(Not, Expr, Ctx); 00369 } 00370 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) { 00371 return Create(Plus, Expr, Ctx); 00372 } 00373 00374 /// @} 00375 /// @name Accessors 00376 /// @{ 00377 00378 /// getOpcode - Get the kind of this unary expression. 00379 Opcode getOpcode() const { return Op; } 00380 00381 /// getSubExpr - Get the child of this unary expression. 00382 const MCExpr *getSubExpr() const { return Expr; } 00383 00384 /// @} 00385 00386 static bool classof(const MCExpr *E) { 00387 return E->getKind() == MCExpr::Unary; 00388 } 00389 }; 00390 00391 /// MCBinaryExpr - Binary assembler expressions. 00392 class MCBinaryExpr : public MCExpr { 00393 public: 00394 enum Opcode { 00395 Add, ///< Addition. 00396 And, ///< Bitwise and. 00397 Div, ///< Signed division. 00398 EQ, ///< Equality comparison. 00399 GT, ///< Signed greater than comparison (result is either 0 or some 00400 ///< target-specific non-zero value) 00401 GTE, ///< Signed greater than or equal comparison (result is either 0 or 00402 ///< some target-specific non-zero value). 00403 LAnd, ///< Logical and. 00404 LOr, ///< Logical or. 00405 LT, ///< Signed less than comparison (result is either 0 or 00406 ///< some target-specific non-zero value). 00407 LTE, ///< Signed less than or equal comparison (result is either 0 or 00408 ///< some target-specific non-zero value). 00409 Mod, ///< Signed remainder. 00410 Mul, ///< Multiplication. 00411 NE, ///< Inequality comparison. 00412 Or, ///< Bitwise or. 00413 Shl, ///< Shift left. 00414 Shr, ///< Shift right (arithmetic or logical, depending on target) 00415 Sub, ///< Subtraction. 00416 Xor ///< Bitwise exclusive or. 00417 }; 00418 00419 private: 00420 Opcode Op; 00421 const MCExpr *LHS, *RHS; 00422 00423 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS) 00424 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {} 00425 00426 public: 00427 /// @name Construction 00428 /// @{ 00429 00430 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS, 00431 const MCExpr *RHS, MCContext &Ctx); 00432 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS, 00433 MCContext &Ctx) { 00434 return Create(Add, LHS, RHS, Ctx); 00435 } 00436 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS, 00437 MCContext &Ctx) { 00438 return Create(And, LHS, RHS, Ctx); 00439 } 00440 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS, 00441 MCContext &Ctx) { 00442 return Create(Div, LHS, RHS, Ctx); 00443 } 00444 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS, 00445 MCContext &Ctx) { 00446 return Create(EQ, LHS, RHS, Ctx); 00447 } 00448 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS, 00449 MCContext &Ctx) { 00450 return Create(GT, LHS, RHS, Ctx); 00451 } 00452 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS, 00453 MCContext &Ctx) { 00454 return Create(GTE, LHS, RHS, Ctx); 00455 } 00456 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS, 00457 MCContext &Ctx) { 00458 return Create(LAnd, LHS, RHS, Ctx); 00459 } 00460 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS, 00461 MCContext &Ctx) { 00462 return Create(LOr, LHS, RHS, Ctx); 00463 } 00464 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS, 00465 MCContext &Ctx) { 00466 return Create(LT, LHS, RHS, Ctx); 00467 } 00468 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS, 00469 MCContext &Ctx) { 00470 return Create(LTE, LHS, RHS, Ctx); 00471 } 00472 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS, 00473 MCContext &Ctx) { 00474 return Create(Mod, LHS, RHS, Ctx); 00475 } 00476 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS, 00477 MCContext &Ctx) { 00478 return Create(Mul, LHS, RHS, Ctx); 00479 } 00480 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS, 00481 MCContext &Ctx) { 00482 return Create(NE, LHS, RHS, Ctx); 00483 } 00484 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS, 00485 MCContext &Ctx) { 00486 return Create(Or, LHS, RHS, Ctx); 00487 } 00488 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS, 00489 MCContext &Ctx) { 00490 return Create(Shl, LHS, RHS, Ctx); 00491 } 00492 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS, 00493 MCContext &Ctx) { 00494 return Create(Shr, LHS, RHS, Ctx); 00495 } 00496 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS, 00497 MCContext &Ctx) { 00498 return Create(Sub, LHS, RHS, Ctx); 00499 } 00500 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS, 00501 MCContext &Ctx) { 00502 return Create(Xor, LHS, RHS, Ctx); 00503 } 00504 00505 /// @} 00506 /// @name Accessors 00507 /// @{ 00508 00509 /// getOpcode - Get the kind of this binary expression. 00510 Opcode getOpcode() const { return Op; } 00511 00512 /// getLHS - Get the left-hand side expression of the binary operator. 00513 const MCExpr *getLHS() const { return LHS; } 00514 00515 /// getRHS - Get the right-hand side expression of the binary operator. 00516 const MCExpr *getRHS() const { return RHS; } 00517 00518 /// @} 00519 00520 static bool classof(const MCExpr *E) { 00521 return E->getKind() == MCExpr::Binary; 00522 } 00523 }; 00524 00525 /// MCTargetExpr - This is an extension point for target-specific MCExpr 00526 /// subclasses to implement. 00527 /// 00528 /// NOTE: All subclasses are required to have trivial destructors because 00529 /// MCExprs are bump pointer allocated and not destructed. 00530 class MCTargetExpr : public MCExpr { 00531 virtual void anchor(); 00532 protected: 00533 MCTargetExpr() : MCExpr(Target) {} 00534 virtual ~MCTargetExpr() {} 00535 public: 00536 00537 virtual void PrintImpl(raw_ostream &OS) const = 0; 00538 virtual bool EvaluateAsRelocatableImpl(MCValue &Res, 00539 const MCAsmLayout *Layout, 00540 const MCFixup *Fixup) const = 0; 00541 virtual void visitUsedExpr(MCStreamer& Streamer) const = 0; 00542 virtual const MCSection *FindAssociatedSection() const = 0; 00543 00544 virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0; 00545 00546 static bool classof(const MCExpr *E) { 00547 return E->getKind() == MCExpr::Target; 00548 } 00549 }; 00550 00551 } // end namespace llvm 00552 00553 #endif