Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 1 | //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===// |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringSwitch.h" |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmLayout.h" |
| 13 | #include "llvm/MC/MCAssembler.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCSymbol.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCValue.h" |
David Greene | 5d2bcb1 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetAsmBackend.h" |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 22 | void MCExpr::print(raw_ostream &OS) const { |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 23 | switch (getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 24 | case MCExpr::Target: |
| 25 | return cast<MCTargetExpr>(this)->PrintImpl(OS); |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 26 | case MCExpr::Constant: |
| 27 | OS << cast<MCConstantExpr>(*this).getValue(); |
| 28 | return; |
| 29 | |
Chris Lattner | d50c2b9 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 30 | case MCExpr::SymbolRef: { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 31 | const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this); |
| 32 | const MCSymbol &Sym = SRE.getSymbol(); |
Chris Lattner | d50c2b9 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 33 | |
| 34 | // Parenthesize names that start with $ so that they don't look like |
| 35 | // absolute names. |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 36 | if (Sym.getName()[0] == '$') |
| 37 | OS << '(' << Sym << ')'; |
| 38 | else |
| 39 | OS << Sym; |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 40 | |
| 41 | if (SRE.getKind() != MCSymbolRefExpr::VK_None) |
| 42 | OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind()); |
| 43 | |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 44 | return; |
Chris Lattner | d50c2b9 | 2009-09-08 23:20:50 +0000 | [diff] [blame] | 45 | } |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 46 | |
| 47 | case MCExpr::Unary: { |
| 48 | const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this); |
| 49 | switch (UE.getOpcode()) { |
| 50 | default: assert(0 && "Invalid opcode!"); |
| 51 | case MCUnaryExpr::LNot: OS << '!'; break; |
| 52 | case MCUnaryExpr::Minus: OS << '-'; break; |
| 53 | case MCUnaryExpr::Not: OS << '~'; break; |
| 54 | case MCUnaryExpr::Plus: OS << '+'; break; |
| 55 | } |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 56 | OS << *UE.getSubExpr(); |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | case MCExpr::Binary: { |
| 61 | const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this); |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 62 | |
| 63 | // Only print parens around the LHS if it is non-trivial. |
| 64 | if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 65 | OS << *BE.getLHS(); |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 66 | } else { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 67 | OS << '(' << *BE.getLHS() << ')'; |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 70 | switch (BE.getOpcode()) { |
| 71 | default: assert(0 && "Invalid opcode!"); |
Chris Lattner | d19ceb9 | 2009-09-08 06:37:35 +0000 | [diff] [blame] | 72 | case MCBinaryExpr::Add: |
| 73 | // Print "X-42" instead of "X+-42". |
| 74 | if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) { |
| 75 | if (RHSC->getValue() < 0) { |
| 76 | OS << RHSC->getValue(); |
| 77 | return; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | OS << '+'; |
| 82 | break; |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 83 | case MCBinaryExpr::And: OS << '&'; break; |
| 84 | case MCBinaryExpr::Div: OS << '/'; break; |
| 85 | case MCBinaryExpr::EQ: OS << "=="; break; |
| 86 | case MCBinaryExpr::GT: OS << '>'; break; |
| 87 | case MCBinaryExpr::GTE: OS << ">="; break; |
| 88 | case MCBinaryExpr::LAnd: OS << "&&"; break; |
| 89 | case MCBinaryExpr::LOr: OS << "||"; break; |
| 90 | case MCBinaryExpr::LT: OS << '<'; break; |
| 91 | case MCBinaryExpr::LTE: OS << "<="; break; |
| 92 | case MCBinaryExpr::Mod: OS << '%'; break; |
| 93 | case MCBinaryExpr::Mul: OS << '*'; break; |
| 94 | case MCBinaryExpr::NE: OS << "!="; break; |
| 95 | case MCBinaryExpr::Or: OS << '|'; break; |
| 96 | case MCBinaryExpr::Shl: OS << "<<"; break; |
| 97 | case MCBinaryExpr::Shr: OS << ">>"; break; |
| 98 | case MCBinaryExpr::Sub: OS << '-'; break; |
| 99 | case MCBinaryExpr::Xor: OS << '^'; break; |
| 100 | } |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 101 | |
| 102 | // Only print parens around the LHS if it is non-trivial. |
| 103 | if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 104 | OS << *BE.getRHS(); |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 105 | } else { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 106 | OS << '(' << *BE.getRHS() << ')'; |
Chris Lattner | be73e8c | 2009-09-08 06:34:07 +0000 | [diff] [blame] | 107 | } |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | assert(0 && "Invalid expression kind!"); |
| 113 | } |
| 114 | |
| 115 | void MCExpr::dump() const { |
Chris Lattner | 8cb9a3b | 2010-01-18 00:37:40 +0000 | [diff] [blame] | 116 | print(dbgs()); |
David Greene | 5d2bcb1 | 2010-01-05 01:28:07 +0000 | [diff] [blame] | 117 | dbgs() << '\n'; |
Daniel Dunbar | 87392fd | 2009-08-31 08:07:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* *** */ |
| 121 | |
Chris Lattner | 8b4ada2 | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 122 | const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS, |
| 123 | const MCExpr *RHS, MCContext &Ctx) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 124 | return new (Ctx) MCBinaryExpr(Opc, LHS, RHS); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 8b4ada2 | 2009-09-08 06:27:48 +0000 | [diff] [blame] | 127 | const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr, |
| 128 | MCContext &Ctx) { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 129 | return new (Ctx) MCUnaryExpr(Opc, Expr); |
| 130 | } |
| 131 | |
| 132 | const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) { |
| 133 | return new (Ctx) MCConstantExpr(Value); |
| 134 | } |
| 135 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 136 | /* *** */ |
| 137 | |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 138 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym, |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 139 | VariantKind Kind, |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 140 | MCContext &Ctx) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 141 | return new (Ctx) MCSymbolRefExpr(Sym, Kind); |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 144 | const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind, |
| 145 | MCContext &Ctx) { |
| 146 | return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx); |
Chris Lattner | 4f3e7aa | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 149 | const MCSymbolRefExpr *MCSymbolRefExpr::CreateTemp(StringRef Name, |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 150 | VariantKind Kind, |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 151 | MCContext &Ctx) { |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 152 | return Create(Ctx.GetOrCreateTemporarySymbol(Name), Kind, Ctx); |
Chris Lattner | 98cdab5 | 2010-03-10 02:25:11 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 155 | StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) { |
| 156 | switch (Kind) { |
| 157 | default: |
| 158 | case VK_Invalid: return "<<invalid>>"; |
| 159 | case VK_None: return "<<none>>"; |
| 160 | |
| 161 | case VK_GOT: return "GOT"; |
| 162 | case VK_GOTOFF: return "GOTOFF"; |
| 163 | case VK_GOTPCREL: return "GOTPCREL"; |
| 164 | case VK_GOTTPOFF: return "GOTTPOFF"; |
| 165 | case VK_INDNTPOFF: return "INDNTPOFF"; |
| 166 | case VK_NTPOFF: return "NTPOFF"; |
| 167 | case VK_PLT: return "PLT"; |
| 168 | case VK_TLSGD: return "TLSGD"; |
| 169 | case VK_TPOFF: return "TPOFF"; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | MCSymbolRefExpr::VariantKind |
| 174 | MCSymbolRefExpr::getVariantKindForName(StringRef Name) { |
| 175 | return StringSwitch<VariantKind>(Name) |
| 176 | .Case("GOT", VK_GOT) |
| 177 | .Case("GOTOFF", VK_GOTOFF) |
| 178 | .Case("GOTPCREL", VK_GOTPCREL) |
| 179 | .Case("GOTTPOFF", VK_GOTTPOFF) |
| 180 | .Case("INDNTPOFF", VK_INDNTPOFF) |
| 181 | .Case("NTPOFF", VK_NTPOFF) |
| 182 | .Case("PLT", VK_PLT) |
| 183 | .Case("TLSGD", VK_TLSGD) |
| 184 | .Case("TPOFF", VK_TPOFF) |
| 185 | .Default(VK_Invalid); |
| 186 | } |
| 187 | |
| 188 | /* *** */ |
| 189 | |
Chris Lattner | df9c438 | 2010-02-08 22:07:36 +0000 | [diff] [blame] | 190 | void MCTargetExpr::Anchor() {} |
Chris Lattner | 4f3e7aa | 2009-09-16 01:26:31 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 192 | /* *** */ |
| 193 | |
Daniel Dunbar | a0e36d5 | 2010-03-12 21:00:45 +0000 | [diff] [blame] | 194 | bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const { |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 195 | MCValue Value; |
| 196 | |
Daniel Dunbar | f82f449 | 2010-03-11 02:28:59 +0000 | [diff] [blame] | 197 | if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 198 | return false; |
| 199 | |
| 200 | Res = Value.getConstant(); |
| 201 | return true; |
| 202 | } |
| 203 | |
Daniel Dunbar | d644c32 | 2009-08-26 09:16:46 +0000 | [diff] [blame] | 204 | static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, |
| 205 | const MCSymbol *RHS_B, int64_t RHS_Cst, |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 206 | MCValue &Res) { |
| 207 | // We can't add or subtract two symbols. |
| 208 | if ((LHS.getSymA() && RHS_A) || |
| 209 | (LHS.getSymB() && RHS_B)) |
| 210 | return false; |
| 211 | |
Daniel Dunbar | d644c32 | 2009-08-26 09:16:46 +0000 | [diff] [blame] | 212 | const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A; |
| 213 | const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B; |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 214 | if (B) { |
Daniel Dunbar | 374d8bd | 2009-06-30 22:49:27 +0000 | [diff] [blame] | 215 | // If we have a negated symbol, then we must have also have a non-negated |
| 216 | // symbol in order to encode the expression. We can do this check later to |
| 217 | // permit expressions which eventually fold to a representable form -- such |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 218 | // as (a + (0 - b)) -- if necessary. |
Daniel Dunbar | 374d8bd | 2009-06-30 22:49:27 +0000 | [diff] [blame] | 219 | if (!A) |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst); |
| 223 | return true; |
| 224 | } |
| 225 | |
Daniel Dunbar | a0e36d5 | 2010-03-12 21:00:45 +0000 | [diff] [blame] | 226 | bool MCExpr::EvaluateAsRelocatable(MCValue &Res, |
| 227 | const MCAsmLayout *Layout) const { |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 228 | switch (getKind()) { |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 229 | case Target: |
Daniel Dunbar | f82f449 | 2010-03-11 02:28:59 +0000 | [diff] [blame] | 230 | return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout); |
Chris Lattner | 5d917a8 | 2010-02-08 19:41:07 +0000 | [diff] [blame] | 231 | |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 232 | case Constant: |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 233 | Res = MCValue::get(cast<MCConstantExpr>(this)->getValue()); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 234 | return true; |
| 235 | |
| 236 | case SymbolRef: { |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 237 | const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol(); |
Daniel Dunbar | 7c3600d | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 238 | |
| 239 | // Evaluate recursively if this is a variable. |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 240 | if (Sym.isVariable()) { |
| 241 | if (!Sym.getValue()->EvaluateAsRelocatable(Res, Layout)) |
| 242 | return false; |
| 243 | |
Daniel Dunbar | d3da362 | 2010-03-14 03:10:40 +0000 | [diff] [blame] | 244 | // Absolutize symbol differences between defined symbols when we have a |
| 245 | // layout object and the target requests it. |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 246 | if (Layout && Res.getSymB() && |
Daniel Dunbar | d3da362 | 2010-03-14 03:10:40 +0000 | [diff] [blame] | 247 | Layout->getAssembler().getBackend().hasAbsolutizedSet() && |
| 248 | Res.getSymA()->isDefined() && Res.getSymB()->isDefined()) { |
Daniel Dunbar | 979ba5b | 2010-03-11 05:53:37 +0000 | [diff] [blame] | 249 | MCSymbolData &A = Layout->getAssembler().getSymbolData(*Res.getSymA()); |
| 250 | MCSymbolData &B = Layout->getAssembler().getSymbolData(*Res.getSymB()); |
| 251 | Res = MCValue::get(+ A.getFragment()->getAddress() + A.getOffset() |
| 252 | - B.getFragment()->getAddress() - B.getOffset() |
| 253 | + Res.getConstant()); |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
Daniel Dunbar | 7c3600d | 2009-10-16 01:33:57 +0000 | [diff] [blame] | 258 | |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 259 | Res = MCValue::get(&Sym, 0, 0); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 260 | return true; |
| 261 | } |
| 262 | |
| 263 | case Unary: { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 264 | const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 265 | MCValue Value; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 266 | |
Daniel Dunbar | f82f449 | 2010-03-11 02:28:59 +0000 | [diff] [blame] | 267 | if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value, Layout)) |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 268 | return false; |
| 269 | |
| 270 | switch (AUE->getOpcode()) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 271 | case MCUnaryExpr::LNot: |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 272 | if (!Value.isAbsolute()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 273 | return false; |
| 274 | Res = MCValue::get(!Value.getConstant()); |
| 275 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 276 | case MCUnaryExpr::Minus: |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 277 | /// -(a - b + const) ==> (b - a - const) |
Daniel Dunbar | b27a41b | 2009-08-11 17:47:52 +0000 | [diff] [blame] | 278 | if (Value.getSymA() && !Value.getSymB()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 279 | return false; |
| 280 | Res = MCValue::get(Value.getSymB(), Value.getSymA(), |
| 281 | -Value.getConstant()); |
| 282 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 283 | case MCUnaryExpr::Not: |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 284 | if (!Value.isAbsolute()) |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 285 | return false; |
| 286 | Res = MCValue::get(~Value.getConstant()); |
| 287 | break; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 288 | case MCUnaryExpr::Plus: |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 289 | Res = Value; |
| 290 | break; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | case Binary: { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 297 | const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 298 | MCValue LHSValue, RHSValue; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 299 | |
Daniel Dunbar | f82f449 | 2010-03-11 02:28:59 +0000 | [diff] [blame] | 300 | if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue, Layout) || |
| 301 | !ABE->getRHS()->EvaluateAsRelocatable(RHSValue, Layout)) |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 302 | return false; |
| 303 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 304 | // We only support a few operations on non-constant expressions, handle |
| 305 | // those first. |
Daniel Dunbar | 80f62d0 | 2009-07-01 06:48:00 +0000 | [diff] [blame] | 306 | if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) { |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 307 | switch (ABE->getOpcode()) { |
| 308 | default: |
| 309 | return false; |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 310 | case MCBinaryExpr::Sub: |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 311 | // Negate RHS and add. |
| 312 | return EvaluateSymbolicAdd(LHSValue, |
| 313 | RHSValue.getSymB(), RHSValue.getSymA(), |
| 314 | -RHSValue.getConstant(), |
| 315 | Res); |
| 316 | |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 317 | case MCBinaryExpr::Add: |
Daniel Dunbar | 3597604 | 2009-06-30 02:08:27 +0000 | [diff] [blame] | 318 | return EvaluateSymbolicAdd(LHSValue, |
| 319 | RHSValue.getSymA(), RHSValue.getSymB(), |
| 320 | RHSValue.getConstant(), |
| 321 | Res); |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 325 | // FIXME: We need target hooks for the evaluation. It may be limited in |
Dan Gohman | 036c130 | 2010-02-08 23:58:47 +0000 | [diff] [blame] | 326 | // width, and gas defines the result of comparisons and right shifts |
| 327 | // differently from Apple as. |
Daniel Dunbar | b79742c | 2009-06-30 16:02:47 +0000 | [diff] [blame] | 328 | int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant(); |
| 329 | int64_t Result = 0; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 330 | switch (ABE->getOpcode()) { |
Daniel Dunbar | 28c251b | 2009-08-31 08:06:59 +0000 | [diff] [blame] | 331 | case MCBinaryExpr::Add: Result = LHS + RHS; break; |
| 332 | case MCBinaryExpr::And: Result = LHS & RHS; break; |
| 333 | case MCBinaryExpr::Div: Result = LHS / RHS; break; |
| 334 | case MCBinaryExpr::EQ: Result = LHS == RHS; break; |
| 335 | case MCBinaryExpr::GT: Result = LHS > RHS; break; |
| 336 | case MCBinaryExpr::GTE: Result = LHS >= RHS; break; |
| 337 | case MCBinaryExpr::LAnd: Result = LHS && RHS; break; |
| 338 | case MCBinaryExpr::LOr: Result = LHS || RHS; break; |
| 339 | case MCBinaryExpr::LT: Result = LHS < RHS; break; |
| 340 | case MCBinaryExpr::LTE: Result = LHS <= RHS; break; |
| 341 | case MCBinaryExpr::Mod: Result = LHS % RHS; break; |
| 342 | case MCBinaryExpr::Mul: Result = LHS * RHS; break; |
| 343 | case MCBinaryExpr::NE: Result = LHS != RHS; break; |
| 344 | case MCBinaryExpr::Or: Result = LHS | RHS; break; |
| 345 | case MCBinaryExpr::Shl: Result = LHS << RHS; break; |
| 346 | case MCBinaryExpr::Shr: Result = LHS >> RHS; break; |
| 347 | case MCBinaryExpr::Sub: Result = LHS - RHS; break; |
| 348 | case MCBinaryExpr::Xor: Result = LHS ^ RHS; break; |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Dunbar | 15d1707 | 2009-06-30 01:49:52 +0000 | [diff] [blame] | 351 | Res = MCValue::get(Result); |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
| 354 | } |
Daniel Dunbar | fc6877a | 2009-06-29 20:40:36 +0000 | [diff] [blame] | 355 | |
Daniel Dunbar | 9643ac5 | 2009-08-31 08:07:22 +0000 | [diff] [blame] | 356 | assert(0 && "Invalid assembly expression kind!"); |
| 357 | return false; |
| 358 | } |