blob: d709ed7342ccb1663c2210a0123683782af64ed5 [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"
Jim Laskey563321a2006-09-06 18:34:40 +000022#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000023#include "llvm/Target/TargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024#include "llvm/Support/Mangler.h"
25#include "llvm/ADT/Statistic.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000026#include <iostream>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027using namespace llvm;
28
29namespace {
30 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
Jim Laskey563321a2006-09-06 18:34:40 +000031
32 struct VISIBILITY_HIDDEN AlphaTargetAsmInfo : public TargetAsmInfo {
33 AlphaTargetAsmInfo() {
34 AlignmentIsInBytes = false;
35 PrivateGlobalPrefix = "$";
36 }
37 };
Andrew Lenharth304d0f32005-01-22 23:41:55 +000038
Jim Laskey563321a2006-09-06 18:34:40 +000039 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000040
41 /// Unique incrementer for label values for referencing Global values.
42 ///
43 unsigned LabelNumber;
Misha Brukman4633f1c2005-04-21 23:13:11 +000044
Jim Laskey563321a2006-09-06 18:34:40 +000045 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, TargetAsmInfo *T)
46 : AsmPrinter(o, tm, T), LabelNumber(0) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000047 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000048
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().
52 ///
53 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
Andrew Lenharthc24b5372005-04-13 17:17:28 +000055 std::string CurSection;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056
57 virtual const char *getPassName() const {
58 return "Alpha Assembly Printer";
59 }
60 bool printInstruction(const MachineInstr *MI);
61 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000062 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000063 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
64 void printMachineInstruction(const MachineInstr *MI);
Misha Brukman4633f1c2005-04-21 23:13:11 +000065 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000066 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
Andrew Lenharth17255992006-06-21 13:37:27 +000068
69 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
70 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000071 bool PrintAsmMemoryOperand(const MachineInstr *MI,
72 unsigned OpNo,
73 unsigned AsmVariant,
74 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075 };
76} // end of anonymous namespace
77
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.
82///
83FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
84 TargetMachine &tm) {
Jim Laskey563321a2006-09-06 18:34:40 +000085 AlphaTargetAsmInfo *TAI = new AlphaTargetAsmInfo();
86 return new AlphaAsmPrinter(o, tm, TAI);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087}
88
89#include "AlphaGenAsmWriter.inc"
90
Nate Begeman391c5d22005-11-30 18:54:35 +000091void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000092{
93 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000094 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000095 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000096 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000097 } else if (MO.isImmediate()) {
98 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000099 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000100 } else {
101 printOp(MO);
102 }
103}
104
105
106void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
107 const MRegisterInfo &RI = *TM.getRegisterInfo();
108 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000109
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000111 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +0000112 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000113 return;
114
Chris Lattner63b3d712006-05-04 17:21:20 +0000115 case MachineOperand::MO_Immediate:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 std::cerr << "printOp() does not handle immediate values\n";
117 abort();
118 return;
119
Nate Begeman37efe672006-04-22 18:53:45 +0000120 case MachineOperand::MO_MachineBasicBlock:
121 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000123
124 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000125 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000126 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000127 return;
128
129 case MachineOperand::MO_ExternalSymbol:
130 O << MO.getSymbolName();
131 return;
132
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000133 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000134 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000136
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137 default:
138 O << "<unknown operand type: " << MO.getType() << ">";
139 return;
140 }
141}
142
143/// printMachineInstruction -- Print out a single Alpha MI to
144/// the current output stream.
145///
146void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
147 ++EmittedInsts;
148 if (printInstruction(MI))
149 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000150
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151 assert(0 && "Unhandled instruction in asm writer!");
152 abort();
153 return;
154}
155
156
157/// runOnMachineFunction - This uses the printMachineInstruction()
158/// method to print assembly for each instruction.
159///
160bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000161 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 O << "\n\n";
163
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000165 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000166
167 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000168 const Function *F = MF.getFunction();
Chris Lattner4632d7a2006-05-09 04:59:56 +0000169 SwitchToTextSection(".text", F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000170 EmitAlignment(4, F);
171 switch (F->getLinkage()) {
172 default: assert(0 && "Unknown linkage type!");
173 case Function::InternalLinkage: // Symbols default to internal.
174 break;
175 case Function::ExternalLinkage:
176 O << "\t.globl " << CurrentFnName << "\n";
177 break;
178 case Function::WeakLinkage:
179 case Function::LinkOnceLinkage:
180 O << "\t.weak " << CurrentFnName << "\n";
181 break;
182 }
183
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000184 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185
186 O << CurrentFnName << ":\n";
187
188 // Print out code for the function.
189 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
190 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000191 printBasicBlockLabel(I, true);
192 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
194 II != E; ++II) {
195 // Print the assembly for the instruction.
196 O << "\t";
197 printMachineInstruction(II);
198 }
199 }
200 ++LabelNumber;
201
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000202 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000203
204 // We didn't modify anything.
205 return false;
206}
207
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208bool AlphaAsmPrinter::doInitialization(Module &M)
209{
210 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000211 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
212 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000213 O << "\t.arch ev6\n";
214 else
215 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000216 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000217 return false;
218}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000219
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000221 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000222
Chris Lattnere4d5c442005-03-15 04:54:21 +0000223 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000224 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000225 // Check to see if this is a special global used by LLVM, if so, emit it.
226 if (EmitSpecialLLVMGlobal(I))
227 continue;
228
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229 O << "\n\n";
230 std::string name = Mang->getValueName(I);
231 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000232 unsigned Size = TD->getTypeSize(C->getType());
233 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000234 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235
Misha Brukman4633f1c2005-04-21 23:13:11 +0000236 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000237 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
238 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000239 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240 if (I->hasInternalLinkage())
241 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000242
Owen Andersona69571c2006-05-03 01:29:57 +0000243 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000244 << "," << (1 << Align)
245 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 } else {
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";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000253 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254 break;
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";
261 // FALL THROUGH
262 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000263 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
264 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265 break;
266 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000267 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 abort();
269 }
270
Chris Lattner8b8b9512005-11-21 07:51:23 +0000271 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 O << "\t.type " << name << ",@object\n";
273 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000274 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000275 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276 }
277 }
278
279 AsmPrinter::doFinalization(M);
280 return false;
281}
Andrew Lenharth17255992006-06-21 13:37:27 +0000282
283/// PrintAsmOperand - Print out an operand for an inline asm expression.
284///
285bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
286 unsigned AsmVariant,
287 const char *ExtraCode) {
288 printOperand(MI, OpNo);
289 return false;
290}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000291
292bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
293 unsigned OpNo,
294 unsigned AsmVariant,
295 const char *ExtraCode) {
296 if (ExtraCode && ExtraCode[0])
297 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000298 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000299 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000300 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000301 return false;
302}