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 "AlphaTargetMachine.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/Mangler.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Support/CommandLine.h"
34 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36 struct AlphaAsmPrinter : public AsmPrinter {
38 /// Unique incrementer for label values for referencing Global values.
42 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
43 : AsmPrinter(o, tm), LabelNumber(0)
45 AlignmentIsInBytes = false;
46 PrivateGlobalPrefix = "$";
49 /// We name each basic block in a Function with a unique number, so
50 /// that we can consistently refer to them later. This is cleared
51 /// at the beginning of each call to runOnMachineFunction().
53 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
55 std::string CurSection;
57 virtual const char *getPassName() const {
58 return "Alpha Assembly Printer";
60 bool printInstruction(const MachineInstr *MI);
61 void printOp(const MachineOperand &MO, bool IsCallOp = false);
62 void printConstantPool(MachineConstantPool *MCP);
63 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
64 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
65 void printMachineInstruction(const MachineInstr *MI);
66 bool runOnMachineFunction(MachineFunction &F);
67 bool doInitialization(Module &M);
68 bool doFinalization(Module &M);
69 void switchSection(std::ostream &OS, const char *NewSection);
71 } // end of anonymous namespace
73 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
74 /// assembly code for a MachineFunction to the given output stream,
75 /// using the given target machine description. This should work
76 /// regardless of whether the function is in SSA form.
78 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
80 return new AlphaAsmPrinter(o, tm);
83 #include "AlphaGenAsmWriter.inc"
85 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
87 const MachineOperand &MO = MI->getOperand(opNum);
88 if (MO.getType() == MachineOperand::MO_MachineRegister) {
89 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
90 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
91 } else if (MO.isImmediate()) {
92 O << MO.getImmedValue();
99 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
100 const MRegisterInfo &RI = *TM.getRegisterInfo();
103 switch (MO.getType()) {
104 case MachineOperand::MO_VirtualRegister:
105 if (Value *V = MO.getVRegValueOrNull()) {
106 O << "<" << V->getName() << ">";
110 case MachineOperand::MO_MachineRegister:
111 case MachineOperand::MO_CCRegister:
112 O << RI.get(MO.getReg()).Name;
115 case MachineOperand::MO_SignExtendedImmed:
116 case MachineOperand::MO_UnextendedImmed:
117 std::cerr << "printOp() does not handle immediate values\n";
121 case MachineOperand::MO_PCRelativeDisp:
122 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
126 case MachineOperand::MO_MachineBasicBlock: {
127 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
128 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
129 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
130 << MBBOp->getBasicBlock()->getName();
134 case MachineOperand::MO_ConstantPoolIndex:
135 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
136 << MO.getConstantPoolIndex();
139 case MachineOperand::MO_ExternalSymbol:
140 O << MO.getSymbolName();
143 case MachineOperand::MO_GlobalAddress:
144 //Abuse PCrel to specify pcrel calls
145 //calls are the only thing that use this flag
146 if (MO.isPCRelative())
147 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
149 O << Mang->getValueName(MO.getGlobal());
153 O << "<unknown operand type: " << MO.getType() << ">";
158 /// printMachineInstruction -- Print out a single Alpha MI to
159 /// the current output stream.
161 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
163 if (printInstruction(MI))
164 return; // Printer was automatically generated
166 assert(0 && "Unhandled instruction in asm writer!");
172 /// runOnMachineFunction - This uses the printMachineInstruction()
173 /// method to print assembly for each instruction.
175 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176 setupMachineFunction(MF);
179 // Print out constants referenced by the function
180 printConstantPool(MF.getConstantPool());
182 // Print out labels for the function.
183 switchSection(O, "text");
185 O << "\t.globl " << CurrentFnName << "\n";
186 O << "\t.ent " << CurrentFnName << "\n";
188 O << CurrentFnName << ":\n";
190 // Print out code for the function.
191 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
193 // Print a label for the basic block.
194 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
195 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
196 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
198 // Print the assembly for the instruction.
200 printMachineInstruction(II);
205 O << "\t.end " << CurrentFnName << "\n";
207 // We didn't modify anything.
212 /// printConstantPool - Print to the current output stream assembly
213 /// representations of the constants in the constant pool MCP. This is
214 /// used to print out constants which have been "spilled to memory" by
215 /// the code generator.
217 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
218 const std::vector<Constant*> &CP = MCP->getConstants();
219 const TargetData &TD = TM.getTargetData();
221 if (CP.empty()) return;
223 switchSection(O, "rodata");
224 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
225 // switchSection(O, "section .rodata, \"dr\"");
226 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
227 O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i
228 << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
229 emitGlobalConstant(CP[i]);
233 bool AlphaAsmPrinter::doInitialization(Module &M)
235 AsmPrinter::doInitialization(M);
236 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
237 || TM.getSubtarget<AlphaSubtarget>().hasCT())
238 O << "\t.arch ev6\n";
240 O << "\t.arch ev56\n";
241 O << "\t.set noat\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.section ." << 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");
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);