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"
24 /// runOnMachineFunction - This uses the printMachineInstruction()
25 /// method to print assembly for each instruction.
27 bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
28 setupMachineFunction(MF);
31 // Print out constants referenced by the function
32 printConstantPool(MF.getConstantPool());
34 // Print out labels for the function.
37 O << "\t.globl\t" << CurrentFnName << "\n";
38 if (!forCygwin && !forDarwin)
39 O << "\t.type\t" << CurrentFnName << ", @function\n";
40 O << CurrentFnName << ":\n";
42 // Print out code for the function.
43 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
45 // Print a label for the basic block.
46 if (I->pred_begin() != I->pred_end())
47 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
48 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
49 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
51 // Print the assembly for the instruction.
53 printMachineInstruction(II);
57 // We didn't modify anything.
61 void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
62 const MRegisterInfo &RI = *TM.getRegisterInfo();
63 switch (MO.getType()) {
64 case MachineOperand::MO_VirtualRegister:
65 case MachineOperand::MO_MachineRegister:
66 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
67 "Virtual registers should not make it this far!");
69 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
70 O << (char)tolower(*Name);
73 case MachineOperand::MO_SignExtendedImmed:
74 case MachineOperand::MO_UnextendedImmed:
75 O << '$' << (int)MO.getImmedValue();
77 case MachineOperand::MO_MachineBasicBlock: {
78 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
79 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
80 << "_" << MBBOp->getNumber () << "\t# "
81 << MBBOp->getBasicBlock ()->getName ();
84 case MachineOperand::MO_PCRelativeDisp:
85 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
88 case MachineOperand::MO_GlobalAddress: {
89 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
91 if (!isCallOp) O << '$';
92 GlobalValue *GV = MO.getGlobal();
93 std::string Name = Mang->getValueName(GV);
95 // Dynamically-resolved functions need a stub for the function. Be
96 // wary however not to output $stub for external functions whose addresses
97 // are taken. Those should be emitted as $non_lazy_ptr below.
98 Function *F = dyn_cast<Function>(GV);
99 if (F && isCallOp && F->isExternal()) {
100 FnStubs.insert(Name);
101 O << "L" << Name << "$stub";
105 // Link-once, External, or Weakly-linked global variables need
106 // non-lazily-resolved stubs
107 if (GV->hasLinkOnceLinkage()) {
108 LinkOnceStubs.insert(Name);
109 O << "L" << Name << "$non_lazy_ptr";
112 if (GV->isExternal() || GV->hasWeakLinkage()) {
113 GVStubs.insert(Name);
114 O << "L" << Name << "$non_lazy_ptr";
117 O << Mang->getValueName(GV);
120 if (!isCallOp) O << '$';
121 O << Mang->getValueName(MO.getGlobal());
122 int Offset = MO.getOffset();
129 case MachineOperand::MO_ExternalSymbol:
130 if (isCallOp && forDarwin) {
131 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
132 FnStubs.insert(Name);
133 O << "L" << Name << "$stub";
136 if (!isCallOp) O << '$';
137 O << GlobalPrefix << MO.getSymbolName();
140 O << "<unknown operand type>"; return;
144 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
145 assert(isMem(MI, Op) && "Invalid memory reference!");
147 const MachineOperand &BaseReg = MI->getOperand(Op);
148 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
149 const MachineOperand &IndexReg = MI->getOperand(Op+2);
150 const MachineOperand &DispSpec = MI->getOperand(Op+3);
152 if (BaseReg.isFrameIndex()) {
153 O << "[frame slot #" << BaseReg.getFrameIndex();
154 if (DispSpec.getImmedValue())
155 O << " + " << DispSpec.getImmedValue();
158 } else if (BaseReg.isConstantPoolIndex()) {
159 O << ".CPI" << CurrentFnName << "_"
160 << BaseReg.getConstantPoolIndex();
161 if (DispSpec.getImmedValue())
162 O << "+" << DispSpec.getImmedValue();
163 if (IndexReg.getReg()) {
167 O << "," << ScaleVal;
173 if (DispSpec.isGlobalAddress()) {
174 printOp(DispSpec, true);
176 int DispVal = DispSpec.getImmedValue();
177 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
181 if (IndexReg.getReg() || BaseReg.getReg()) {
183 if (BaseReg.getReg())
186 if (IndexReg.getReg()) {
190 O << "," << ScaleVal;
197 /// printMachineInstruction -- Print out a single X86 LLVM instruction
198 /// MI in Intel syntax to the current output stream.
200 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
202 // Call the autogenerated instruction printer routines.
203 printInstruction(MI);
206 // Include the auto-generated portion of the assembly writer.
207 #include "X86GenAsmWriter.inc"