1 //===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to AT&T format assembly
12 // language. This printer is the output mechanism used by `llc'.
14 //===----------------------------------------------------------------------===//
16 #include "X86ATTAsmPrinter.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Support/Mangler.h"
21 #include "llvm/Target/TargetOptions.h"
26 /// runOnMachineFunction - This uses the printMachineInstruction()
27 /// method to print assembly for each instruction.
29 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
30 // Let PassManager know we need debug information and relay
31 // the MachineDebugInfo address on to DwarfWriter.
32 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
34 SetupMachineFunction(MF);
37 // Emit pre-function debug information.
40 // Print out constants referenced by the function
41 EmitConstantPool(MF.getConstantPool());
43 // Print out labels for the function.
44 const Function *F = MF.getFunction();
45 switch (F->getLinkage()) {
46 default: assert(0 && "Unknown linkage type!");
47 case Function::InternalLinkage: // Symbols default to internal.
48 SwitchSection(".text", F);
49 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
51 case Function::ExternalLinkage:
52 SwitchSection(".text", F);
53 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
54 O << "\t.globl\t" << CurrentFnName << "\n";
56 case Function::WeakLinkage:
57 case Function::LinkOnceLinkage:
59 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
61 O << "\t.globl\t" << CurrentFnName << "\n";
62 O << "\t.weak_definition\t" << CurrentFnName << "\n";
64 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
65 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
66 << ",\"ax\",@progbits\n";
67 O << "\t.weak " << CurrentFnName << "\n";
71 O << CurrentFnName << ":\n";
73 // Print out code for the function.
74 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
76 // Print a label for the basic block.
77 if (I->pred_begin() != I->pred_end())
78 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
79 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
81 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
83 // Print the assembly for the instruction.
85 printMachineInstruction(II);
88 if (HasDotTypeDotSizeDirective)
89 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
91 // Emit post-function debug information.
94 // We didn't modify anything.
98 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
99 const char *Modifier) {
100 const MachineOperand &MO = MI->getOperand(OpNo);
101 const MRegisterInfo &RI = *TM.getRegisterInfo();
102 switch (MO.getType()) {
103 case MachineOperand::MO_VirtualRegister:
104 case MachineOperand::MO_MachineRegister:
105 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
106 "Virtual registers should not make it this far!");
108 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
109 O << (char)tolower(*Name);
112 case MachineOperand::MO_SignExtendedImmed:
113 case MachineOperand::MO_UnextendedImmed:
114 if (!Modifier || strcmp(Modifier, "debug") != 0)
116 O << (int)MO.getImmedValue();
118 case MachineOperand::MO_MachineBasicBlock: {
119 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
120 O << PrivateGlobalPrefix << "BB"
121 << Mang->getValueName(MBBOp->getParent()->getFunction())
122 << "_" << MBBOp->getNumber () << "\t# "
123 << MBBOp->getBasicBlock ()->getName ();
126 case MachineOperand::MO_PCRelativeDisp:
127 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
130 case MachineOperand::MO_ConstantPoolIndex: {
131 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
132 if (!isMemOp) O << '$';
133 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
134 << MO.getConstantPoolIndex();
135 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
136 O << "-\"L" << getFunctionNumber() << "$pb\"";
137 int Offset = MO.getOffset();
144 case MachineOperand::MO_GlobalAddress: {
145 bool isCallOp = Modifier && !strcmp(Modifier, "call");
146 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
147 if (!isMemOp && !isCallOp) O << '$';
148 // Darwin block shameless ripped from PPCAsmPrinter.cpp
149 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
150 GlobalValue *GV = MO.getGlobal();
151 std::string Name = Mang->getValueName(GV);
152 // Link-once, External, or Weakly-linked global variables need
153 // non-lazily-resolved stubs
154 if (GV->isExternal() || GV->hasWeakLinkage() ||
155 GV->hasLinkOnceLinkage()) {
156 // Dynamically-resolved functions need a stub for the function.
157 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
158 FnStubs.insert(Name);
159 O << "L" << Name << "$stub";
161 GVStubs.insert(Name);
162 O << "L" << Name << "$non_lazy_ptr";
165 O << Mang->getValueName(GV);
167 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
168 O << "-\"L" << getFunctionNumber() << "$pb\"";
170 O << Mang->getValueName(MO.getGlobal());
171 int Offset = MO.getOffset();
178 case MachineOperand::MO_ExternalSymbol: {
179 bool isCallOp = Modifier && !strcmp(Modifier, "call");
180 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
181 std::string Name(GlobalPrefix);
182 Name += MO.getSymbolName();
183 FnStubs.insert(Name);
184 O << "L" << Name << "$stub";
187 if (!isCallOp) O << '$';
188 O << GlobalPrefix << MO.getSymbolName();
192 O << "<unknown operand type>"; return;
196 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
197 unsigned char value = MI->getOperand(Op).getImmedValue();
198 assert(value <= 7 && "Invalid ssecc argument!");
200 case 0: O << "eq"; break;
201 case 1: O << "lt"; break;
202 case 2: O << "le"; break;
203 case 3: O << "unord"; break;
204 case 4: O << "neq"; break;
205 case 5: O << "nlt"; break;
206 case 6: O << "nle"; break;
207 case 7: O << "ord"; break;
211 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
212 assert(isMem(MI, Op) && "Invalid memory reference!");
214 const MachineOperand &BaseReg = MI->getOperand(Op);
215 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
216 const MachineOperand &IndexReg = MI->getOperand(Op+2);
217 const MachineOperand &DispSpec = MI->getOperand(Op+3);
219 if (BaseReg.isFrameIndex()) {
220 O << "[frame slot #" << BaseReg.getFrameIndex();
221 if (DispSpec.getImmedValue())
222 O << " + " << DispSpec.getImmedValue();
227 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
228 printOperand(MI, Op+3, "mem");
230 int DispVal = DispSpec.getImmedValue();
231 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
235 if (IndexReg.getReg() || BaseReg.getReg()) {
237 if (BaseReg.getReg())
238 printOperand(MI, Op);
240 if (IndexReg.getReg()) {
242 printOperand(MI, Op+2);
244 O << "," << ScaleVal;
251 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
252 O << "\"L" << getFunctionNumber() << "$pb\"\n";
253 O << "\"L" << getFunctionNumber() << "$pb\":";
256 /// printMachineInstruction -- Print out a single X86 LLVM instruction
257 /// MI in Intel syntax to the current output stream.
259 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
261 // This works around some Darwin assembler bugs.
263 switch (MI->getOpcode()) {
265 O << "rep/movsb (%esi),(%edi)\n";
268 O << "rep/movsl (%esi),(%edi)\n";
271 O << "rep/movsw (%esi),(%edi)\n";
287 // Call the autogenerated instruction printer routines.
288 printInstruction(MI);
291 // Include the auto-generated portion of the assembly writer.
292 #include "X86GenAsmWriter.inc"