blob: a654be9849a9c3313f88dc6c8107ff0eb389f5fb [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Bill Wendlingcb819f12009-02-18 23:12:06 +000023#include "llvm/CodeGen/DwarfWriter.h"
Jim Laskey563321a2006-09-06 18:34:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner11fd2f12006-12-06 18:19:53 +000026#include "llvm/Support/Compiler.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000027#include "llvm/Support/Mangler.h"
Owen Andersoncb371882008-08-21 00:14:44 +000028#include "llvm/Support/raw_ostream.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000029#include "llvm/ADT/Statistic.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030using namespace llvm;
31
Chris Lattner95b2c7d2006-12-19 22:59:26 +000032STATISTIC(EmittedInsts, "Number of machine instrs printed");
33
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034namespace {
Jim Laskey563321a2006-09-06 18:34:40 +000035 struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036 /// Unique incrementer for label values for referencing Global values.
37 ///
Misha Brukman4633f1c2005-04-21 23:13:11 +000038
Bill Wendling57f0db82009-02-24 08:30:20 +000039 AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm,
40 const TargetAsmInfo *T, bool F)
41 : AsmPrinter(o, tm, T, F) {}
Andrew Lenharth304d0f32005-01-22 23:41:55 +000042
Andrew Lenharth304d0f32005-01-22 23:41:55 +000043 virtual const char *getPassName() const {
44 return "Alpha Assembly Printer";
45 }
46 bool printInstruction(const MachineInstr *MI);
47 void printOp(const MachineOperand &MO, bool IsCallOp = false);
Nate Begeman391c5d22005-11-30 18:54:35 +000048 void printOperand(const MachineInstr *MI, int opNum);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049 void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000050 void printModuleLevelGV(const GlobalVariable* GVar);
Misha Brukman4633f1c2005-04-21 23:13:11 +000051 bool runOnMachineFunction(MachineFunction &F);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000052 bool doInitialization(Module &M);
53 bool doFinalization(Module &M);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000054
Andrew Lenharth17255992006-06-21 13:37:27 +000055 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56 unsigned AsmVariant, const char *ExtraCode);
Anton Korobeynikov79579c92008-08-07 09:53:57 +000057 bool PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000058 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +000059 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +000060 const char *ExtraCode);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061 };
62} // end of anonymous namespace
63
64/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
65/// assembly code for a MachineFunction to the given output stream,
66/// using the given target machine description. This should work
67/// regardless of whether the function is in SSA form.
68///
Owen Andersoncb371882008-08-21 00:14:44 +000069FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
Bill Wendling57f0db82009-02-24 08:30:20 +000070 TargetMachine &tm,
71 bool fast) {
72 return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), fast);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000073}
74
75#include "AlphaGenAsmWriter.inc"
76
Nate Begeman391c5d22005-11-30 18:54:35 +000077void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078{
79 const MachineOperand &MO = MI->getOperand(opNum);
Chris Lattner2d90ac72006-05-04 18:05:43 +000080 if (MO.getType() == MachineOperand::MO_Register) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000081 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
82 "Not physreg??");
Bill Wendling74ab84c2008-02-26 21:11:01 +000083 O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
Dan Gohmand735b802008-10-03 15:45:36 +000084 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +000085 O << MO.getImm();
86 assert(MO.getImm() < (1 << 30));
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087 } else {
88 printOp(MO);
89 }
90}
91
92
93void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
Dan Gohman6f0d0242008-02-10 18:45:23 +000094 const TargetRegisterInfo &RI = *TM.getRegisterInfo();
Misha Brukman4633f1c2005-04-21 23:13:11 +000095
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +000097 case MachineOperand::MO_Register:
Bill Wendling74ab84c2008-02-26 21:11:01 +000098 O << RI.get(MO.getReg()).AsmName;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099 return;
100
Chris Lattner63b3d712006-05-04 17:21:20 +0000101 case MachineOperand::MO_Immediate:
Bill Wendlingf5da1332006-12-07 22:21:48 +0000102 cerr << "printOp() does not handle immediate values\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 abort();
104 return;
105
Nate Begeman37efe672006-04-22 18:53:45 +0000106 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000107 printBasicBlockLabel(MO.getMBB());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000108 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000109
110 case MachineOperand::MO_ConstantPoolIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000111 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000112 << MO.getIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000113 return;
114
115 case MachineOperand::MO_ExternalSymbol:
116 O << MO.getSymbolName();
117 return;
118
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000119 case MachineOperand::MO_GlobalAddress: {
120 GlobalValue *GV = MO.getGlobal();
121 O << Mang->getValueName(GV);
122 if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
123 ExtWeakSymbols.insert(GV);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000124 return;
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000125 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000126
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000127 case MachineOperand::MO_JumpTableIndex:
Evan Cheng347d39f2007-10-14 05:57:21 +0000128 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000129 << '_' << MO.getIndex();
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000130 return;
131
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000132 default:
133 O << "<unknown operand type: " << MO.getType() << ">";
134 return;
135 }
136}
137
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138/// runOnMachineFunction - This uses the printMachineInstruction()
139/// method to print assembly for each instruction.
140///
141bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000142 this->MF = &MF;
143
Chris Lattner8b8b9512005-11-21 07:51:23 +0000144 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 O << "\n\n";
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000148 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000150 // Print out jump tables referenced by the function
Chris Lattner1da31ee2006-10-05 03:01:21 +0000151 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Andrew Lenharthea4f9d52006-09-18 18:01:03 +0000152
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000154 const Function *F = MF.getFunction();
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000155 SwitchToSection(TAI->SectionForGlobal(F));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000156
Andrew Lenharthfd895432006-02-04 19:13:09 +0000157 EmitAlignment(4, F);
158 switch (F->getLinkage()) {
159 default: assert(0 && "Unknown linkage type!");
160 case Function::InternalLinkage: // Symbols default to internal.
Rafael Espindola19caec72009-01-15 21:51:46 +0000161 case Function::PrivateLinkage:
Andrew Lenharthfd895432006-02-04 19:13:09 +0000162 break;
163 case Function::ExternalLinkage:
164 O << "\t.globl " << CurrentFnName << "\n";
165 break;
Duncan Sands667d4b82009-03-07 15:45:40 +0000166 case Function::WeakAnyLinkage:
167 case Function::WeakODRLinkage:
168 case Function::LinkOnceAnyLinkage:
169 case Function::LinkOnceODRLinkage:
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000170 O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
Andrew Lenharthfd895432006-02-04 19:13:09 +0000171 break;
172 }
173
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000174 printVisibility(CurrentFnName, F->getVisibility());
175
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000176 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000177
178 O << CurrentFnName << ":\n";
179
180 // Print out code for the function.
181 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
182 I != E; ++I) {
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000183 if (I != MF.begin()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000184 printBasicBlockLabel(I, true, true);
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000185 O << '\n';
186 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
188 II != E; ++II) {
189 // Print the assembly for the instruction.
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000190 ++EmittedInsts;
Andrew Lenharth0fb25902006-12-07 23:55:55 +0000191 if (!printInstruction(II)) {
192 assert(0 && "Unhandled instruction in asm writer!");
193 abort();
194 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000195 }
196 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000198 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000199
200 // We didn't modify anything.
201 return false;
202}
203
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000204bool AlphaAsmPrinter::doInitialization(Module &M)
205{
Andrew Lenharth3553d862007-01-24 21:09:16 +0000206 if(TM.getSubtarget<AlphaSubtarget>().hasCT())
207 O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000208 else
Andrew Lenharth3553d862007-01-24 21:09:16 +0000209 O << "\t.arch ev6\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000210 O << "\t.set noat\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000211 return AsmPrinter::doInitialization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000212}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000213
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000214void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
Owen Andersona69571c2006-05-03 01:29:57 +0000215 const TargetData *TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000216
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000217 if (!GVar->hasInitializer()) return; // External global require no code
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000219 // Check to see if this is a special global used by LLVM, if so, emit it.
220 if (EmitSpecialLLVMGlobal(GVar))
221 return;
222
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000223 std::string name = Mang->getValueName(GVar);
224 Constant *C = GVar->getInitializer();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000225 unsigned Size = TD->getTypePaddedSize(C->getType());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000226 unsigned Align = TD->getPreferredAlignmentLog(GVar);
227
228 // 0: Switch to section
Anton Korobeynikovc25e1ea2008-09-24 22:14:23 +0000229 SwitchToSection(TAI->SectionForGlobal(GVar));
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000230
231 // 1: Check visibility
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +0000232 printVisibility(name, GVar->getVisibility());
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000233
234 // 2: Kind
235 switch (GVar->getLinkage()) {
Duncan Sands667d4b82009-03-07 15:45:40 +0000236 case GlobalValue::LinkOnceAnyLinkage:
237 case GlobalValue::LinkOnceODRLinkage:
238 case GlobalValue::WeakAnyLinkage:
239 case GlobalValue::WeakODRLinkage:
Duncan Sands4dc2b392009-03-11 20:14:15 +0000240 case GlobalValue::CommonLinkage:
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000241 O << TAI->getWeakRefDirective() << name << '\n';
242 break;
243 case GlobalValue::AppendingLinkage:
244 case GlobalValue::ExternalLinkage:
245 O << TAI->getGlobalDirective() << name << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000246 break;
247 case GlobalValue::InternalLinkage:
Rafael Espindola19caec72009-01-15 21:51:46 +0000248 case GlobalValue::PrivateLinkage:
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000249 break;
250 default:
251 assert(0 && "Unknown linkage type!");
252 cerr << "Unknown linkage type!\n";
253 abort();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254 }
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000255
256 // 3: Type, Size, Align
257 if (TAI->hasDotTypeDotSizeDirective()) {
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000258 O << "\t.type\t" << name << ", @object\n";
259 O << "\t.size\t" << name << ", " << Size << "\n";
Andrew Lenharth921a83a2007-02-13 23:41:16 +0000260 }
Andrew Lenharth913ab052006-12-07 17:39:14 +0000261
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000262 EmitAlignment(Align, GVar);
263
264 O << name << ":\n";
265
266 // If the initializer is a extern weak symbol, remember to emit the weak
267 // reference!
268 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
269 if (GV->hasExternalWeakLinkage())
270 ExtWeakSymbols.insert(GV);
271
272 EmitGlobalConstant(C);
273 O << '\n';
274}
275
276bool AlphaAsmPrinter::doFinalization(Module &M) {
277 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
278 I != E; ++I)
279 printModuleLevelGV(I);
280
Dan Gohmanb8275a32007-07-25 19:33:14 +0000281 return AsmPrinter::doFinalization(M);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000282}
Andrew Lenharth17255992006-06-21 13:37:27 +0000283
284/// PrintAsmOperand - Print out an operand for an inline asm expression.
285///
286bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000287 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000288 const char *ExtraCode) {
Andrew Lenharth17255992006-06-21 13:37:27 +0000289 printOperand(MI, OpNo);
290 return false;
291}
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000292
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000293bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000294 unsigned OpNo,
Anton Korobeynikov79579c92008-08-07 09:53:57 +0000295 unsigned AsmVariant,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000296 const char *ExtraCode) {
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000297 if (ExtraCode && ExtraCode[0])
298 return true; // Unknown modifier.
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000299 O << "0(";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000300 printOperand(MI, OpNo);
Andrew Lenharth78c252c2006-07-03 17:57:34 +0000301 O << ")";
Andrew Lenharthdf97cc62006-06-21 15:42:36 +0000302 return false;
303}