blob: 29627c518d956b6c5e39f0d8544d010a61feff04 [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"
Andrew Lenharth01269522005-01-24 18:37:48 +000025
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
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 case MachineOperand::MO_MachineBasicBlock: {
119 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
Chris Lattner87744a22005-11-21 07:38:08 +0000120 O << PrivateGlobalPrefix << "LBB"
121 << Mang->getValueName(MBBOp->getParent()->getFunction())
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
123 << MBBOp->getBasicBlock()->getName();
124 return;
125 }
126
127 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner60530102005-11-21 08:29:17 +0000128 O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
Chris Lattner81a994e2005-11-21 06:51:52 +0000129 << MO.getConstantPoolIndex();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130 return;
131
132 case MachineOperand::MO_ExternalSymbol:
133 O << MO.getSymbolName();
134 return;
135
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000136 case MachineOperand::MO_GlobalAddress:
137 //Abuse PCrel to specify pcrel calls
138 //calls are the only thing that use this flag
Andrew Lenhartheececba2005-12-25 17:36:48 +0000139// if (MO.isPCRelative())
140// O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
141// else
Andrew Lenharthc24b5372005-04-13 17:17:28 +0000142 O << Mang->getValueName(MO.getGlobal());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000144
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 default:
146 O << "<unknown operand type: " << MO.getType() << ">";
147 return;
148 }
149}
150
151/// printMachineInstruction -- Print out a single Alpha MI to
152/// the current output stream.
153///
154void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
155 ++EmittedInsts;
156 if (printInstruction(MI))
157 return; // Printer was automatically generated
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 assert(0 && "Unhandled instruction in asm writer!");
160 abort();
161 return;
162}
163
164
165/// runOnMachineFunction - This uses the printMachineInstruction()
166/// method to print assembly for each instruction.
167///
168bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000169 SetupMachineFunction(MF);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 O << "\n\n";
171
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000172 // Print out constants referenced by the function
Chris Lattner60530102005-11-21 08:29:17 +0000173 EmitConstantPool(MF.getConstantPool());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000174
175 // Print out labels for the function.
Chris Lattner0a70a492005-11-21 07:30:28 +0000176 SwitchSection("\t.section .text", MF.getFunction());
Chris Lattner8b8b9512005-11-21 07:51:23 +0000177 EmitAlignment(4);
Andrew Lenharth044f31f2005-05-27 03:39:30 +0000178 O << "\t.globl " << CurrentFnName << "\n";
179 O << "\t.ent " << CurrentFnName << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180
181 O << CurrentFnName << ":\n";
182
183 // Print out code for the function.
184 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
185 I != E; ++I) {
186 // Print a label for the basic block.
Chris Lattner87744a22005-11-21 07:38:08 +0000187 O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
188 << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
190 II != E; ++II) {
191 // Print the assembly for the instruction.
192 O << "\t";
193 printMachineInstruction(II);
194 }
195 }
196 ++LabelNumber;
197
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{
206 AsmPrinter::doInitialization(M);
Andrew Lenharth120ab482005-09-29 22:54:56 +0000207 if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
208 || TM.getSubtarget<AlphaSubtarget>().hasCT())
Andrew Lenharth3ae18292005-04-14 16:24:00 +0000209 O << "\t.arch ev6\n";
210 else
211 O << "\t.arch ev56\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000212 O << "\t.set noat\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000213 return false;
214}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000215
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000216bool AlphaAsmPrinter::doFinalization(Module &M) {
217 const TargetData &TD = TM.getTargetData();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000218
Chris Lattnere4d5c442005-03-15 04:54:21 +0000219 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220 if (I->hasInitializer()) { // External global require no code
221 O << "\n\n";
222 std::string name = Mang->getValueName(I);
223 Constant *C = I->getInitializer();
224 unsigned Size = TD.getTypeSize(C->getType());
225 unsigned Align = TD.getTypeAlignmentShift(C->getType());
226
Misha Brukman4633f1c2005-04-21 23:13:11 +0000227 if (C->isNullValue() &&
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
229 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner0a70a492005-11-21 07:30:28 +0000230 SwitchSection("\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000231 if (I->hasInternalLinkage())
232 O << "\t.local " << name << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000233
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000234 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
235 << "," << (1 << Align);
236 O << "\t\t# ";
237 WriteAsOperand(O, I, true, true, &M);
238 O << "\n";
239 } else {
240 switch (I->getLinkage()) {
241 case GlobalValue::LinkOnceLinkage:
242 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
243 // Nonnull linkonce -> weak
244 O << "\t.weak " << name << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
Chris Lattner0a70a492005-11-21 07:30:28 +0000246 SwitchSection("", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247 break;
248 case GlobalValue::AppendingLinkage:
249 // FIXME: appending linkage variables should go into a section of
250 // their name or something. For now, just emit them as external.
251 case GlobalValue::ExternalLinkage:
252 // If external or appending, declare as a global symbol
253 O << "\t.globl " << name << "\n";
254 // FALL THROUGH
255 case GlobalValue::InternalLinkage:
Chris Lattner0a70a492005-11-21 07:30:28 +0000256 SwitchSection(C->isNullValue() ? "\t.section .bss" :
257 "\t.section .data", I);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 break;
259 case GlobalValue::GhostLinkage:
Andrew Lenharth3dc15f32005-03-10 19:02:02 +0000260 std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261 abort();
262 }
263
Chris Lattner8b8b9512005-11-21 07:51:23 +0000264 EmitAlignment(Align);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265 O << "\t.type " << name << ",@object\n";
266 O << "\t.size " << name << "," << Size << "\n";
267 O << name << ":\t\t\t\t# ";
268 WriteAsOperand(O, I, true, true, &M);
269 O << " = ";
270 WriteAsOperand(O, C, false, false, &M);
271 O << "\n";
Chris Lattner8b8b9512005-11-21 07:51:23 +0000272 EmitGlobalConstant(C);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000273 }
274 }
275
276 AsmPrinter::doFinalization(M);
277 return false;
278}