blob: e51b78457bb886c3ecdab54ddb9e5fff481d956e [file] [log] [blame]
Andrew Lenharth01269522005-01-24 18:37:48 +00001//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to GAS-format Alpha assembly language.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Alpha.h"
16#include "AlphaInstrInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000017#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000019#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/AsmPrinter.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/Support/Mangler.h"
24#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000025#include <iostream>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026using namespace llvm;
27
28namespace {
29 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
30
31 struct AlphaAsmPrinter : public AsmPrinter {
32
33 /// Unique incrementer for label values for referencing Global values.
34 ///
35 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000036
37 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
Chris Lattner87744a22005-11-21 07:38:08 +000038 : AsmPrinter(o, tm), LabelNumber(0) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000039 AlignmentIsInBytes = false;
Chris Lattner81a994e2005-11-21 06:51:52 +000040 PrivateGlobalPrefix = "$";
Andrew Lenharth440e6882005-02-04 14:09:38 +000041 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000042
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().
46 ///
47 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000049 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050
51 virtual const char *getPassName() const {
52 return "Alpha Assembly Printer";
53 }
54 bool printInstruction(const MachineInstr *MI);
55 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000056 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
58 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000059 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000060 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
62 };
63} // end of anonymous namespace
64
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.
69///
70FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
71 TargetMachine &tm) {
72 return new AlphaAsmPrinter(o, tm);
73}
74
75#include "AlphaGenAsmWriter.inc"
76
Nate Begeman391c5d22005-11-30 18:54:35 +000077void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078{
79 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattnerea50fab2006-05-04 01:15:02 +000080 if (MO.getType() == MachineOperand::MO_VirtualRegister) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000081 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000082 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083 } else if (MO.isImmediate()) {
84 O << MO.getImmedValue();
85 } else {
86 printOp(MO);
87 }
88}
89
90
91void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
92 const MRegisterInfo &RI = *TM.getRegisterInfo();
93 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +000094
Andrew Lenharth304d0f32005-01-22 23:41:55 +000095 switch (MO.getType()) {
96 case MachineOperand::MO_VirtualRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +000097 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000098 return;
99
Chris Lattner63b3d712006-05-04 17:21:20 +0000100 case MachineOperand::MO_Immediate:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 std::cerr << "printOp() does not handle immediate values\n";
102 abort();
103 return;
104
Nate Begeman37efe672006-04-22 18:53:45 +0000105 case MachineOperand::MO_MachineBasicBlock:
106 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000107 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000108
109 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000110 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000111 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000112 return;
113
114 case MachineOperand::MO_ExternalSymbol:
115 O << MO.getSymbolName();
116 return;
117
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000118 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000119 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000120 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000121
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 default:
123 O << "<unknown operand type: " << MO.getType() << ">";
124 return;
125 }
126}
127
128/// printMachineInstruction -- Print out a single Alpha MI to
129/// the current output stream.
130///
131void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
132 ++EmittedInsts;
133 if (printInstruction(MI))
134 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000135
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000136 assert(0 && "Unhandled instruction in asm writer!");
137 abort();
138 return;
139}
140
141
142/// runOnMachineFunction - This uses the printMachineInstruction()
143/// method to print assembly for each instruction.
144///
145bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000146 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 O << "\n\n";
148
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000150 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151
152 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000153 const Function *F = MF.getFunction();
154 SwitchSection(".text", F);
155 EmitAlignment(4, F);
156 switch (F->getLinkage()) {
157 default: assert(0 && "Unknown linkage type!");
158 case Function::InternalLinkage: // Symbols default to internal.
159 break;
160 case Function::ExternalLinkage:
161 O << "\t.globl " << CurrentFnName << "\n";
162 break;
163 case Function::WeakLinkage:
164 case Function::LinkOnceLinkage:
165 O << "\t.weak " << CurrentFnName << "\n";
166 break;
167 }
168
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000169 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170
171 O << CurrentFnName << ":\n";
172
173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
175 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000176 printBasicBlockLabel(I, true);
177 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000178 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
179 II != E; ++II) {
180 // Print the assembly for the instruction.
181 O << "\t";
182 printMachineInstruction(II);
183 }
184 }
185 ++LabelNumber;
186
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000187 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000188
189 // We didn't modify anything.
190 return false;
191}
192
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193bool AlphaAsmPrinter::doInitialization(Module &M)
194{
195 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000196 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
197 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000198 O << "\t.arch ev6\n";
199 else
200 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000201 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000202 return false;
203}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000204
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000205bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000206 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000207
Chris Lattnere4d5c442005-03-15 04:54:21 +0000208 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000209 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000210 // Check to see if this is a special global used by LLVM, if so, emit it.
211 if (EmitSpecialLLVMGlobal(I))
212 continue;
213
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214 O << "\n\n";
215 std::string name = Mang->getValueName(I);
216 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000217 unsigned Size = TD->getTypeSize(C->getType());
218 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000219 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220
Misha Brukman4633f1c2005-04-21 23:13:11 +0000221 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
223 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner0a70a492005-11-21 07:30:28 +0000224 SwitchSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000225 if (I->hasInternalLinkage())
226 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000227
Owen Andersona69571c2006-05-03 01:29:57 +0000228 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000229 << "," << (1 << Align)
230 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000231 } else {
232 switch (I->getLinkage()) {
233 case GlobalValue::LinkOnceLinkage:
234 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
235 // Nonnull linkonce -> weak
236 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000237 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner0a70a492005-11-21 07:30:28 +0000238 SwitchSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000239 break;
240 case GlobalValue::AppendingLinkage:
241 // FIXME: appending linkage variables should go into a section of
242 // their name or something. For now, just emit them as external.
243 case GlobalValue::ExternalLinkage:
244 // If external or appending, declare as a global symbol
245 O << "\t.globl " << name << "\n";
246 // FALL THROUGH
247 case GlobalValue::InternalLinkage:
Chris Lattner0a70a492005-11-21 07:30:28 +0000248 SwitchSection(C->isNullValue() ? "\t.section .bss" :
249 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 break;
251 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000252 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 abort();
254 }
255
Chris Lattner8b8b9512005-11-21 07:51:23 +0000256 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 O << "\t.type " << name << ",@object\n";
258 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000259 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000260 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 }
262 }
263
264 AsmPrinter::doFinalization(M);
265 return false;
266}