blob: b0eb21eab62f61afdd440f5f138d91366dfd3640 [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2//
3// This file implements all of the stuff neccesary to output a .s file from
4// LLVM. The code in this file assumes that the specified module has already
5// been compiled into the internal data structures of the Module.
6//
Chris Lattnerf57b8452002-04-27 06:56:12 +00007// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
8// The FunctionPass is pipelined together with all of the rest of the code
9// generation stages, and the Pass runs at the end to emit code for global
10// variables and such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInternals.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerc019a172002-02-03 07:48:06 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000017#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000019#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000020#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000021#include "llvm/Function.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000022#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000023#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000024#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000025#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/StringExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000027#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000028using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000029
30namespace {
31
Vikram S. Adved198c472002-03-18 03:07:26 +000032class GlobalIdTable: public Annotation {
33 static AnnotationID AnnotId;
34 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000035
Vikram S. Adved198c472002-03-18 03:07:26 +000036 typedef std::hash_map<const Value*, int> ValIdMap;
37 typedef ValIdMap::const_iterator ValIdMapConstIterator;
38 typedef ValIdMap:: iterator ValIdMapIterator;
39public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000040 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000041 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000042
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000043 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000044};
45
46AnnotationID GlobalIdTable::AnnotId =
47 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
48
49//===---------------------------------------------------------------------===//
50// Code Shared By the two printer passes, as a mixin
51//===---------------------------------------------------------------------===//
52
53class AsmPrinter {
54 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000055public:
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000057 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000058
Chris Lattnere88f78c2001-09-19 13:47:27 +000059 enum Sections {
60 Unknown,
61 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000062 ReadOnlyData,
63 InitRWData,
64 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000065 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000066
Chris Lattner59ba1092002-02-04 15:53:23 +000067 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000068 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
69
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000070 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000071 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000072 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000073 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000074 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000075 idTable = new GlobalIdTable(&M);
76 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000077 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000078 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000079 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000080 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000081 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000082 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000083 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000084 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000085 }
86 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000087 }
88
Vikram S. Adved198c472002-03-18 03:07:26 +000089 // Check if a name is external or accessible from external code.
90 // Only functions can currently be external. "main" is the only name
91 // that is visible externally.
92 bool isExternal(const Value* V) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000093 const Function *F = dyn_cast<Function>(V);
94 return F && (F->isExternal() || F->getName() == "main");
Vikram S. Adved198c472002-03-18 03:07:26 +000095 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000096
Chris Lattnere88f78c2001-09-19 13:47:27 +000097 // enterSection - Use this method to enter a different section of the output
98 // executable. This is used to only output neccesary section transitions.
99 //
100 void enterSection(enum Sections S) {
101 if (S == CurSection) return; // Only switch section if neccesary
102 CurSection = S;
103
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000104 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000105 switch (S)
106 {
107 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000108 case Text: toAsm << "\".text\""; break;
109 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
110 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
111 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000112 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000113 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000114 }
115
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000116 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000117 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000118
119 // Symbol names in Sparc assembly language have these rules:
120 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
121 // (b) A name beginning in "." is treated as a local name.
122 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
123 //
124 if (S[0] == '_' || isdigit(S[0]))
125 Result += "ll";
126
127 for (unsigned i = 0; i < S.size(); ++i)
128 {
129 char C = S[i];
130 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
131 Result += C;
132 else
133 {
134 Result += '_';
135 Result += char('0' + ((unsigned char)C >> 4));
136 Result += char('0' + (C & 0xF));
137 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000138 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000139 return Result;
140 }
141
Chris Lattnere88f78c2001-09-19 13:47:27 +0000142 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000143 // the name of the identifier if possible (qualified by the type), and
144 // use a numbered value based on prefix otherwise.
145 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000146 //
147 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000148 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
149
150 Result = Result + (V->hasName()? V->getName() : string(Prefix));
151
152 // Qualify all internal names with a unique id.
153 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000154 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000155 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
157 if (I == idTable->valToIdMap.end())
158 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000159 else
160 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000161 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000162 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000164
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000165 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000166 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000167
Chris Lattnere88f78c2001-09-19 13:47:27 +0000168 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000169 string getID(const Function *F) {
170 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000171 }
172 string getID(const BasicBlock *BB) {
173 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
174 }
175 string getID(const GlobalVariable *GV) {
176 return getID(GV, "LLVMGlobal_", ".G_");
177 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000178 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000179 return getID(CV, "LLVMConst_", ".C_");
180 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000181};
182
183
184
185//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000186// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000187//===----------------------------------------------------------------------===//
188
Chris Lattnerf57b8452002-04-27 06:56:12 +0000189struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000190 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000191 : AsmPrinter(os, t) {}
192
Chris Lattner96c466b2002-04-29 14:57:45 +0000193 const char *getPassName() const {
194 return "Output Sparc Assembly for Functions";
195 }
196
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000197 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000198 startModule(M);
199 return false;
200 }
201
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000202 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000203 startFunction(F);
204 emitFunction(F);
205 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000206 return false;
207 }
208
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000210 endModule();
211 return false;
212 }
213
Chris Lattner97e52e42002-04-28 21:27:06 +0000214 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
215 AU.setPreservesAll();
216 }
217
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000218 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000219private :
220 void emitBasicBlock(const BasicBlock *BB);
221 void emitMachineInst(const MachineInstr *MI);
222
223 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
224 void printOneOperand(const MachineOperand &Op);
225
226 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
227 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000228
Chris Lattnere88f78c2001-09-19 13:47:27 +0000229 unsigned getOperandMask(unsigned Opcode) {
230 switch (Opcode) {
231 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000232 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000233 default: return 0; // By default, don't hack operands...
234 }
235 }
236};
237
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000238inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000239SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
240 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000241 switch (MI->getOpCode()) {
242 case JMPLCALL:
243 case JMPLRET: return (opNum == 0);
244 default: return false;
245 }
246}
247
248
249inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000250SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
251 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000252 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
253 return (opNum == 0);
254 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
255 return (opNum == 1);
256 else
257 return false;
258}
259
260
261#define PrintOp1PlusOp2(Op1, Op2) \
262 printOneOperand(Op1); \
263 toAsm << "+"; \
264 printOneOperand(Op2);
265
266unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000268 unsigned int opNum)
269{
270 const MachineOperand& Op = MI->getOperand(opNum);
271
272 if (OpIsBranchTargetLabel(MI, opNum))
273 {
274 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
275 return 2;
276 }
277 else if (OpIsMemoryAddressBase(MI, opNum))
278 {
279 toAsm << "[";
280 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
281 toAsm << "]";
282 return 2;
283 }
284 else
285 {
286 printOneOperand(Op);
287 return 1;
288 }
289}
290
291
292void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000293SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &op)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000294{
295 switch (op.getOperandType())
296 {
297 case MachineOperand::MO_VirtualRegister:
298 case MachineOperand::MO_CCRegister:
299 case MachineOperand::MO_MachineRegister:
300 {
301 int RegNum = (int)op.getAllocatedRegNum();
302
Vikram S. Advefbd21612002-03-31 19:03:58 +0000303 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000304 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
305 toAsm << "<NULL VALUE>";
306 } else {
307 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
308 }
309 break;
310 }
311
312 case MachineOperand::MO_PCRelativeDisp:
313 {
314 const Value *Val = op.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000315 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
316
317 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000319 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000320 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000321 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000323 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000324 toAsm << getID(CV);
325 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000326 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000327 break;
328 }
329
330 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve242a8082002-05-19 15:25:51 +0000331 toAsm << op.getImmedValue();
332 break;
333
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000334 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve242a8082002-05-19 15:25:51 +0000335 toAsm << (uint64_t) op.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000336 break;
337
338 default:
339 toAsm << op; // use dump field
340 break;
341 }
342}
343
344
345void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000346SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347{
348 unsigned Opcode = MI->getOpCode();
349
350 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
351 return; // IGNORE PHI NODES
352
353 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
354
355 unsigned Mask = getOperandMask(Opcode);
356
357 bool NeedComma = false;
358 unsigned N = 1;
359 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
360 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
361 if (NeedComma) toAsm << ", "; // Handle comma outputing
362 NeedComma = true;
363 N = printOperands(MI, OpNum);
364 }
365 else
366 N = 1;
367
368 toAsm << "\n";
369}
370
371void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000372SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000373{
374 // Emit a label for the basic block
375 toAsm << getID(BB) << ":\n";
376
377 // Get the vector of machine instructions corresponding to this bb.
378 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
379 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
380
381 // Loop over all of the instructions in the basic block...
382 for (; MII != MIE; ++MII)
383 emitMachineInst(*MII);
384 toAsm << "\n"; // Seperate BB's with newlines
385}
386
387void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000388SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000390 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000391 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392 enterSection(AsmPrinter::Text);
393 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
394 //toAsm << "\t.type\t" << methName << ",#function\n";
395 toAsm << "\t.type\t" << methName << ", 2\n";
396 toAsm << methName << ":\n";
397
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000398 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000399 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
400 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000401
402 // Output a .size directive so the debugger knows the extents of the function
403 toAsm << ".EndOf_" << methName << ":\n\t.size "
404 << methName << ", .EndOf_"
405 << methName << "-" << methName << "\n";
406
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000407 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000408 toAsm << "\n\n";
409}
410
411} // End anonymous namespace
412
Chris Lattnerf57b8452002-04-27 06:56:12 +0000413Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000414 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415}
416
417
418
419
420
421//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000422// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000423//===----------------------------------------------------------------------===//
424
425namespace {
426
427class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
428public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000429 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
430 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431
Chris Lattner96c466b2002-04-29 14:57:45 +0000432 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
433
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000434 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000435 startModule(M);
436 emitGlobalsAndConstants(M);
437 endModule();
438 return false;
439 }
440
Chris Lattner97e52e42002-04-28 21:27:06 +0000441 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
442 AU.setPreservesAll();
443 }
444
445private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000446 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000447
448 void printGlobalVariable(const GlobalVariable *GV);
449 void printSingleConstant( const Constant* CV);
450 void printConstantValueOnly(const Constant* CV);
451 void printConstant( const Constant* CV, std::string valID = "");
452
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000453 static void FoldConstants(const Module &M,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000454 std::hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000455};
456
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000457
458// Can we treat the specified array as a string? Only if it is an array of
459// ubytes or non-negative sbytes.
460//
Chris Lattner122787b2002-06-05 18:08:26 +0000461static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000462 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
463 if (ETy == Type::UByteTy) return true;
464 if (ETy != Type::SByteTy) return false;
465
466 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000467 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000468 return false;
469
470 return true;
471}
472
473// toOctal - Convert the low order bits of X into an octal letter
474static inline char toOctal(int X) {
475 return (X&7)+'0';
476}
477
478// getAsCString - Return the specified array as a C compatible string, only if
479// the predicate isStringCompatible is true.
480//
Chris Lattner122787b2002-06-05 18:08:26 +0000481static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000482 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000483
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000484 string Result;
485 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
486 Result = "\"";
487 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
488 unsigned char C = (ETy == Type::SByteTy) ?
489 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
490 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
491
Vikram S. Adve242a8082002-05-19 15:25:51 +0000492 if (C == '"') {
493 Result += "\\\"";
494 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000495 Result += C;
496 } else {
497 switch(C) {
498 case '\a': Result += "\\a"; break;
499 case '\b': Result += "\\b"; break;
500 case '\f': Result += "\\f"; break;
501 case '\n': Result += "\\n"; break;
502 case '\r': Result += "\\r"; break;
503 case '\t': Result += "\\t"; break;
504 case '\v': Result += "\\v"; break;
505 default:
506 Result += '\\';
507 Result += toOctal(C >> 6);
508 Result += toOctal(C >> 3);
509 Result += toOctal(C >> 0);
510 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000511 }
512 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000513 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000514 Result += "\"";
515
516 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000517}
518
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000519inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000520ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000521{
522 return (arrayType->getElementType() == Type::UByteTy ||
523 arrayType->getElementType() == Type::SByteTy);
524}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000525
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000526inline const string
527TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000528{
529 switch(type->getPrimitiveID())
530 {
531 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
532 return ".byte";
533 case Type::UShortTyID: case Type::ShortTyID:
534 return ".half";
535 case Type::UIntTyID: case Type::IntTyID:
536 return ".word";
537 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
538 return ".xword";
539 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000540 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000541 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000542 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000543 case Type::ArrayTyID:
544 if (ArrayTypeIsString((ArrayType*) type))
545 return ".ascii";
546 else
547 return "<InvaliDataTypeForPrinting>";
548 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000549 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000550 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000551}
552
Vikram S. Adve21447222001-11-10 02:03:06 +0000553// Get the size of the constant for the given target.
554// If this is an unsized array, return 0.
555//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000556inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000557ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000558{
Chris Lattner122787b2002-06-05 18:08:26 +0000559 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000560 {
Chris Lattner122787b2002-06-05 18:08:26 +0000561 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000562 if (ArrayTypeIsString(aty))
563 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000564 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000565
566 return target.findOptimalStorageSize(CV->getType());
567}
568
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000569
570
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000571// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000572// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
573//
574inline unsigned int
575SizeToAlignment(unsigned int size, const TargetMachine& target)
576{
577 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
578 if (size > (unsigned) cacheLineSize / 2)
579 return cacheLineSize;
580 else
581 for (unsigned sz=1; /*no condition*/; sz *= 2)
582 if (sz >= size)
583 return sz;
584}
585
586// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000587//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000588inline unsigned int
589TypeToAlignment(const Type* type, const TargetMachine& target)
590{
Vikram S. Adve21447222001-11-10 02:03:06 +0000591 return SizeToAlignment(target.findOptimalStorageSize(type), target);
592}
593
594// Get the size of the constant and then use SizeToAlignment.
595// Handles strings as a special case;
596inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000597ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000598{
Chris Lattner122787b2002-06-05 18:08:26 +0000599 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000600 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
601 return SizeToAlignment(1 + CPA->getNumOperands(), target);
602
603 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000604}
605
606
Vikram S. Adve21447222001-11-10 02:03:06 +0000607// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000608void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000609SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000610{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000611 assert(CV->getType() != Type::VoidTy &&
612 CV->getType() != Type::TypeTy &&
613 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000614 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000615
Chris Lattnerf678dc62002-04-11 21:44:02 +0000616 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
617 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000618
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000619 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000620
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000621 if (CV->getType()->isPrimitiveType())
622 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000623 if (CV->getType()->isFloatingPoint()) {
624 // FP Constants are printed as integer constants to avoid losing
625 // precision...
626 double Val = cast<ConstantFP>(CV)->getValue();
627 if (CV->getType() == Type::FloatTy) {
628 float FVal = (float)Val;
629 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
630 toAsm << *(unsigned int*)ProxyPtr;
631 } else if (CV->getType() == Type::DoubleTy) {
632 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
633 toAsm << *(uint64_t*)ProxyPtr;
634 } else {
635 assert(0 && "Unknown floating point type!");
636 }
637
638 toAsm << "\t! " << CV->getType()->getDescription()
639 << " value: " << Val << "\n";
640 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000641 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000642 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000643 }
Chris Lattner122787b2002-06-05 18:08:26 +0000644 else if (const ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000645 {
Chris Lattner697954c2002-01-20 22:54:45 +0000646 assert(CPP->isNullValue() &&
647 "Cannot yet print non-null pointer constants to assembly");
648 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000649 }
Chris Lattner697954c2002-01-20 22:54:45 +0000650 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000651 {
652 assert(0 && "Cannot yet initialize pointer refs in assembly");
653 }
654 else
655 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000656 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000657 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000658}
659
Vikram S. Adve21447222001-11-10 02:03:06 +0000660// Print a constant value or values (it may be an aggregate).
661// Uses printSingleConstant() to print each individual value.
662void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000663SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000664{
Chris Lattner122787b2002-06-05 18:08:26 +0000665 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000666
667 if (CPA && isStringCompatible(CPA))
668 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000669 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000670 }
671 else if (CPA)
672 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000673 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000674 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000675 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000676 }
Chris Lattner122787b2002-06-05 18:08:26 +0000677 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000678 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000679 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000680 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000681 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000682 }
683 else
Chris Lattner122787b2002-06-05 18:08:26 +0000684 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000685}
686
687// Print a constant (which may be an aggregate) prefixed by all the
688// appropriate directives. Uses printConstantValueOnly() to print the
689// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000690void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000691SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000692{
693 if (valID.length() == 0)
694 valID = getID(CV);
695
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000696 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000697
698 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000699 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000700 if (CPA && isStringCompatible(CPA))
701 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000702 toAsm << valID << ":\n";
703 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000704 return;
705 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000706
Chris Lattner697954c2002-01-20 22:54:45 +0000707 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000708
709 unsigned int constSize = ConstantToSize(CV, Target);
710 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000711 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000712
Chris Lattner697954c2002-01-20 22:54:45 +0000713 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000714
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000715 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000716}
717
718
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000719void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000720 std::hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000721 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
722 if (!I->isExternal()) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000723 const std::hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000724 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000725 MC.insert(pool.begin(), pool.end());
726 }
727}
728
729void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000730{
Chris Lattner697954c2002-01-20 22:54:45 +0000731 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000732
733 if (GV->hasInitializer())
734 printConstant(GV->getInitializer(), getID(GV));
735 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000736 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
737 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000738 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000739 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000740 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000741 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000742 }
743}
744
745
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000746void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000747 // First, get the constants there were marked by the code generator for
748 // inclusion in the assembly code data area and fold them all into a
749 // single constant pool since there may be lots of duplicates. Also,
750 // lets force these constants into the slot table so that we can get
751 // unique names for unnamed constants also.
752 //
Chris Lattner697954c2002-01-20 22:54:45 +0000753 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000754 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000755
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000756 // Now, emit the three data sections separately; the cost of I/O should
757 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000758
759 // Section 1 : Read-only data section (implies initialized)
760 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000761 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
762 if (GI->hasInitializer() && GI->isConstant())
763 printGlobalVariable(GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000764
Chris Lattner697954c2002-01-20 22:54:45 +0000765 for (std::hash_set<const Constant*>::const_iterator
766 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000767 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000768 printConstant(*I);
769
Vikram S. Advefbd21612002-03-31 19:03:58 +0000770 // Section 2 : Initialized read-write data section
771 enterSection(AsmPrinter::InitRWData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000772 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
773 if (GI->hasInitializer() && !GI->isConstant())
774 printGlobalVariable(GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000775
Vikram S. Advefbd21612002-03-31 19:03:58 +0000776 // Section 3 : Uninitialized read-write data section
777 enterSection(AsmPrinter::UninitRWData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000778 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
779 if (!GI->hasInitializer())
780 printGlobalVariable(GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000781
Chris Lattner697954c2002-01-20 22:54:45 +0000782 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000783}
784
Chris Lattnere88f78c2001-09-19 13:47:27 +0000785} // End anonymous namespace
786
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000787Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
788 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000789}