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"
25 /// runOnMachineFunction - This uses the printMachineInstruction()
26 /// method to print assembly for each instruction.
28 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
29 SetupMachineFunction(MF);
32 // Print out constants referenced by the function
33 EmitConstantPool(MF.getConstantPool());
35 // Print out labels for the function.
36 const Function *F = MF.getFunction();
37 switch (F->getLinkage()) {
38 default: assert(0 && "Unknown linkage type!");
39 case Function::InternalLinkage: // Symbols default to internal.
40 SwitchSection(".text", F);
41 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
43 case Function::ExternalLinkage:
44 SwitchSection(".text", F);
45 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
46 O << "\t.globl\t" << CurrentFnName << "\n";
48 case Function::WeakLinkage:
49 case Function::LinkOnceLinkage:
51 SwitchSection(".section __TEXT,__textcoal_nt,coalesced,pure_instructions",
53 O << "\t.weak_definition\t" << CurrentFnName << "\n";
55 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
56 O << "\t.section\t.llvm.linkonce.t." << CurrentFnName
57 << ",\"ax\",@progbits\n";
58 O << "\t.weak " << CurrentFnName << "\n";
62 O << CurrentFnName << ":\n";
64 // Print out code for the function.
65 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
67 // Print a label for the basic block.
68 if (I->pred_begin() != I->pred_end())
69 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
70 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
72 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
74 // Print the assembly for the instruction.
76 printMachineInstruction(II);
79 if (HasDotTypeDotSizeDirective)
80 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
82 // We didn't modify anything.
86 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
87 const char *Modifier) {
88 const MachineOperand &MO = MI->getOperand(OpNo);
89 const MRegisterInfo &RI = *TM.getRegisterInfo();
90 switch (MO.getType()) {
91 case MachineOperand::MO_VirtualRegister:
92 case MachineOperand::MO_MachineRegister:
93 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
94 "Virtual registers should not make it this far!");
96 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
97 O << (char)tolower(*Name);
100 case MachineOperand::MO_SignExtendedImmed:
101 case MachineOperand::MO_UnextendedImmed:
102 O << '$' << (int)MO.getImmedValue();
104 case MachineOperand::MO_MachineBasicBlock: {
105 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
106 O << PrivateGlobalPrefix << "BB"
107 << Mang->getValueName(MBBOp->getParent()->getFunction())
108 << "_" << MBBOp->getNumber () << "\t# "
109 << MBBOp->getBasicBlock ()->getName ();
112 case MachineOperand::MO_PCRelativeDisp:
113 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
116 case MachineOperand::MO_GlobalAddress: {
117 bool isCallOp = Modifier && !strcmp(Modifier, "call");
118 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
119 // Darwin block shameless ripped from PPCAsmPrinter.cpp
121 if (!isMemOp && !isCallOp) O << '$';
122 GlobalValue *GV = MO.getGlobal();
123 std::string Name = Mang->getValueName(GV);
124 // Link-once, External, or Weakly-linked global variables need
125 // non-lazily-resolved stubs
126 if (GV->isExternal() || GV->hasWeakLinkage() ||
127 GV->hasLinkOnceLinkage()) {
128 // Dynamically-resolved functions need a stub for the function.
129 if (isCallOp && isa<Function>(GV) && cast<Function>(GV)->isExternal()) {
130 FnStubs.insert(Name);
131 O << "L" << Name << "$stub";
133 GVStubs.insert(Name);
134 O << "L" << Name << "$non_lazy_ptr";
137 O << Mang->getValueName(GV);
139 int Offset = MO.getOffset();
146 if (!isMemOp && !isCallOp) O << '$';
147 O << Mang->getValueName(MO.getGlobal());
148 int Offset = MO.getOffset();
155 case MachineOperand::MO_ExternalSymbol: {
156 bool isCallOp = Modifier && !strcmp(Modifier, "call");
157 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
158 if (isCallOp && forDarwin) {
159 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
160 FnStubs.insert(Name);
161 O << "L" << Name << "$stub";
164 if (!isMemOp && !isCallOp) O << '$';
165 O << GlobalPrefix << MO.getSymbolName();
169 O << "<unknown operand type>"; return;
173 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
174 unsigned char value = MI->getOperand(Op).getImmedValue();
175 assert(value <= 7 && "Invalid ssecc argument!");
177 case 0: O << "eq"; break;
178 case 1: O << "lt"; break;
179 case 2: O << "le"; break;
180 case 3: O << "unord"; break;
181 case 4: O << "neq"; break;
182 case 5: O << "nlt"; break;
183 case 6: O << "nle"; break;
184 case 7: O << "ord"; break;
188 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
189 assert(isMem(MI, Op) && "Invalid memory reference!");
191 const MachineOperand &BaseReg = MI->getOperand(Op);
192 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
193 const MachineOperand &IndexReg = MI->getOperand(Op+2);
194 const MachineOperand &DispSpec = MI->getOperand(Op+3);
196 if (BaseReg.isFrameIndex()) {
197 O << "[frame slot #" << BaseReg.getFrameIndex();
198 if (DispSpec.getImmedValue())
199 O << " + " << DispSpec.getImmedValue();
202 } else if (BaseReg.isConstantPoolIndex()) {
203 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
204 << BaseReg.getConstantPoolIndex();
205 if (DispSpec.getImmedValue())
206 O << "+" << DispSpec.getImmedValue();
207 if (IndexReg.getReg()) {
209 printOperand(MI, Op+2);
211 O << "," << ScaleVal;
217 if (DispSpec.isGlobalAddress()) {
218 printOperand(MI, Op+3, "mem");
220 int DispVal = DispSpec.getImmedValue();
221 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
225 if (IndexReg.getReg() || BaseReg.getReg()) {
227 if (BaseReg.getReg())
228 printOperand(MI, Op);
230 if (IndexReg.getReg()) {
232 printOperand(MI, Op+2);
234 O << "," << ScaleVal;
241 /// printMachineInstruction -- Print out a single X86 LLVM instruction
242 /// MI in Intel syntax to the current output stream.
244 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
246 // This works around some Darwin assembler bugs.
248 switch (MI->getOpcode()) {
250 O << "rep/movsb (%esi),(%edi)\n";
253 O << "rep/movsl (%esi),(%edi)\n";
256 O << "rep/movsw (%esi),(%edi)\n";
272 // Call the autogenerated instruction printer routines.
273 printInstruction(MI);
276 // Include the auto-generated portion of the assembly writer.
277 #include "X86GenAsmWriter.inc"