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 "llvm/Module.h"
18 #include "llvm/Type.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/ValueTypes.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
32 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34 struct AlphaAsmPrinter : public AsmPrinter {
36 /// Unique incrementer for label values for referencing Global values.
40 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
41 : AsmPrinter(o, tm), LabelNumber(0)
43 AlignmentIsInBytes = false;
46 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
50 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
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 << "LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
124 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
125 << MBBOp->getBasicBlock()->getName();
129 case MachineOperand::MO_ConstantPoolIndex:
130 O << "CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
133 case MachineOperand::MO_ExternalSymbol:
134 O << MO.getSymbolName();
137 case MachineOperand::MO_GlobalAddress:
138 O << Mang->getValueName(MO.getGlobal());
142 O << "<unknown operand type: " << MO.getType() << ">";
147 /// printMachineInstruction -- Print out a single Alpha MI to
148 /// the current output stream.
150 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
152 if (printInstruction(MI))
153 return; // Printer was automatically generated
155 assert(0 && "Unhandled instruction in asm writer!");
161 /// runOnMachineFunction - This uses the printMachineInstruction()
162 /// method to print assembly for each instruction.
164 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
165 setupMachineFunction(MF);
168 // Print out constants referenced by the function
169 printConstantPool(MF.getConstantPool());
171 // Print out labels for the function.
174 O << "\t.globl\t" << CurrentFnName << "\n";
175 O << "\t.ent\t" << CurrentFnName << "\n";
177 O << CurrentFnName << ":\n";
179 // Print out code for the function.
180 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
182 // Print a label for the basic block.
183 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
184 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
185 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187 // Print the assembly for the instruction.
189 printMachineInstruction(II);
194 O << "\t.end " << CurrentFnName << "\n";
196 // We didn't modify anything.
201 /// printConstantPool - Print to the current output stream assembly
202 /// representations of the constants in the constant pool MCP. This is
203 /// used to print out constants which have been "spilled to memory" by
204 /// the code generator.
206 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
207 const std::vector<Constant*> &CP = MCP->getConstants();
208 const TargetData &TD = TM.getTargetData();
210 if (CP.empty()) return;
212 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
213 O << "\t.section\t.rodata\n";
214 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
215 O << "CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
217 emitGlobalConstant(CP[i]);
221 bool AlphaAsmPrinter::doInitialization(Module &M)
223 AsmPrinter::doInitialization(M);
224 O << "\t.arch ev56\n";
229 // SwitchSection - Switch to the specified section of the executable if we are
230 // not already in it!
232 static void SwitchSection(std::ostream &OS, std::string &CurSection,
233 const char *NewSection) {
234 if (CurSection != NewSection) {
235 CurSection = NewSection;
236 if (!CurSection.empty())
237 OS << "\t" << NewSection << "\n";
241 bool AlphaAsmPrinter::doFinalization(Module &M) {
242 const TargetData &TD = TM.getTargetData();
243 std::string CurSection;
245 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
246 if (I->hasInitializer()) { // External global require no code
248 std::string name = Mang->getValueName(I);
249 Constant *C = I->getInitializer();
250 unsigned Size = TD.getTypeSize(C->getType());
251 unsigned Align = TD.getTypeAlignmentShift(C->getType());
253 if (C->isNullValue() &&
254 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
255 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
256 SwitchSection(O, CurSection, ".data");
257 if (I->hasInternalLinkage())
258 O << "\t.local " << name << "\n";
260 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
261 << "," << (1 << Align);
263 WriteAsOperand(O, I, true, true, &M);
266 switch (I->getLinkage()) {
267 case GlobalValue::LinkOnceLinkage:
268 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
269 // Nonnull linkonce -> weak
270 O << "\t.weak " << name << "\n";
271 SwitchSection(O, CurSection, "");
272 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
274 case GlobalValue::AppendingLinkage:
275 // FIXME: appending linkage variables should go into a section of
276 // their name or something. For now, just emit them as external.
277 case GlobalValue::ExternalLinkage:
278 // If external or appending, declare as a global symbol
279 O << "\t.globl " << name << "\n";
281 case GlobalValue::InternalLinkage:
282 if (C->isNullValue())
283 SwitchSection(O, CurSection, ".data"); //was .bss
285 SwitchSection(O, CurSection, ".data");
287 case GlobalValue::GhostLinkage:
288 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
292 emitAlignment(Align);
293 O << "\t.type " << name << ",@object\n";
294 O << "\t.size " << name << "," << Size << "\n";
295 O << name << ":\t\t\t\t# ";
296 WriteAsOperand(O, I, true, true, &M);
298 WriteAsOperand(O, C, false, false, &M);
300 emitGlobalConstant(C);
304 AsmPrinter::doFinalization(M);