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;
48 /// We name each basic block in a Function with a unique number, so
49 /// that we can consistently refer to them later. This is cleared
50 /// at the beginning of each call to runOnMachineFunction().
52 typedef std::map<const Value *, unsigned> ValueMapTy;
53 ValueMapTy NumberForBB;
54 std::string CurSection;
56 virtual const char *getPassName() const {
57 return "Alpha Assembly Printer";
59 bool printInstruction(const MachineInstr *MI);
60 void printOp(const MachineOperand &MO, bool IsCallOp = false);
61 void printConstantPool(MachineConstantPool *MCP);
62 void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
63 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
64 void printMachineInstruction(const MachineInstr *MI);
65 bool runOnMachineFunction(MachineFunction &F);
66 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
68 void SwitchSection(std::ostream &OS, const char *NewSection);
70 } // end of anonymous namespace
72 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
73 /// assembly code for a MachineFunction to the given output stream,
74 /// using the given target machine description. This should work
75 /// regardless of whether the function is in SSA form.
77 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
79 return new AlphaAsmPrinter(o, tm);
82 #include "AlphaGenAsmWriter.inc"
84 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
86 const MachineOperand &MO = MI->getOperand(opNum);
87 if (MO.getType() == MachineOperand::MO_MachineRegister) {
88 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
89 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
90 } else if (MO.isImmediate()) {
91 O << MO.getImmedValue();
98 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
99 const MRegisterInfo &RI = *TM.getRegisterInfo();
102 switch (MO.getType()) {
103 case MachineOperand::MO_VirtualRegister:
104 if (Value *V = MO.getVRegValueOrNull()) {
105 O << "<" << V->getName() << ">";
109 case MachineOperand::MO_MachineRegister:
110 case MachineOperand::MO_CCRegister:
111 O << RI.get(MO.getReg()).Name;
114 case MachineOperand::MO_SignExtendedImmed:
115 case MachineOperand::MO_UnextendedImmed:
116 std::cerr << "printOp() does not handle immediate values\n";
120 case MachineOperand::MO_PCRelativeDisp:
121 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
125 case MachineOperand::MO_MachineBasicBlock: {
126 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
127 O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
128 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
129 << MBBOp->getBasicBlock()->getName();
133 case MachineOperand::MO_ConstantPoolIndex:
134 O << "$CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex();
137 case MachineOperand::MO_ExternalSymbol:
138 O << MO.getSymbolName();
141 case MachineOperand::MO_GlobalAddress:
142 //Abuse PCrel to specify pcrel calls
143 //calls are the only thing that use this flag
144 if (MO.isPCRelative())
145 O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
147 O << Mang->getValueName(MO.getGlobal());
151 O << "<unknown operand type: " << MO.getType() << ">";
156 /// printMachineInstruction -- Print out a single Alpha MI to
157 /// the current output stream.
159 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
161 if (printInstruction(MI))
162 return; // Printer was automatically generated
164 assert(0 && "Unhandled instruction in asm writer!");
170 /// runOnMachineFunction - This uses the printMachineInstruction()
171 /// method to print assembly for each instruction.
173 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
174 setupMachineFunction(MF);
177 // Print out constants referenced by the function
178 printConstantPool(MF.getConstantPool());
180 // Print out labels for the function.
181 SwitchSection(O, "text");
183 O << "\t.globl " << CurrentFnName << "\n";
184 O << "\t.ent " << CurrentFnName << "\n";
186 O << CurrentFnName << ":\n";
188 // Print out code for the function.
189 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
191 // Print a label for the basic block.
192 O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
193 << CommentString << " " << I->getBasicBlock()->getName() << "\n";
194 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
196 // Print the assembly for the instruction.
198 printMachineInstruction(II);
203 O << "\t.end " << CurrentFnName << "\n";
205 // We didn't modify anything.
210 /// printConstantPool - Print to the current output stream assembly
211 /// representations of the constants in the constant pool MCP. This is
212 /// used to print out constants which have been "spilled to memory" by
213 /// the code generator.
215 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
216 const std::vector<Constant*> &CP = MCP->getConstants();
217 const TargetData &TD = TM.getTargetData();
219 if (CP.empty()) return;
221 SwitchSection(O, "rodata");
222 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
223 // SwitchSection(O, "section .rodata, \"dr\"");
224 emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
225 O << "$CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t" << CommentString
227 emitGlobalConstant(CP[i]);
231 bool AlphaAsmPrinter::doInitialization(Module &M)
233 AsmPrinter::doInitialization(M);
234 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
235 || TM.getSubtarget<AlphaSubtarget>().hasCT())
236 O << "\t.arch ev6\n";
238 O << "\t.arch ev56\n";
239 O << "\t.set noat\n";
244 // SwitchSection - Switch to the specified section of the executable if we are
245 // not already in it!
247 void AlphaAsmPrinter::SwitchSection(std::ostream &OS, const char *NewSection)
249 if (CurSection != NewSection) {
250 CurSection = NewSection;
251 if (!CurSection.empty())
252 OS << "\t.section ." << NewSection << "\n";
256 bool AlphaAsmPrinter::doFinalization(Module &M) {
257 const TargetData &TD = TM.getTargetData();
259 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
260 if (I->hasInitializer()) { // External global require no code
262 std::string name = Mang->getValueName(I);
263 Constant *C = I->getInitializer();
264 unsigned Size = TD.getTypeSize(C->getType());
265 unsigned Align = TD.getTypeAlignmentShift(C->getType());
267 if (C->isNullValue() &&
268 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
269 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
270 SwitchSection(O, "data");
271 if (I->hasInternalLinkage())
272 O << "\t.local " << name << "\n";
274 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
275 << "," << (1 << Align);
277 WriteAsOperand(O, I, true, true, &M);
280 switch (I->getLinkage()) {
281 case GlobalValue::LinkOnceLinkage:
282 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
283 // Nonnull linkonce -> weak
284 O << "\t.weak " << name << "\n";
285 SwitchSection(O, "");
286 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
288 case GlobalValue::AppendingLinkage:
289 // FIXME: appending linkage variables should go into a section of
290 // their name or something. For now, just emit them as external.
291 case GlobalValue::ExternalLinkage:
292 // If external or appending, declare as a global symbol
293 O << "\t.globl " << name << "\n";
295 case GlobalValue::InternalLinkage:
296 if (C->isNullValue())
297 SwitchSection(O, "bss");
299 SwitchSection(O, "data");
301 case GlobalValue::GhostLinkage:
302 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
306 emitAlignment(Align);
307 O << "\t.type " << name << ",@object\n";
308 O << "\t.size " << name << "," << Size << "\n";
309 O << name << ":\t\t\t\t# ";
310 WriteAsOperand(O, I, true, true, &M);
312 WriteAsOperand(O, C, false, false, &M);
314 emitGlobalConstant(C);
318 AsmPrinter::doFinalization(M);