1 //===- MCExpr.h - Assembly Level Expressions --------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCEXPR_H
11 #define LLVM_MC_MCEXPR_H
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/Support/Casting.h"
15 #include "llvm/Support/DataTypes.h"
27 typedef DenseMap<const MCSectionData*, uint64_t> SectionAddrMap;
29 /// MCExpr - Base class for the full range of assembler expressions which are
30 /// needed for parsing.
34 Binary, ///< Binary expressions.
35 Constant, ///< Constant expressions.
36 SymbolRef, ///< References to labels and assigned expressions.
37 Unary, ///< Unary expressions.
38 Target ///< Target specific expression.
44 MCExpr(const MCExpr&); // DO NOT IMPLEMENT
45 void operator=(const MCExpr&); // DO NOT IMPLEMENT
47 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout,
48 const SectionAddrMap *Addrs) const;
50 explicit MCExpr(ExprKind _Kind) : Kind(_Kind) {}
52 bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout,
53 const SectionAddrMap *Addrs,
59 ExprKind getKind() const { return Kind; }
62 /// @name Utility Methods
65 void print(raw_ostream &OS) const;
69 /// @name Expression Evaluation
72 /// EvaluateAsAbsolute - Try to evaluate the expression to an absolute value.
74 /// @param Res - The absolute value, if evaluation succeeds.
75 /// @param Layout - The assembler layout object to use for evaluating symbol
76 /// values. If not given, then only non-symbolic expressions will be
78 /// @result - True on success.
79 bool EvaluateAsAbsolute(int64_t &Res) const {
80 return EvaluateAsAbsolute(Res, 0, 0);
82 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const{
83 return EvaluateAsAbsolute(Res, &Layout, 0);
85 bool EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
86 const SectionAddrMap &Addrs) const {
87 return EvaluateAsAbsolute(Res, &Layout, &Addrs);
90 /// EvaluateAsRelocatable - Try to evaluate the expression to a relocatable
91 /// value, i.e. an expression of the fixed form (a - b + constant).
93 /// @param Res - The relocatable value, if evaluation succeeds.
94 /// @param Layout - The assembler layout object to use for evaluating values.
95 /// @result - True on success.
96 bool EvaluateAsRelocatable(MCValue &Res,
97 const MCAsmLayout *Layout = 0) const {
98 return EvaluateAsRelocatableImpl(Res, Layout, 0, false);
103 static bool classof(const MCExpr *) { return true; }
106 inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
111 //// MCConstantExpr - Represent a constant integer expression.
112 class MCConstantExpr : public MCExpr {
115 explicit MCConstantExpr(int64_t _Value)
116 : MCExpr(MCExpr::Constant), Value(_Value) {}
119 /// @name Construction
122 static const MCConstantExpr *Create(int64_t Value, MCContext &Ctx);
128 int64_t getValue() const { return Value; }
132 static bool classof(const MCExpr *E) {
133 return E->getKind() == MCExpr::Constant;
135 static bool classof(const MCConstantExpr *) { return true; }
138 /// MCSymbolRefExpr - Represent a reference to a symbol from inside an
141 /// A symbol reference in an expression may be a use of a label, a use of an
142 /// assembler variable (defined constant), or constitute an implicit definition
143 /// of the symbol as external.
144 class MCSymbolRefExpr : public MCExpr {
163 VK_TLVP, // Mach-O thread local variable relocation
164 VK_ARM_HI16, // The R_ARM_MOVT_ABS relocation (:upper16: in the .s file)
165 VK_ARM_LO16, // The R_ARM_MOVW_ABS_NC relocation (:lower16: in the .w file)
166 // FIXME: We'd really like to use the generic Kinds listed above for these.
167 VK_ARM_PLT, // ARM-style PLT references. i.e., (PLT) instead of @PLT
168 VK_ARM_TLSGD, // ditto for TLSGD, GOT, GOTOFF, TPOFF and GOTTPOFF
175 VK_PPC_HA16, // ha16(symbol)
176 VK_PPC_LO16 // lo16(symbol)
180 /// The symbol being referenced.
181 const MCSymbol *Symbol;
183 /// The symbol reference modifier.
184 const VariantKind Kind;
186 explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind)
187 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind) {}
190 /// @name Construction
193 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
194 return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
197 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
199 static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
206 const MCSymbol &getSymbol() const { return *Symbol; }
208 VariantKind getKind() const { return Kind; }
211 /// @name Static Utility Functions
214 static StringRef getVariantKindName(VariantKind Kind);
216 static VariantKind getVariantKindForName(StringRef Name);
220 static bool classof(const MCExpr *E) {
221 return E->getKind() == MCExpr::SymbolRef;
223 static bool classof(const MCSymbolRefExpr *) { return true; }
226 /// MCUnaryExpr - Unary assembler expressions.
227 class MCUnaryExpr : public MCExpr {
230 LNot, ///< Logical negation.
231 Minus, ///< Unary minus.
232 Not, ///< Bitwise negation.
233 Plus ///< Unary plus.
240 MCUnaryExpr(Opcode _Op, const MCExpr *_Expr)
241 : MCExpr(MCExpr::Unary), Op(_Op), Expr(_Expr) {}
244 /// @name Construction
247 static const MCUnaryExpr *Create(Opcode Op, const MCExpr *Expr,
249 static const MCUnaryExpr *CreateLNot(const MCExpr *Expr, MCContext &Ctx) {
250 return Create(LNot, Expr, Ctx);
252 static const MCUnaryExpr *CreateMinus(const MCExpr *Expr, MCContext &Ctx) {
253 return Create(Minus, Expr, Ctx);
255 static const MCUnaryExpr *CreateNot(const MCExpr *Expr, MCContext &Ctx) {
256 return Create(Not, Expr, Ctx);
258 static const MCUnaryExpr *CreatePlus(const MCExpr *Expr, MCContext &Ctx) {
259 return Create(Plus, Expr, Ctx);
266 /// getOpcode - Get the kind of this unary expression.
267 Opcode getOpcode() const { return Op; }
269 /// getSubExpr - Get the child of this unary expression.
270 const MCExpr *getSubExpr() const { return Expr; }
274 static bool classof(const MCExpr *E) {
275 return E->getKind() == MCExpr::Unary;
277 static bool classof(const MCUnaryExpr *) { return true; }
280 /// MCBinaryExpr - Binary assembler expressions.
281 class MCBinaryExpr : public MCExpr {
285 And, ///< Bitwise and.
286 Div, ///< Signed division.
287 EQ, ///< Equality comparison.
288 GT, ///< Signed greater than comparison (result is either 0 or some
289 ///< target-specific non-zero value)
290 GTE, ///< Signed greater than or equal comparison (result is either 0 or
291 ///< some target-specific non-zero value).
292 LAnd, ///< Logical and.
293 LOr, ///< Logical or.
294 LT, ///< Signed less than comparison (result is either 0 or
295 ///< some target-specific non-zero value).
296 LTE, ///< Signed less than or equal comparison (result is either 0 or
297 ///< some target-specific non-zero value).
298 Mod, ///< Signed remainder.
299 Mul, ///< Multiplication.
300 NE, ///< Inequality comparison.
302 Shl, ///< Shift left.
303 Shr, ///< Shift right (arithmetic or logical, depending on target)
304 Sub, ///< Subtraction.
305 Xor ///< Bitwise exclusive or.
310 const MCExpr *LHS, *RHS;
312 MCBinaryExpr(Opcode _Op, const MCExpr *_LHS, const MCExpr *_RHS)
313 : MCExpr(MCExpr::Binary), Op(_Op), LHS(_LHS), RHS(_RHS) {}
316 /// @name Construction
319 static const MCBinaryExpr *Create(Opcode Op, const MCExpr *LHS,
320 const MCExpr *RHS, MCContext &Ctx);
321 static const MCBinaryExpr *CreateAdd(const MCExpr *LHS, const MCExpr *RHS,
323 return Create(Add, LHS, RHS, Ctx);
325 static const MCBinaryExpr *CreateAnd(const MCExpr *LHS, const MCExpr *RHS,
327 return Create(And, LHS, RHS, Ctx);
329 static const MCBinaryExpr *CreateDiv(const MCExpr *LHS, const MCExpr *RHS,
331 return Create(Div, LHS, RHS, Ctx);
333 static const MCBinaryExpr *CreateEQ(const MCExpr *LHS, const MCExpr *RHS,
335 return Create(EQ, LHS, RHS, Ctx);
337 static const MCBinaryExpr *CreateGT(const MCExpr *LHS, const MCExpr *RHS,
339 return Create(GT, LHS, RHS, Ctx);
341 static const MCBinaryExpr *CreateGTE(const MCExpr *LHS, const MCExpr *RHS,
343 return Create(GTE, LHS, RHS, Ctx);
345 static const MCBinaryExpr *CreateLAnd(const MCExpr *LHS, const MCExpr *RHS,
347 return Create(LAnd, LHS, RHS, Ctx);
349 static const MCBinaryExpr *CreateLOr(const MCExpr *LHS, const MCExpr *RHS,
351 return Create(LOr, LHS, RHS, Ctx);
353 static const MCBinaryExpr *CreateLT(const MCExpr *LHS, const MCExpr *RHS,
355 return Create(LT, LHS, RHS, Ctx);
357 static const MCBinaryExpr *CreateLTE(const MCExpr *LHS, const MCExpr *RHS,
359 return Create(LTE, LHS, RHS, Ctx);
361 static const MCBinaryExpr *CreateMod(const MCExpr *LHS, const MCExpr *RHS,
363 return Create(Mod, LHS, RHS, Ctx);
365 static const MCBinaryExpr *CreateMul(const MCExpr *LHS, const MCExpr *RHS,
367 return Create(Mul, LHS, RHS, Ctx);
369 static const MCBinaryExpr *CreateNE(const MCExpr *LHS, const MCExpr *RHS,
371 return Create(NE, LHS, RHS, Ctx);
373 static const MCBinaryExpr *CreateOr(const MCExpr *LHS, const MCExpr *RHS,
375 return Create(Or, LHS, RHS, Ctx);
377 static const MCBinaryExpr *CreateShl(const MCExpr *LHS, const MCExpr *RHS,
379 return Create(Shl, LHS, RHS, Ctx);
381 static const MCBinaryExpr *CreateShr(const MCExpr *LHS, const MCExpr *RHS,
383 return Create(Shr, LHS, RHS, Ctx);
385 static const MCBinaryExpr *CreateSub(const MCExpr *LHS, const MCExpr *RHS,
387 return Create(Sub, LHS, RHS, Ctx);
389 static const MCBinaryExpr *CreateXor(const MCExpr *LHS, const MCExpr *RHS,
391 return Create(Xor, LHS, RHS, Ctx);
398 /// getOpcode - Get the kind of this binary expression.
399 Opcode getOpcode() const { return Op; }
401 /// getLHS - Get the left-hand side expression of the binary operator.
402 const MCExpr *getLHS() const { return LHS; }
404 /// getRHS - Get the right-hand side expression of the binary operator.
405 const MCExpr *getRHS() const { return RHS; }
409 static bool classof(const MCExpr *E) {
410 return E->getKind() == MCExpr::Binary;
412 static bool classof(const MCBinaryExpr *) { return true; }
415 /// MCTargetExpr - This is an extension point for target-specific MCExpr
416 /// subclasses to implement.
418 /// NOTE: All subclasses are required to have trivial destructors because
419 /// MCExprs are bump pointer allocated and not destructed.
420 class MCTargetExpr : public MCExpr {
421 virtual void Anchor();
423 MCTargetExpr() : MCExpr(Target) {}
424 virtual ~MCTargetExpr() {}
427 virtual void PrintImpl(raw_ostream &OS) const = 0;
428 virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
429 const MCAsmLayout *Layout) const = 0;
432 static bool classof(const MCExpr *E) {
433 return E->getKind() == MCExpr::Target;
435 static bool classof(const MCTargetExpr *) { return true; }
438 } // end namespace llvm