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 AlphaTargetAsmInfo : public TargetAsmInfo {
33 AlphaTargetAsmInfo() {
34 AlignmentIsInBytes = false;
35 PrivateGlobalPrefix = "$";
39 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
41 /// Unique incrementer for label values for referencing Global values.
45 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, TargetAsmInfo *T)
46 : AsmPrinter(o, tm, T), LabelNumber(0) {
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 printOperand(const MachineInstr *MI, int opNum);
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);
69 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
71 bool PrintAsmMemoryOperand(const MachineInstr *MI,
74 const char *ExtraCode);
76 } // end of anonymous namespace
78 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
79 /// assembly code for a MachineFunction to the given output stream,
80 /// using the given target machine description. This should work
81 /// regardless of whether the function is in SSA form.
83 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
85 AlphaTargetAsmInfo *TAI = new AlphaTargetAsmInfo();
86 return new AlphaAsmPrinter(o, tm, TAI);
89 #include "AlphaGenAsmWriter.inc"
91 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
93 const MachineOperand &MO = MI->getOperand(opNum);
94 if (MO.getType() == MachineOperand::MO_Register) {
95 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
96 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
97 } else if (MO.isImmediate()) {
98 O << MO.getImmedValue();
99 assert(MO.getImmedValue() < (1 << 30));
106 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
107 const MRegisterInfo &RI = *TM.getRegisterInfo();
110 switch (MO.getType()) {
111 case MachineOperand::MO_Register:
112 O << RI.get(MO.getReg()).Name;
115 case MachineOperand::MO_Immediate:
116 std::cerr << "printOp() does not handle immediate values\n";
120 case MachineOperand::MO_MachineBasicBlock:
121 printBasicBlockLabel(MO.getMachineBasicBlock());
124 case MachineOperand::MO_ConstantPoolIndex:
125 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
126 << MO.getConstantPoolIndex();
129 case MachineOperand::MO_ExternalSymbol:
130 O << MO.getSymbolName();
133 case MachineOperand::MO_GlobalAddress:
134 O << Mang->getValueName(MO.getGlobal());
138 O << "<unknown operand type: " << MO.getType() << ">";
143 /// printMachineInstruction -- Print out a single Alpha MI to
144 /// the current output stream.
146 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
148 if (printInstruction(MI))
149 return; // Printer was automatically generated
151 assert(0 && "Unhandled instruction in asm writer!");
157 /// runOnMachineFunction - This uses the printMachineInstruction()
158 /// method to print assembly for each instruction.
160 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
161 SetupMachineFunction(MF);
164 // Print out constants referenced by the function
165 EmitConstantPool(MF.getConstantPool());
167 // Print out labels for the function.
168 const Function *F = MF.getFunction();
169 SwitchToTextSection(".text", F);
171 switch (F->getLinkage()) {
172 default: assert(0 && "Unknown linkage type!");
173 case Function::InternalLinkage: // Symbols default to internal.
175 case Function::ExternalLinkage:
176 O << "\t.globl " << CurrentFnName << "\n";
178 case Function::WeakLinkage:
179 case Function::LinkOnceLinkage:
180 O << "\t.weak " << 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 printBasicBlockLabel(I, true);
193 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
195 // Print the assembly for the instruction.
197 printMachineInstruction(II);
202 O << "\t.end " << CurrentFnName << "\n";
204 // We didn't modify anything.
208 bool AlphaAsmPrinter::doInitialization(Module &M)
210 AsmPrinter::doInitialization(M);
211 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
212 || TM.getSubtarget<AlphaSubtarget>().hasCT())
213 O << "\t.arch ev6\n";
215 O << "\t.arch ev56\n";
216 O << "\t.set noat\n";
220 bool AlphaAsmPrinter::doFinalization(Module &M) {
221 const TargetData *TD = TM.getTargetData();
223 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
224 if (I->hasInitializer()) { // External global require no code
225 // Check to see if this is a special global used by LLVM, if so, emit it.
226 if (EmitSpecialLLVMGlobal(I))
230 std::string name = Mang->getValueName(I);
231 Constant *C = I->getInitializer();
232 unsigned Size = TD->getTypeSize(C->getType());
233 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
234 unsigned Align = getPreferredAlignmentLog(I);
236 if (C->isNullValue() &&
237 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
238 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
239 SwitchToDataSection("\t.section .data", I);
240 if (I->hasInternalLinkage())
241 O << "\t.local " << name << "\n";
243 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
244 << "," << (1 << Align)
247 switch (I->getLinkage()) {
248 case GlobalValue::LinkOnceLinkage:
249 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
250 // Nonnull linkonce -> weak
251 O << "\t.weak " << name << "\n";
252 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
253 SwitchToDataSection("", I);
255 case GlobalValue::AppendingLinkage:
256 // FIXME: appending linkage variables should go into a section of
257 // their name or something. For now, just emit them as external.
258 case GlobalValue::ExternalLinkage:
259 // If external or appending, declare as a global symbol
260 O << "\t.globl " << name << "\n";
262 case GlobalValue::InternalLinkage:
263 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
264 "\t.section .data", I);
266 case GlobalValue::GhostLinkage:
267 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
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);