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 SetupMachineFunction(MF);
33 // Print out constants referenced by the function
34 EmitConstantPool(MF.getConstantPool());
36 // Print out labels for the function.
37 const Function *F = MF.getFunction();
38 switch (F->getLinkage()) {
39 default: assert(0 && "Unknown linkage type!");
40 case Function::InternalLinkage: // Symbols default to internal.
41 SwitchSection(".text", F);
42 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
44 case Function::ExternalLinkage:
45 SwitchSection(".text", F);
46 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
47 O << "\t.globl\t" << CurrentFnName << "\n";
49 case Function::WeakLinkage:
50 case Function::LinkOnceLinkage:
52 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
54 O << "\t.globl\t" << CurrentFnName << "\n";
55 O << "\t.weak_definition\t" << CurrentFnName << "\n";
57 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
58 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
59 << ",\"ax\",@progbits\n";
60 O << "\t.weak " << CurrentFnName << "\n";
64 O << CurrentFnName << ":\n";
66 // Print out code for the function.
67 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
69 // Print a label for the basic block.
70 if (I->pred_begin() != I->pred_end())
71 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
72 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
74 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
76 // Print the assembly for the instruction.
78 printMachineInstruction(II);
81 if (HasDotTypeDotSizeDirective)
82 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
84 // We didn't modify anything.
88 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
89 const char *Modifier) {
90 const MachineOperand &MO = MI->getOperand(OpNo);
91 const MRegisterInfo &RI = *TM.getRegisterInfo();
92 switch (MO.getType()) {
93 case MachineOperand::MO_VirtualRegister:
94 case MachineOperand::MO_MachineRegister:
95 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
96 "Virtual registers should not make it this far!");
98 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
99 O << (char)tolower(*Name);
102 case MachineOperand::MO_SignExtendedImmed:
103 case MachineOperand::MO_UnextendedImmed:
104 O << '$' << (int)MO.getImmedValue();
106 case MachineOperand::MO_MachineBasicBlock: {
107 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
108 O << PrivateGlobalPrefix << "BB"
109 << Mang->getValueName(MBBOp->getParent()->getFunction())
110 << "_" << MBBOp->getNumber () << "\t# "
111 << MBBOp->getBasicBlock ()->getName ();
114 case MachineOperand::MO_PCRelativeDisp:
115 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
118 case MachineOperand::MO_GlobalAddress: {
119 bool isCallOp = Modifier && !strcmp(Modifier, "call");
120 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
121 if (!isMemOp && !isCallOp) O << '$';
122 // Darwin block shameless ripped from PPCAsmPrinter.cpp
123 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
124 GlobalValue *GV = MO.getGlobal();
125 std::string Name = Mang->getValueName(GV);
126 // Link-once, External, or Weakly-linked global variables need
127 // non-lazily-resolved stubs
128 if (GV->isExternal() || GV->hasWeakLinkage() ||
129 GV->hasLinkOnceLinkage()) {
130 // Dynamically-resolved functions need a stub for the function.
131 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
132 FnStubs.insert(Name);
133 O << "L" << Name << "$stub";
135 GVStubs.insert(Name);
136 O << "L" << Name << "$non_lazy_ptr";
139 O << Mang->getValueName(GV);
141 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
142 O << "-\"L" << getFunctionNumber() << "$pb\"";
144 O << Mang->getValueName(MO.getGlobal());
145 int Offset = MO.getOffset();
152 case MachineOperand::MO_ExternalSymbol: {
153 bool isCallOp = Modifier && !strcmp(Modifier, "call");
154 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
155 std::string Name(GlobalPrefix);
156 Name += MO.getSymbolName();
157 FnStubs.insert(Name);
158 O << "L" << Name << "$stub";
161 if (!isCallOp) O << '$';
162 O << GlobalPrefix << MO.getSymbolName();
166 O << "<unknown operand type>"; return;
170 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
171 unsigned char value = MI->getOperand(Op).getImmedValue();
172 assert(value <= 7 && "Invalid ssecc argument!");
174 case 0: O << "eq"; break;
175 case 1: O << "lt"; break;
176 case 2: O << "le"; break;
177 case 3: O << "unord"; break;
178 case 4: O << "neq"; break;
179 case 5: O << "nlt"; break;
180 case 6: O << "nle"; break;
181 case 7: O << "ord"; break;
185 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
186 assert(isMem(MI, Op) && "Invalid memory reference!");
188 const MachineOperand &BaseReg = MI->getOperand(Op);
189 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
190 const MachineOperand &IndexReg = MI->getOperand(Op+2);
191 const MachineOperand &DispSpec = MI->getOperand(Op+3);
193 if (BaseReg.isFrameIndex()) {
194 O << "[frame slot #" << BaseReg.getFrameIndex();
195 if (DispSpec.getImmedValue())
196 O << " + " << DispSpec.getImmedValue();
199 } else if (BaseReg.isConstantPoolIndex()) {
200 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
201 << BaseReg.getConstantPoolIndex();
202 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
203 O << "-\"L" << getFunctionNumber() << "$pb\"";
204 if (DispSpec.getImmedValue())
205 O << "+" << DispSpec.getImmedValue();
206 if (IndexReg.getReg()) {
208 printOperand(MI, Op+2);
210 O << "," << ScaleVal;
216 if (DispSpec.isGlobalAddress()) {
217 printOperand(MI, Op+3, "mem");
219 int DispVal = DispSpec.getImmedValue();
220 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
224 if (IndexReg.getReg() || BaseReg.getReg()) {
226 if (BaseReg.getReg())
227 printOperand(MI, Op);
229 if (IndexReg.getReg()) {
231 printOperand(MI, Op+2);
233 O << "," << ScaleVal;
240 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
241 O << "\"L" << getFunctionNumber() << "$pb\"\n";
242 O << "\"L" << getFunctionNumber() << "$pb\":";
245 /// printMachineInstruction -- Print out a single X86 LLVM instruction
246 /// MI in Intel syntax to the current output stream.
248 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
250 // This works around some Darwin assembler bugs.
252 switch (MI->getOpcode()) {
254 O << "rep/movsb (%esi),(%edi)\n";
257 O << "rep/movsl (%esi),(%edi)\n";
260 O << "rep/movsw (%esi),(%edi)\n";
276 // Call the autogenerated instruction printer routines.
277 printInstruction(MI);
280 // Include the auto-generated portion of the assembly writer.
281 #include "X86GenAsmWriter.inc"