llvm.org GIT mirror llvm / 4e815f8
MC: Allow modifiers in MCSymbolRefExpr, and eliminate X86MCTargetExpr. - Although it would be nice to allow this decoupling, the assembler needs to be able to reason about MCSymbolRefExprs in too many places to make this viable. We can use a target specific encoding of the variant if this becomes an issue. - This patch also extends llvm-mc to support parsing of the modifiers, as opposed to lumping them in with the symbol. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98592 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 9 years ago
10 changed file(s) with 127 addition(s) and 137 deletion(s). Raw diff Collapse all Expand all
120120 /// assembler variable (defined constant), or constitute an implicit definition
121121 /// of the symbol as external.
122122 class MCSymbolRefExpr : public MCExpr {
123 public:
124 enum VariantKind {
125 VK_None,
126 VK_Invalid,
127
128 VK_GOT,
129 VK_GOTOFF,
130 VK_GOTPCREL,
131 VK_GOTTPOFF,
132 VK_INDNTPOFF,
133 VK_NTPOFF,
134 VK_PLT,
135 VK_TLSGD,
136 VK_TPOFF
137 };
138
139 private:
140 /// The symbol being referenced.
123141 const MCSymbol *Symbol;
124142
125 explicit MCSymbolRefExpr(const MCSymbol *_Symbol)
126 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol) {}
143 /// The symbol reference modifier.
144 const VariantKind Kind;
145
146 explicit MCSymbolRefExpr(const MCSymbol *_Symbol, VariantKind _Kind)
147 : MCExpr(MCExpr::SymbolRef), Symbol(_Symbol), Kind(_Kind) {}
127148
128149 public:
129150 /// @name Construction
130151 /// @{
131152
132 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx);
133 static const MCSymbolRefExpr *Create(StringRef Name, MCContext &Ctx);
153 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, MCContext &Ctx) {
154 return MCSymbolRefExpr::Create(Symbol, VK_None, Ctx);
155 }
156
157 static const MCSymbolRefExpr *Create(const MCSymbol *Symbol, VariantKind Kind,
158 MCContext &Ctx);
159 static const MCSymbolRefExpr *Create(StringRef Name, VariantKind Kind,
160 MCContext &Ctx);
134161
135162 /// CreateTemp - Create a reference to an assembler temporary label with the
136163 /// specified name.
137 static const MCSymbolRefExpr *CreateTemp(StringRef Name, MCContext &Ctx);
164 static const MCSymbolRefExpr *CreateTemp(StringRef Name, VariantKind Kind,
165 MCContext &Ctx);
138166
139167 /// @}
140168 /// @name Accessors
141169 /// @{
142170
143171 const MCSymbol &getSymbol() const { return *Symbol; }
172
173 VariantKind getKind() const { return Kind; }
174
175 /// @}
176 /// @name Static Utility Functions
177 /// @{
178
179 static StringRef getVariantKindName(VariantKind Kind);
180
181 static VariantKind getVariantKindForName(StringRef Name);
144182
145183 /// @}
146184
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/MC/MCExpr.h"
10 #include "llvm/ADT/StringSwitch.h"
1011 #include "llvm/MC/MCAsmLayout.h"
1112 #include "llvm/MC/MCAssembler.h"
1213 #include "llvm/MC/MCContext.h"
2627 return;
2728
2829 case MCExpr::SymbolRef: {
29 const MCSymbol &Sym = cast(*this).getSymbol();
30 const MCSymbolRefExpr &SRE = cast(*this);
31 const MCSymbol &Sym = SRE.getSymbol();
3032
3133 // Parenthesize names that start with $ so that they don't look like
3234 // absolute names.
3436 OS << '(' << Sym << ')';
3537 else
3638 OS << Sym;
39
40 if (SRE.getKind() != MCSymbolRefExpr::VK_None)
41 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
42
3743 return;
3844 }
3945
126132 return new (Ctx) MCConstantExpr(Value);
127133 }
128134
135 /* *** */
136
129137 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
138 VariantKind Kind,
130139 MCContext &Ctx) {
131 return new (Ctx) MCSymbolRefExpr(Sym);
132 }
133
134 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
135 return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
140 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
141 }
142
143 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
144 MCContext &Ctx) {
145 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
136146 }
137147
138148 const MCSymbolRefExpr *MCSymbolRefExpr::CreateTemp(StringRef Name,
149 VariantKind Kind,
139150 MCContext &Ctx) {
140 return Create(Ctx.GetOrCreateTemporarySymbol(Name), Ctx);
141 }
151 return Create(Ctx.GetOrCreateTemporarySymbol(Name), Kind, Ctx);
152 }
153
154 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
155 switch (Kind) {
156 default:
157 case VK_Invalid: return "<>";
158 case VK_None: return "<>";
159
160 case VK_GOT: return "GOT";
161 case VK_GOTOFF: return "GOTOFF";
162 case VK_GOTPCREL: return "GOTPCREL";
163 case VK_GOTTPOFF: return "GOTTPOFF";
164 case VK_INDNTPOFF: return "INDNTPOFF";
165 case VK_NTPOFF: return "NTPOFF";
166 case VK_PLT: return "PLT";
167 case VK_TLSGD: return "TLSGD";
168 case VK_TPOFF: return "TPOFF";
169 }
170 }
171
172 MCSymbolRefExpr::VariantKind
173 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
174 return StringSwitch(Name)
175 .Case("GOT", VK_GOT)
176 .Case("GOTOFF", VK_GOTOFF)
177 .Case("GOTPCREL", VK_GOTPCREL)
178 .Case("GOTTPOFF", VK_GOTTPOFF)
179 .Case("INDNTPOFF", VK_INDNTPOFF)
180 .Case("NTPOFF", VK_NTPOFF)
181 .Case("PLT", VK_PLT)
182 .Case("TLSGD", VK_TLSGD)
183 .Case("TPOFF", VK_TPOFF)
184 .Default(VK_Invalid);
185 }
186
187 /* *** */
142188
143189 void MCTargetExpr::Anchor() {}
144190
261261 case AsmToken::String:
262262 case AsmToken::Identifier: {
263263 // This is a symbol reference.
264 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
264 std::pair Split = getTok().getIdentifier().split('@');
265 MCSymbol *Sym = CreateSymbol(Split.first);
266
267 // Lookup the symbol variant if used.
268 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
269 if (Split.first.size() != getTok().getIdentifier().size())
270 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
271
265272 EndLoc = Lexer.getLoc();
266273 Lex(); // Eat identifier.
267274
268275 // If this is an absolute variable reference, substitute it now to preserve
269276 // semantics in the face of reassignment.
270277 if (Sym->getValue() && isa(Sym->getValue())) {
278 if (Variant)
279 return Error(EndLoc, "unexpected modified on variable reference");
280
271281 Res = Sym->getValue();
272282 return false;
273283 }
274284
275285 // Otherwise create a symbol ref.
276 Res = MCSymbolRefExpr::Create(Sym, getContext());
286 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
277287 return false;
278288 }
279289 case AsmToken::Integer:
1515 #include "X86AsmPrinter.h"
1616 #include "X86COFFMachineModuleInfo.h"
1717 #include "X86MCAsmInfo.h"
18 #include "X86MCTargetExpr.h"
1918 #include "llvm/Analysis/DebugInfo.h"
2019 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
2120 #include "llvm/MC/MCContext.h"
141140 // FIXME: We would like an efficient form for this, so we don't have to do a
142141 // lot of extra uniquing.
143142 const MCExpr *Expr = 0;
144 X86MCTargetExpr::VariantKind RefKind = X86MCTargetExpr::Invalid;
143 MCSymbolRefExpr::VariantKind RefKind = MCSymbolRefExpr::VK_None;
145144
146145 switch (MO.getTargetFlags()) {
147146 default: llvm_unreachable("Unknown target flag on GV operand");
152151 case X86II::MO_DARWIN_STUB:
153152 break;
154153
155 case X86II::MO_TLSGD: RefKind = X86MCTargetExpr::TLSGD; break;
156 case X86II::MO_GOTTPOFF: RefKind = X86MCTargetExpr::GOTTPOFF; break;
157 case X86II::MO_INDNTPOFF: RefKind = X86MCTargetExpr::INDNTPOFF; break;
158 case X86II::MO_TPOFF: RefKind = X86MCTargetExpr::TPOFF; break;
159 case X86II::MO_NTPOFF: RefKind = X86MCTargetExpr::NTPOFF; break;
160 case X86II::MO_GOTPCREL: RefKind = X86MCTargetExpr::GOTPCREL; break;
161 case X86II::MO_GOT: RefKind = X86MCTargetExpr::GOT; break;
162 case X86II::MO_GOTOFF: RefKind = X86MCTargetExpr::GOTOFF; break;
163 case X86II::MO_PLT: RefKind = X86MCTargetExpr::PLT; break;
154 case X86II::MO_TLSGD: RefKind = MCSymbolRefExpr::VK_TLSGD; break;
155 case X86II::MO_GOTTPOFF: RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
156 case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
157 case X86II::MO_TPOFF: RefKind = MCSymbolRefExpr::VK_TPOFF; break;
158 case X86II::MO_NTPOFF: RefKind = MCSymbolRefExpr::VK_NTPOFF; break;
159 case X86II::MO_GOTPCREL: RefKind = MCSymbolRefExpr::VK_GOTPCREL; break;
160 case X86II::MO_GOT: RefKind = MCSymbolRefExpr::VK_GOT; break;
161 case X86II::MO_GOTOFF: RefKind = MCSymbolRefExpr::VK_GOTOFF; break;
162 case X86II::MO_PLT: RefKind = MCSymbolRefExpr::VK_PLT; break;
164163 case X86II::MO_PIC_BASE_OFFSET:
165164 case X86II::MO_DARWIN_NONLAZY_PIC_BASE:
166165 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE:
172171 break;
173172 }
174173
175 if (Expr == 0) {
176 if (RefKind == X86MCTargetExpr::Invalid)
177 Expr = MCSymbolRefExpr::Create(Sym, Ctx);
178 else
179 Expr = X86MCTargetExpr::Create(Sym, RefKind, Ctx);
180 }
174 if (Expr == 0)
175 Expr = MCSymbolRefExpr::Create(Sym, RefKind, Ctx);
181176
182177 if (!MO.isJTI() && MO.getOffset())
183178 Expr = MCBinaryExpr::CreateAdd(Expr,
2626 X86JITInfo.cpp
2727 X86MCAsmInfo.cpp
2828 X86MCCodeEmitter.cpp
29 X86MCTargetExpr.cpp
3029 X86RegisterInfo.cpp
3130 X86Subtarget.cpp
3231 X86TargetMachine.cpp
1515 #include "X86.h"
1616 #include "X86InstrBuilder.h"
1717 #include "X86ISelLowering.h"
18 #include "X86MCTargetExpr.h"
1918 #include "X86TargetMachine.h"
2019 #include "X86TargetObjectFile.h"
2120 #include "llvm/CallingConv.h"
3635 #include "llvm/CodeGen/PseudoSourceValue.h"
3736 #include "llvm/MC/MCAsmInfo.h"
3837 #include "llvm/MC/MCContext.h"
38 #include "llvm/MC/MCExpr.h"
3939 #include "llvm/MC/MCSymbol.h"
4040 #include "llvm/ADT/BitVector.h"
4141 #include "llvm/ADT/SmallSet.h"
11201120 Subtarget->isPICStyleGOT());
11211121 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
11221122 // entries.
1123 return X86MCTargetExpr::Create(MBB->getSymbol(),
1124 X86MCTargetExpr::GOTOFF, Ctx);
1123 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1124 MCSymbolRefExpr::VK_GOTOFF, Ctx);
11251125 }
11261126
11271127 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
+0
-49
lib/Target/X86/X86MCTargetExpr.cpp less more
None //===- X86MCTargetExpr.cpp - X86 Target Specific MCExpr Implementation ----===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "X86MCTargetExpr.h"
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/MC/MCSymbol.h"
12 #include "llvm/MC/MCValue.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 X86MCTargetExpr *X86MCTargetExpr::Create(const MCSymbol *Sym, VariantKind K,
17 MCContext &Ctx) {
18 return new (Ctx) X86MCTargetExpr(Sym, K);
19 }
20
21 void X86MCTargetExpr::PrintImpl(raw_ostream &OS) const {
22 OS << *Sym;
23
24 switch (Kind) {
25 case Invalid: OS << "@"; break;
26 case GOT: OS << "@GOT"; break;
27 case GOTOFF: OS << "@GOTOFF"; break;
28 case GOTPCREL: OS << "@GOTPCREL"; break;
29 case GOTTPOFF: OS << "@GOTTPOFF"; break;
30 case INDNTPOFF: OS << "@INDNTPOFF"; break;
31 case NTPOFF: OS << "@NTPOFF"; break;
32 case PLT: OS << "@PLT"; break;
33 case TLSGD: OS << "@TLSGD"; break;
34 case TPOFF: OS << "@TPOFF"; break;
35 }
36 }
37
38 bool X86MCTargetExpr::EvaluateAsRelocatableImpl(MCValue &Res,
39 const MCAsmLayout *Layout) const {
40 // FIXME: I don't know if this is right, it followed MCSymbolRefExpr.
41
42 // Evaluate recursively if this is a variable.
43 if (Sym->isVariable())
44 return Sym->getValue()->EvaluateAsRelocatable(Res, Layout);
45
46 Res = MCValue::get(Sym, 0, 0);
47 return true;
48 }
+0
-49
lib/Target/X86/X86MCTargetExpr.h less more
None //===- X86MCTargetExpr.h - X86 Target Specific MCExpr -----------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef X86_MCTARGETEXPR_H
10 #define X86_MCTARGETEXPR_H
11
12 #include "llvm/MC/MCExpr.h"
13
14 namespace llvm {
15
16 /// X86MCTargetExpr - This class represents symbol variants, like foo@GOT.
17 class X86MCTargetExpr : public MCTargetExpr {
18 public:
19 enum VariantKind {
20 Invalid,
21 GOT,
22 GOTOFF,
23 GOTPCREL,
24 GOTTPOFF,
25 INDNTPOFF,
26 NTPOFF,
27 PLT,
28 TLSGD,
29 TPOFF
30 };
31 private:
32 /// Sym - The symbol being referenced.
33 const MCSymbol * const Sym;
34 /// Kind - The modifier.
35 const VariantKind Kind;
36
37 X86MCTargetExpr(const MCSymbol *S, VariantKind K) : Sym(S), Kind(K) {}
38 public:
39 static X86MCTargetExpr *Create(const MCSymbol *Sym, VariantKind K,
40 MCContext &Ctx);
41
42 void PrintImpl(raw_ostream &OS) const;
43 bool EvaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout) const;
44 };
45
46 } // end namespace llvm
47
48 #endif
66 //
77 //===----------------------------------------------------------------------===//
88
9 #include "X86MCTargetExpr.h"
109 #include "X86TargetObjectFile.h"
1110 #include "X86TargetMachine.h"
1211 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
1312 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
1414 #include "llvm/MC/MCSectionMachO.h"
1515 #include "llvm/Target/Mangler.h"
1616 #include "llvm/ADT/SmallString.h"
2828 if (Encoding & (DW_EH_PE_indirect | DW_EH_PE_pcrel)) {
2929 const MCSymbol *Sym = Mang->getSymbol(GV);
3030 const MCExpr *Res =
31 X86MCTargetExpr::Create(Sym, X86MCTargetExpr::GOTPCREL, getContext());
31 MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_GOTPCREL, getContext());
3232 const MCExpr *Four = MCConstantExpr::Create(4, getContext());
3333 return MCBinaryExpr::CreateAdd(Res, Four, getContext());
3434 }
3838 declare void @__cxa_end_catch()
3939
4040 ; X64: Leh_frame_common_begin:
41 ; X64: .long (___gxx_personality_v0@GOTPCREL)+4
41 ; X64: .long ___gxx_personality_v0@GOTPCREL+4
4242
4343 ; X32: Leh_frame_common_begin:
4444 ; X32: .long L___gxx_personality_v0$non_lazy_ptr-