blob: be8973a337a261f67e52f40e97aa82ba75f55b96 [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"
Chris Lattner11fd2f12006-12-06 18:19:53 +000024#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000025#include "llvm/Support/Mangler.h"
26#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027using namespace llvm;
28
29namespace {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030 Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
Jim Laskey563321a2006-09-06 18:34:40 +000031
Jim Laskey563321a2006-09-06 18:34:40 +000032 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033
34 /// Unique incrementer for label values for referencing Global values.
35 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000036
Jim Laskeya0f3d172006-09-07 22:06:40 +000037 AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
Andrew Lenharth0fb25902006-12-07 23:55:55 +000038 : AsmPrinter(o, tm, T) {
Andrew Lenharth440e6882005-02-04 14:09:38 +000039 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +000040
Andrew Lenharth304d0f32005-01-22 23:41:55 +000041 virtual const char *getPassName() const {
42 return "Alpha Assembly Printer";
43 }
44 bool printInstruction(const MachineInstr *MI);
45 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000046 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Misha Brukman4633f1c2005-04-21 23:13:11 +000048 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049 bool doInitialization(Module &M);
50 bool doFinalization(Module &M);
Chris Lattner6e796292006-10-05 02:47:13 +000051
Andrew Lenharth17255992006-06-21 13:37:27 +000052 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53 unsigned AsmVariant, const char *ExtraCode);
Andrew Lenharthdf97cc62006-06-21 15:42:36 +000054 bool PrintAsmMemoryOperand(const MachineInstr *MI,
55 unsigned OpNo,
56 unsigned AsmVariant,
57 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000058 };
59} // end of anonymous namespace
60
61/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
62/// assembly code for a MachineFunction to the given output stream,
63/// using the given target machine description. This should work
64/// regardless of whether the function is in SSA form.
65///
Chris Lattner6e796292006-10-05 02:47:13 +000066FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
67 TargetMachine &tm) {
Jim Laskeya0f3d172006-09-07 22:06:40 +000068 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069}
70
71#include "AlphaGenAsmWriter.inc"
72
Nate Begeman391c5d22005-11-30 18:54:35 +000073void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000074{
75 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000076 if (MO.getType() == MachineOperand::MO_Register) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000077 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000078 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000079 } else if (MO.isImmediate()) {
80 O << MO.getImmedValue();
Andrew Lenharth3aa92e42006-05-17 19:24:31 +000081 assert(MO.getImmedValue() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000082 } else {
83 printOp(MO);
84 }
85}
86
87
88void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
89 const MRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000090
Andrew Lenharth304d0f32005-01-22 23:41:55 +000091 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000092 case MachineOperand::MO_Register:
Andrew Lenharth01269522005-01-24 18:37:48 +000093 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 return;
95
Chris Lattner63b3d712006-05-04 17:21:20 +000096 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +000097 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +000098 abort();
99 return;
100
Nate Begeman37efe672006-04-22 18:53:45 +0000101 case MachineOperand::MO_MachineBasicBlock:
102 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000104
105 case MachineOperand::MO_ConstantPoolIndex:
Jim Laskey563321a2006-09-06 18:34:40 +0000106 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000107 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000108 return;
109
110 case MachineOperand::MO_ExternalSymbol:
111 O << MO.getSymbolName();
112 return;
113
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000114 case MachineOperand::MO_GlobalAddress:
Chris Lattner34fb2ca2006-05-04 00:49:59 +0000115 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000116 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000118 case MachineOperand::MO_JumpTableIndex:
119 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
120 << '_' << MO.getJumpTableIndex();
121 return;
122
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000123 default:
124 O << "<unknown operand type: " << MO.getType() << ">";
125 return;
126 }
127}
128
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129/// runOnMachineFunction - This uses the printMachineInstruction()
130/// method to print assembly for each instruction.
131///
132bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000133 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000134 O << "\n\n";
135
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000136 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000137 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000139 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000140 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000141
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000143 const Function *F = MF.getFunction();
Chris Lattner6e796292006-10-05 02:47:13 +0000144 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
145
Andrew Lenharthfd895432006-02-04 19:13:09 +0000146 EmitAlignment(4, F);
147 switch (F->getLinkage()) {
148 default: assert(0 && "Unknown linkage type!");
149 case Function::InternalLinkage: // Symbols default to internal.
150 break;
151 case Function::ExternalLinkage:
152 O << "\t.globl " << CurrentFnName << "\n";
153 break;
154 case Function::WeakLinkage:
155 case Function::LinkOnceLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000156 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000157 break;
158 }
159
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000160 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161
162 O << CurrentFnName << ":\n";
163
164 // Print out code for the function.
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
166 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000167 if (I != MF.begin()) {
168 printBasicBlockLabel(I, true);
169 O << '\n';
170 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000171 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
172 II != E; ++II) {
173 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000174 ++EmittedInsts;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000175 O << "\t";
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000176 if (!printInstruction(II)) {
177 assert(0 && "Unhandled instruction in asm writer!");
178 abort();
179 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180 }
181 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000182
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000183 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000184
185 // We didn't modify anything.
186 return false;
187}
188
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189bool AlphaAsmPrinter::doInitialization(Module &M)
190{
191 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000192 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
193 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000194 O << "\t.arch ev6\n";
195 else
196 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000197 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000198 return false;
199}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000200
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000201bool AlphaAsmPrinter::doFinalization(Module &M) {
Owen Andersona69571c2006-05-03 01:29:57 +0000202 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000203
Chris Lattnere4d5c442005-03-15 04:54:21 +0000204 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000205 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000206 // Check to see if this is a special global used by LLVM, if so, emit it.
207 if (EmitSpecialLLVMGlobal(I))
208 continue;
209
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000210 O << "\n\n";
211 std::string name = Mang->getValueName(I);
212 Constant *C = I->getInitializer();
Owen Andersona69571c2006-05-03 01:29:57 +0000213 unsigned Size = TD->getTypeSize(C->getType());
214 // unsigned Align = TD->getTypeAlignmentShift(C->getType());
Devang Patelf9c197e2006-10-24 20:32:14 +0000215 unsigned Align = TD->getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000216
Misha Brukman4633f1c2005-04-21 23:13:11 +0000217 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
219 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner4632d7a2006-05-09 04:59:56 +0000220 SwitchToDataSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000221 if (I->hasInternalLinkage())
222 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000223
Owen Andersona69571c2006-05-03 01:29:57 +0000224 O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000225 << "," << (1 << Align)
226 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227 } else {
228 switch (I->getLinkage()) {
229 case GlobalValue::LinkOnceLinkage:
230 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
231 // Nonnull linkonce -> weak
232 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner4632d7a2006-05-09 04:59:56 +0000234 SwitchToDataSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235 break;
236 case GlobalValue::AppendingLinkage:
237 // FIXME: appending linkage variables should go into a section of
238 // their name or something. For now, just emit them as external.
239 case GlobalValue::ExternalLinkage:
240 // If external or appending, declare as a global symbol
241 O << "\t.globl " << name << "\n";
242 // FALL THROUGH
243 case GlobalValue::InternalLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000244 SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
245 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 break;
247 case GlobalValue::GhostLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000248 cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000249 abort();
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000250 case GlobalValue::DLLImportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000251 cerr << "DLLImport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 abort();
253 case GlobalValue::DLLExportLinkage:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000254 cerr << "DLLExport linkage is not supported by this target!\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000255 abort();
256 default:
257 assert(0 && "Unknown linkage type!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 }
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
Andrew Lenharth913ab052006-12-07 17:39:14 +0000268 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
269 if (I->hasExternalWeakLinkage()) {
270 O << "\n\n";
271 std::string name = Mang->getValueName(I);
272 O << "\t.weak " << name << "\n";
273 }
274
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 AsmPrinter::doFinalization(M);
276 return false;
277}
Andrew Lenharth17255992006-06-21 13:37:27 +0000278
279/// PrintAsmOperand - Print out an operand for an inline asm expression.
280///
281bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
282 unsigned AsmVariant,
283 const char *ExtraCode) {
284 printOperand(MI, OpNo);
285 return false;
286}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000287
288bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
289 unsigned OpNo,
290 unsigned AsmVariant,
291 const char *ExtraCode) {
292 if (ExtraCode && ExtraCode[0])
293 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000294 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000295 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000296 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000297 return false;
298}