blob: b078b68bd7f978de34046cae136df87bc6e35e00 [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);
80 if (MO.getType() == MachineOperand::MO_MachineRegister) {
81 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:
97 if (Value *V = MO.getVRegValueOrNull()) {
98 O << "<" << V->getName() << ">";
99 return;
100 }
101 // FALLTHROUGH
102 case MachineOperand::MO_MachineRegister:
103 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000104 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 return;
106
107 case MachineOperand::MO_SignExtendedImmed:
108 case MachineOperand::MO_UnextendedImmed:
109 std::cerr << "printOp() does not handle immediate values\n";
110 abort();
111 return;
112
113 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000114 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 abort();
116 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Nate Begeman37efe672006-04-22 18:53:45 +0000118 case MachineOperand::MO_MachineBasicBlock:
119 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000120 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000121
122 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000123 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000124 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 return;
126
127 case MachineOperand::MO_ExternalSymbol:
128 O << MO.getSymbolName();
129 return;
130
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000131 case MachineOperand::MO_GlobalAddress:
132 //Abuse PCrel to specify pcrel calls
133 //calls are the only thing that use this flag
Andrew Lenhartheececba2005-12-25 17:36:48 +0000134// if (MO.isPCRelative())
135// O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
136// else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000137 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000139
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 default:
141 O << "<unknown operand type: " << MO.getType() << ">";
142 return;
143 }
144}
145
146/// printMachineInstruction -- Print out a single Alpha MI to
147/// the current output stream.
148///
149void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
150 ++EmittedInsts;
151 if (printInstruction(MI))
152 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154 assert(0 && "Unhandled instruction in asm writer!");
155 abort();
156 return;
157}
158
159
160/// runOnMachineFunction - This uses the printMachineInstruction()
161/// method to print assembly for each instruction.
162///
163bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000164 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000165 O << "\n\n";
166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000168 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000169
170 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000171 const Function *F = MF.getFunction();
172 SwitchSection(".text", F);
173 EmitAlignment(4, F);
174 switch (F->getLinkage()) {
175 default: assert(0 && "Unknown linkage type!");
176 case Function::InternalLinkage: // Symbols default to internal.
177 break;
178 case Function::ExternalLinkage:
179 O << "\t.globl " << CurrentFnName << "\n";
180 break;
181 case Function::WeakLinkage:
182 case Function::LinkOnceLinkage:
183 O << "\t.weak " << CurrentFnName << "\n";
184 break;
185 }
186
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000187 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000188
189 O << CurrentFnName << ":\n";
190
191 // Print out code for the function.
192 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
193 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000194 printBasicBlockLabel(I, true);
195 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
197 II != E; ++II) {
198 // Print the assembly for the instruction.
199 O << "\t";
200 printMachineInstruction(II);
201 }
202 }
203 ++LabelNumber;
204
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000205 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206
207 // We didn't modify anything.
208 return false;
209}
210
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000211bool AlphaAsmPrinter::doInitialization(Module &M)
212{
213 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000214 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
215 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000216 O << "\t.arch ev6\n";
217 else
218 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000219 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220 return false;
221}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000222
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223bool AlphaAsmPrinter::doFinalization(Module &M) {
224 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000225
Chris Lattnere4d5c442005-03-15 04:54:21 +0000226 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000228 // Check to see if this is a special global used by LLVM, if so, emit it.
229 if (EmitSpecialLLVMGlobal(I))
230 continue;
231
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000232 O << "\n\n";
233 std::string name = Mang->getValueName(I);
234 Constant *C = I->getInitializer();
235 unsigned Size = TD.getTypeSize(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000236 // unsigned Align = TD.getTypeAlignmentShift(C->getType());
237 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
241 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner0a70a492005-11-21 07:30:28 +0000242 SwitchSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243 if (I->hasInternalLinkage())
244 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000245
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000247 << "," << (1 << Align)
248 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000249 } else {
250 switch (I->getLinkage()) {
251 case GlobalValue::LinkOnceLinkage:
252 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
253 // Nonnull linkonce -> weak
254 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner0a70a492005-11-21 07:30:28 +0000256 SwitchSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 break;
258 case GlobalValue::AppendingLinkage:
259 // FIXME: appending linkage variables should go into a section of
260 // their name or something. For now, just emit them as external.
261 case GlobalValue::ExternalLinkage:
262 // If external or appending, declare as a global symbol
263 O << "\t.globl " << name << "\n";
264 // FALL THROUGH
265 case GlobalValue::InternalLinkage:
Chris Lattner0a70a492005-11-21 07:30:28 +0000266 SwitchSection(C->isNullValue() ? "\t.section .bss" :
267 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 break;
269 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000270 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 abort();
272 }
273
Chris Lattner8b8b9512005-11-21 07:51:23 +0000274 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 O << "\t.type " << name << ",@object\n";
276 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000277 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000278 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279 }
280 }
281
282 AsmPrinter::doFinalization(M);
283 return false;
284}