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 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
59 // We didn't modify anything.
63 void X86ATTAsmPrinter::printOp(const MachineOperand &MO, bool isCallOp) {
64 const MRegisterInfo &RI = *TM.getRegisterInfo();
65 switch (MO.getType()) {
66 case MachineOperand::MO_VirtualRegister:
67 case MachineOperand::MO_MachineRegister:
68 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
69 "Virtual registers should not make it this far!");
71 for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name)
72 O << (char)tolower(*Name);
75 case MachineOperand::MO_SignExtendedImmed:
76 case MachineOperand::MO_UnextendedImmed:
77 O << '$' << (int)MO.getImmedValue();
79 case MachineOperand::MO_MachineBasicBlock: {
80 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
81 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
82 << "_" << MBBOp->getNumber () << "\t# "
83 << MBBOp->getBasicBlock ()->getName ();
86 case MachineOperand::MO_PCRelativeDisp:
87 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
90 case MachineOperand::MO_GlobalAddress: {
91 // Darwin block shameless ripped from PowerPCAsmPrinter.cpp
93 if (!isCallOp) O << '$';
94 GlobalValue *GV = MO.getGlobal();
95 std::string Name = Mang->getValueName(GV);
97 // Dynamically-resolved functions need a stub for the function. Be
98 // wary however not to output $stub for external functions whose addresses
99 // are taken. Those should be emitted as $non_lazy_ptr below.
100 Function *F = dyn_cast<Function>(GV);
101 if (F && isCallOp && F->isExternal()) {
102 FnStubs.insert(Name);
103 O << "L" << Name << "$stub";
107 // Link-once, External, or Weakly-linked global variables need
108 // non-lazily-resolved stubs
109 if (GV->hasLinkOnceLinkage()) {
110 LinkOnceStubs.insert(Name);
111 O << "L" << Name << "$non_lazy_ptr";
114 if (GV->isExternal() || GV->hasWeakLinkage()) {
115 GVStubs.insert(Name);
116 O << "L" << Name << "$non_lazy_ptr";
119 O << Mang->getValueName(GV);
122 if (!isCallOp) O << '$';
123 O << Mang->getValueName(MO.getGlobal());
124 int Offset = MO.getOffset();
131 case MachineOperand::MO_ExternalSymbol:
132 if (isCallOp && forDarwin) {
133 std::string Name(GlobalPrefix); Name += MO.getSymbolName();
134 FnStubs.insert(Name);
135 O << "L" << Name << "$stub";
138 if (!isCallOp) O << '$';
139 O << GlobalPrefix << MO.getSymbolName();
142 O << "<unknown operand type>"; return;
146 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
147 assert(isMem(MI, Op) && "Invalid memory reference!");
149 const MachineOperand &BaseReg = MI->getOperand(Op);
150 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
151 const MachineOperand &IndexReg = MI->getOperand(Op+2);
152 const MachineOperand &DispSpec = MI->getOperand(Op+3);
154 if (BaseReg.isFrameIndex()) {
155 O << "[frame slot #" << BaseReg.getFrameIndex();
156 if (DispSpec.getImmedValue())
157 O << " + " << DispSpec.getImmedValue();
160 } else if (BaseReg.isConstantPoolIndex()) {
161 O << ".CPI" << CurrentFnName << "_"
162 << BaseReg.getConstantPoolIndex();
163 if (DispSpec.getImmedValue())
164 O << "+" << DispSpec.getImmedValue();
165 if (IndexReg.getReg()) {
169 O << "," << ScaleVal;
175 if (DispSpec.isGlobalAddress()) {
176 printOp(DispSpec, true);
178 int DispVal = DispSpec.getImmedValue();
179 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
183 if (IndexReg.getReg() || BaseReg.getReg()) {
185 if (BaseReg.getReg())
188 if (IndexReg.getReg()) {
192 O << "," << ScaleVal;
199 /// printMachineInstruction -- Print out a single X86 LLVM instruction
200 /// MI in Intel syntax to the current output stream.
202 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
204 // Call the autogenerated instruction printer routines.
205 printInstruction(MI);
208 // Include the auto-generated portion of the assembly writer.
209 #include "X86GenAsmWriter.inc"