blob: 6e36f1152344f1041d1b9ba54c61e3c4e1a7c394 [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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "asm-printer"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000016#include "Alpha.h"
17#include "AlphaInstrInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000018#include "AlphaTargetMachine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/Module.h"
Chris Lattner5b3a4552005-03-17 15:38:16 +000020#include "llvm/Type.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/Assembly/Writer.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000022#include "llvm/CodeGen/AsmPrinter.h"
Jim Laskey563321a2006-09-06 18:34:40 +000023#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner11fd2f12006-12-06 18:19:53 +000025#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000026#include "llvm/Support/Mangler.h"
27#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000028using namespace llvm;
29
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000033 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034
35 /// Unique incrementer for label values for referencing Global values.
36 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000037
Jim Laskeya0f3d172006-09-07 22:06:40 +000038 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
Andrew Lenharth0fb25902006-12-07 23:55:55 +000039 : AsmPrinter(o, tm, T) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000040 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000041
Andrew Lenharth304d0f32005-01-22 23:41:55 +000042 virtual const char *getPassName() const {
43 return "Alpha Assembly Printer";
44 }
45 bool printInstruction(const MachineInstr *MI);
46 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000047 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000048 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Misha Brukman4633f1c2005-04-21 23:13:11 +000049 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050 bool doInitialization(Module &M);
51 bool doFinalization(Module &M);
Chris Lattner6e796292006-10-05 02:47:13 +000052
Andrew Lenharth17255992006-06-21 13:37:27 +000053 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000055 bool PrintAsmMemoryOperand(const MachineInstr *MI,
56 unsigned OpNo,
57 unsigned AsmVariant,
58 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000059 };
60} // end of anonymous namespace
61
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.
66///
Chris Lattner6e796292006-10-05 02:47:13 +000067FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
68 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000069 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070}
71
72#include "AlphaGenAsmWriter.inc"
73
Nate Begeman391c5d22005-11-30 18:54:35 +000074void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075{
76 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000077 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000079 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 } else if (MO.isImmediate()) {
81 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000082 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083 } else {
84 printOp(MO);
85 }
86}
87
88
89void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
90 const MRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Andrew Lenharth304d0f32005-01-22 23:41:55 +000092 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000093 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +000094 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000095 return;
96
Chris Lattner63b3d712006-05-04 17:21:20 +000097 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +000098 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099 abort();
100 return;
101
Nate Begeman37efe672006-04-22 18:53:45 +0000102 case MachineOperand::MO_MachineBasicBlock:
103 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000104 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105
106 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000107 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000108 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000109 return;
110
111 case MachineOperand::MO_ExternalSymbol:
112 O << MO.getSymbolName();
113 return;
114
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000115 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000116 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000117 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000118
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000119 case MachineOperand::MO_JumpTableIndex:
120 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
121 << '_' << MO.getJumpTableIndex();
122 return;
123
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000124 default:
125 O << "<unknown operand type: " << MO.getType() << ">";
126 return;
127 }
128}
129
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130/// runOnMachineFunction - This uses the printMachineInstruction()
131/// method to print assembly for each instruction.
132///
133bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000134 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135 O << "\n\n";
136
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000138 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000140 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000141 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000144 const Function *F = MF.getFunction();
Chris Lattner6e796292006-10-05 02:47:13 +0000145 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
146
Andrew Lenharthfd895432006-02-04 19:13:09 +0000147 EmitAlignment(4, F);
148 switch (F->getLinkage()) {
149 default: assert(0 && "Unknown linkage type!");
150 case Function::InternalLinkage: // Symbols default to internal.
151 break;
152 case Function::ExternalLinkage:
153 O << "\t.globl " << CurrentFnName << "\n";
154 break;
155 case Function::WeakLinkage:
156 case Function::LinkOnceLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000157 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000158 break;
159 }
160
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000161 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162
163 O << CurrentFnName << ":\n";
164
165 // Print out code for the function.
166 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
167 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000168 if (I != MF.begin()) {
169 printBasicBlockLabel(I, true);
170 O << '\n';
171 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000172 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
173 II != E; ++II) {
174 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000175 ++EmittedInsts;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000176 O << "\t";
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000177 if (!printInstruction(II)) {
178 assert(0 && "Unhandled instruction in asm writer!");
179 abort();
180 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 }
182 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000183
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000184 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185
186 // We didn't modify anything.
187 return false;
188}
189
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000190bool AlphaAsmPrinter::doInitialization(Module &M)
191{
192 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000193 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
194 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000195 O << "\t.arch ev6\n";
196 else
197 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000198 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000199 return false;
200}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000201
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000202bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000203 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000204
Chris Lattnere4d5c442005-03-15 04:54:21 +0000205 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000207 // Check to see if this is a special global used by LLVM, if so, emit it.
208 if (EmitSpecialLLVMGlobal(I))
209 continue;
210
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000211 O << "\n\n";
212 std::string name = Mang->getValueName(I);
213 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000214 unsigned Size = TD->getTypeSize(C->getType());
215 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Devang Patelf9c197e2006-10-24 20:32:14 +0000216 unsigned Align = TD->getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000217
Misha Brukman4633f1c2005-04-21 23:13:11 +0000218 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000219 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
220 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000221 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 if (I->hasInternalLinkage())
223 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000224
Owen Andersona69571c2006-05-03 01:29:57 +0000225 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000226 << "," << (1 << Align)
227 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228 } else {
229 switch (I->getLinkage()) {
230 case GlobalValue::LinkOnceLinkage:
231 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
232 // Nonnull linkonce -> weak
233 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000234 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000235 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236 break;
237 case GlobalValue::AppendingLinkage:
238 // FIXME: appending linkage variables should go into a section of
239 // their name or something. For now, just emit them as external.
240 case GlobalValue::ExternalLinkage:
241 // If external or appending, declare as a global symbol
242 O << "\t.globl " << name << "\n";
243 // FALL THROUGH
244 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000245 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
246 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247 break;
248 case GlobalValue::GhostLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000249 cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000251 case GlobalValue::DLLImportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000252 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000253 abort();
254 case GlobalValue::DLLExportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000255 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000256 abort();
257 default:
258 assert(0 && "Unknown linkage type!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259 }
260
Chris Lattner8b8b9512005-11-21 07:51:23 +0000261 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 O << "\t.type " << name << ",@object\n";
263 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000264 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000265 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 }
267 }
268
Andrew Lenharth913ab052006-12-07 17:39:14 +0000269 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
270 if (I->hasExternalWeakLinkage()) {
271 O << "\n\n";
272 std::string name = Mang->getValueName(I);
273 O << "\t.weak " << name << "\n";
274 }
275
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276 AsmPrinter::doFinalization(M);
277 return false;
278}
Andrew Lenharth17255992006-06-21 13:37:27 +0000279
280/// PrintAsmOperand - Print out an operand for an inline asm expression.
281///
282bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
283 unsigned AsmVariant,
284 const char *ExtraCode) {
285 printOperand(MI, OpNo);
286 return false;
287}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000288
289bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
290 unsigned OpNo,
291 unsigned AsmVariant,
292 const char *ExtraCode) {
293 if (ExtraCode && ExtraCode[0])
294 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000295 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000296 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000297 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000298 return false;
299}