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/TargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/Mangler.h"
25 #include "llvm/ADT/Statistic.h"
30 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
32 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
34 /// Unique incrementer for label values for referencing Global values.
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39 : AsmPrinter(o, tm, T), LabelNumber(0) {
42 /// We name each basic block in a Function with a unique number, so
43 /// that we can consistently refer to them later. This is cleared
44 /// at the beginning of each call to runOnMachineFunction().
46 typedef std::map<const Value *, unsigned> ValueMapTy;
47 ValueMapTy NumberForBB;
48 std::string CurSection;
50 virtual const char *getPassName() const {
51 return "Alpha Assembly Printer";
53 bool printInstruction(const MachineInstr *MI);
54 void printOp(const MachineOperand &MO, bool IsCallOp = false);
55 void printOperand(const MachineInstr *MI, int opNum);
56 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
57 void printMachineInstruction(const MachineInstr *MI);
58 bool runOnMachineFunction(MachineFunction &F);
59 bool doInitialization(Module &M);
60 bool doFinalization(Module &M);
62 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63 unsigned AsmVariant, const char *ExtraCode);
64 bool PrintAsmMemoryOperand(const MachineInstr *MI,
67 const char *ExtraCode);
69 } // end of anonymous namespace
71 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
72 /// assembly code for a MachineFunction to the given output stream,
73 /// using the given target machine description. This should work
74 /// regardless of whether the function is in SSA form.
76 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
78 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
81 #include "AlphaGenAsmWriter.inc"
83 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
85 const MachineOperand &MO = MI->getOperand(opNum);
86 if (MO.getType() == MachineOperand::MO_Register) {
87 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
88 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
89 } else if (MO.isImmediate()) {
90 O << MO.getImmedValue();
91 assert(MO.getImmedValue() < (1 << 30));
98 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
99 const MRegisterInfo &RI = *TM.getRegisterInfo();
102 switch (MO.getType()) {
103 case MachineOperand::MO_Register:
104 O << RI.get(MO.getReg()).Name;
107 case MachineOperand::MO_Immediate:
108 std::cerr << "printOp() does not handle immediate values\n";
112 case MachineOperand::MO_MachineBasicBlock:
113 printBasicBlockLabel(MO.getMachineBasicBlock());
116 case MachineOperand::MO_ConstantPoolIndex:
117 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
118 << MO.getConstantPoolIndex();
121 case MachineOperand::MO_ExternalSymbol:
122 O << MO.getSymbolName();
125 case MachineOperand::MO_GlobalAddress:
126 O << Mang->getValueName(MO.getGlobal());
130 O << "<unknown operand type: " << MO.getType() << ">";
135 /// printMachineInstruction -- Print out a single Alpha MI to
136 /// the current output stream.
138 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
140 if (printInstruction(MI))
141 return; // Printer was automatically generated
143 assert(0 && "Unhandled instruction in asm writer!");
149 /// runOnMachineFunction - This uses the printMachineInstruction()
150 /// method to print assembly for each instruction.
152 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
153 SetupMachineFunction(MF);
156 // Print out constants referenced by the function
157 EmitConstantPool(MF.getConstantPool());
159 // Print out labels for the function.
160 const Function *F = MF.getFunction();
161 SwitchToTextSection(".text", F);
163 switch (F->getLinkage()) {
164 default: assert(0 && "Unknown linkage type!");
165 case Function::InternalLinkage: // Symbols default to internal.
167 case Function::ExternalLinkage:
168 O << "\t.globl " << CurrentFnName << "\n";
170 case Function::WeakLinkage:
171 case Function::LinkOnceLinkage:
172 O << "\t.weak " << CurrentFnName << "\n";
176 O << "\t.ent " << CurrentFnName << "\n";
178 O << CurrentFnName << ":\n";
180 // Print out code for the function.
181 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
183 printBasicBlockLabel(I, true);
185 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187 // Print the assembly for the instruction.
189 printMachineInstruction(II);
194 O << "\t.end " << CurrentFnName << "\n";
196 // We didn't modify anything.
200 bool AlphaAsmPrinter::doInitialization(Module &M)
202 AsmPrinter::doInitialization(M);
203 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
204 || TM.getSubtarget<AlphaSubtarget>().hasCT())
205 O << "\t.arch ev6\n";
207 O << "\t.arch ev56\n";
208 O << "\t.set noat\n";
212 bool AlphaAsmPrinter::doFinalization(Module &M) {
213 const TargetData *TD = TM.getTargetData();
215 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
216 if (I->hasInitializer()) { // External global require no code
217 // Check to see if this is a special global used by LLVM, if so, emit it.
218 if (EmitSpecialLLVMGlobal(I))
222 std::string name = Mang->getValueName(I);
223 Constant *C = I->getInitializer();
224 unsigned Size = TD->getTypeSize(C->getType());
225 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
226 unsigned Align = getPreferredAlignmentLog(I);
228 if (C->isNullValue() &&
229 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
230 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
231 SwitchToDataSection("\t.section .data", I);
232 if (I->hasInternalLinkage())
233 O << "\t.local " << name << "\n";
235 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
236 << "," << (1 << Align)
239 switch (I->getLinkage()) {
240 case GlobalValue::LinkOnceLinkage:
241 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
242 // Nonnull linkonce -> weak
243 O << "\t.weak " << name << "\n";
244 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
245 SwitchToDataSection("", I);
247 case GlobalValue::AppendingLinkage:
248 // FIXME: appending linkage variables should go into a section of
249 // their name or something. For now, just emit them as external.
250 case GlobalValue::ExternalLinkage:
251 // If external or appending, declare as a global symbol
252 O << "\t.globl " << name << "\n";
254 case GlobalValue::InternalLinkage:
255 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
256 "\t.section .data", I);
258 case GlobalValue::GhostLinkage:
259 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
261 case GlobalValue::DLLImportLinkage:
262 std::cerr << "DLLImport linkage is not supported by this target!\n";
264 case GlobalValue::DLLExportLinkage:
265 std::cerr << "DLLExport linkage is not supported by this target!\n";
268 assert(0 && "Unknown linkage type!");
271 EmitAlignment(Align);
272 O << "\t.type " << name << ",@object\n";
273 O << "\t.size " << name << "," << Size << "\n";
275 EmitGlobalConstant(C);
279 AsmPrinter::doFinalization(M);
283 /// PrintAsmOperand - Print out an operand for an inline asm expression.
285 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
287 const char *ExtraCode) {
288 printOperand(MI, OpNo);
292 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
295 const char *ExtraCode) {
296 if (ExtraCode && ExtraCode[0])
297 return true; // Unknown modifier.
299 printOperand(MI, OpNo);