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 SwitchSection("\t.text\n", MF.getFunction());
37 EmitAlignment(4); // FIXME: This should be parameterized somewhere.
38 if (!MF.getFunction()->hasInternalLinkage())
39 O << "\t.globl\t" << CurrentFnName << "\n";
40 if (HasDotTypeDotSizeDirective)
41 O << "\t.type\t" << CurrentFnName << ", @function\n";
42 O << CurrentFnName << ":\n";
44 // Print out code for the function.
45 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
47 // Print a label for the basic block.
48 if (I->pred_begin() != I->pred_end())
49 O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber()
50 << ":\t" << CommentString << " " << I->getBasicBlock()->getName()
52 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
54 // Print the assembly for the instruction.
56 printMachineInstruction(II);
59 if (HasDotTypeDotSizeDirective)
60 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
62 // We didn't modify anything.
66 void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
67 const char *Modifier) {
68 const MachineOperand &MO = MI->getOperand(OpNo);
69 const MRegisterInfo &RI = *TM.getRegisterInfo();
70 switch (MO.getType()) {
71 case MachineOperand::MO_VirtualRegister:
72 case MachineOperand::MO_MachineRegister:
73 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
74 "Virtual registers should not make it this far!");
76 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
77 O << (char)tolower(*Name);
80 case MachineOperand::MO_SignExtendedImmed:
81 case MachineOperand::MO_UnextendedImmed:
82 O << '$' << (int)MO.getImmedValue();
84 case MachineOperand::MO_MachineBasicBlock: {
85 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
86 O << PrivateGlobalPrefix << "BB"
87 << Mang->getValueName(MBBOp->getParent()->getFunction())
88 << "_" << MBBOp->getNumber () << "\t# "
89 << MBBOp->getBasicBlock ()->getName ();
92 case MachineOperand::MO_PCRelativeDisp:
93 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
96 case MachineOperand::MO_GlobalAddress: {
97 bool isCallOp = Modifier && !strcmp(Modifier, "call");
98 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
100 if (!isCallOp) O << '$';
101 GlobalValue *GV = MO.getGlobal();
102 std::string Name = Mang->getValueName(GV);
104 // Dynamically-resolved functions need a stub for the function. Be
105 // wary however not to output $stub for external functions whose addresses
106 // are taken. Those should be emitted as $non_lazy_ptr below.
107 Function *F = dyn_cast<Function>(GV);
108 if (F && isCallOp && F->isExternal()) {
109 FnStubs.insert(Name);
110 O << "L" << Name << "$stub";
111 } else if (GV->hasLinkOnceLinkage()) {
112 // Link-once, External, or Weakly-linked global variables need
113 // non-lazily-resolved stubs
114 LinkOnceStubs.insert(Name);
115 O << "L" << Name << "$non_lazy_ptr";
116 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
117 GVStubs.insert(Name);
118 O << "L" << Name << "$non_lazy_ptr";
120 O << Mang->getValueName(GV);
122 int Offset = MO.getOffset();
129 if (!isCallOp) O << '$';
130 O << Mang->getValueName(MO.getGlobal());
131 int Offset = MO.getOffset();
138 case MachineOperand::MO_ExternalSymbol: {
139 bool isCallOp = Modifier && !strcmp(Modifier, "call");
140 if (isCallOp && forDarwin) {
141 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
142 FnStubs.insert(Name);
143 O << "L" << Name << "$stub";
146 if (!isCallOp) O << '$';
147 O << GlobalPrefix << MO.getSymbolName();
151 O << "<unknown operand type>"; return;
155 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
156 unsigned char value = MI->getOperand(Op).getImmedValue();
157 assert(value <= 7 && "Invalid ssecc argument!");
159 case 0: O << "eq"; break;
160 case 1: O << "lt"; break;
161 case 2: O << "le"; break;
162 case 3: O << "unord"; break;
163 case 4: O << "neq"; break;
164 case 5: O << "nlt"; break;
165 case 6: O << "nle"; break;
166 case 7: O << "ord"; break;
170 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
171 assert(isMem(MI, Op) && "Invalid memory reference!");
173 const MachineOperand &BaseReg = MI->getOperand(Op);
174 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
175 const MachineOperand &IndexReg = MI->getOperand(Op+2);
176 const MachineOperand &DispSpec = MI->getOperand(Op+3);
178 if (BaseReg.isFrameIndex()) {
179 O << "[frame slot #" << BaseReg.getFrameIndex();
180 if (DispSpec.getImmedValue())
181 O << " + " << DispSpec.getImmedValue();
184 } else if (BaseReg.isConstantPoolIndex()) {
185 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
186 << BaseReg.getConstantPoolIndex();
187 if (DispSpec.getImmedValue())
188 O << "+" << DispSpec.getImmedValue();
189 if (IndexReg.getReg()) {
191 printOperand(MI, Op+2);
193 O << "," << ScaleVal;
199 if (DispSpec.isGlobalAddress()) {
200 printOperand(MI, Op+3, "call");
202 int DispVal = DispSpec.getImmedValue();
203 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
207 if (IndexReg.getReg() || BaseReg.getReg()) {
209 if (BaseReg.getReg())
210 printOperand(MI, Op);
212 if (IndexReg.getReg()) {
214 printOperand(MI, Op+2);
216 O << "," << ScaleVal;
223 /// printMachineInstruction -- Print out a single X86 LLVM instruction
224 /// MI in Intel syntax to the current output stream.
226 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
228 // This works around some Darwin assembler bugs.
230 switch (MI->getOpcode()) {
232 O << "rep/movsb (%esi),(%edi)\n";
235 O << "rep/movsl (%esi),(%edi)\n";
238 O << "rep/movsw (%esi),(%edi)\n";
254 // Call the autogenerated instruction printer routines.
255 printInstruction(MI);
258 // Include the auto-generated portion of the assembly writer.
259 #include "X86GenAsmWriter.inc"