blob: 5d1f522ca0aeee9c431f722c9ec0eec2aca01af7 [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);
62 };
63} // end of anonymous namespace
64
65/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
66/// assembly code for a MachineFunction to the given output stream,
67/// using the given target machine description. This should work
68/// regardless of whether the function is in SSA form.
69///
70FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
71 TargetMachine &tm) {
72 return new AlphaAsmPrinter(o, tm);
73}
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);
80 if (MO.getType() == MachineOperand::MO_MachineRegister) {
81 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
Andrew Lenharth01269522005-01-24 18:37:48 +000082 O << TM.getRegisterInfo()->get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083 } else if (MO.isImmediate()) {
84 O << MO.getImmedValue();
85 } else {
86 printOp(MO);
87 }
88}
89
90
91void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
92 const MRegisterInfo &RI = *TM.getRegisterInfo();
93 int new_symbol;
Misha Brukman4633f1c2005-04-21 23:13:11 +000094
Andrew Lenharth304d0f32005-01-22 23:41:55 +000095 switch (MO.getType()) {
96 case MachineOperand::MO_VirtualRegister:
97 if (Value *V = MO.getVRegValueOrNull()) {
98 O << "<" << V->getName() << ">";
99 return;
100 }
101 // FALLTHROUGH
102 case MachineOperand::MO_MachineRegister:
103 case MachineOperand::MO_CCRegister:
Andrew Lenharth01269522005-01-24 18:37:48 +0000104 O << RI.get(MO.getReg()).Name;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 return;
106
107 case MachineOperand::MO_SignExtendedImmed:
108 case MachineOperand::MO_UnextendedImmed:
109 std::cerr << "printOp() does not handle immediate values\n";
110 abort();
111 return;
112
113 case MachineOperand::MO_PCRelativeDisp:
Andrew Lenharth01269522005-01-24 18:37:48 +0000114 std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 abort();
116 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Nate Begeman37efe672006-04-22 18:53:45 +0000118 case MachineOperand::MO_MachineBasicBlock:
119 printBasicBlockLabel(MO.getMachineBasicBlock());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000120 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000121
122 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000123 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000124 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 return;
126
127 case MachineOperand::MO_ExternalSymbol:
128 O << MO.getSymbolName();
129 return;
130
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000131 case MachineOperand::MO_GlobalAddress:
132 //Abuse PCrel to specify pcrel calls
133 //calls are the only thing that use this flag
Andrew Lenhartheececba2005-12-25 17:36:48 +0000134// if (MO.isPCRelative())
135// O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
136// else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000137 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000139
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 default:
141 O << "<unknown operand type: " << MO.getType() << ">";
142 return;
143 }
144}
145
146/// printMachineInstruction -- Print out a single Alpha MI to
147/// the current output stream.
148///
149void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
150 ++EmittedInsts;
151 if (printInstruction(MI))
152 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154 assert(0 && "Unhandled instruction in asm writer!");
155 abort();
156 return;
157}
158
159
160/// runOnMachineFunction - This uses the printMachineInstruction()
161/// method to print assembly for each instruction.
162///
163bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000164 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000165 O << "\n\n";
166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000168 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000169
170 // Print out labels for the function.
Andrew Lenharthfd895432006-02-04 19:13:09 +0000171 const Function *F = MF.getFunction();
172 SwitchSection(".text", F);
173 EmitAlignment(4, F);
174 switch (F->getLinkage()) {
175 default: assert(0 && "Unknown linkage type!");
176 case Function::InternalLinkage: // Symbols default to internal.
177 break;
178 case Function::ExternalLinkage:
179 O << "\t.globl " << CurrentFnName << "\n";
180 break;
181 case Function::WeakLinkage:
182 case Function::LinkOnceLinkage:
183 O << "\t.weak " << CurrentFnName << "\n";
184 break;
185 }
186
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000187 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000188
189 O << CurrentFnName << ":\n";
190
191 // Print out code for the function.
192 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
193 I != E; ++I) {
194 // Print a label for the basic block.
Chris Lattner87744a22005-11-21 07:38:08 +0000195 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
196 << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
198 II != E; ++II) {
199 // Print the assembly for the instruction.
200 O << "\t";
201 printMachineInstruction(II);
202 }
203 }
204 ++LabelNumber;
205
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000206 O << "\t.end " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000207
208 // We didn't modify anything.
209 return false;
210}
211
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000212bool AlphaAsmPrinter::doInitialization(Module &M)
213{
214 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000215 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
216 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000217 O << "\t.arch ev6\n";
218 else
219 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000220 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000221 return false;
222}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000223
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000224bool AlphaAsmPrinter::doFinalization(Module &M) {
225 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000226
Chris Lattnere4d5c442005-03-15 04:54:21 +0000227 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228 if (I->hasInitializer()) { // External global require no code
Chris Lattner04f96742006-03-09 06:14:35 +0000229 // Check to see if this is a special global used by LLVM, if so, emit it.
230 if (EmitSpecialLLVMGlobal(I))
231 continue;
232
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233 O << "\n\n";
234 std::string name = Mang->getValueName(I);
235 Constant *C = I->getInitializer();
236 unsigned Size = TD.getTypeSize(C->getType());
Andrew Lenharth054e2a72006-02-06 17:15:17 +0000237 // unsigned Align = TD.getTypeAlignmentShift(C->getType());
238 unsigned Align = getPreferredAlignmentLog(I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000239
Misha Brukman4633f1c2005-04-21 23:13:11 +0000240 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
242 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner0a70a492005-11-21 07:30:28 +0000243 SwitchSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000244 if (I->hasInternalLinkage())
245 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Jim Laskeydae29982006-02-27 10:29:04 +0000248 << "," << (1 << Align)
249 << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 } else {
251 switch (I->getLinkage()) {
252 case GlobalValue::LinkOnceLinkage:
253 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
254 // Nonnull linkonce -> weak
255 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner0a70a492005-11-21 07:30:28 +0000257 SwitchSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 break;
259 case GlobalValue::AppendingLinkage:
260 // FIXME: appending linkage variables should go into a section of
261 // their name or something. For now, just emit them as external.
262 case GlobalValue::ExternalLinkage:
263 // If external or appending, declare as a global symbol
264 O << "\t.globl " << name << "\n";
265 // FALL THROUGH
266 case GlobalValue::InternalLinkage:
Chris Lattner0a70a492005-11-21 07:30:28 +0000267 SwitchSection(C->isNullValue() ? "\t.section .bss" :
268 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000269 break;
270 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000271 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 abort();
273 }
274
Chris Lattner8b8b9512005-11-21 07:51:23 +0000275 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276 O << "\t.type " << name << ",@object\n";
277 O << "\t.size " << name << "," << Size << "\n";
Jim Laskeydae29982006-02-27 10:29:04 +0000278 O << name << ":\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000279 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000280 }
281 }
282
283 AsmPrinter::doFinalization(M);
284 return false;
285}