split MachineInstr -> MCInst lowering into its own class (not
[llvm/avr.git] / lib / Target / X86 / AsmPrinter / X86MCInstLower.cpp
blobed8207415f5677f5f9595f5ffbdfbed9053a29c8
1 //===-- X86MCInstLower.cpp - Convert X86 MachineInstr to an MCInst --------===//
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 // This file contains code to lower X86 MachineInstrs to their corresponding
11 // MCInst records.
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"
25 using namespace llvm;
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.
36 SmallString<60> Name;
38 if (getSubtarget().isTargetDarwin()) {
39 raw_svector_ostream(Name) << 'L' << AsmPrinter.getFunctionNumber() << "$pb";
40 } else {
41 assert(getSubtarget().isTargetELF() && "Don't know how to print PIC label!");
42 raw_svector_ostream(Name) << ".Lllvm$" << AsmPrinter.getFunctionNumber()
43 << ".$piclabel";
45 return Ctx.GetOrCreateSymbol(Name.str());
49 /// LowerGlobalAddressOperand - Lower an MO_GlobalAddress operand to an
50 /// MCOperand.
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.
73 break;
74 case X86II::MO_DLLIMPORT: {
75 // Handle dllimport linkage.
76 const char *Prefix = "__imp_";
77 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
78 break;
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];
85 if (StubSym == 0) {
86 Name.clear();
87 Mang->getNameWithPrefix(Name, GV, false);
88 StubSym = Ctx.GetOrCreateSymbol(Name.str());
90 return Sym;
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];
96 if (StubSym == 0) {
97 Name.clear();
98 Mang->getNameWithPrefix(Name, GV, false);
99 StubSym = Ctx.GetOrCreateSymbol(Name.str());
101 return Sym;
103 case X86II::MO_DARWIN_STUB: {
104 Name += "$stub";
105 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
106 MCSymbol *&StubSym = AsmPrinter.FnStubs[Sym];
107 if (StubSym == 0) {
108 Name.clear();
109 Mang->getNameWithPrefix(Name, GV, false);
110 StubSym = Ctx.GetOrCreateSymbol(Name.str());
112 return Sym;
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.
140 break;
141 case X86II::MO_DLLIMPORT: {
142 // Handle dllimport linkage.
143 const char *Prefix = "__imp_";
144 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
145 break;
147 case X86II::MO_DARWIN_STUB: {
148 Name += "$stub";
149 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
150 MCSymbol *&StubSym = AsmPrinter.FnStubs[Sym];
151 if (StubSym == 0) {
152 Name.erase(Name.end()-5, Name.end());
153 StubSym = Ctx.GetOrCreateSymbol(Name.str());
155 return Sym;
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()) {
178 default:
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:
184 break;
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()) {
209 default:
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:
215 break;
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:
252 case X86II::MO_GOT:
253 case X86II::MO_GOTOFF:
254 case X86II::MO_PLT:
255 break;
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(),
262 Ctx),
263 Ctx);
264 break;
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(),
279 Ctx);
280 DotExpr = MCBinaryExpr::CreateSub(DotExpr, PICBase, Ctx);
281 Expr = MCBinaryExpr::CreateAdd(Expr, DotExpr, Ctx);
282 break;
286 if (!MO.isJTI() && MO.getOffset())
287 Expr = MCBinaryExpr::CreateAdd(Expr,
288 MCConstantExpr::Create(MO.getOffset(), Ctx),
289 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();
298 if (Reg != 0)
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);
322 MCOperand MCOp;
323 switch (MO.getType()) {
324 default:
325 MI->dump();
326 llvm_unreachable("unknown operand type");
327 case MachineOperand::MO_Register:
328 MCOp = MCOperand::CreateReg(MO.getReg());
329 break;
330 case MachineOperand::MO_Immediate:
331 MCOp = MCOperand::CreateImm(MO.getImm());
332 break;
333 case MachineOperand::MO_MachineBasicBlock:
334 // FIXME: Kill MBBLabel operand type!
335 MCOp = MCOperand::CreateMBBLabel(AsmPrinter.getFunctionNumber(),
336 MO.getMBB()->getNumber());
337 break;
338 case MachineOperand::MO_GlobalAddress:
339 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
340 break;
341 case MachineOperand::MO_ExternalSymbol:
342 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
343 break;
344 case MachineOperand::MO_JumpTableIndex:
345 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
346 break;
347 case MachineOperand::MO_ConstantPoolIndex:
348 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
349 break;
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);
359 break;
360 case X86::MOV16r0:
361 OutMI.setOpcode(X86::MOV32r0);
362 lower_subreg32(&OutMI, 0);
363 break;
364 case X86::MOVZX16rr8:
365 OutMI.setOpcode(X86::MOVZX32rr8);
366 lower_subreg32(&OutMI, 0);
367 break;
368 case X86::MOVZX16rm8:
369 OutMI.setOpcode(X86::MOVZX32rm8);
370 lower_subreg32(&OutMI, 0);
371 break;
372 case X86::MOVSX16rr8:
373 OutMI.setOpcode(X86::MOVSX32rr8);
374 lower_subreg32(&OutMI, 0);
375 break;
376 case X86::MOVSX16rm8:
377 OutMI.setOpcode(X86::MOVSX32rm8);
378 lower_subreg32(&OutMI, 0);
379 break;
380 case X86::MOVZX64rr32:
381 OutMI.setOpcode(X86::MOV32rr);
382 lower_subreg32(&OutMI, 0);
383 break;
384 case X86::MOVZX64rm32:
385 OutMI.setOpcode(X86::MOV32rm);
386 lower_subreg32(&OutMI, 0);
387 break;
388 case X86::MOV64ri64i32:
389 OutMI.setOpcode(X86::MOV32ri);
390 lower_subreg32(&OutMI, 0);
391 break;
392 case X86::MOVZX64rr8:
393 OutMI.setOpcode(X86::MOVZX32rr8);
394 lower_subreg32(&OutMI, 0);
395 break;
396 case X86::MOVZX64rm8:
397 OutMI.setOpcode(X86::MOVZX32rm8);
398 lower_subreg32(&OutMI, 0);
399 break;
400 case X86::MOVZX64rr16:
401 OutMI.setOpcode(X86::MOVZX32rr16);
402 lower_subreg32(&OutMI, 0);
403 break;
404 case X86::MOVZX64rm16:
405 OutMI.setOpcode(X86::MOVZX32rm16);
406 lower_subreg32(&OutMI, 0);
407 break;
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:
420 printLabel(MI);
421 return;
422 case TargetInstrInfo::INLINEASM:
423 O << '\t';
424 printInlineAsm(MI);
425 return;
426 case TargetInstrInfo::IMPLICIT_DEF:
427 printImplicitDef(MI);
428 return;
429 case X86::MOVPC32r: {
430 MCInst TmpInst;
431 // This is a pseudo op for a two instruction sequence with a label, which
432 // looks like:
433 // call "L1$pb"
434 // "L1$pb":
435 // popl %esi
437 // Emit the call.
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,
443 OutContext)));
444 printInstruction(&TmpInst);
445 O << '\n';
447 // Emit the label.
448 OutStreamer.EmitLabel(PICBase);
450 // popl $reg
451 TmpInst.setOpcode(X86::POP32r);
452 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
453 printInstruction(&TmpInst);
454 return;
458 MCInst TmpInst;
459 MCInstLowering.Lower(MI, TmpInst);
462 printInstruction(&TmpInst);