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.
36 emitAlignment(4); // FIXME: This should be parameterized somewhere.
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";
104 } else if (GV->hasLinkOnceLinkage()) {
105 // Link-once, External, or Weakly-linked global variables need
106 // non-lazily-resolved stubs
107 LinkOnceStubs.insert(Name);
108 O << "L" << Name << "$non_lazy_ptr";
109 } else if (GV->isExternal() || GV->hasWeakLinkage()) {
110 GVStubs.insert(Name);
111 O << "L" << Name << "$non_lazy_ptr";
113 O << Mang->getValueName(GV);
115 int Offset = MO.getOffset();
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::printSSECC(const MachineInstr *MI, unsigned Op,
148 unsigned char value = MI->getOperand(Op).getImmedValue();
149 assert(value <= 7 && "Invalid ssecc argument!");
151 case 0: O << "eq"; break;
152 case 1: O << "lt"; break;
153 case 2: O << "le"; break;
154 case 3: O << "unord"; break;
155 case 4: O << "neq"; break;
156 case 5: O << "nlt"; break;
157 case 6: O << "nle"; break;
158 case 7: O << "ord"; break;
162 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
163 assert(isMem(MI, Op) && "Invalid memory reference!");
165 const MachineOperand &BaseReg = MI->getOperand(Op);
166 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
167 const MachineOperand &IndexReg = MI->getOperand(Op+2);
168 const MachineOperand &DispSpec = MI->getOperand(Op+3);
170 if (BaseReg.isFrameIndex()) {
171 O << "[frame slot #" << BaseReg.getFrameIndex();
172 if (DispSpec.getImmedValue())
173 O << " + " << DispSpec.getImmedValue();
176 } else if (BaseReg.isConstantPoolIndex()) {
177 O << ".CPI" << CurrentFnName << "_"
178 << BaseReg.getConstantPoolIndex();
179 if (DispSpec.getImmedValue())
180 O << "+" << DispSpec.getImmedValue();
181 if (IndexReg.getReg()) {
185 O << "," << ScaleVal;
191 if (DispSpec.isGlobalAddress()) {
192 printOp(DispSpec, true);
194 int DispVal = DispSpec.getImmedValue();
195 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
199 if (IndexReg.getReg() || BaseReg.getReg()) {
201 if (BaseReg.getReg())
204 if (IndexReg.getReg()) {
208 O << "," << ScaleVal;
215 /// printMachineInstruction -- Print out a single X86 LLVM instruction
216 /// MI in Intel syntax to the current output stream.
218 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
220 // Call the autogenerated instruction printer routines.
221 printInstruction(MI);
224 // Include the auto-generated portion of the assembly writer.
225 #include "X86GenAsmWriter.inc"