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"
25 /// runOnMachineFunction - This uses the printMachineInstruction()
26 /// method to print assembly for each instruction.
28 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>());
35 SetupMachineFunction(MF);
39 // Emit pre-function debug information.
43 // Print out constants referenced by the function
44 EmitConstantPool(MF.getConstantPool());
46 // Print out labels for the function.
47 const Function *F = MF.getFunction();
48 switch (F->getLinkage()) {
49 default: assert(0 && "Unknown linkage type!");
50 case Function::InternalLinkage: // Symbols default to internal.
51 SwitchSection(".text", F);
52 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
54 case Function::ExternalLinkage:
55 SwitchSection(".text", F);
56 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
57 O << "\t.globl\t" << CurrentFnName << "\n";
59 case Function::WeakLinkage:
60 case Function::LinkOnceLinkage:
62 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
64 O << "\t.globl\t" << CurrentFnName << "\n";
65 O << "\t.weak_definition\t" << CurrentFnName << "\n";
67 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
68 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
69 << ",\"ax\",@progbits\n";
70 O << "\t.weak " << CurrentFnName << "\n";
74 O << CurrentFnName << ":\n";
76 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
79 // Print a label for the basic block.
80 if (I->pred_begin() != I->pred_end())
81 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
82 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
84 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
86 // Print the assembly for the instruction.
88 printMachineInstruction(II);
91 if (HasDotTypeDotSizeDirective)
92 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
95 // Emit post-function debug information.
99 // We didn't modify anything.
103 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
104 const char *Modifier) {
105 const MachineOperand &MO = MI->getOperand(OpNo);
106 const MRegisterInfo &RI = *TM.getRegisterInfo();
107 switch (MO.getType()) {
108 case MachineOperand::MO_VirtualRegister:
109 case MachineOperand::MO_MachineRegister:
110 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
111 "Virtual registers should not make it this far!");
113 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
114 O << (char)tolower(*Name);
117 case MachineOperand::MO_SignExtendedImmed:
118 case MachineOperand::MO_UnextendedImmed:
119 if (!Modifier || strcmp(Modifier, "debug") != 0)
121 O << (int)MO.getImmedValue();
123 case MachineOperand::MO_MachineBasicBlock: {
124 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
125 O << PrivateGlobalPrefix << "BB"
126 << Mang->getValueName(MBBOp->getParent()->getFunction())
127 << "_" << MBBOp->getNumber () << "\t# "
128 << MBBOp->getBasicBlock ()->getName ();
131 case MachineOperand::MO_PCRelativeDisp:
132 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
135 case MachineOperand::MO_ConstantPoolIndex: {
136 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
137 if (!isMemOp) O << '$';
138 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
139 << MO.getConstantPoolIndex();
140 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
141 O << "-\"L" << getFunctionNumber() << "$pb\"";
142 int Offset = MO.getOffset();
149 case MachineOperand::MO_GlobalAddress: {
150 bool isCallOp = Modifier && !strcmp(Modifier, "call");
151 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
152 if (!isMemOp && !isCallOp) O << '$';
153 // Darwin block shameless ripped from PPCAsmPrinter.cpp
154 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
155 GlobalValue *GV = MO.getGlobal();
156 std::string Name = Mang->getValueName(GV);
157 // Link-once, External, or Weakly-linked global variables need
158 // non-lazily-resolved stubs
159 if (GV->isExternal() || GV->hasWeakLinkage() ||
160 GV->hasLinkOnceLinkage()) {
161 // Dynamically-resolved functions need a stub for the function.
162 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
163 FnStubs.insert(Name);
164 O << "L" << Name << "$stub";
166 GVStubs.insert(Name);
167 O << "L" << Name << "$non_lazy_ptr";
170 O << Mang->getValueName(GV);
172 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
173 O << "-\"L" << getFunctionNumber() << "$pb\"";
175 O << Mang->getValueName(MO.getGlobal());
176 int Offset = MO.getOffset();
183 case MachineOperand::MO_ExternalSymbol: {
184 bool isCallOp = Modifier && !strcmp(Modifier, "call");
185 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
186 std::string Name(GlobalPrefix);
187 Name += MO.getSymbolName();
188 FnStubs.insert(Name);
189 O << "L" << Name << "$stub";
192 if (!isCallOp) O << '$';
193 O << GlobalPrefix << MO.getSymbolName();
197 O << "<unknown operand type>"; return;
201 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
202 unsigned char value = MI->getOperand(Op).getImmedValue();
203 assert(value <= 7 && "Invalid ssecc argument!");
205 case 0: O << "eq"; break;
206 case 1: O << "lt"; break;
207 case 2: O << "le"; break;
208 case 3: O << "unord"; break;
209 case 4: O << "neq"; break;
210 case 5: O << "nlt"; break;
211 case 6: O << "nle"; break;
212 case 7: O << "ord"; break;
216 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
217 assert(isMem(MI, Op) && "Invalid memory reference!");
219 const MachineOperand &BaseReg = MI->getOperand(Op);
220 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
221 const MachineOperand &IndexReg = MI->getOperand(Op+2);
222 const MachineOperand &DispSpec = MI->getOperand(Op+3);
224 if (BaseReg.isFrameIndex()) {
225 O << "[frame slot #" << BaseReg.getFrameIndex();
226 if (DispSpec.getImmedValue())
227 O << " + " << DispSpec.getImmedValue();
232 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
233 printOperand(MI, Op+3, "mem");
235 int DispVal = DispSpec.getImmedValue();
236 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
240 if (IndexReg.getReg() || BaseReg.getReg()) {
242 if (BaseReg.getReg())
243 printOperand(MI, Op);
245 if (IndexReg.getReg()) {
247 printOperand(MI, Op+2);
249 O << "," << ScaleVal;
256 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
257 O << "\"L" << getFunctionNumber() << "$pb\"\n";
258 O << "\"L" << getFunctionNumber() << "$pb\":";
261 /// printMachineInstruction -- Print out a single X86 LLVM instruction
262 /// MI in Intel syntax to the current output stream.
264 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
266 // This works around some Darwin assembler bugs.
268 switch (MI->getOpcode()) {
270 O << "rep/movsb (%esi),(%edi)\n";
273 O << "rep/movsl (%esi),(%edi)\n";
276 O << "rep/movsw (%esi),(%edi)\n";
292 // Call the autogenerated instruction printer routines.
293 printInstruction(MI);
296 // Include the auto-generated portion of the assembly writer.
297 #include "X86GenAsmWriter.inc"