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::printOp(const MachineOperand &MO, bool isCallOp) {
67 const MRegisterInfo &RI = *TM.getRegisterInfo();
68 switch (MO.getType()) {
69 case MachineOperand::MO_VirtualRegister:
70 case MachineOperand::MO_MachineRegister:
71 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
72 "Virtual registers should not make it this far!");
74 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
75 O << (char)tolower(*Name);
78 case MachineOperand::MO_SignExtendedImmed:
79 case MachineOperand::MO_UnextendedImmed:
80 O << '$' << (int)MO.getImmedValue();
82 case MachineOperand::MO_MachineBasicBlock: {
83 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
84 O << PrivateGlobalPrefix << "BB"
85 << Mang->getValueName(MBBOp->getParent()->getFunction())
86 << "_" << MBBOp->getNumber () << "\t# "
87 << MBBOp->getBasicBlock ()->getName ();
90 case MachineOperand::MO_PCRelativeDisp:
91 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
94 case MachineOperand::MO_GlobalAddress: {
95 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
97 if (!isCallOp) O << '$';
98 GlobalValue *GV = MO.getGlobal();
99 std::string Name = Mang->getValueName(GV);
101 // Dynamically-resolved functions need a stub for the function. Be
102 // wary however not to output $stub for external functions whose addresses
103 // are taken. Those should be emitted as $non_lazy_ptr below.
104 Function *F = dyn_cast<Function>(GV);
105 if (F && isCallOp && F->isExternal()) {
106 FnStubs.insert(Name);
107 O << "L" << Name << "$stub";
108 } else if (GV->hasLinkOnceLinkage()) {
109 // Link-once, External, or Weakly-linked global variables need
110 // non-lazily-resolved stubs
111 LinkOnceStubs.insert(Name);
112 O << "L" << Name << "$non_lazy_ptr";
113 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
114 GVStubs.insert(Name);
115 O << "L" << Name << "$non_lazy_ptr";
117 O << Mang->getValueName(GV);
119 int Offset = MO.getOffset();
126 if (!isCallOp) O << '$';
127 O << Mang->getValueName(MO.getGlobal());
128 int Offset = MO.getOffset();
135 case MachineOperand::MO_ExternalSymbol:
136 if (isCallOp && forDarwin) {
137 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
138 FnStubs.insert(Name);
139 O << "L" << Name << "$stub";
142 if (!isCallOp) O << '$';
143 O << GlobalPrefix << MO.getSymbolName();
146 O << "<unknown operand type>"; return;
150 void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
151 unsigned char value = MI->getOperand(Op).getImmedValue();
152 assert(value <= 7 && "Invalid ssecc argument!");
154 case 0: O << "eq"; break;
155 case 1: O << "lt"; break;
156 case 2: O << "le"; break;
157 case 3: O << "unord"; break;
158 case 4: O << "neq"; break;
159 case 5: O << "nlt"; break;
160 case 6: O << "nle"; break;
161 case 7: O << "ord"; break;
165 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
166 assert(isMem(MI, Op) && "Invalid memory reference!");
168 const MachineOperand &BaseReg = MI->getOperand(Op);
169 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
170 const MachineOperand &IndexReg = MI->getOperand(Op+2);
171 const MachineOperand &DispSpec = MI->getOperand(Op+3);
173 if (BaseReg.isFrameIndex()) {
174 O << "[frame slot #" << BaseReg.getFrameIndex();
175 if (DispSpec.getImmedValue())
176 O << " + " << DispSpec.getImmedValue();
179 } else if (BaseReg.isConstantPoolIndex()) {
180 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
181 << BaseReg.getConstantPoolIndex();
182 if (DispSpec.getImmedValue())
183 O << "+" << DispSpec.getImmedValue();
184 if (IndexReg.getReg()) {
188 O << "," << ScaleVal;
194 if (DispSpec.isGlobalAddress()) {
195 printOp(DispSpec, true);
197 int DispVal = DispSpec.getImmedValue();
198 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
202 if (IndexReg.getReg() || BaseReg.getReg()) {
204 if (BaseReg.getReg())
207 if (IndexReg.getReg()) {
211 O << "," << ScaleVal;
218 /// printMachineInstruction -- Print out a single X86 LLVM instruction
219 /// MI in Intel syntax to the current output stream.
221 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
223 // Call the autogenerated instruction printer routines.
224 printInstruction(MI);
227 // Include the auto-generated portion of the assembly writer.
228 #include "X86GenAsmWriter.inc"