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;
34 extern cl::opt<bool> EnableAlphaCT;
38 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
40 struct AlphaAsmPrinter : public AsmPrinter {
42 /// Unique incrementer for label values for referencing Global values.
46 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
47 : AsmPrinter(o, tm), LabelNumber(0)
49 AlignmentIsInBytes = false;
52 /// We name each basic block in a Function with a unique number, so
53 /// that we can consistently refer to them later. This is cleared
54 /// at the beginning of each call to runOnMachineFunction().
56 typedef std::map<const Value *, unsigned> ValueMapTy;
57 ValueMapTy NumberForBB;
58 std::string CurSection;
60 virtual const char *getPassName() const {
61 return "Alpha Assembly Printer";
63 bool printInstruction(const MachineInstr *MI);
64 void printOp(const MachineOperand &MO, bool IsCallOp = false);
65 void printConstantPool(MachineConstantPool *MCP);
66 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
67 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
68 void printMachineInstruction(const MachineInstr *MI);
69 bool runOnMachineFunction(MachineFunction &F);
70 bool doInitialization(Module &M);
71 bool doFinalization(Module &M);
72 void SwitchSection(std::ostream &OS, const char *NewSection);
74 } // end of anonymous namespace
76 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
77 /// assembly code for a MachineFunction to the given output stream,
78 /// using the given target machine description. This should work
79 /// regardless of whether the function is in SSA form.
81 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
83 return new AlphaAsmPrinter(o, tm);
86 #include "AlphaGenAsmWriter.inc"
88 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
90 const MachineOperand &MO = MI->getOperand(opNum);
91 if (MO.getType() == MachineOperand::MO_MachineRegister) {
92 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
93 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
94 } else if (MO.isImmediate()) {
95 O << MO.getImmedValue();
102 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
103 const MRegisterInfo &RI = *TM.getRegisterInfo();
106 switch (MO.getType()) {
107 case MachineOperand::MO_VirtualRegister:
108 if (Value *V = MO.getVRegValueOrNull()) {
109 O << "<" << V->getName() << ">";
113 case MachineOperand::MO_MachineRegister:
114 case MachineOperand::MO_CCRegister:
115 O << RI.get(MO.getReg()).Name;
118 case MachineOperand::MO_SignExtendedImmed:
119 case MachineOperand::MO_UnextendedImmed:
120 std::cerr << "printOp() does not handle immediate values\n";
124 case MachineOperand::MO_PCRelativeDisp:
125 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
129 case MachineOperand::MO_MachineBasicBlock: {
130 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
131 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
132 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
133 << MBBOp->getBasicBlock()->getName();
137 case MachineOperand::MO_ConstantPoolIndex:
138 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
141 case MachineOperand::MO_ExternalSymbol:
142 O << MO.getSymbolName();
145 case MachineOperand::MO_GlobalAddress:
146 //Abuse PCrel to specify pcrel calls
147 //calls are the only thing that use this flag
148 if (MO.isPCRelative())
149 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
151 O << Mang->getValueName(MO.getGlobal());
155 O << "<unknown operand type: " << MO.getType() << ">";
160 /// printMachineInstruction -- Print out a single Alpha MI to
161 /// the current output stream.
163 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
165 if (printInstruction(MI))
166 return; // Printer was automatically generated
168 assert(0 && "Unhandled instruction in asm writer!");
174 /// runOnMachineFunction - This uses the printMachineInstruction()
175 /// method to print assembly for each instruction.
177 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
178 setupMachineFunction(MF);
181 // Print out constants referenced by the function
182 printConstantPool(MF.getConstantPool());
184 // Print out labels for the function.
185 SwitchSection(O, "text");
187 O << "\t.globl " << CurrentFnName << "\n";
188 O << "\t.ent " << CurrentFnName << "\n";
190 O << CurrentFnName << ":\n";
192 // Print out code for the function.
193 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
195 // Print a label for the basic block.
196 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
197 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
198 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
200 // Print the assembly for the instruction.
202 printMachineInstruction(II);
207 O << "\t.end " << CurrentFnName << "\n";
209 // We didn't modify anything.
214 /// printConstantPool - Print to the current output stream assembly
215 /// representations of the constants in the constant pool MCP. This is
216 /// used to print out constants which have been "spilled to memory" by
217 /// the code generator.
219 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
220 const std::vector<Constant*> &CP = MCP->getConstants();
221 const TargetData &TD = TM.getTargetData();
223 if (CP.empty()) return;
225 SwitchSection(O, "section .rodata");
226 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
227 // SwitchSection(O, "section .rodata, \"dr\"");
228 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
229 O << "$CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
231 emitGlobalConstant(CP[i]);
235 bool AlphaAsmPrinter::doInitialization(Module &M)
237 AsmPrinter::doInitialization(M);
238 if(EnableAlphaFTOI || EnableAlphaCT)
239 O << "\t.arch ev6\n";
241 O << "\t.arch ev56\n";
246 // SwitchSection - Switch to the specified section of the executable if we are
247 // not already in it!
249 void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
251 if (CurSection != NewSection) {
252 CurSection = NewSection;
253 if (!CurSection.empty())
254 OS << "\t." << NewSection << "\n";
258 bool AlphaAsmPrinter::doFinalization(Module &M) {
259 const TargetData &TD = TM.getTargetData();
261 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
262 if (I->hasInitializer()) { // External global require no code
264 std::string name = Mang->getValueName(I);
265 Constant *C = I->getInitializer();
266 unsigned Size = TD.getTypeSize(C->getType());
267 unsigned Align = TD.getTypeAlignmentShift(C->getType());
269 if (C->isNullValue() &&
270 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
271 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
272 SwitchSection(O, "data");
273 if (I->hasInternalLinkage())
274 O << "\t.local " << name << "\n";
276 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
277 << "," << (1 << Align);
279 WriteAsOperand(O, I, true, true, &M);
282 switch (I->getLinkage()) {
283 case GlobalValue::LinkOnceLinkage:
284 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
285 // Nonnull linkonce -> weak
286 O << "\t.weak " << name << "\n";
287 SwitchSection(O, "");
288 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
290 case GlobalValue::AppendingLinkage:
291 // FIXME: appending linkage variables should go into a section of
292 // their name or something. For now, just emit them as external.
293 case GlobalValue::ExternalLinkage:
294 // If external or appending, declare as a global symbol
295 O << "\t.globl " << name << "\n";
297 case GlobalValue::InternalLinkage:
298 if (C->isNullValue())
299 SwitchSection(O, "bss"); //was .bss
301 SwitchSection(O, "data");
303 case GlobalValue::GhostLinkage:
304 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
308 emitAlignment(Align);
309 O << "\t.type " << name << ",@object\n";
310 O << "\t.size " << name << "," << Size << "\n";
311 O << name << ":\t\t\t\t# ";
312 WriteAsOperand(O, I, true, true, &M);
314 WriteAsOperand(O, C, false, false, &M);
316 emitGlobalConstant(C);
320 AsmPrinter::doFinalization(M);