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"
28 #include "llvm/Support/CommandLine.h"
33 extern cl::opt<bool> EnableAlphaFTOI;
37 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39 struct AlphaAsmPrinter : public AsmPrinter {
41 /// Unique incrementer for label values for referencing Global values.
45 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
46 : AsmPrinter(o, tm), LabelNumber(0)
48 AlignmentIsInBytes = false;
51 /// We name each basic block in a Function with a unique number, so
52 /// that we can consistently refer to them later. This is cleared
53 /// at the beginning of each call to runOnMachineFunction().
55 typedef std::map<const Value *, unsigned> ValueMapTy;
56 ValueMapTy NumberForBB;
57 std::string CurSection;
59 virtual const char *getPassName() const {
60 return "Alpha Assembly Printer";
62 bool printInstruction(const MachineInstr *MI);
63 void printOp(const MachineOperand &MO, bool IsCallOp = false);
64 void printConstantPool(MachineConstantPool *MCP);
65 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
66 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
67 void printMachineInstruction(const MachineInstr *MI);
68 bool runOnMachineFunction(MachineFunction &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
71 void SwitchSection(std::ostream &OS, const char *NewSection);
73 } // end of anonymous namespace
75 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
76 /// assembly code for a MachineFunction to the given output stream,
77 /// using the given target machine description. This should work
78 /// regardless of whether the function is in SSA form.
80 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
82 return new AlphaAsmPrinter(o, tm);
85 #include "AlphaGenAsmWriter.inc"
87 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
89 const MachineOperand &MO = MI->getOperand(opNum);
90 if (MO.getType() == MachineOperand::MO_MachineRegister) {
91 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
92 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
93 } else if (MO.isImmediate()) {
94 O << MO.getImmedValue();
101 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
102 const MRegisterInfo &RI = *TM.getRegisterInfo();
105 switch (MO.getType()) {
106 case MachineOperand::MO_VirtualRegister:
107 if (Value *V = MO.getVRegValueOrNull()) {
108 O << "<" << V->getName() << ">";
112 case MachineOperand::MO_MachineRegister:
113 case MachineOperand::MO_CCRegister:
114 O << RI.get(MO.getReg()).Name;
117 case MachineOperand::MO_SignExtendedImmed:
118 case MachineOperand::MO_UnextendedImmed:
119 std::cerr << "printOp() does not handle immediate values\n";
123 case MachineOperand::MO_PCRelativeDisp:
124 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
128 case MachineOperand::MO_MachineBasicBlock: {
129 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
130 O << "LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
131 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
132 << MBBOp->getBasicBlock()->getName();
136 case MachineOperand::MO_ConstantPoolIndex:
137 O << "CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
140 case MachineOperand::MO_ExternalSymbol:
141 O << MO.getSymbolName();
144 case MachineOperand::MO_GlobalAddress:
145 //Abuse PCrel to specify pcrel calls
146 //calls are the only thing that use this flag
147 if (MO.isPCRelative())
148 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
150 O << Mang->getValueName(MO.getGlobal());
154 O << "<unknown operand type: " << MO.getType() << ">";
159 /// printMachineInstruction -- Print out a single Alpha MI to
160 /// the current output stream.
162 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
164 if (printInstruction(MI))
165 return; // Printer was automatically generated
167 assert(0 && "Unhandled instruction in asm writer!");
173 /// runOnMachineFunction - This uses the printMachineInstruction()
174 /// method to print assembly for each instruction.
176 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
177 setupMachineFunction(MF);
180 // Print out constants referenced by the function
181 printConstantPool(MF.getConstantPool());
183 // Print out labels for the function.
184 SwitchSection(O, "text");
186 O << "\t.globl\t" << CurrentFnName << "\n";
187 O << "\t.ent\t" << CurrentFnName << "\n";
189 O << CurrentFnName << ":\n";
191 // Print out code for the function.
192 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
194 // Print a label for the basic block.
195 O << "LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
196 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
197 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
199 // Print the assembly for the instruction.
201 printMachineInstruction(II);
206 O << "\t.end " << CurrentFnName << "\n";
208 // We didn't modify anything.
213 /// printConstantPool - Print to the current output stream assembly
214 /// representations of the constants in the constant pool MCP. This is
215 /// used to print out constants which have been "spilled to memory" by
216 /// the code generator.
218 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
219 const std::vector<Constant*> &CP = MCP->getConstants();
220 const TargetData &TD = TM.getTargetData();
222 if (CP.empty()) return;
224 SwitchSection(O, "section .rodata");
225 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
226 // SwitchSection(O, "section .rodata, \"dr\"");
227 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
228 O << "CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
230 emitGlobalConstant(CP[i]);
234 bool AlphaAsmPrinter::doInitialization(Module &M)
236 AsmPrinter::doInitialization(M);
238 O << "\t.arch ev6\n";
240 O << "\t.arch ev56\n";
245 // SwitchSection - Switch to the specified section of the executable if we are
246 // not already in it!
248 void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
250 if (CurSection != NewSection) {
251 CurSection = NewSection;
252 if (!CurSection.empty())
253 OS << "\t." << NewSection << "\n";
257 bool AlphaAsmPrinter::doFinalization(Module &M) {
258 const TargetData &TD = TM.getTargetData();
260 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
261 if (I->hasInitializer()) { // External global require no code
263 std::string name = Mang->getValueName(I);
264 Constant *C = I->getInitializer();
265 unsigned Size = TD.getTypeSize(C->getType());
266 unsigned Align = TD.getTypeAlignmentShift(C->getType());
268 if (C->isNullValue() &&
269 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
270 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
271 SwitchSection(O, "data");
272 if (I->hasInternalLinkage())
273 O << "\t.local " << name << "\n";
275 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
276 << "," << (1 << Align);
278 WriteAsOperand(O, I, true, true, &M);
281 switch (I->getLinkage()) {
282 case GlobalValue::LinkOnceLinkage:
283 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
284 // Nonnull linkonce -> weak
285 O << "\t.weak " << name << "\n";
286 SwitchSection(O, "");
287 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
289 case GlobalValue::AppendingLinkage:
290 // FIXME: appending linkage variables should go into a section of
291 // their name or something. For now, just emit them as external.
292 case GlobalValue::ExternalLinkage:
293 // If external or appending, declare as a global symbol
294 O << "\t.globl " << name << "\n";
296 case GlobalValue::InternalLinkage:
297 if (C->isNullValue())
298 SwitchSection(O, "bss"); //was .bss
300 SwitchSection(O, "data");
302 case GlobalValue::GhostLinkage:
303 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
307 emitAlignment(Align);
308 O << "\t.type " << name << ",@object\n";
309 O << "\t.size " << name << "," << Size << "\n";
310 O << name << ":\t\t\t\t# ";
311 WriteAsOperand(O, I, true, true, &M);
313 WriteAsOperand(O, C, false, false, &M);
315 emitGlobalConstant(C);
319 AsmPrinter::doFinalization(M);