blob: 634aa01bc92a12d97ef508a536ae54219db6dcea [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);
Andrew Lenharth17255992006-06-21 13:37:27 +000062
63 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
64 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000065 };
66} // end of anonymous namespace
67
68/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
69/// assembly code for a MachineFunction to the given output stream,
70/// using the given target machine description. This should work
71/// regardless of whether the function is in SSA form.
72///
73FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
74 TargetMachine &tm) {
75 return new AlphaAsmPrinter(o, tm);
76}
77
78#include "AlphaGenAsmWriter.inc"
79
Nate Begeman391c5d22005-11-30 18:54:35 +000080void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000081{
82 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000083 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000084 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000085 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086 } else if (MO.isImmediate()) {
87 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000088 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 } else {
90 printOp(MO);
91 }
92}
93
94
95void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
96 const MRegisterInfo &RI = *TM.getRegisterInfo();
97 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +000098
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000100 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +0000101 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 return;
103
Chris Lattner63b3d712006-05-04 17:21:20 +0000104 case MachineOperand::MO_Immediate:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 std::cerr << "printOp() does not handle immediate values\n";
106 abort();
107 return;
108
Nate Begeman37efe672006-04-22 18:53:45 +0000109 case MachineOperand::MO_MachineBasicBlock:
110 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000112
113 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000114 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000115 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 return;
117
118 case MachineOperand::MO_ExternalSymbol:
119 O << MO.getSymbolName();
120 return;
121
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000122 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000123 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000124 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000125
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000126 default:
127 O << "<unknown operand type: " << MO.getType() << ">";
128 return;
129 }
130}
131
132/// printMachineInstruction -- Print out a single Alpha MI to
133/// the current output stream.
134///
135void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
136 ++EmittedInsts;
137 if (printInstruction(MI))
138 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000139
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 assert(0 && "Unhandled instruction in asm writer!");
141 abort();
142 return;
143}
144
145
146/// runOnMachineFunction - This uses the printMachineInstruction()
147/// method to print assembly for each instruction.
148///
149bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000150 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151 O << "\n\n";
152
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000154 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155
156 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000157 const Function *F = MF.getFunction();
Chris Lattner4632d7a2006-05-09 04:59:56 +0000158 SwitchToTextSection(".text", F);
Andrew Lenharthfd895432006-02-04 19:13:09 +0000159 EmitAlignment(4, F);
160 switch (F->getLinkage()) {
161 default: assert(0 && "Unknown linkage type!");
162 case Function::InternalLinkage: // Symbols default to internal.
163 break;
164 case Function::ExternalLinkage:
165 O << "\t.globl " << CurrentFnName << "\n";
166 break;
167 case Function::WeakLinkage:
168 case Function::LinkOnceLinkage:
169 O << "\t.weak " << CurrentFnName << "\n";
170 break;
171 }
172
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000173 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000174
175 O << CurrentFnName << ":\n";
176
177 // Print out code for the function.
178 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
179 I != E; ++I) {
Nate Begemancdf38c42006-05-02 05:37:32 +0000180 printBasicBlockLabel(I, true);
181 O << '\n';
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000182 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
183 II != E; ++II) {
184 // Print the assembly for the instruction.
185 O << "\t";
186 printMachineInstruction(II);
187 }
188 }
189 ++LabelNumber;
190
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000191 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000192
193 // We didn't modify anything.
194 return false;
195}
196
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197bool AlphaAsmPrinter::doInitialization(Module &M)
198{
199 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000200 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
201 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000202 O << "\t.arch ev6\n";
203 else
204 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000205 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206 return false;
207}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000208
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000209bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000210 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000211
Chris Lattnere4d5c442005-03-15 04:54:21 +0000212 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000213 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000214 // Check to see if this is a special global used by LLVM, if so, emit it.
215 if (EmitSpecialLLVMGlobal(I))
216 continue;
217
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 O << "\n\n";
219 std::string name = Mang->getValueName(I);
220 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000221 unsigned Size = TD->getTypeSize(C->getType());
222 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000223 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000224
Misha Brukman4633f1c2005-04-21 23:13:11 +0000225 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000226 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
227 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000228 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229 if (I->hasInternalLinkage())
230 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000231
Owen Andersona69571c2006-05-03 01:29:57 +0000232 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000233 << "," << (1 << Align)
234 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235 } else {
236 switch (I->getLinkage()) {
237 case GlobalValue::LinkOnceLinkage:
238 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
239 // Nonnull linkonce -> weak
240 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000242 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243 break;
244 case GlobalValue::AppendingLinkage:
245 // FIXME: appending linkage variables should go into a section of
246 // their name or something. For now, just emit them as external.
247 case GlobalValue::ExternalLinkage:
248 // If external or appending, declare as a global symbol
249 O << "\t.globl " << name << "\n";
250 // FALL THROUGH
251 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000252 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
253 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254 break;
255 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000256 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 abort();
258 }
259
Chris Lattner8b8b9512005-11-21 07:51:23 +0000260 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 O << "\t.type " << name << ",@object\n";
262 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000263 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000264 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265 }
266 }
267
268 AsmPrinter::doFinalization(M);
269 return false;
270}
Andrew Lenharth17255992006-06-21 13:37:27 +0000271
272/// PrintAsmOperand - Print out an operand for an inline asm expression.
273///
274bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
275 unsigned AsmVariant,
276 const char *ExtraCode) {
277 printOperand(MI, OpNo);
278 return false;
279}