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 printBasicBlockLabel(I, true);
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 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
113 "Virtual registers should not make it this far!");
115 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
116 O << (char)tolower(*Name);
119 case MachineOperand::MO_Immediate:
120 if (!Modifier || strcmp(Modifier, "debug") != 0)
122 O << (int)MO.getImmedValue();
124 case MachineOperand::MO_MachineBasicBlock:
125 printBasicBlockLabel(MO.getMachineBasicBlock());
127 case MachineOperand::MO_JumpTableIndex: {
128 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
129 if (!isMemOp) O << '$';
130 O << PrivateGlobalPrefix << "JTI" << getFunctionNumber() << "_"
131 << MO.getJumpTableIndex();
132 // FIXME: PIC relocation model
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\":";
262 bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
264 const MRegisterInfo &RI = *TM.getRegisterInfo();
265 unsigned Reg = MO.getReg();
266 const char *Name = RI.get(Reg).Name;
268 default: return true; // Unknown mode.
269 case 'b': // Print QImode register
271 default: return true;
272 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
275 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
278 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
281 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
298 case 'h': // Print QImode high register
300 default: return true;
301 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
304 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
307 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
310 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
315 case 'w': // Print HImode register
317 default: return true;
318 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
321 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
324 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
327 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
344 case 'k': // Print SImode register
346 default: return true;
347 case X86::AH: case X86::AL: case X86::AX: case X86::EAX:
350 case X86::DH: case X86::DL: case X86::DX: case X86::EDX:
353 case X86::CH: case X86::CL: case X86::CX: case X86::ECX:
356 case X86::BH: case X86::BL: case X86::BX: case X86::EBX:
379 /// PrintAsmOperand - Print out an operand for an inline asm expression.
381 bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
383 const char *ExtraCode) {
384 // Does this asm operand have a single letter operand modifier?
385 if (ExtraCode && ExtraCode[0]) {
386 if (ExtraCode[1] != 0) return true; // Unknown modifier.
388 switch (ExtraCode[0]) {
389 default: return true; // Unknown modifier.
390 case 'b': // Print QImode register
391 case 'h': // Print QImode high register
392 case 'w': // Print HImode register
393 case 'k': // Print SImode register
394 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
398 printOperand(MI, OpNo);
402 bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
405 const char *ExtraCode) {
406 if (ExtraCode && ExtraCode[0])
407 return true; // Unknown modifier.
408 printMemReference(MI, OpNo);
412 /// printMachineInstruction -- Print out a single X86 LLVM instruction
413 /// MI in Intel syntax to the current output stream.
415 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
417 // This works around some Darwin assembler bugs.
419 switch (MI->getOpcode()) {
421 O << "rep/movsb (%esi),(%edi)\n";
424 O << "rep/movsl (%esi),(%edi)\n";
427 O << "rep/movsw (%esi),(%edi)\n";
443 // Call the autogenerated instruction printer routines.
444 printInstruction(MI);
447 // Include the auto-generated portion of the assembly writer.
448 #include "X86GenAsmWriter.inc"