blob: 03b9469efb5e3b8635117c2a8774e61dcc012fb4 [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 Lenharth3553d862007-01-24 21:09:16 +0000193 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
194 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000195 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000196 O << "\t.arch ev6\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());
Evan Chengde268f72007-01-24 07:03:39 +0000214 // unsigned Align = TD->getPreferredTypeAlignmentShift(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}