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 if (!isCallOp) O << '$';
90 O << Mang->getValueName(MO.getGlobal());
91 int Offset = MO.getOffset();
98 case MachineOperand::MO_ExternalSymbol:
99 if (!isCallOp) O << '$';
100 O << GlobalPrefix << MO.getSymbolName();
103 O << "<unknown operand type>"; return;
107 void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){
108 assert(isMem(MI, Op) && "Invalid memory reference!");
110 const MachineOperand &BaseReg = MI->getOperand(Op);
111 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
112 const MachineOperand &IndexReg = MI->getOperand(Op+2);
113 const MachineOperand &DispSpec = MI->getOperand(Op+3);
115 if (BaseReg.isFrameIndex()) {
116 O << "[frame slot #" << BaseReg.getFrameIndex();
117 if (DispSpec.getImmedValue())
118 O << " + " << DispSpec.getImmedValue();
121 } else if (BaseReg.isConstantPoolIndex()) {
122 O << ".CPI" << CurrentFnName << "_"
123 << BaseReg.getConstantPoolIndex();
124 if (DispSpec.getImmedValue())
125 O << "+" << DispSpec.getImmedValue();
126 if (IndexReg.getReg()) {
130 O << "," << ScaleVal;
136 if (DispSpec.isGlobalAddress()) {
137 printOp(DispSpec, true);
139 int DispVal = DispSpec.getImmedValue();
140 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
144 if (IndexReg.getReg() || BaseReg.getReg()) {
146 if (BaseReg.getReg())
149 if (IndexReg.getReg()) {
153 O << "," << ScaleVal;
160 /// printMachineInstruction -- Print out a single X86 LLVM instruction
161 /// MI in Intel syntax to the current output stream.
163 void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
165 // Call the autogenerated instruction printer routines.
166 printInstruction(MI);
169 // Include the auto-generated portion of the assembly writer.
170 #include "X86GenAsmWriter.inc"