pass machinemoduleinfo down into getSymbolForDwarfGlobalReference,
[llvm/avr.git] / lib / Target / X86 / AsmPrinter / X86MCInstLower.cpp
blob116e4b050230114c1191938cad36067c692014ec
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/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"
26 using namespace llvm;
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 {
40 SmallString<60> Name;
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
48 /// MCOperand.
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.
70 break;
71 case X86II::MO_DLLIMPORT: {
72 // Handle dllimport linkage.
73 const char *Prefix = "__imp_";
74 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
75 break;
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);
83 if (StubSym == 0) {
84 Name.clear();
85 Mang->getNameWithPrefix(Name, GV, false);
86 StubSym = Ctx.GetOrCreateSymbol(Name.str());
88 return Sym;
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);
94 if (StubSym == 0) {
95 Name.clear();
96 Mang->getNameWithPrefix(Name, GV, false);
97 StubSym = Ctx.GetOrCreateSymbol(Name.str());
99 return Sym;
101 case X86II::MO_DARWIN_STUB: {
102 Name += "$stub";
103 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
104 const MCSymbol *&StubSym = getMachOMMI().getFnStubEntry(Sym);
105 if (StubSym == 0) {
106 Name.clear();
107 Mang->getNameWithPrefix(Name, GV, false);
108 StubSym = Ctx.GetOrCreateSymbol(Name.str());
110 return Sym;
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.
138 break;
139 case X86II::MO_DLLIMPORT: {
140 // Handle dllimport linkage.
141 const char *Prefix = "__imp_";
142 Name.insert(Name.begin(), Prefix, Prefix+strlen(Prefix));
143 break;
145 case X86II::MO_DARWIN_STUB: {
146 Name += "$stub";
147 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name.str());
148 const MCSymbol *&StubSym = getMachOMMI().getFnStubEntry(Sym);
150 if (StubSym == 0) {
151 Name.erase(Name.end()-5, Name.end());
152 StubSym = Ctx.GetOrCreateSymbol(Name.str());
154 return Sym;
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()) {
177 default:
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:
183 break;
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()) {
208 default:
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:
214 break;
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:
251 case X86II::MO_GOT:
252 case X86II::MO_GOTOFF:
253 case X86II::MO_PLT:
254 break;
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),
261 Ctx);
262 break;
265 if (!MO.isJTI() && MO.getOffset())
266 Expr = MCBinaryExpr::CreateAdd(Expr,
267 MCConstantExpr::Create(MO.getOffset(), Ctx),
268 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();
277 if (Reg != 0)
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);
301 MCOperand MCOp;
302 switch (MO.getType()) {
303 default:
304 MI->dump();
305 llvm_unreachable("unknown operand type");
306 case MachineOperand::MO_Register:
307 MCOp = MCOperand::CreateReg(MO.getReg());
308 break;
309 case MachineOperand::MO_Immediate:
310 MCOp = MCOperand::CreateImm(MO.getImm());
311 break;
312 case MachineOperand::MO_MachineBasicBlock:
313 MCOp = MCOperand::CreateExpr(MCSymbolRefExpr::Create(
314 AsmPrinter.GetMBBSymbol(MO.getMBB()->getNumber()), Ctx));
315 break;
316 case MachineOperand::MO_GlobalAddress:
317 MCOp = LowerSymbolOperand(MO, GetGlobalAddressSymbol(MO));
318 break;
319 case MachineOperand::MO_ExternalSymbol:
320 MCOp = LowerSymbolOperand(MO, GetExternalSymbolSymbol(MO));
321 break;
322 case MachineOperand::MO_JumpTableIndex:
323 MCOp = LowerSymbolOperand(MO, GetJumpTableSymbol(MO));
324 break;
325 case MachineOperand::MO_ConstantPoolIndex:
326 MCOp = LowerSymbolOperand(MO, GetConstantPoolIndexSymbol(MO));
327 break;
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);
337 break;
338 case X86::MOV16r0:
339 OutMI.setOpcode(X86::MOV32r0);
340 lower_subreg32(&OutMI, 0);
341 break;
342 case X86::MOVZX16rr8:
343 OutMI.setOpcode(X86::MOVZX32rr8);
344 lower_subreg32(&OutMI, 0);
345 break;
346 case X86::MOVZX16rm8:
347 OutMI.setOpcode(X86::MOVZX32rm8);
348 lower_subreg32(&OutMI, 0);
349 break;
350 case X86::MOVSX16rr8:
351 OutMI.setOpcode(X86::MOVSX32rr8);
352 lower_subreg32(&OutMI, 0);
353 break;
354 case X86::MOVSX16rm8:
355 OutMI.setOpcode(X86::MOVSX32rm8);
356 lower_subreg32(&OutMI, 0);
357 break;
358 case X86::MOVZX64rr32:
359 OutMI.setOpcode(X86::MOV32rr);
360 lower_subreg32(&OutMI, 0);
361 break;
362 case X86::MOVZX64rm32:
363 OutMI.setOpcode(X86::MOV32rm);
364 lower_subreg32(&OutMI, 0);
365 break;
366 case X86::MOV64ri64i32:
367 OutMI.setOpcode(X86::MOV32ri);
368 lower_subreg32(&OutMI, 0);
369 break;
370 case X86::MOVZX64rr8:
371 OutMI.setOpcode(X86::MOVZX32rr8);
372 lower_subreg32(&OutMI, 0);
373 break;
374 case X86::MOVZX64rm8:
375 OutMI.setOpcode(X86::MOVZX32rm8);
376 lower_subreg32(&OutMI, 0);
377 break;
378 case X86::MOVZX64rr16:
379 OutMI.setOpcode(X86::MOVZX32rr16);
380 lower_subreg32(&OutMI, 0);
381 break;
382 case X86::MOVZX64rm16:
383 OutMI.setOpcode(X86::MOVZX32rm16);
384 lower_subreg32(&OutMI, 0);
385 break;
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:
398 printLabel(MI);
399 return;
400 case TargetInstrInfo::INLINEASM:
401 O << '\t';
402 printInlineAsm(MI);
403 return;
404 case TargetInstrInfo::IMPLICIT_DEF:
405 printImplicitDef(MI);
406 return;
407 case X86::MOVPC32r: {
408 MCInst TmpInst;
409 // This is a pseudo op for a two instruction sequence with a label, which
410 // looks like:
411 // call "L1$pb"
412 // "L1$pb":
413 // popl %esi
415 // Emit the call.
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,
421 OutContext)));
422 printMCInst(&TmpInst);
423 O << '\n';
425 // Emit the label.
426 OutStreamer.EmitLabel(PICBase);
428 // popl $reg
429 TmpInst.setOpcode(X86::POP32r);
430 TmpInst.getOperand(0) = MCOperand::CreateReg(MI->getOperand(0).getReg());
431 printMCInst(&TmpInst);
432 return;
435 case X86::ADD32ri: {
436 // Lower the MO_GOT_ABSOLUTE_ADDRESS form of ADD32ri.
437 if (MI->getOperand(2).getTargetFlags() != X86II::MO_GOT_ABSOLUTE_ADDRESS)
438 break;
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);
464 MCInst TmpInst;
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);
470 return;
474 MCInst TmpInst;
475 MCInstLowering.Lower(MI, TmpInst);
478 printMCInst(&TmpInst);