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/AsmPrinter.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/Mangler.h"
24 #include "llvm/ADT/Statistic.h"
29 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
31 struct AlphaAsmPrinter : public AsmPrinter {
33 /// Unique incrementer for label values for referencing Global values.
37 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
38 : AsmPrinter(o, tm), LabelNumber(0) {
39 AlignmentIsInBytes = false;
40 PrivateGlobalPrefix = "$";
43 /// We name each basic block in a Function with a unique number, so
44 /// that we can consistently refer to them later. This is cleared
45 /// at the beginning of each call to runOnMachineFunction().
47 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
49 std::string CurSection;
51 virtual const char *getPassName() const {
52 return "Alpha Assembly Printer";
54 bool printInstruction(const MachineInstr *MI);
55 void printOp(const MachineOperand &MO, bool IsCallOp = false);
56 void printOperand(const MachineInstr *MI, int opNum);
57 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
58 void printMachineInstruction(const MachineInstr *MI);
59 bool runOnMachineFunction(MachineFunction &F);
60 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
63 } // end of anonymous namespace
65 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66 /// assembly code for a MachineFunction to the given output stream,
67 /// using the given target machine description. This should work
68 /// regardless of whether the function is in SSA form.
70 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
72 return new AlphaAsmPrinter(o, tm);
75 #include "AlphaGenAsmWriter.inc"
77 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
79 const MachineOperand &MO = MI->getOperand(opNum);
80 if (MO.getType() == MachineOperand::MO_MachineRegister) {
81 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
82 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
83 } else if (MO.isImmediate()) {
84 O << MO.getImmedValue();
91 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
92 const MRegisterInfo &RI = *TM.getRegisterInfo();
95 switch (MO.getType()) {
96 case MachineOperand::MO_VirtualRegister:
97 if (Value *V = MO.getVRegValueOrNull()) {
98 O << "<" << V->getName() << ">";
102 case MachineOperand::MO_MachineRegister:
103 case MachineOperand::MO_CCRegister:
104 O << RI.get(MO.getReg()).Name;
107 case MachineOperand::MO_SignExtendedImmed:
108 case MachineOperand::MO_UnextendedImmed:
109 std::cerr << "printOp() does not handle immediate values\n";
113 case MachineOperand::MO_PCRelativeDisp:
114 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
118 case MachineOperand::MO_MachineBasicBlock: {
119 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
120 O << PrivateGlobalPrefix << "LBB"
121 << Mang->getValueName(MBBOp->getParent()->getFunction())
122 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
123 << MBBOp->getBasicBlock()->getName();
127 case MachineOperand::MO_ConstantPoolIndex:
128 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
129 << MO.getConstantPoolIndex();
132 case MachineOperand::MO_ExternalSymbol:
133 O << MO.getSymbolName();
136 case MachineOperand::MO_GlobalAddress:
137 //Abuse PCrel to specify pcrel calls
138 //calls are the only thing that use this flag
139 // if (MO.isPCRelative())
140 // O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
142 O << Mang->getValueName(MO.getGlobal());
146 O << "<unknown operand type: " << MO.getType() << ">";
151 /// printMachineInstruction -- Print out a single Alpha MI to
152 /// the current output stream.
154 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
156 if (printInstruction(MI))
157 return; // Printer was automatically generated
159 assert(0 && "Unhandled instruction in asm writer!");
165 /// runOnMachineFunction - This uses the printMachineInstruction()
166 /// method to print assembly for each instruction.
168 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
169 SetupMachineFunction(MF);
172 // Print out constants referenced by the function
173 EmitConstantPool(MF.getConstantPool());
175 // Print out labels for the function.
176 const Function *F = MF.getFunction();
177 SwitchSection(".text", F);
179 switch (F->getLinkage()) {
180 default: assert(0 && "Unknown linkage type!");
181 case Function::InternalLinkage: // Symbols default to internal.
183 case Function::ExternalLinkage:
184 O << "\t.globl " << CurrentFnName << "\n";
186 case Function::WeakLinkage:
187 case Function::LinkOnceLinkage:
188 O << "\t.weak " << CurrentFnName << "\n";
192 O << "\t.ent " << CurrentFnName << "\n";
194 O << CurrentFnName << ":\n";
196 // Print out code for the function.
197 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
199 // Print a label for the basic block.
200 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
201 << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
202 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
204 // Print the assembly for the instruction.
206 printMachineInstruction(II);
211 O << "\t.end " << CurrentFnName << "\n";
213 // We didn't modify anything.
217 bool AlphaAsmPrinter::doInitialization(Module &M)
219 AsmPrinter::doInitialization(M);
220 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
221 || TM.getSubtarget<AlphaSubtarget>().hasCT())
222 O << "\t.arch ev6\n";
224 O << "\t.arch ev56\n";
225 O << "\t.set noat\n";
229 bool AlphaAsmPrinter::doFinalization(Module &M) {
230 const TargetData &TD = TM.getTargetData();
232 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
233 if (I->hasInitializer()) { // External global require no code
235 std::string name = Mang->getValueName(I);
236 Constant *C = I->getInitializer();
237 unsigned Size = TD.getTypeSize(C->getType());
238 // unsigned Align = TD.getTypeAlignmentShift(C->getType());
239 unsigned Align = getPreferredAlignmentLog(I);
241 if (C->isNullValue() &&
242 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
243 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
244 SwitchSection("\t.section .data", I);
245 if (I->hasInternalLinkage())
246 O << "\t.local " << name << "\n";
248 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
249 << "," << (1 << Align)
252 switch (I->getLinkage()) {
253 case GlobalValue::LinkOnceLinkage:
254 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
255 // Nonnull linkonce -> weak
256 O << "\t.weak " << name << "\n";
257 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
258 SwitchSection("", I);
260 case GlobalValue::AppendingLinkage:
261 // FIXME: appending linkage variables should go into a section of
262 // their name or something. For now, just emit them as external.
263 case GlobalValue::ExternalLinkage:
264 // If external or appending, declare as a global symbol
265 O << "\t.globl " << name << "\n";
267 case GlobalValue::InternalLinkage:
268 SwitchSection(C->isNullValue() ? "\t.section .bss" :
269 "\t.section .data", I);
271 case GlobalValue::GhostLinkage:
272 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
276 EmitAlignment(Align);
277 O << "\t.type " << name << ",@object\n";
278 O << "\t.size " << name << "," << Size << "\n";
280 EmitGlobalConstant(C);
284 AsmPrinter::doFinalization(M);