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) {
31 // Let PassManager know we need debug information and relay
32 // the MachineDebugInfo address on to DwarfWriter.
33 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
36 SetupMachineFunction(MF);
40 // Emit pre-function debug information.
44 // Print out constants referenced by the function
45 EmitConstantPool(MF.getConstantPool());
47 // Print out labels for the function.
48 const Function *F = MF.getFunction();
49 switch (F->getLinkage()) {
50 default: assert(0 && "Unknown linkage type!");
51 case Function::InternalLinkage: // Symbols default to internal.
52 SwitchSection(".text", F);
53 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
55 case Function::ExternalLinkage:
56 SwitchSection(".text", F);
57 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
58 O << "\t.globl\t" << CurrentFnName << "\n";
60 case Function::WeakLinkage:
61 case Function::LinkOnceLinkage:
63 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
65 O << "\t.globl\t" << CurrentFnName << "\n";
66 O << "\t.weak_definition\t" << CurrentFnName << "\n";
68 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
69 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
70 << ",\"ax\",@progbits\n";
71 O << "\t.weak " << CurrentFnName << "\n";
75 O << CurrentFnName << ":\n";
77 // Print out code for the function.
78 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
80 // Print a label for the basic block.
81 if (I->pred_begin() != I->pred_end())
82 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
83 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
85 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
87 // Print the assembly for the instruction.
89 printMachineInstruction(II);
92 if (HasDotTypeDotSizeDirective)
93 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
96 // Emit post-function debug information.
100 // We didn't modify anything.
104 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
105 const char *Modifier) {
106 const MachineOperand &MO = MI->getOperand(OpNo);
107 const MRegisterInfo &RI = *TM.getRegisterInfo();
108 switch (MO.getType()) {
109 case MachineOperand::MO_VirtualRegister:
110 case MachineOperand::MO_MachineRegister:
111 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
112 "Virtual registers should not make it this far!");
114 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
115 O << (char)tolower(*Name);
118 case MachineOperand::MO_SignExtendedImmed:
119 case MachineOperand::MO_UnextendedImmed:
120 if (!Modifier || strcmp(Modifier, "debug") != 0)
122 O << (int)MO.getImmedValue();
124 case MachineOperand::MO_MachineBasicBlock: {
125 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
126 O << PrivateGlobalPrefix << "BB"
127 << Mang->getValueName(MBBOp->getParent()->getFunction())
128 << "_" << MBBOp->getNumber () << "\t# "
129 << MBBOp->getBasicBlock ()->getName ();
132 case MachineOperand::MO_PCRelativeDisp:
133 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
136 case MachineOperand::MO_ConstantPoolIndex: {
137 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
138 if (!isMemOp) O << '$';
139 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
140 << MO.getConstantPoolIndex();
141 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
142 O << "-\"L" << getFunctionNumber() << "$pb\"";
143 int Offset = MO.getOffset();
150 case MachineOperand::MO_GlobalAddress: {
151 bool isCallOp = Modifier && !strcmp(Modifier, "call");
152 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
153 if (!isMemOp && !isCallOp) O << '$';
154 // Darwin block shameless ripped from PPCAsmPrinter.cpp
155 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
156 GlobalValue *GV = MO.getGlobal();
157 std::string Name = Mang->getValueName(GV);
158 // Link-once, External, or Weakly-linked global variables need
159 // non-lazily-resolved stubs
160 if (GV->isExternal() || GV->hasWeakLinkage() ||
161 GV->hasLinkOnceLinkage()) {
162 // Dynamically-resolved functions need a stub for the function.
163 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
164 FnStubs.insert(Name);
165 O << "L" << Name << "$stub";
167 GVStubs.insert(Name);
168 O << "L" << Name << "$non_lazy_ptr";
171 O << Mang->getValueName(GV);
173 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
174 O << "-\"L" << getFunctionNumber() << "$pb\"";
176 O << Mang->getValueName(MO.getGlobal());
177 int Offset = MO.getOffset();
184 case MachineOperand::MO_ExternalSymbol: {
185 bool isCallOp = Modifier && !strcmp(Modifier, "call");
186 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
187 std::string Name(GlobalPrefix);
188 Name += MO.getSymbolName();
189 FnStubs.insert(Name);
190 O << "L" << Name << "$stub";
193 if (!isCallOp) O << '$';
194 O << GlobalPrefix << MO.getSymbolName();
198 O << "<unknown operand type>"; return;
202 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
203 unsigned char value = MI->getOperand(Op).getImmedValue();
204 assert(value <= 7 && "Invalid ssecc argument!");
206 case 0: O << "eq"; break;
207 case 1: O << "lt"; break;
208 case 2: O << "le"; break;
209 case 3: O << "unord"; break;
210 case 4: O << "neq"; break;
211 case 5: O << "nlt"; break;
212 case 6: O << "nle"; break;
213 case 7: O << "ord"; break;
217 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
218 assert(isMem(MI, Op) && "Invalid memory reference!");
220 const MachineOperand &BaseReg = MI->getOperand(Op);
221 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
222 const MachineOperand &IndexReg = MI->getOperand(Op+2);
223 const MachineOperand &DispSpec = MI->getOperand(Op+3);
225 if (BaseReg.isFrameIndex()) {
226 O << "[frame slot #" << BaseReg.getFrameIndex();
227 if (DispSpec.getImmedValue())
228 O << " + " << DispSpec.getImmedValue();
233 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
234 printOperand(MI, Op+3, "mem");
236 int DispVal = DispSpec.getImmedValue();
237 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
241 if (IndexReg.getReg() || BaseReg.getReg()) {
243 if (BaseReg.getReg())
244 printOperand(MI, Op);
246 if (IndexReg.getReg()) {
248 printOperand(MI, Op+2);
250 O << "," << ScaleVal;
257 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
258 O << "\"L" << getFunctionNumber() << "$pb\"\n";
259 O << "\"L" << getFunctionNumber() << "$pb\":";
262 /// printMachineInstruction -- Print out a single X86 LLVM instruction
263 /// MI in Intel syntax to the current output stream.
265 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
267 // This works around some Darwin assembler bugs.
269 switch (MI->getOpcode()) {
271 O << "rep/movsb (%esi),(%edi)\n";
274 O << "rep/movsl (%esi),(%edi)\n";
277 O << "rep/movsw (%esi),(%edi)\n";
293 // Call the autogenerated instruction printer routines.
294 printInstruction(MI);
297 // Include the auto-generated portion of the assembly writer.
298 #include "X86GenAsmWriter.inc"