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 {
35 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix()
36 << AsmPrinter
.getFunctionNumber() << "$pb";
37 return Ctx
.GetOrCreateSymbol(Name
.str());
41 /// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
43 MCSymbol
*X86MCInstLower::
44 GetGlobalAddressSymbol(const MachineOperand
&MO
) const {
45 const GlobalValue
*GV
= MO
.getGlobal();
47 bool isImplicitlyPrivate
= false;
48 if (MO
.getTargetFlags() == X86II::MO_DARWIN_STUB
||
49 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY
||
50 MO
.getTargetFlags() == X86II::MO_DARWIN_NONLAZY_PIC_BASE
||
51 MO
.getTargetFlags() == X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
)
52 isImplicitlyPrivate
= true;
54 SmallString
<128> Name
;
55 Mang
->getNameWithPrefix(Name
, GV
, isImplicitlyPrivate
);
57 if (getSubtarget().isTargetCygMing())
58 AsmPrinter
.DecorateCygMingName(Name
, GV
);
60 switch (MO
.getTargetFlags()) {
61 default: llvm_unreachable("Unknown target flag on GV operand");
62 case X86II::MO_NO_FLAG
: // No flag.
63 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
65 case X86II::MO_DLLIMPORT
: {
66 // Handle dllimport linkage.
67 const char *Prefix
= "__imp_";
68 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
71 case X86II::MO_DARWIN_NONLAZY
:
72 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
: {
73 Name
+= "$non_lazy_ptr";
74 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
75 MCSymbol
*&StubSym
= AsmPrinter
.GVStubs
[Sym
];
78 Mang
->getNameWithPrefix(Name
, GV
, false);
79 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
83 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
: {
84 Name
+= "$non_lazy_ptr";
85 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
86 MCSymbol
*&StubSym
= AsmPrinter
.HiddenGVStubs
[Sym
];
89 Mang
->getNameWithPrefix(Name
, GV
, false);
90 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
94 case X86II::MO_DARWIN_STUB
: {
96 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
97 MCSymbol
*&StubSym
= AsmPrinter
.FnStubs
[Sym
];
100 Mang
->getNameWithPrefix(Name
, GV
, false);
101 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
105 // FIXME: These probably should be a modifier on the symbol or something??
106 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
107 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
108 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
109 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
110 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
111 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
112 case X86II::MO_GOT
: Name
+= "@GOT"; break;
113 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
114 case X86II::MO_PLT
: Name
+= "@PLT"; break;
117 return Ctx
.GetOrCreateSymbol(Name
.str());
120 MCSymbol
*X86MCInstLower::
121 GetExternalSymbolSymbol(const MachineOperand
&MO
) const {
122 SmallString
<128> Name
;
123 Name
+= AsmPrinter
.MAI
->getGlobalPrefix();
124 Name
+= MO
.getSymbolName();
126 switch (MO
.getTargetFlags()) {
127 default: llvm_unreachable("Unknown target flag on GV operand");
128 case X86II::MO_NO_FLAG
: // No flag.
129 case X86II::MO_GOT_ABSOLUTE_ADDRESS
: // Doesn't modify symbol name.
130 case X86II::MO_PIC_BASE_OFFSET
: // Doesn't modify symbol name.
132 case X86II::MO_DLLIMPORT
: {
133 // Handle dllimport linkage.
134 const char *Prefix
= "__imp_";
135 Name
.insert(Name
.begin(), Prefix
, Prefix
+strlen(Prefix
));
138 case X86II::MO_DARWIN_STUB
: {
140 MCSymbol
*Sym
= Ctx
.GetOrCreateSymbol(Name
.str());
141 MCSymbol
*&StubSym
= AsmPrinter
.FnStubs
[Sym
];
143 Name
.erase(Name
.end()-5, Name
.end());
144 StubSym
= Ctx
.GetOrCreateSymbol(Name
.str());
148 // FIXME: These probably should be a modifier on the symbol or something??
149 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
150 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
151 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
152 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
153 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
154 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
155 case X86II::MO_GOT
: Name
+= "@GOT"; break;
156 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
157 case X86II::MO_PLT
: Name
+= "@PLT"; break;
160 return Ctx
.GetOrCreateSymbol(Name
.str());
163 MCSymbol
*X86MCInstLower::GetJumpTableSymbol(const MachineOperand
&MO
) const {
164 SmallString
<256> Name
;
165 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "JTI"
166 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
168 switch (MO
.getTargetFlags()) {
170 llvm_unreachable("Unknown target flag on GV operand");
171 case X86II::MO_NO_FLAG
: // No flag.
172 case X86II::MO_PIC_BASE_OFFSET
:
173 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
174 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
176 // FIXME: These probably should be a modifier on the symbol or something??
177 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
178 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
179 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
180 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
181 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
182 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
183 case X86II::MO_GOT
: Name
+= "@GOT"; break;
184 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
185 case X86II::MO_PLT
: Name
+= "@PLT"; break;
188 // Create a symbol for the name.
189 return Ctx
.GetOrCreateSymbol(Name
.str());
193 MCSymbol
*X86MCInstLower::
194 GetConstantPoolIndexSymbol(const MachineOperand
&MO
) const {
195 SmallString
<256> Name
;
196 raw_svector_ostream(Name
) << AsmPrinter
.MAI
->getPrivateGlobalPrefix() << "CPI"
197 << AsmPrinter
.getFunctionNumber() << '_' << MO
.getIndex();
199 switch (MO
.getTargetFlags()) {
201 llvm_unreachable("Unknown target flag on GV operand");
202 case X86II::MO_NO_FLAG
: // No flag.
203 case X86II::MO_PIC_BASE_OFFSET
:
204 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
205 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
207 // FIXME: These probably should be a modifier on the symbol or something??
208 case X86II::MO_TLSGD
: Name
+= "@TLSGD"; break;
209 case X86II::MO_GOTTPOFF
: Name
+= "@GOTTPOFF"; break;
210 case X86II::MO_INDNTPOFF
: Name
+= "@INDNTPOFF"; break;
211 case X86II::MO_TPOFF
: Name
+= "@TPOFF"; break;
212 case X86II::MO_NTPOFF
: Name
+= "@NTPOFF"; break;
213 case X86II::MO_GOTPCREL
: Name
+= "@GOTPCREL"; break;
214 case X86II::MO_GOT
: Name
+= "@GOT"; break;
215 case X86II::MO_GOTOFF
: Name
+= "@GOTOFF"; break;
216 case X86II::MO_PLT
: Name
+= "@PLT"; break;
219 // Create a symbol for the name.
220 return Ctx
.GetOrCreateSymbol(Name
.str());
223 MCOperand
X86MCInstLower::LowerSymbolOperand(const MachineOperand
&MO
,
224 MCSymbol
*Sym
) const {
225 // FIXME: We would like an efficient form for this, so we don't have to do a
226 // lot of extra uniquing.
227 const MCExpr
*Expr
= MCSymbolRefExpr::Create(Sym
, Ctx
);
229 switch (MO
.getTargetFlags()) {
230 default: llvm_unreachable("Unknown target flag on GV operand");
231 case X86II::MO_NO_FLAG
: // No flag.
233 // These affect the name of the symbol, not any suffix.
234 case X86II::MO_DARWIN_NONLAZY
:
235 case X86II::MO_DLLIMPORT
:
236 case X86II::MO_DARWIN_STUB
:
237 case X86II::MO_TLSGD
:
238 case X86II::MO_GOTTPOFF
:
239 case X86II::MO_INDNTPOFF
:
240 case X86II::MO_TPOFF
:
241 case X86II::MO_NTPOFF
:
242 case X86II::MO_GOTPCREL
:
244 case X86II::MO_GOTOFF
:
247 case X86II::MO_PIC_BASE_OFFSET
:
248 case X86II::MO_DARWIN_NONLAZY_PIC_BASE
:
249 case X86II::MO_DARWIN_HIDDEN_NONLAZY_PIC_BASE
:
250 // Subtract the pic base.
251 Expr
= MCBinaryExpr::CreateSub(Expr
,
252 MCSymbolRefExpr::Create(GetPICBaseSymbol(), Ctx
),
257 if (!MO
.isJTI() && MO
.getOffset())
258 Expr
= MCBinaryExpr::CreateAdd(Expr
,
259 MCConstantExpr::Create(MO
.getOffset(), Ctx
),
261 return MCOperand::CreateExpr(Expr
);
266 static void lower_subreg32(MCInst
*MI
, unsigned OpNo
) {
267 // Convert registers in the addr mode according to subreg32.
268 unsigned Reg
= MI
->getOperand(OpNo
).getReg();
270 MI
->getOperand(OpNo
).setReg(getX86SubSuperRegister(Reg
, MVT::i32
));
273 static void lower_lea64_32mem(MCInst
*MI
, unsigned OpNo
) {
274 // Convert registers in the addr mode according to subreg64.
275 for (unsigned i
= 0; i
!= 4; ++i
) {
276 if (!MI
->getOperand(OpNo
+i
).isReg()) continue;
278 unsigned Reg
= MI
->getOperand(OpNo
+i
).getReg();
279 if (Reg
== 0) continue;
281 MI
->getOperand(OpNo
+i
).setReg(getX86SubSuperRegister(Reg
, MVT::i64
));
287 void X86MCInstLower::Lower(const MachineInstr
*MI
, MCInst
&OutMI
) const {
288 OutMI
.setOpcode(MI
->getOpcode());
290 for (unsigned i
= 0, e
= MI
->getNumOperands(); i
!= e
; ++i
) {
291 const MachineOperand
&MO
= MI
->getOperand(i
);
294 switch (MO
.getType()) {
297 llvm_unreachable("unknown operand type");
298 case MachineOperand::MO_Register
:
299 MCOp
= MCOperand::CreateReg(MO
.getReg());
301 case MachineOperand::MO_Immediate
:
302 MCOp
= MCOperand::CreateImm(MO
.getImm());
304 case MachineOperand::MO_MachineBasicBlock
:
305 MCOp
= MCOperand::CreateExpr(MCSymbolRefExpr::Create(
306 AsmPrinter
.GetMBBSymbol(MO
.getMBB()->getNumber()), Ctx
));
308 case MachineOperand::MO_GlobalAddress
:
309 MCOp
= LowerSymbolOperand(MO
, GetGlobalAddressSymbol(MO
));
311 case MachineOperand::MO_ExternalSymbol
:
312 MCOp
= LowerSymbolOperand(MO
, GetExternalSymbolSymbol(MO
));
314 case MachineOperand::MO_JumpTableIndex
:
315 MCOp
= LowerSymbolOperand(MO
, GetJumpTableSymbol(MO
));
317 case MachineOperand::MO_ConstantPoolIndex
:
318 MCOp
= LowerSymbolOperand(MO
, GetConstantPoolIndexSymbol(MO
));
322 OutMI
.addOperand(MCOp
);
325 // Handle a few special cases to eliminate operand modifiers.
326 switch (OutMI
.getOpcode()) {
327 case X86::LEA64_32r
: // Handle 'subreg rewriting' for the lea64_32mem operand.
328 lower_lea64_32mem(&OutMI
, 1);
331 OutMI
.setOpcode(X86::MOV32r0
);
332 lower_subreg32(&OutMI
, 0);
334 case X86::MOVZX16rr8
:
335 OutMI
.setOpcode(X86::MOVZX32rr8
);
336 lower_subreg32(&OutMI
, 0);
338 case X86::MOVZX16rm8
:
339 OutMI
.setOpcode(X86::MOVZX32rm8
);
340 lower_subreg32(&OutMI
, 0);
342 case X86::MOVSX16rr8
:
343 OutMI
.setOpcode(X86::MOVSX32rr8
);
344 lower_subreg32(&OutMI
, 0);
346 case X86::MOVSX16rm8
:
347 OutMI
.setOpcode(X86::MOVSX32rm8
);
348 lower_subreg32(&OutMI
, 0);
350 case X86::MOVZX64rr32
:
351 OutMI
.setOpcode(X86::MOV32rr
);
352 lower_subreg32(&OutMI
, 0);
354 case X86::MOVZX64rm32
:
355 OutMI
.setOpcode(X86::MOV32rm
);
356 lower_subreg32(&OutMI
, 0);
358 case X86::MOV64ri64i32
:
359 OutMI
.setOpcode(X86::MOV32ri
);
360 lower_subreg32(&OutMI
, 0);
362 case X86::MOVZX64rr8
:
363 OutMI
.setOpcode(X86::MOVZX32rr8
);
364 lower_subreg32(&OutMI
, 0);
366 case X86::MOVZX64rm8
:
367 OutMI
.setOpcode(X86::MOVZX32rm8
);
368 lower_subreg32(&OutMI
, 0);
370 case X86::MOVZX64rr16
:
371 OutMI
.setOpcode(X86::MOVZX32rr16
);
372 lower_subreg32(&OutMI
, 0);
374 case X86::MOVZX64rm16
:
375 OutMI
.setOpcode(X86::MOVZX32rm16
);
376 lower_subreg32(&OutMI
, 0);
383 void X86ATTAsmPrinter::
384 printInstructionThroughMCStreamer(const MachineInstr
*MI
) {
385 X86MCInstLower
MCInstLowering(OutContext
, Mang
, *this);
386 switch (MI
->getOpcode()) {
387 case TargetInstrInfo::DBG_LABEL
:
388 case TargetInstrInfo::EH_LABEL
:
389 case TargetInstrInfo::GC_LABEL
:
392 case TargetInstrInfo::INLINEASM
:
396 case TargetInstrInfo::IMPLICIT_DEF
:
397 printImplicitDef(MI
);
399 case X86::MOVPC32r
: {
401 // This is a pseudo op for a two instruction sequence with a label, which
408 MCSymbol
*PICBase
= MCInstLowering
.GetPICBaseSymbol();
409 TmpInst
.setOpcode(X86::CALLpcrel32
);
410 // FIXME: We would like an efficient form for this, so we don't have to do a
411 // lot of extra uniquing.
412 TmpInst
.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase
,
414 printMCInst(&TmpInst
);
418 OutStreamer
.EmitLabel(PICBase
);
421 TmpInst
.setOpcode(X86::POP32r
);
422 TmpInst
.getOperand(0) = MCOperand::CreateReg(MI
->getOperand(0).getReg());
423 printMCInst(&TmpInst
);
428 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
429 if (MI
->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS
)
432 // Okay, we have something like:
433 // EAX = ADD32ri EAX, MO_GOT_ABSOLUTE_ADDRESS(@MYGLOBAL)
435 // For this, we want to print something like:
436 // MYGLOBAL + (. - PICBASE)
437 // However, we can't generate a ".", so just emit a new label here and refer
438 // to it. We know that this operand flag occurs at most once per function.
439 SmallString
<64> Name
;
440 raw_svector_ostream(Name
) << MAI
->getPrivateGlobalPrefix()
441 << "picbaseref" << getFunctionNumber();
442 MCSymbol
*DotSym
= OutContext
.GetOrCreateSymbol(Name
.str());
443 OutStreamer
.EmitLabel(DotSym
);
445 // Now that we have emitted the label, lower the complex operand expression.
446 MCSymbol
*OpSym
= MCInstLowering
.GetExternalSymbolSymbol(MI
->getOperand(2));
448 const MCExpr
*DotExpr
= MCSymbolRefExpr::Create(DotSym
, OutContext
);
449 const MCExpr
*PICBase
=
450 MCSymbolRefExpr::Create(MCInstLowering
.GetPICBaseSymbol(), OutContext
);
451 DotExpr
= MCBinaryExpr::CreateSub(DotExpr
, PICBase
, OutContext
);
453 DotExpr
= MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(OpSym
,OutContext
),
454 DotExpr
, OutContext
);
457 TmpInst
.setOpcode(X86::ADD32ri
);
458 TmpInst
.addOperand(MCOperand::CreateReg(MI
->getOperand(0).getReg()));
459 TmpInst
.addOperand(MCOperand::CreateReg(MI
->getOperand(1).getReg()));
460 TmpInst
.addOperand(MCOperand::CreateExpr(DotExpr
));
461 printMCInst(&TmpInst
);
467 MCInstLowering
.Lower(MI
, TmpInst
);
470 printMCInst(&TmpInst
);