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/CodeGen/MachineModuleInfoImpls.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/Support/FormattedStream.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/ADT/SmallString.h"
29 const X86Subtarget
&X86MCInstLower::getSubtarget() const {
30 return AsmPrinter
.getSubtarget();
33 MachineModuleInfoMachO
&X86MCInstLower::getMachOMMI() const {
34 assert(getSubtarget().isTargetDarwin() &&"Can only get MachO info on darwin");
35 return AsmPrinter
.MMI
->getObjFileInfo
<MachineModuleInfoMachO
>();
39 MCSymbol
*X86MCInstLower::GetPICBaseSymbol() const {
41 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix()
42 << AsmPrinter
.getFunctionNumber() << "$pb";
43 return Ctx
.GetOrCreateSymbol(Name
.str());
47 /// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
49 MCSymbol
*X86MCInstLower::
50 GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
51 const GlobalValue
*GV
= MO
.getGlobal();
53 bool isImplicitlyPrivate
= false;
54 if (MO
.getTargetFlags() == X86II::MO_DARWIN_STUB
||
55 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
56 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
||
57 MO
.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
)
58 isImplicitlyPrivate
= true;
60 SmallString
<128> Name
;
61 Mang
->getNameWithPrefix(Name
, GV
, isImplicitlyPrivate
);
63 if (getSubtarget().isTargetCygMing())
64 AsmPrinter
.DecorateCygMingName(Name
, GV
);
66 switch (MO
.getTargetFlags()) {
67 default: llvm_unreachable("Unknown target flag on GV operand");
68 case X86II::MO_NO_FLAG
: // No flag.
69 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
71 case X86II::MO_DLLIMPORT
: {
72 // Handle dllimport linkage.
73 const char *Prefix
= "__imp_";
74 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
77 case X86II::MO_DARWIN_NONLAZY
:
78 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
: {
79 Name
+= "$non_lazy_ptr";
80 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
82 const MCSymbol
*&StubSym
= getMachOMMI().getGVStubEntry(Sym
);
85 Mang
->getNameWithPrefix(Name
, GV
, false);
86 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
90 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
: {
91 Name
+= "$non_lazy_ptr";
92 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
93 const MCSymbol
*&StubSym
= getMachOMMI().getHiddenGVStubEntry(Sym
);
96 Mang
->getNameWithPrefix(Name
, GV
, false);
97 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
101 case X86II::MO_DARWIN_STUB
: {
103 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
104 const MCSymbol
*&StubSym
= getMachOMMI().getFnStubEntry(Sym
);
107 Mang
->getNameWithPrefix(Name
, GV
, false);
108 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
112 // FIXME: These probably should be a modifier on the symbol or something??
113 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
114 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
115 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
116 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
117 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
118 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
119 case X86II::MO_GOT
: Name
+= "@GOT"; break;
120 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
121 case X86II::MO_PLT
: Name
+= "@PLT"; break;
124 return Ctx
.GetOrCreateSymbol(Name
.str());
127 MCSymbol
*X86MCInstLower::
128 GetExternalSymbolSymbol(const MachineOperand
&MO
) const {
129 SmallString
<128> Name
;
130 Name
+= AsmPrinter
.MAI
->getGlobalPrefix();
131 Name
+= MO
.getSymbolName();
133 switch (MO
.getTargetFlags()) {
134 default: llvm_unreachable("Unknown target flag on GV operand");
135 case X86II::MO_NO_FLAG
: // No flag.
136 case X86II::MO_GOT_ABSOLUTE_ADDRESS
: // Doesn't modify symbol name.
137 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
139 case X86II::MO_DLLIMPORT
: {
140 // Handle dllimport linkage.
141 const char *Prefix
= "__imp_";
142 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
145 case X86II::MO_DARWIN_STUB
: {
147 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
148 const MCSymbol
*&StubSym
= getMachOMMI().getFnStubEntry(Sym
);
151 Name
.erase(Name
.end()-5, Name
.end());
152 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
156 // FIXME: These probably should be a modifier on the symbol or something??
157 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
158 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
159 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
160 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
161 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
162 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
163 case X86II::MO_GOT
: Name
+= "@GOT"; break;
164 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
165 case X86II::MO_PLT
: Name
+= "@PLT"; break;
168 return Ctx
.GetOrCreateSymbol(Name
.str());
171 MCSymbol
*X86MCInstLower::GetJumpTableSymbol(const MachineOperand
&MO
) const {
172 SmallString
<256> Name
;
173 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "JTI"
174 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
176 switch (MO
.getTargetFlags()) {
178 llvm_unreachable("Unknown target flag on GV operand");
179 case X86II::MO_NO_FLAG
: // No flag.
180 case X86II::MO_PIC_BASE_OFFSET
:
181 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
182 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
184 // FIXME: These probably should be a modifier on the symbol or something??
185 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
186 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
187 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
188 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
189 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
190 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
191 case X86II::MO_GOT
: Name
+= "@GOT"; break;
192 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
193 case X86II::MO_PLT
: Name
+= "@PLT"; break;
196 // Create a symbol for the name.
197 return Ctx
.GetOrCreateSymbol(Name
.str());
201 MCSymbol
*X86MCInstLower::
202 GetConstantPoolIndexSymbol(const MachineOperand
&MO
) const {
203 SmallString
<256> Name
;
204 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "CPI"
205 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
207 switch (MO
.getTargetFlags()) {
209 llvm_unreachable("Unknown target flag on GV operand");
210 case X86II::MO_NO_FLAG
: // No flag.
211 case X86II::MO_PIC_BASE_OFFSET
:
212 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
213 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
215 // FIXME: These probably should be a modifier on the symbol or something??
216 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
217 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
218 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
219 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
220 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
221 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
222 case X86II::MO_GOT
: Name
+= "@GOT"; break;
223 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
224 case X86II::MO_PLT
: Name
+= "@PLT"; break;
227 // Create a symbol for the name.
228 return Ctx
.GetOrCreateSymbol(Name
.str());
231 MCOperand
X86MCInstLower::LowerSymbolOperand(const MachineOperand
&MO
,
232 MCSymbol
*Sym
) const {
233 // FIXME: We would like an efficient form for this, so we don't have to do a
234 // lot of extra uniquing.
235 const MCExpr
*Expr
= MCSymbolRefExpr::Create(Sym
, Ctx
);
237 switch (MO
.getTargetFlags()) {
238 default: llvm_unreachable("Unknown target flag on GV operand");
239 case X86II::MO_NO_FLAG
: // No flag.
241 // These affect the name of the symbol, not any suffix.
242 case X86II::MO_DARWIN_NONLAZY
:
243 case X86II::MO_DLLIMPORT
:
244 case X86II::MO_DARWIN_STUB
:
245 case X86II::MO_TLSGD
:
246 case X86II::MO_GOTTPOFF
:
247 case X86II::MO_INDNTPOFF
:
248 case X86II::MO_TPOFF
:
249 case X86II::MO_NTPOFF
:
250 case X86II::MO_GOTPCREL
:
252 case X86II::MO_GOTOFF
:
255 case X86II::MO_PIC_BASE_OFFSET
:
256 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
257 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
258 // Subtract the pic base.
259 Expr
= MCBinaryExpr::CreateSub(Expr
,
260 MCSymbolRefExpr::Create(GetPICBaseSymbol(), Ctx
),
265 if (!MO
.isJTI() && MO
.getOffset())
266 Expr
= MCBinaryExpr::CreateAdd(Expr
,
267 MCConstantExpr::Create(MO
.getOffset(), Ctx
),
269 return MCOperand::CreateExpr(Expr
);
274 static void lower_subreg32(MCInst
*MI
, unsigned OpNo
) {
275 // Convert registers in the addr mode according to subreg32.
276 unsigned Reg
= MI
->getOperand(OpNo
).getReg();
278 MI
->getOperand(OpNo
).setReg(getX86SubSuperRegister(Reg
, MVT::i32
));
281 static void lower_lea64_32mem(MCInst
*MI
, unsigned OpNo
) {
282 // Convert registers in the addr mode according to subreg64.
283 for (unsigned i
= 0; i
!= 4; ++i
) {
284 if (!MI
->getOperand(OpNo
+i
).isReg()) continue;
286 unsigned Reg
= MI
->getOperand(OpNo
+i
).getReg();
287 if (Reg
== 0) continue;
289 MI
->getOperand(OpNo
+i
).setReg(getX86SubSuperRegister(Reg
, MVT::i64
));
295 void X86MCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
296 OutMI
.setOpcode(MI
->getOpcode());
298 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
299 const MachineOperand
&MO
= MI
->getOperand(i
);
302 switch (MO
.getType()) {
305 llvm_unreachable("unknown operand type");
306 case MachineOperand::MO_Register
:
307 MCOp
= MCOperand::CreateReg(MO
.getReg());
309 case MachineOperand::MO_Immediate
:
310 MCOp
= MCOperand::CreateImm(MO
.getImm());
312 case MachineOperand::MO_MachineBasicBlock
:
313 MCOp
= MCOperand::CreateExpr(MCSymbolRefExpr::Create(
314 AsmPrinter
.GetMBBSymbol(MO
.getMBB()->getNumber()), Ctx
));
316 case MachineOperand::MO_GlobalAddress
:
317 MCOp
= LowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
319 case MachineOperand::MO_ExternalSymbol
:
320 MCOp
= LowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
322 case MachineOperand::MO_JumpTableIndex
:
323 MCOp
= LowerSymbolOperand(MO
, GetJumpTableSymbol(MO
));
325 case MachineOperand::MO_ConstantPoolIndex
:
326 MCOp
= LowerSymbolOperand(MO
, GetConstantPoolIndexSymbol(MO
));
330 OutMI
.addOperand(MCOp
);
333 // Handle a few special cases to eliminate operand modifiers.
334 switch (OutMI
.getOpcode()) {
335 case X86::LEA64_32r
: // Handle 'subreg rewriting' for the lea64_32mem operand.
336 lower_lea64_32mem(&OutMI
, 1);
339 OutMI
.setOpcode(X86::MOV32r0
);
340 lower_subreg32(&OutMI
, 0);
342 case X86::MOVZX16rr8
:
343 OutMI
.setOpcode(X86::MOVZX32rr8
);
344 lower_subreg32(&OutMI
, 0);
346 case X86::MOVZX16rm8
:
347 OutMI
.setOpcode(X86::MOVZX32rm8
);
348 lower_subreg32(&OutMI
, 0);
350 case X86::MOVSX16rr8
:
351 OutMI
.setOpcode(X86::MOVSX32rr8
);
352 lower_subreg32(&OutMI
, 0);
354 case X86::MOVSX16rm8
:
355 OutMI
.setOpcode(X86::MOVSX32rm8
);
356 lower_subreg32(&OutMI
, 0);
358 case X86::MOVZX64rr32
:
359 OutMI
.setOpcode(X86::MOV32rr
);
360 lower_subreg32(&OutMI
, 0);
362 case X86::MOVZX64rm32
:
363 OutMI
.setOpcode(X86::MOV32rm
);
364 lower_subreg32(&OutMI
, 0);
366 case X86::MOV64ri64i32
:
367 OutMI
.setOpcode(X86::MOV32ri
);
368 lower_subreg32(&OutMI
, 0);
370 case X86::MOVZX64rr8
:
371 OutMI
.setOpcode(X86::MOVZX32rr8
);
372 lower_subreg32(&OutMI
, 0);
374 case X86::MOVZX64rm8
:
375 OutMI
.setOpcode(X86::MOVZX32rm8
);
376 lower_subreg32(&OutMI
, 0);
378 case X86::MOVZX64rr16
:
379 OutMI
.setOpcode(X86::MOVZX32rr16
);
380 lower_subreg32(&OutMI
, 0);
382 case X86::MOVZX64rm16
:
383 OutMI
.setOpcode(X86::MOVZX32rm16
);
384 lower_subreg32(&OutMI
, 0);
391 void X86ATTAsmPrinter::
392 printInstructionThroughMCStreamer(const MachineInstr
*MI
) {
393 X86MCInstLower
MCInstLowering(OutContext
, Mang
, *this);
394 switch (MI
->getOpcode()) {
395 case TargetInstrInfo::DBG_LABEL
:
396 case TargetInstrInfo::EH_LABEL
:
397 case TargetInstrInfo::GC_LABEL
:
400 case TargetInstrInfo::INLINEASM
:
404 case TargetInstrInfo::IMPLICIT_DEF
:
405 printImplicitDef(MI
);
407 case X86::MOVPC32r
: {
409 // This is a pseudo op for a two instruction sequence with a label, which
416 MCSymbol
*PICBase
= MCInstLowering
.GetPICBaseSymbol();
417 TmpInst
.setOpcode(X86::CALLpcrel32
);
418 // FIXME: We would like an efficient form for this, so we don't have to do a
419 // lot of extra uniquing.
420 TmpInst
.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase
,
422 printMCInst(&TmpInst
);
426 OutStreamer
.EmitLabel(PICBase
);
429 TmpInst
.setOpcode(X86::POP32r
);
430 TmpInst
.getOperand(0) = MCOperand::CreateReg(MI
->getOperand(0).getReg());
431 printMCInst(&TmpInst
);
436 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
437 if (MI
->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS
)
440 // Okay, we have something like:
441 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
443 // For this, we want to print something like:
444 // MYGLOBAL + (. - PICBASE)
445 // However, we can't generate a ".", so just emit a new label here and refer
446 // to it. We know that this operand flag occurs at most once per function.
447 SmallString
<64> Name
;
448 raw_svector_ostream(Name
) << MAI
->getPrivateGlobalPrefix()
449 << "picbaseref" << getFunctionNumber();
450 MCSymbol
*DotSym
= OutContext
.GetOrCreateSymbol(Name
.str());
451 OutStreamer
.EmitLabel(DotSym
);
453 // Now that we have emitted the label, lower the complex operand expression.
454 MCSymbol
*OpSym
= MCInstLowering
.GetExternalSymbolSymbol(MI
->getOperand(2));
456 const MCExpr
*DotExpr
= MCSymbolRefExpr::Create(DotSym
, OutContext
);
457 const MCExpr
*PICBase
=
458 MCSymbolRefExpr::Create(MCInstLowering
.GetPICBaseSymbol(), OutContext
);
459 DotExpr
= MCBinaryExpr::CreateSub(DotExpr
, PICBase
, OutContext
);
461 DotExpr
= MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym
,OutContext
),
462 DotExpr
, OutContext
);
465 TmpInst
.setOpcode(X86::ADD32ri
);
466 TmpInst
.addOperand(MCOperand::CreateReg(MI
->getOperand(0).getReg()));
467 TmpInst
.addOperand(MCOperand::CreateReg(MI
->getOperand(1).getReg()));
468 TmpInst
.addOperand(MCOperand::CreateExpr(DotExpr
));
469 printMCInst(&TmpInst
);
475 MCInstLowering
.Lower(MI
, TmpInst
);
478 printMCInst(&TmpInst
);