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/Assembly/Writer.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
23 #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)
43 /// We name each basic block in a Function with a unique number, so
44 /// that we can consistently refer to them later. This is cleared
45 /// at the beginning of each call to runOnMachineFunction().
47 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
50 virtual const char *getPassName() const {
51 return "Alpha Assembly Printer";
53 bool printInstruction(const MachineInstr *MI);
54 void printOp(const MachineOperand &MO, bool IsCallOp = false);
55 void printConstantPool(MachineConstantPool *MCP);
56 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
57 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
58 void printMachineInstruction(const MachineInstr *MI);
59 bool runOnMachineFunction(MachineFunction &F);
60 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
63 } // end of anonymous namespace
65 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66 /// assembly code for a MachineFunction to the given output stream,
67 /// using the given target machine description. This should work
68 /// regardless of whether the function is in SSA form.
70 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
72 return new AlphaAsmPrinter(o, tm);
75 #include "AlphaGenAsmWriter.inc"
77 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
79 const MachineOperand &MO = MI->getOperand(opNum);
80 if (MO.getType() == MachineOperand::MO_MachineRegister) {
81 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
82 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
83 } else if (MO.isImmediate()) {
84 O << MO.getImmedValue();
91 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
92 const MRegisterInfo &RI = *TM.getRegisterInfo();
95 switch (MO.getType()) {
96 case MachineOperand::MO_VirtualRegister:
97 if (Value *V = MO.getVRegValueOrNull()) {
98 O << "<" << V->getName() << ">";
102 case MachineOperand::MO_MachineRegister:
103 case MachineOperand::MO_CCRegister:
104 O << RI.get(MO.getReg()).Name;
107 case MachineOperand::MO_SignExtendedImmed:
108 case MachineOperand::MO_UnextendedImmed:
109 std::cerr << "printOp() does not handle immediate values\n";
113 case MachineOperand::MO_PCRelativeDisp:
114 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
118 case MachineOperand::MO_MachineBasicBlock: {
119 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
120 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
121 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
122 << MBBOp->getBasicBlock()->getName();
126 case MachineOperand::MO_ConstantPoolIndex:
127 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
130 case MachineOperand::MO_ExternalSymbol:
131 O << MO.getSymbolName();
134 case MachineOperand::MO_GlobalAddress:
135 O << Mang->getValueName(MO.getGlobal());
139 O << "<unknown operand type: " << MO.getType() << ">";
144 /// printMachineInstruction -- Print out a single Alpha MI to
145 /// the current output stream.
147 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
149 if (printInstruction(MI))
150 return; // Printer was automatically generated
152 assert(0 && "Unhandled instruction in asm writer!");
158 /// runOnMachineFunction - This uses the printMachineInstruction()
159 /// method to print assembly for each instruction.
161 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
162 setupMachineFunction(MF);
165 if (CurrentFnName.compare("main") == 0)
167 O << "\n\n#HACK\n\t.text\n\t.ent __main\n__main:\n\tret $31,($26),1\n\t.end __main\n#ENDHACK\n\n";
170 // Print out constants referenced by the function
171 printConstantPool(MF.getConstantPool());
173 // Print out labels for the function.
176 O << "\t.globl\t" << CurrentFnName << "\n";
177 O << "\t.ent\t" << CurrentFnName << "\n";
179 O << CurrentFnName << ":\n";
181 // Print out code for the function.
182 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
184 // Print a label for the basic block.
185 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
186 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
187 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
189 // Print the assembly for the instruction.
191 printMachineInstruction(II);
196 O << "\t.end\t" << CurrentFnName << "\n";
198 // We didn't modify anything.
203 /// printConstantPool - Print to the current output stream assembly
204 /// representations of the constants in the constant pool MCP. This is
205 /// used to print out constants which have been "spilled to memory" by
206 /// the code generator.
208 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
209 const std::vector<Constant*> &CP = MCP->getConstants();
210 const TargetData &TD = TM.getTargetData();
212 if (CP.empty()) return;
215 // for (unsigned i = 0, e = CP.size(); i != e; ++i) {
216 // O << "\t.section\t.rodata\n";
217 // emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
218 // O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
219 // << *CP[i] << "\n";
220 // //emitGlobalConstant(CP[i]);
224 bool AlphaAsmPrinter::doInitialization(Module &M)
226 AsmPrinter::doInitialization(M);
227 O << "\t.arch ev56\n";
232 // SwitchSection - Switch to the specified section of the executable if we are
233 // not already in it!
235 static void SwitchSection(std::ostream &OS, std::string &CurSection,
236 const char *NewSection) {
237 if (CurSection != NewSection) {
238 CurSection = NewSection;
239 if (!CurSection.empty())
240 OS << "\t" << NewSection << "\n";
244 bool AlphaAsmPrinter::doFinalization(Module &M) {
245 const TargetData &TD = TM.getTargetData();
246 std::string CurSection;
248 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
249 if (I->hasInitializer()) { // External global require no code
251 std::string name = Mang->getValueName(I);
252 Constant *C = I->getInitializer();
253 unsigned Size = TD.getTypeSize(C->getType());
254 unsigned Align = TD.getTypeAlignmentShift(C->getType());
256 if (C->isNullValue() &&
257 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
258 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
259 SwitchSection(O, CurSection, ".data");
260 if (I->hasInternalLinkage())
261 O << "\t.local " << name << "\n";
263 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
264 << "," << (1 << Align);
266 WriteAsOperand(O, I, true, true, &M);
269 switch (I->getLinkage()) {
270 case GlobalValue::LinkOnceLinkage:
271 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
272 // Nonnull linkonce -> weak
273 O << "\t.weak " << name << "\n";
274 SwitchSection(O, CurSection, "");
275 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
277 case GlobalValue::AppendingLinkage:
278 // FIXME: appending linkage variables should go into a section of
279 // their name or something. For now, just emit them as external.
280 case GlobalValue::ExternalLinkage:
281 // If external or appending, declare as a global symbol
282 O << "\t.globl " << name << "\n";
284 case GlobalValue::InternalLinkage:
285 if (C->isNullValue())
286 SwitchSection(O, CurSection, ".bss");
288 SwitchSection(O, CurSection, ".data");
290 case GlobalValue::GhostLinkage:
291 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
295 emitAlignment(Align);
296 O << "\t.type " << name << ",@object\n";
297 O << "\t.size " << name << "," << Size << "\n";
298 O << name << ":\t\t\t\t# ";
299 WriteAsOperand(O, I, true, true, &M);
301 WriteAsOperand(O, C, false, false, &M);
303 emitGlobalConstant(C);
307 AsmPrinter::doFinalization(M);