1 //===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
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 GAS-format Alpha assembly language.
13 //===----------------------------------------------------------------------===//
16 #include "AlphaInstrInfo.h"
17 #include "AlphaTargetMachine.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/ADT/Statistic.h"
31 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
33 struct AlphaAsmPrinter : public AsmPrinter {
35 /// Unique incrementer for label values for referencing Global values.
39 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
40 : AsmPrinter(o, tm), LabelNumber(0) {
41 AlignmentIsInBytes = false;
42 PrivateGlobalPrefix = "$";
45 /// We name each basic block in a Function with a unique number, so
46 /// that we can consistently refer to them later. This is cleared
47 /// at the beginning of each call to runOnMachineFunction().
49 typedef std::map<const Value *, unsigned> ValueMapTy;
50 ValueMapTy NumberForBB;
51 std::string CurSection;
53 virtual const char *getPassName() const {
54 return "Alpha Assembly Printer";
56 bool printInstruction(const MachineInstr *MI);
57 void printOp(const MachineOperand &MO, bool IsCallOp = false);
58 void printConstantPool(MachineConstantPool *MCP);
59 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
60 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
61 void printMachineInstruction(const MachineInstr *MI);
62 bool runOnMachineFunction(MachineFunction &F);
63 bool doInitialization(Module &M);
64 bool doFinalization(Module &M);
66 } // end of anonymous namespace
68 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
69 /// assembly code for a MachineFunction to the given output stream,
70 /// using the given target machine description. This should work
71 /// regardless of whether the function is in SSA form.
73 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
75 return new AlphaAsmPrinter(o, tm);
78 #include "AlphaGenAsmWriter.inc"
80 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
82 const MachineOperand &MO = MI->getOperand(opNum);
83 if (MO.getType() == MachineOperand::MO_MachineRegister) {
84 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
85 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
86 } else if (MO.isImmediate()) {
87 O << MO.getImmedValue();
94 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
95 const MRegisterInfo &RI = *TM.getRegisterInfo();
98 switch (MO.getType()) {
99 case MachineOperand::MO_VirtualRegister:
100 if (Value *V = MO.getVRegValueOrNull()) {
101 O << "<" << V->getName() << ">";
105 case MachineOperand::MO_MachineRegister:
106 case MachineOperand::MO_CCRegister:
107 O << RI.get(MO.getReg()).Name;
110 case MachineOperand::MO_SignExtendedImmed:
111 case MachineOperand::MO_UnextendedImmed:
112 std::cerr << "printOp() does not handle immediate values\n";
116 case MachineOperand::MO_PCRelativeDisp:
117 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
121 case MachineOperand::MO_MachineBasicBlock: {
122 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
123 O << PrivateGlobalPrefix << "LBB"
124 << Mang->getValueName(MBBOp->getParent()->getFunction())
125 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
126 << MBBOp->getBasicBlock()->getName();
130 case MachineOperand::MO_ConstantPoolIndex:
131 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
132 << MO.getConstantPoolIndex();
135 case MachineOperand::MO_ExternalSymbol:
136 O << MO.getSymbolName();
139 case MachineOperand::MO_GlobalAddress:
140 //Abuse PCrel to specify pcrel calls
141 //calls are the only thing that use this flag
142 if (MO.isPCRelative())
143 O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
145 O << Mang->getValueName(MO.getGlobal());
149 O << "<unknown operand type: " << MO.getType() << ">";
154 /// printMachineInstruction -- Print out a single Alpha MI to
155 /// the current output stream.
157 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
159 if (printInstruction(MI))
160 return; // Printer was automatically generated
162 assert(0 && "Unhandled instruction in asm writer!");
168 /// runOnMachineFunction - This uses the printMachineInstruction()
169 /// method to print assembly for each instruction.
171 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
172 SetupMachineFunction(MF);
175 // Print out constants referenced by the function
176 printConstantPool(MF.getConstantPool());
178 // Print out labels for the function.
179 SwitchSection("\t.section .text", MF.getFunction());
181 O << "\t.globl " << CurrentFnName << "\n";
182 O << "\t.ent " << CurrentFnName << "\n";
184 O << CurrentFnName << ":\n";
186 // Print out code for the function.
187 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
189 // Print a label for the basic block.
190 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
191 << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
192 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
194 // Print the assembly for the instruction.
196 printMachineInstruction(II);
201 O << "\t.end " << CurrentFnName << "\n";
203 // We didn't modify anything.
208 /// printConstantPool - Print to the current output stream assembly
209 /// representations of the constants in the constant pool MCP. This is
210 /// used to print out constants which have been "spilled to memory" by
211 /// the code generator.
213 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
214 const std::vector<Constant*> &CP = MCP->getConstants();
215 const TargetData &TD = TM.getTargetData();
217 if (CP.empty()) return;
219 SwitchSection("\t.section .rodata", 0);
220 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
221 EmitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
222 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
223 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
224 EmitGlobalConstant(CP[i]);
228 bool AlphaAsmPrinter::doInitialization(Module &M)
230 AsmPrinter::doInitialization(M);
231 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
232 || TM.getSubtarget<AlphaSubtarget>().hasCT())
233 O << "\t.arch ev6\n";
235 O << "\t.arch ev56\n";
236 O << "\t.set noat\n";
240 bool AlphaAsmPrinter::doFinalization(Module &M) {
241 const TargetData &TD = TM.getTargetData();
243 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
244 if (I->hasInitializer()) { // External global require no code
246 std::string name = Mang->getValueName(I);
247 Constant *C = I->getInitializer();
248 unsigned Size = TD.getTypeSize(C->getType());
249 unsigned Align = TD.getTypeAlignmentShift(C->getType());
251 if (C->isNullValue() &&
252 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
253 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
254 SwitchSection("\t.section .data", I);
255 if (I->hasInternalLinkage())
256 O << "\t.local " << name << "\n";
258 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
259 << "," << (1 << Align);
261 WriteAsOperand(O, I, true, true, &M);
264 switch (I->getLinkage()) {
265 case GlobalValue::LinkOnceLinkage:
266 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
267 // Nonnull linkonce -> weak
268 O << "\t.weak " << name << "\n";
269 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
270 SwitchSection("", I);
272 case GlobalValue::AppendingLinkage:
273 // FIXME: appending linkage variables should go into a section of
274 // their name or something. For now, just emit them as external.
275 case GlobalValue::ExternalLinkage:
276 // If external or appending, declare as a global symbol
277 O << "\t.globl " << name << "\n";
279 case GlobalValue::InternalLinkage:
280 SwitchSection(C->isNullValue() ? "\t.section .bss" :
281 "\t.section .data", I);
283 case GlobalValue::GhostLinkage:
284 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
288 EmitAlignment(Align);
289 O << "\t.type " << name << ",@object\n";
290 O << "\t.size " << name << "," << Size << "\n";
291 O << name << ":\t\t\t\t# ";
292 WriteAsOperand(O, I, true, true, &M);
294 WriteAsOperand(O, C, false, false, &M);
296 EmitGlobalConstant(C);
300 AsmPrinter::doFinalization(M);