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 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
17 #include "AlphaInstrInfo.h"
18 #include "AlphaTargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
33 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
35 /// Unique incrementer for label values for referencing Global values.
38 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39 : AsmPrinter(o, tm, T) {
42 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
47 void printOperand(const MachineInstr *MI, int opNum);
48 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
49 bool runOnMachineFunction(MachineFunction &F);
50 bool doInitialization(Module &M);
51 bool doFinalization(Module &M);
53 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
55 bool PrintAsmMemoryOperand(const MachineInstr *MI,
58 const char *ExtraCode);
60 } // end of anonymous namespace
62 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
63 /// assembly code for a MachineFunction to the given output stream,
64 /// using the given target machine description. This should work
65 /// regardless of whether the function is in SSA form.
67 FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
69 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
72 #include "AlphaGenAsmWriter.inc"
74 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
76 const MachineOperand &MO = MI->getOperand(opNum);
77 if (MO.getType() == MachineOperand::MO_Register) {
78 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
80 } else if (MO.isImmediate()) {
81 O << MO.getImmedValue();
82 assert(MO.getImmedValue() < (1 << 30));
89 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
90 const MRegisterInfo &RI = *TM.getRegisterInfo();
92 switch (MO.getType()) {
93 case MachineOperand::MO_Register:
94 O << RI.get(MO.getReg()).Name;
97 case MachineOperand::MO_Immediate:
98 cerr << "printOp() does not handle immediate values\n";
102 case MachineOperand::MO_MachineBasicBlock:
103 printBasicBlockLabel(MO.getMachineBasicBlock());
106 case MachineOperand::MO_ConstantPoolIndex:
107 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
108 << MO.getConstantPoolIndex();
111 case MachineOperand::MO_ExternalSymbol:
112 O << MO.getSymbolName();
115 case MachineOperand::MO_GlobalAddress: {
116 GlobalValue *GV = MO.getGlobal();
117 O << Mang->getValueName(GV);
118 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
119 ExtWeakSymbols.insert(GV);
123 case MachineOperand::MO_JumpTableIndex:
124 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
125 << '_' << MO.getJumpTableIndex();
129 O << "<unknown operand type: " << MO.getType() << ">";
134 /// runOnMachineFunction - This uses the printMachineInstruction()
135 /// method to print assembly for each instruction.
137 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
138 SetupMachineFunction(MF);
141 // Print out constants referenced by the function
142 EmitConstantPool(MF.getConstantPool());
144 // Print out jump tables referenced by the function
145 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
147 // Print out labels for the function.
148 const Function *F = MF.getFunction();
149 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
152 switch (F->getLinkage()) {
153 default: assert(0 && "Unknown linkage type!");
154 case Function::InternalLinkage: // Symbols default to internal.
156 case Function::ExternalLinkage:
157 O << "\t.globl " << CurrentFnName << "\n";
159 case Function::WeakLinkage:
160 case Function::LinkOnceLinkage:
161 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
165 O << "\t.ent " << CurrentFnName << "\n";
167 O << CurrentFnName << ":\n";
169 // Print out code for the function.
170 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
172 if (I != MF.begin()) {
173 printBasicBlockLabel(I, true);
176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
178 // Print the assembly for the instruction.
181 if (!printInstruction(II)) {
182 assert(0 && "Unhandled instruction in asm writer!");
188 O << "\t.end " << CurrentFnName << "\n";
190 // We didn't modify anything.
194 bool AlphaAsmPrinter::doInitialization(Module &M)
196 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
197 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
199 O << "\t.arch ev6\n";
200 O << "\t.set noat\n";
201 AsmPrinter::doInitialization(M);
205 bool AlphaAsmPrinter::doFinalization(Module &M) {
206 const TargetData *TD = TM.getTargetData();
208 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
210 if (!I->hasInitializer()) continue; // External global require no code
212 // Check to see if this is a special global used by LLVM, if so, emit it.
213 if (EmitSpecialLLVMGlobal(I))
216 std::string name = Mang->getValueName(I);
217 Constant *C = I->getInitializer();
218 unsigned Size = TD->getTypeSize(C->getType());
219 unsigned Align = TD->getPreferredAlignmentLog(I);
222 if (I->hasHiddenVisibility())
223 O << TAI->getHiddenDirective() << name << "\n";
226 switch (I->getLinkage()) {
227 case GlobalValue::LinkOnceLinkage:
228 case GlobalValue::WeakLinkage:
229 O << TAI->getWeakRefDirective() << name << '\n';
231 case GlobalValue::AppendingLinkage:
232 case GlobalValue::ExternalLinkage:
233 O << "\t.globl " << name << "\n";
235 case GlobalValue::InternalLinkage:
238 assert(0 && "Unknown linkage type!");
239 cerr << "Unknown linkage type!\n";
243 //3: Section (if changed)
244 if (I->hasSection() &&
245 (I->getSection() == ".ctors" ||
246 I->getSection() == ".dtors")) {
247 std::string SectionName = ".section\t" + I->getSection()
248 + ",\"aw\",@progbits";
249 SwitchToDataSection(SectionName.c_str());
251 if (C->isNullValue())
252 SwitchToDataSection("\t.section\t.bss", I);
254 SwitchToDataSection("\t.section\t.data", I);
257 //4: Type, Size, Align
258 O << "\t.type\t" << name << ", @object\n";
259 O << "\t.size\t" << name << ", " << Size << "\n";
260 EmitAlignment(Align, I);
264 // If the initializer is a extern weak symbol, remember to emit the weak
266 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
267 if (GV->hasExternalWeakLinkage())
268 ExtWeakSymbols.insert(GV);
270 EmitGlobalConstant(C);
274 AsmPrinter::doFinalization(M);
278 /// PrintAsmOperand - Print out an operand for an inline asm expression.
280 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
282 const char *ExtraCode) {
283 printOperand(MI, OpNo);
287 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
290 const char *ExtraCode) {
291 if (ExtraCode && ExtraCode[0])
292 return true; // Unknown modifier.
294 printOperand(MI, OpNo);