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)
42 AlignmentIsInBytes = false;
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;
52 virtual const char *getPassName() const {
53 return "Alpha Assembly Printer";
55 bool printInstruction(const MachineInstr *MI);
56 void printOp(const MachineOperand &MO, bool IsCallOp = false);
57 void printConstantPool(MachineConstantPool *MCP);
58 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
59 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
60 void printMachineInstruction(const MachineInstr *MI);
61 bool runOnMachineFunction(MachineFunction &F);
62 bool doInitialization(Module &M);
63 bool doFinalization(Module &M);
65 } // end of anonymous namespace
67 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
68 /// assembly code for a MachineFunction to the given output stream,
69 /// using the given target machine description. This should work
70 /// regardless of whether the function is in SSA form.
72 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
74 return new AlphaAsmPrinter(o, tm);
77 #include "AlphaGenAsmWriter.inc"
79 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
81 const MachineOperand &MO = MI->getOperand(opNum);
82 if (MO.getType() == MachineOperand::MO_MachineRegister) {
83 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
84 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
85 } else if (MO.isImmediate()) {
86 O << MO.getImmedValue();
93 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
94 const MRegisterInfo &RI = *TM.getRegisterInfo();
97 switch (MO.getType()) {
98 case MachineOperand::MO_VirtualRegister:
99 if (Value *V = MO.getVRegValueOrNull()) {
100 O << "<" << V->getName() << ">";
104 case MachineOperand::MO_MachineRegister:
105 case MachineOperand::MO_CCRegister:
106 O << RI.get(MO.getReg()).Name;
109 case MachineOperand::MO_SignExtendedImmed:
110 case MachineOperand::MO_UnextendedImmed:
111 std::cerr << "printOp() does not handle immediate values\n";
115 case MachineOperand::MO_PCRelativeDisp:
116 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
120 case MachineOperand::MO_MachineBasicBlock: {
121 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
122 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
123 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
124 << MBBOp->getBasicBlock()->getName();
128 case MachineOperand::MO_ConstantPoolIndex:
129 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
132 case MachineOperand::MO_ExternalSymbol:
133 O << MO.getSymbolName();
136 case MachineOperand::MO_GlobalAddress:
137 O << Mang->getValueName(MO.getGlobal());
141 O << "<unknown operand type: " << MO.getType() << ">";
146 /// printMachineInstruction -- Print out a single Alpha MI to
147 /// the current output stream.
149 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
151 if (printInstruction(MI))
152 return; // Printer was automatically generated
154 assert(0 && "Unhandled instruction in asm writer!");
160 /// runOnMachineFunction - This uses the printMachineInstruction()
161 /// method to print assembly for each instruction.
163 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
164 setupMachineFunction(MF);
167 // Print out constants referenced by the function
168 printConstantPool(MF.getConstantPool());
170 // Print out labels for the function.
173 O << "\t.globl\t" << CurrentFnName << "\n";
174 O << "\t.ent\t" << CurrentFnName << "\n";
176 O << CurrentFnName << ":\n";
178 // Print out code for the function.
179 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
181 // Print a label for the basic block.
182 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
183 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
184 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
186 // Print the assembly for the instruction.
188 printMachineInstruction(II);
193 O << "\t.end\t" << CurrentFnName << "\n";
195 // We didn't modify anything.
200 /// printConstantPool - Print to the current output stream assembly
201 /// representations of the constants in the constant pool MCP. This is
202 /// used to print out constants which have been "spilled to memory" by
203 /// the code generator.
205 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
206 const std::vector<Constant*> &CP = MCP->getConstants();
207 const TargetData &TD = TM.getTargetData();
209 if (CP.empty()) return;
211 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
212 O << "\t.section\t.rodata\n";
213 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
214 O << "$CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
216 emitGlobalConstant(CP[i]);
220 bool AlphaAsmPrinter::doInitialization(Module &M)
222 AsmPrinter::doInitialization(M);
223 O << "\t.arch ev56\n";
228 // SwitchSection - Switch to the specified section of the executable if we are
229 // not already in it!
231 static void SwitchSection(std::ostream &OS, std::string &CurSection,
232 const char *NewSection) {
233 if (CurSection != NewSection) {
234 CurSection = NewSection;
235 if (!CurSection.empty())
236 OS << "\t" << NewSection << "\n";
240 bool AlphaAsmPrinter::doFinalization(Module &M) {
241 const TargetData &TD = TM.getTargetData();
242 std::string CurSection;
244 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
245 if (I->hasInitializer()) { // External global require no code
247 std::string name = Mang->getValueName(I);
248 Constant *C = I->getInitializer();
249 unsigned Size = TD.getTypeSize(C->getType());
250 unsigned Align = TD.getTypeAlignmentShift(C->getType());
252 if (C->isNullValue() &&
253 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
254 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
255 SwitchSection(O, CurSection, ".data");
256 if (I->hasInternalLinkage())
257 O << "\t.local " << name << "\n";
259 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
260 << "," << (1 << Align);
262 WriteAsOperand(O, I, true, true, &M);
265 switch (I->getLinkage()) {
266 case GlobalValue::LinkOnceLinkage:
267 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
268 // Nonnull linkonce -> weak
269 O << "\t.weak " << name << "\n";
270 SwitchSection(O, CurSection, "");
271 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
273 case GlobalValue::AppendingLinkage:
274 // FIXME: appending linkage variables should go into a section of
275 // their name or something. For now, just emit them as external.
276 case GlobalValue::ExternalLinkage:
277 // If external or appending, declare as a global symbol
278 O << "\t.globl " << name << "\n";
280 case GlobalValue::InternalLinkage:
281 if (C->isNullValue())
282 SwitchSection(O, CurSection, ".bss");
284 SwitchSection(O, CurSection, ".data");
286 case GlobalValue::GhostLinkage:
287 std::cerr << "GhostLinkage cannot appear in X86AsmPrinter!\n";
291 emitAlignment(Align);
292 O << "\t.type " << name << ",@object\n";
293 O << "\t.size " << name << "," << Size << "\n";
294 O << name << ":\t\t\t\t# ";
295 WriteAsOperand(O, I, true, true, &M);
297 WriteAsOperand(O, C, false, false, &M);
299 emitGlobalConstant(C);
303 AsmPrinter::doFinalization(M);