1 //===-- X86MCInstLower.cpp - Convert X86 MachineInstr to an MCInst --------===//
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 // This file contains code to lower X86 MachineInstrs to their corresponding
13 //===----------------------------------------------------------------------===//
15 #include "X86MCInstLower.h"
16 #include "X86ATTAsmPrinter.h"
17 #include "X86MCAsmInfo.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Support/FormattedStream.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/ADT/SmallString.h"
28 const X86Subtarget
&X86MCInstLower::getSubtarget() const {
29 return AsmPrinter
.getSubtarget();
33 MCSymbol
*X86MCInstLower::GetPICBaseSymbol() const {
34 // FIXME: the actual label generated doesn't matter here! Just mangle in
35 // something unique (the function number) with Private prefix.
38 if (getSubtarget().isTargetDarwin()) {
39 raw_svector_ostream(Name
) << 'L' << AsmPrinter
.getFunctionNumber() << "$pb";
41 assert(getSubtarget().isTargetELF() && "Don't know how to print PIC label!");
42 raw_svector_ostream(Name
) << ".Lllvm$" << AsmPrinter
.getFunctionNumber()
45 return Ctx
.GetOrCreateSymbol(Name
.str());
49 /// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
51 MCSymbol
*X86MCInstLower::
52 GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
53 const GlobalValue
*GV
= MO
.getGlobal();
55 bool isImplicitlyPrivate
= false;
56 if (MO
.getTargetFlags() == X86II::MO_DARWIN_STUB
||
57 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
58 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
||
59 MO
.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
)
60 isImplicitlyPrivate
= true;
62 SmallString
<128> Name
;
63 Mang
->getNameWithPrefix(Name
, GV
, isImplicitlyPrivate
);
65 if (getSubtarget().isTargetCygMing())
66 AsmPrinter
.DecorateCygMingName(Name
, GV
);
68 switch (MO
.getTargetFlags()) {
69 default: llvm_unreachable("Unknown target flag on GV operand");
70 case X86II::MO_NO_FLAG
: // No flag.
71 case X86II::MO_GOT_ABSOLUTE_ADDRESS
: // Doesn't modify symbol name.
72 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
74 case X86II::MO_DLLIMPORT
: {
75 // Handle dllimport linkage.
76 const char *Prefix
= "__imp_";
77 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
80 case X86II::MO_DARWIN_NONLAZY
:
81 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
: {
82 Name
+= "$non_lazy_ptr";
83 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
84 MCSymbol
*&StubSym
= AsmPrinter
.GVStubs
[Sym
];
87 Mang
->getNameWithPrefix(Name
, GV
, false);
88 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
92 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
: {
93 Name
+= "$non_lazy_ptr";
94 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
95 MCSymbol
*&StubSym
= AsmPrinter
.HiddenGVStubs
[Sym
];
98 Mang
->getNameWithPrefix(Name
, GV
, false);
99 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
103 case X86II::MO_DARWIN_STUB
: {
105 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
106 MCSymbol
*&StubSym
= AsmPrinter
.FnStubs
[Sym
];
109 Mang
->getNameWithPrefix(Name
, GV
, false);
110 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
114 // FIXME: These probably should be a modifier on the symbol or something??
115 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
116 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
117 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
118 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
119 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
120 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
121 case X86II::MO_GOT
: Name
+= "@GOT"; break;
122 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
123 case X86II::MO_PLT
: Name
+= "@PLT"; break;
126 return Ctx
.GetOrCreateSymbol(Name
.str());
129 MCSymbol
*X86MCInstLower::
130 GetExternalSymbolSymbol(const MachineOperand
&MO
) const {
131 SmallString
<128> Name
;
132 Name
+= AsmPrinter
.MAI
->getGlobalPrefix();
133 Name
+= MO
.getSymbolName();
135 switch (MO
.getTargetFlags()) {
136 default: llvm_unreachable("Unknown target flag on GV operand");
137 case X86II::MO_NO_FLAG
: // No flag.
138 case X86II::MO_GOT_ABSOLUTE_ADDRESS
: // Doesn't modify symbol name.
139 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
141 case X86II::MO_DLLIMPORT
: {
142 // Handle dllimport linkage.
143 const char *Prefix
= "__imp_";
144 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
147 case X86II::MO_DARWIN_STUB
: {
149 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
150 MCSymbol
*&StubSym
= AsmPrinter
.FnStubs
[Sym
];
152 Name
.erase(Name
.end()-5, Name
.end());
153 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
157 // FIXME: These probably should be a modifier on the symbol or something??
158 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
159 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
160 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
161 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
162 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
163 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
164 case X86II::MO_GOT
: Name
+= "@GOT"; break;
165 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
166 case X86II::MO_PLT
: Name
+= "@PLT"; break;
169 return Ctx
.GetOrCreateSymbol(Name
.str());
172 MCSymbol
*X86MCInstLower::GetJumpTableSymbol(const MachineOperand
&MO
) const {
173 SmallString
<256> Name
;
174 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "JTI"
175 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
177 switch (MO
.getTargetFlags()) {
179 llvm_unreachable("Unknown target flag on GV operand");
180 case X86II::MO_NO_FLAG
: // No flag.
181 case X86II::MO_PIC_BASE_OFFSET
:
182 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
183 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
185 // FIXME: These probably should be a modifier on the symbol or something??
186 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
187 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
188 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
189 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
190 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
191 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
192 case X86II::MO_GOT
: Name
+= "@GOT"; break;
193 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
194 case X86II::MO_PLT
: Name
+= "@PLT"; break;
197 // Create a symbol for the name.
198 return Ctx
.GetOrCreateSymbol(Name
.str());
202 MCSymbol
*X86MCInstLower::
203 GetConstantPoolIndexSymbol(const MachineOperand
&MO
) const {
204 SmallString
<256> Name
;
205 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "CPI"
206 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
208 switch (MO
.getTargetFlags()) {
210 llvm_unreachable("Unknown target flag on GV operand");
211 case X86II::MO_NO_FLAG
: // No flag.
212 case X86II::MO_PIC_BASE_OFFSET
:
213 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
214 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
216 // FIXME: These probably should be a modifier on the symbol or something??
217 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
218 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
219 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
220 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
221 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
222 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
223 case X86II::MO_GOT
: Name
+= "@GOT"; break;
224 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
225 case X86II::MO_PLT
: Name
+= "@PLT"; break;
228 // Create a symbol for the name.
229 return Ctx
.GetOrCreateSymbol(Name
.str());
232 MCOperand
X86MCInstLower::LowerSymbolOperand(const MachineOperand
&MO
,
233 MCSymbol
*Sym
) const {
234 // FIXME: We would like an efficient form for this, so we don't have to do a
235 // lot of extra uniquing.
236 const MCExpr
*Expr
= MCSymbolRefExpr::Create(Sym
, Ctx
);
238 switch (MO
.getTargetFlags()) {
239 default: llvm_unreachable("Unknown target flag on GV operand");
240 case X86II::MO_NO_FLAG
: // No flag.
242 // These affect the name of the symbol, not any suffix.
243 case X86II::MO_DARWIN_NONLAZY
:
244 case X86II::MO_DLLIMPORT
:
245 case X86II::MO_DARWIN_STUB
:
246 case X86II::MO_TLSGD
:
247 case X86II::MO_GOTTPOFF
:
248 case X86II::MO_INDNTPOFF
:
249 case X86II::MO_TPOFF
:
250 case X86II::MO_NTPOFF
:
251 case X86II::MO_GOTPCREL
:
253 case X86II::MO_GOTOFF
:
256 case X86II::MO_PIC_BASE_OFFSET
:
257 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
258 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
259 // Subtract the pic base.
260 Expr
= MCBinaryExpr::CreateSub(Expr
,
261 MCSymbolRefExpr::Create(GetPICBaseSymbol(),
265 case X86II::MO_GOT_ABSOLUTE_ADDRESS
: {
266 // For this, we want to print something like:
267 // MYSYMBOL + (. - PICBASE)
268 // However, we can't generate a ".", so just emit a new label here and refer
269 // to it. We know that this operand flag occurs at most once per function.
270 SmallString
<64> Name
;
271 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix()
272 << "picbaseref" << AsmPrinter
.getFunctionNumber();
273 MCSymbol
*DotSym
= Ctx
.GetOrCreateSymbol(Name
.str());
274 // FIXME: This instruction should be lowered before we get here...
275 AsmPrinter
.OutStreamer
.EmitLabel(DotSym
);
277 const MCExpr
*DotExpr
= MCSymbolRefExpr::Create(DotSym
, Ctx
);
278 const MCExpr
*PICBase
= MCSymbolRefExpr::Create(GetPICBaseSymbol(),
280 DotExpr
= MCBinaryExpr::CreateSub(DotExpr
, PICBase
, Ctx
);
281 Expr
= MCBinaryExpr::CreateAdd(Expr
, DotExpr
, Ctx
);
286 if (!MO
.isJTI() && MO
.getOffset())
287 Expr
= MCBinaryExpr::CreateAdd(Expr
,
288 MCConstantExpr::Create(MO
.getOffset(), Ctx
),
290 return MCOperand::CreateExpr(Expr
);
295 static void lower_subreg32(MCInst
*MI
, unsigned OpNo
) {
296 // Convert registers in the addr mode according to subreg32.
297 unsigned Reg
= MI
->getOperand(OpNo
).getReg();
299 MI
->getOperand(OpNo
).setReg(getX86SubSuperRegister(Reg
, MVT::i32
));
302 static void lower_lea64_32mem(MCInst
*MI
, unsigned OpNo
) {
303 // Convert registers in the addr mode according to subreg64.
304 for (unsigned i
= 0; i
!= 4; ++i
) {
305 if (!MI
->getOperand(OpNo
+i
).isReg()) continue;
307 unsigned Reg
= MI
->getOperand(OpNo
+i
).getReg();
308 if (Reg
== 0) continue;
310 MI
->getOperand(OpNo
+i
).setReg(getX86SubSuperRegister(Reg
, MVT::i64
));
316 void X86MCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
317 OutMI
.setOpcode(MI
->getOpcode());
319 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
320 const MachineOperand
&MO
= MI
->getOperand(i
);
323 switch (MO
.getType()) {
326 llvm_unreachable("unknown operand type");
327 case MachineOperand::MO_Register
:
328 MCOp
= MCOperand::CreateReg(MO
.getReg());
330 case MachineOperand::MO_Immediate
:
331 MCOp
= MCOperand::CreateImm(MO
.getImm());
333 case MachineOperand::MO_MachineBasicBlock
:
334 // FIXME: Kill MBBLabel operand type!
335 MCOp
= MCOperand::CreateMBBLabel(AsmPrinter
.getFunctionNumber(),
336 MO
.getMBB()->getNumber());
338 case MachineOperand::MO_GlobalAddress
:
339 MCOp
= LowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
341 case MachineOperand::MO_ExternalSymbol
:
342 MCOp
= LowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
344 case MachineOperand::MO_JumpTableIndex
:
345 MCOp
= LowerSymbolOperand(MO
, GetJumpTableSymbol(MO
));
347 case MachineOperand::MO_ConstantPoolIndex
:
348 MCOp
= LowerSymbolOperand(MO
, GetConstantPoolIndexSymbol(MO
));
352 OutMI
.addOperand(MCOp
);
355 // Handle a few special cases to eliminate operand modifiers.
356 switch (OutMI
.getOpcode()) {
357 case X86::LEA64_32r
: // Handle 'subreg rewriting' for the lea64_32mem operand.
358 lower_lea64_32mem(&OutMI
, 1);
361 OutMI
.setOpcode(X86::MOV32r0
);
362 lower_subreg32(&OutMI
, 0);
364 case X86::MOVZX16rr8
:
365 OutMI
.setOpcode(X86::MOVZX32rr8
);
366 lower_subreg32(&OutMI
, 0);
368 case X86::MOVZX16rm8
:
369 OutMI
.setOpcode(X86::MOVZX32rm8
);
370 lower_subreg32(&OutMI
, 0);
372 case X86::MOVSX16rr8
:
373 OutMI
.setOpcode(X86::MOVSX32rr8
);
374 lower_subreg32(&OutMI
, 0);
376 case X86::MOVSX16rm8
:
377 OutMI
.setOpcode(X86::MOVSX32rm8
);
378 lower_subreg32(&OutMI
, 0);
380 case X86::MOVZX64rr32
:
381 OutMI
.setOpcode(X86::MOV32rr
);
382 lower_subreg32(&OutMI
, 0);
384 case X86::MOVZX64rm32
:
385 OutMI
.setOpcode(X86::MOV32rm
);
386 lower_subreg32(&OutMI
, 0);
388 case X86::MOV64ri64i32
:
389 OutMI
.setOpcode(X86::MOV32ri
);
390 lower_subreg32(&OutMI
, 0);
392 case X86::MOVZX64rr8
:
393 OutMI
.setOpcode(X86::MOVZX32rr8
);
394 lower_subreg32(&OutMI
, 0);
396 case X86::MOVZX64rm8
:
397 OutMI
.setOpcode(X86::MOVZX32rm8
);
398 lower_subreg32(&OutMI
, 0);
400 case X86::MOVZX64rr16
:
401 OutMI
.setOpcode(X86::MOVZX32rr16
);
402 lower_subreg32(&OutMI
, 0);
404 case X86::MOVZX64rm16
:
405 OutMI
.setOpcode(X86::MOVZX32rm16
);
406 lower_subreg32(&OutMI
, 0);
413 void X86ATTAsmPrinter::
414 printInstructionThroughMCStreamer(const MachineInstr
*MI
) {
415 X86MCInstLower
MCInstLowering(OutContext
, Mang
, *this);
416 switch (MI
->getOpcode()) {
417 case TargetInstrInfo::DBG_LABEL
:
418 case TargetInstrInfo::EH_LABEL
:
419 case TargetInstrInfo::GC_LABEL
:
422 case TargetInstrInfo::INLINEASM
:
426 case TargetInstrInfo::IMPLICIT_DEF
:
427 printImplicitDef(MI
);
429 case X86::MOVPC32r
: {
431 // This is a pseudo op for a two instruction sequence with a label, which
438 MCSymbol
*PICBase
= MCInstLowering
.GetPICBaseSymbol();
439 TmpInst
.setOpcode(X86::CALLpcrel32
);
440 // FIXME: We would like an efficient form for this, so we don't have to do a
441 // lot of extra uniquing.
442 TmpInst
.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase
,
444 printInstruction(&TmpInst
);
448 OutStreamer
.EmitLabel(PICBase
);
451 TmpInst
.setOpcode(X86::POP32r
);
452 TmpInst
.getOperand(0) = MCOperand::CreateReg(MI
->getOperand(0).getReg());
453 printInstruction(&TmpInst
);
459 MCInstLowering
.Lower(MI
, TmpInst
);
462 printInstruction(&TmpInst
);