make formatting of expressions more closely match the existing asmprinter.
[oota-llvm.git] / lib / MC / MCExpr.cpp
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/MC/MCValue.h"
14 #include "llvm/Support/raw_ostream.h"
15 using namespace llvm;
16
17 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
18   switch (getKind()) {
19   case MCExpr::Constant:
20     OS << cast<MCConstantExpr>(*this).getValue();
21     return;
22
23   case MCExpr::SymbolRef:
24     cast<MCSymbolRefExpr>(*this).getSymbol().print(OS, MAI);
25     return;
26
27   case MCExpr::Unary: {
28     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
29     switch (UE.getOpcode()) {
30     default: assert(0 && "Invalid opcode!");
31     case MCUnaryExpr::LNot:  OS << '!'; break;
32     case MCUnaryExpr::Minus: OS << '-'; break;
33     case MCUnaryExpr::Not:   OS << '~'; break;
34     case MCUnaryExpr::Plus:  OS << '+'; break;
35     }
36     UE.getSubExpr()->print(OS, MAI);
37     return;
38   }
39
40   case MCExpr::Binary: {
41     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
42     
43     // Only print parens around the LHS if it is non-trivial.
44     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
45       BE.getLHS()->print(OS, MAI);
46     } else {
47       OS << '(';
48       BE.getLHS()->print(OS, MAI);
49       OS << ')';
50     }
51     
52     switch (BE.getOpcode()) {
53     default: assert(0 && "Invalid opcode!");
54     case MCBinaryExpr::Add:  OS <<  '+'; break;
55     case MCBinaryExpr::And:  OS <<  '&'; break;
56     case MCBinaryExpr::Div:  OS <<  '/'; break;
57     case MCBinaryExpr::EQ:   OS << "=="; break;
58     case MCBinaryExpr::GT:   OS <<  '>'; break;
59     case MCBinaryExpr::GTE:  OS << ">="; break;
60     case MCBinaryExpr::LAnd: OS << "&&"; break;
61     case MCBinaryExpr::LOr:  OS << "||"; break;
62     case MCBinaryExpr::LT:   OS <<  '<'; break;
63     case MCBinaryExpr::LTE:  OS << "<="; break;
64     case MCBinaryExpr::Mod:  OS <<  '%'; break;
65     case MCBinaryExpr::Mul:  OS <<  '*'; break;
66     case MCBinaryExpr::NE:   OS << "!="; break;
67     case MCBinaryExpr::Or:   OS <<  '|'; break;
68     case MCBinaryExpr::Shl:  OS << "<<"; break;
69     case MCBinaryExpr::Shr:  OS << ">>"; break;
70     case MCBinaryExpr::Sub:  OS <<  '-'; break;
71     case MCBinaryExpr::Xor:  OS <<  '^'; break;
72     }
73     
74     // Only print parens around the LHS if it is non-trivial.
75     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
76       BE.getRHS()->print(OS, MAI);
77     } else {
78       OS << '(';
79       BE.getRHS()->print(OS, MAI);
80       OS << ')';
81     }
82     return;
83   }
84   }
85
86   assert(0 && "Invalid expression kind!");
87 }
88
89 void MCExpr::dump() const {
90   print(errs(), 0);
91   errs() << '\n';
92 }
93
94 /* *** */
95
96 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
97                                          const MCExpr *RHS, MCContext &Ctx) {
98   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
99 }
100
101 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
102                                        MCContext &Ctx) {
103   return new (Ctx) MCUnaryExpr(Opc, Expr);
104 }
105
106 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
107   return new (Ctx) MCConstantExpr(Value);
108 }
109
110 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
111                                                MCContext &Ctx) {
112   return new (Ctx) MCSymbolRefExpr(Sym);
113 }
114
115 /* *** */
116
117 bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
118   MCValue Value;
119   
120   if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute())
121     return false;
122
123   Res = Value.getConstant();
124   return true;
125 }
126
127 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, 
128                                 const MCSymbol *RHS_B, int64_t RHS_Cst,
129                                 MCValue &Res) {
130   // We can't add or subtract two symbols.
131   if ((LHS.getSymA() && RHS_A) ||
132       (LHS.getSymB() && RHS_B))
133     return false;
134
135   const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
136   const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
137   if (B) {
138     // If we have a negated symbol, then we must have also have a non-negated
139     // symbol in order to encode the expression. We can do this check later to
140     // permit expressions which eventually fold to a representable form -- such
141     // as (a + (0 - b)) -- if necessary.
142     if (!A)
143       return false;
144   }
145   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
146   return true;
147 }
148
149 bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
150   switch (getKind()) {
151   case Constant:
152     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
153     return true;
154
155   case SymbolRef: {
156     const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
157     if (const MCValue *Value = Ctx.GetSymbolValue(&Sym))
158       Res = *Value;
159     else
160       Res = MCValue::get(&Sym, 0, 0);
161     return true;
162   }
163
164   case Unary: {
165     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
166     MCValue Value;
167
168     if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
169       return false;
170
171     switch (AUE->getOpcode()) {
172     case MCUnaryExpr::LNot:
173       if (!Value.isAbsolute())
174         return false;
175       Res = MCValue::get(!Value.getConstant());
176       break;
177     case MCUnaryExpr::Minus:
178       /// -(a - b + const) ==> (b - a - const)
179       if (Value.getSymA() && !Value.getSymB())
180         return false;
181       Res = MCValue::get(Value.getSymB(), Value.getSymA(), 
182                          -Value.getConstant()); 
183       break;
184     case MCUnaryExpr::Not:
185       if (!Value.isAbsolute())
186         return false;
187       Res = MCValue::get(~Value.getConstant()); 
188       break;
189     case MCUnaryExpr::Plus:
190       Res = Value;
191       break;
192     }
193
194     return true;
195   }
196
197   case Binary: {
198     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
199     MCValue LHSValue, RHSValue;
200     
201     if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
202         !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
203       return false;
204
205     // We only support a few operations on non-constant expressions, handle
206     // those first.
207     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
208       switch (ABE->getOpcode()) {
209       default:
210         return false;
211       case MCBinaryExpr::Sub:
212         // Negate RHS and add.
213         return EvaluateSymbolicAdd(LHSValue,
214                                    RHSValue.getSymB(), RHSValue.getSymA(),
215                                    -RHSValue.getConstant(),
216                                    Res);
217
218       case MCBinaryExpr::Add:
219         return EvaluateSymbolicAdd(LHSValue,
220                                    RHSValue.getSymA(), RHSValue.getSymB(),
221                                    RHSValue.getConstant(),
222                                    Res);
223       }
224     }
225
226     // FIXME: We need target hooks for the evaluation. It may be limited in
227     // width, and gas defines the result of comparisons differently from Apple
228     // as (the result is sign extended).
229     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
230     int64_t Result = 0;
231     switch (ABE->getOpcode()) {
232     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
233     case MCBinaryExpr::And:  Result = LHS & RHS; break;
234     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
235     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
236     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
237     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
238     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
239     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
240     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
241     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
242     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
243     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
244     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
245     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
246     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
247     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
248     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
249     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
250     }
251
252     Res = MCValue::get(Result);
253     return true;
254   }
255   }
256
257   assert(0 && "Invalid assembly expression kind!");
258   return false;
259 }