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);
38 // Print out constants referenced by the function
39 EmitConstantPool(MF.getConstantPool());
41 // Print out jump tables referenced by the function
42 EmitJumpTableInfo(MF.getJumpTableInfo());
44 // Print out labels for the function.
45 const Function *F = MF.getFunction();
46 switch (F->getLinkage()) {
47 default: assert(0 && "Unknown linkage type!");
48 case Function::InternalLinkage: // Symbols default to internal.
49 SwitchSection(".text", F);
50 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
52 case Function::ExternalLinkage:
53 SwitchSection(".text", F);
54 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
55 O << "\t.globl\t" << CurrentFnName << "\n";
57 case Function::WeakLinkage:
58 case Function::LinkOnceLinkage:
60 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
62 O << "\t.globl\t" << CurrentFnName << "\n";
63 O << "\t.weak_definition\t" << CurrentFnName << "\n";
65 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
66 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
67 << ",\"ax\",@progbits\n";
68 O << "\t.weak " << CurrentFnName << "\n";
72 O << CurrentFnName << ":\n";
75 // Emit pre-function debug information.
76 DW.BeginFunction(&MF);
79 // Print out code for the function.
80 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
82 // Print a label for the basic block.
83 if (I->pred_begin() != I->pred_end())
84 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
85 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
87 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
89 // Print the assembly for the instruction.
91 printMachineInstruction(II);
94 if (HasDotTypeDotSizeDirective)
95 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
98 // Emit post-function debug information.
102 // We didn't modify anything.
106 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
107 const char *Modifier) {
108 const MachineOperand &MO = MI->getOperand(OpNo);
109 const MRegisterInfo &RI = *TM.getRegisterInfo();
110 switch (MO.getType()) {
111 case MachineOperand::MO_VirtualRegister:
112 case MachineOperand::MO_MachineRegister:
113 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
114 "Virtual registers should not make it this far!");
116 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
117 O << (char)tolower(*Name);
120 case MachineOperand::MO_SignExtendedImmed:
121 case MachineOperand::MO_UnextendedImmed:
122 if (!Modifier || strcmp(Modifier, "debug") != 0)
124 O << (int)MO.getImmedValue();
126 case MachineOperand::MO_MachineBasicBlock:
127 printBasicBlockLabel(MO.getMachineBasicBlock());
129 case MachineOperand::MO_PCRelativeDisp:
130 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
133 case MachineOperand::MO_JumpTableIndex: {
134 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
135 if (!isMemOp) O << '$';
136 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
137 << MO.getJumpTableIndex();
138 // FIXME: PIC relocation model
141 case MachineOperand::MO_ConstantPoolIndex: {
142 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
143 if (!isMemOp) O << '$';
144 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
145 << MO.getConstantPoolIndex();
146 if (forDarwin && TM.getRelocationModel() == Reloc::PIC)
147 O << "-\"L" << getFunctionNumber() << "$pb\"";
148 int Offset = MO.getOffset();
155 case MachineOperand::MO_GlobalAddress: {
156 bool isCallOp = Modifier && !strcmp(Modifier, "call");
157 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
158 if (!isMemOp && !isCallOp) O << '$';
159 // Darwin block shameless ripped from PPCAsmPrinter.cpp
160 if (forDarwin && TM.getRelocationModel() != Reloc::Static) {
161 GlobalValue *GV = MO.getGlobal();
162 std::string Name = Mang->getValueName(GV);
163 // Link-once, External, or Weakly-linked global variables need
164 // non-lazily-resolved stubs
165 if (GV->isExternal() || GV->hasWeakLinkage() ||
166 GV->hasLinkOnceLinkage()) {
167 // Dynamically-resolved functions need a stub for the function.
168 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
169 FnStubs.insert(Name);
170 O << "L" << Name << "$stub";
172 GVStubs.insert(Name);
173 O << "L" << Name << "$non_lazy_ptr";
176 O << Mang->getValueName(GV);
178 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC)
179 O << "-\"L" << getFunctionNumber() << "$pb\"";
181 O << Mang->getValueName(MO.getGlobal());
182 int Offset = MO.getOffset();
189 case MachineOperand::MO_ExternalSymbol: {
190 bool isCallOp = Modifier && !strcmp(Modifier, "call");
191 if (isCallOp && forDarwin && TM.getRelocationModel() != Reloc::Static) {
192 std::string Name(GlobalPrefix);
193 Name += MO.getSymbolName();
194 FnStubs.insert(Name);
195 O << "L" << Name << "$stub";
198 if (!isCallOp) O << '$';
199 O << GlobalPrefix << MO.getSymbolName();
203 O << "<unknown operand type>"; return;
207 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
208 unsigned char value = MI->getOperand(Op).getImmedValue();
209 assert(value <= 7 && "Invalid ssecc argument!");
211 case 0: O << "eq"; break;
212 case 1: O << "lt"; break;
213 case 2: O << "le"; break;
214 case 3: O << "unord"; break;
215 case 4: O << "neq"; break;
216 case 5: O << "nlt"; break;
217 case 6: O << "nle"; break;
218 case 7: O << "ord"; break;
222 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
223 assert(isMem(MI, Op) && "Invalid memory reference!");
225 const MachineOperand &BaseReg = MI->getOperand(Op);
226 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
227 const MachineOperand &IndexReg = MI->getOperand(Op+2);
228 const MachineOperand &DispSpec = MI->getOperand(Op+3);
230 if (BaseReg.isFrameIndex()) {
231 O << "[frame slot #" << BaseReg.getFrameIndex();
232 if (DispSpec.getImmedValue())
233 O << " + " << DispSpec.getImmedValue();
238 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
239 printOperand(MI, Op+3, "mem");
241 int DispVal = DispSpec.getImmedValue();
242 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
246 if (IndexReg.getReg() || BaseReg.getReg()) {
248 if (BaseReg.getReg())
249 printOperand(MI, Op);
251 if (IndexReg.getReg()) {
253 printOperand(MI, Op+2);
255 O << "," << ScaleVal;
262 void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
263 O << "\"L" << getFunctionNumber() << "$pb\"\n";
264 O << "\"L" << getFunctionNumber() << "$pb\":";
267 /// printMachineInstruction -- Print out a single X86 LLVM instruction
268 /// MI in Intel syntax to the current output stream.
270 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
272 // This works around some Darwin assembler bugs.
274 switch (MI->getOpcode()) {
276 O << "rep/movsb (%esi),(%edi)\n";
279 O << "rep/movsl (%esi),(%edi)\n";
282 O << "rep/movsw (%esi),(%edi)\n";
298 // Call the autogenerated instruction printer routines.
299 printInstruction(MI);
302 // Include the auto-generated portion of the assembly writer.
303 #include "X86GenAsmWriter.inc"