blob: 098da9074e5d51cf6a4ce887f5bddc725d95c1e7 [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//
7// The entry point of this file is the UltraSparc::emitAssembly method.
8//
9//===----------------------------------------------------------------------===//
10
11#include "SparcInternals.h"
12#include "llvm/Analysis/SlotCalculator.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000013#include "llvm/Transforms/Linker.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000014#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000015#include "llvm/GlobalVariable.h"
16#include "llvm/GlobalValue.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000017#include "llvm/ConstantVals.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000019#include "llvm/BasicBlock.h"
20#include "llvm/Method.h"
21#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/StringExtras.h"
23#include "Support/HashExtras.h"
Vikram S. Adve29ff8732001-11-08 05:12:37 +000024#include <locale.h>
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000026
27namespace {
28
Chris Lattnere88f78c2001-09-19 13:47:27 +000029
Vikram S. Adve953c83e2001-10-28 21:38:52 +000030class SparcAsmPrinter {
Chris Lattner697954c2002-01-20 22:54:45 +000031 typedef std::hash_map<const Value*, int> ValIdMap;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000032 typedef ValIdMap:: iterator ValIdMapIterator;
33 typedef ValIdMap::const_iterator ValIdMapConstIterator;
34
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::ostream &toAsm;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000036 SlotCalculator Table; // map anonymous values to unique integer IDs
37 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
38 const UltraSparc &Target;
39
Chris Lattnere88f78c2001-09-19 13:47:27 +000040 enum Sections {
41 Unknown,
42 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000043 ReadOnlyData,
44 InitRWData,
45 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000046 } CurSection;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000047
Chris Lattnere88f78c2001-09-19 13:47:27 +000048public:
Chris Lattner697954c2002-01-20 22:54:45 +000049 inline SparcAsmPrinter(std::ostream &o, const Module *M, const UltraSparc &t)
Vikram S. Adve29ff8732001-11-08 05:12:37 +000050 : toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
Chris Lattnere88f78c2001-09-19 13:47:27 +000051 emitModule(M);
52 }
53
54private :
55 void emitModule(const Module *M);
Chris Lattnere88f78c2001-09-19 13:47:27 +000056 void emitMethod(const Method *M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +000057 void emitGlobalsAndConstants(const Module* module);
Chris Lattnere88f78c2001-09-19 13:47:27 +000058 //void processMethodArgument(const MethodArgument *MA);
59 void emitBasicBlock(const BasicBlock *BB);
60 void emitMachineInst(const MachineInstr *MI);
Chris Lattnere88f78c2001-09-19 13:47:27 +000061
Vikram S. Adve21447222001-11-10 02:03:06 +000062 void printGlobalVariable( const GlobalVariable* GV);
Chris Lattnere9bb2df2001-12-03 22:26:30 +000063 void printSingleConstant( const Constant* CV);
64 void printConstantValueOnly(const Constant* CV);
Chris Lattner697954c2002-01-20 22:54:45 +000065 void printConstant( const Constant* CV, std::string valID = "");
Vikram S. Adve953c83e2001-10-28 21:38:52 +000066
67 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
68 void printOneOperand(const MachineOperand &Op);
Chris Lattnere88f78c2001-09-19 13:47:27 +000069
Vikram S. Adve953c83e2001-10-28 21:38:52 +000070 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
71 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
72
Chris Lattnere88f78c2001-09-19 13:47:27 +000073 // enterSection - Use this method to enter a different section of the output
74 // executable. This is used to only output neccesary section transitions.
75 //
76 void enterSection(enum Sections S) {
77 if (S == CurSection) return; // Only switch section if neccesary
78 CurSection = S;
79
Vikram S. Adve29ff8732001-11-08 05:12:37 +000080 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000081 switch (S)
82 {
83 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000084 case Text: toAsm << "\".text\""; break;
85 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
86 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
87 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000088 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000089 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000090 }
91
Chris Lattner697954c2002-01-20 22:54:45 +000092 std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000093 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000094
95 // Symbol names in Sparc assembly language have these rules:
96 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
97 // (b) A name beginning in "." is treated as a local name.
98 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
99 //
100 if (S[0] == '_' || isdigit(S[0]))
101 Result += "ll";
102
103 for (unsigned i = 0; i < S.size(); ++i)
104 {
105 char C = S[i];
106 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
107 Result += C;
108 else
109 {
110 Result += '_';
111 Result += char('0' + ((unsigned char)C >> 4));
112 Result += char('0' + (C & 0xF));
113 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000114 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000115 return Result;
116 }
117
Chris Lattnere88f78c2001-09-19 13:47:27 +0000118 // getID - Return a valid identifier for the specified value. Base it on
119 // the name of the identifier if possible, use a numbered value based on
120 // prefix otherwise. FPrefix is always prepended to the output identifier.
121 //
122 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000123 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000124 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
125 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000126 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000127 } else {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000128 int valId = Table.getValSlot(V);
129 if (valId == -1) {
130 ValIdMapConstIterator I = valToIdMap.find(V);
131 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
132 : (*I).second;
133 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000134 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000135 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000136 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000137 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000138
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000140 string getID(const Module *M) {
141 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000142 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000143 string getID(const Method *M) {
144 return getID(M, "LLVMMethod_");
145 }
146 string getID(const BasicBlock *BB) {
147 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
148 }
149 string getID(const GlobalVariable *GV) {
150 return getID(GV, "LLVMGlobal_", ".G_");
151 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000152 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000153 return getID(CV, "LLVMConst_", ".C_");
154 }
155
Chris Lattnere88f78c2001-09-19 13:47:27 +0000156 unsigned getOperandMask(unsigned Opcode) {
157 switch (Opcode) {
158 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000159 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000160 default: return 0; // By default, don't hack operands...
161 }
162 }
163};
164
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000165
166// Can we treat the specified array as a string? Only if it is an array of
167// ubytes or non-negative sbytes.
168//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000169static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000170 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
171 if (ETy == Type::UByteTy) return true;
172 if (ETy != Type::SByteTy) return false;
173
174 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000175 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000176 return false;
177
178 return true;
179}
180
181// toOctal - Convert the low order bits of X into an octal letter
182static inline char toOctal(int X) {
183 return (X&7)+'0';
184}
185
186// getAsCString - Return the specified array as a C compatible string, only if
187// the predicate isStringCompatible is true.
188//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000189static string getAsCString(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000190 if (isStringCompatible(CPA)) {
191 string Result;
192 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
193 Result = "\"";
194 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
195 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000196 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
197 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000198
199 if (isprint(C)) {
200 Result += C;
201 } else {
202 switch(C) {
203 case '\a': Result += "\\a"; break;
204 case '\b': Result += "\\b"; break;
205 case '\f': Result += "\\f"; break;
206 case '\n': Result += "\\n"; break;
207 case '\r': Result += "\\r"; break;
208 case '\t': Result += "\\t"; break;
209 case '\v': Result += "\\v"; break;
210 default:
211 Result += '\\';
212 Result += toOctal(C >> 6);
213 Result += toOctal(C >> 3);
214 Result += toOctal(C >> 0);
215 break;
216 }
217 }
218 }
219 Result += "\"";
220
221 return Result;
222 } else {
223 return CPA->getStrValue();
224 }
225}
226
227
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000228inline bool
229SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
230 unsigned int opNum) {
231 switch (MI->getOpCode()) {
232 case JMPLCALL:
233 case JMPLRET: return (opNum == 0);
234 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000235 }
236}
237
238
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000239inline bool
240SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
241 unsigned int opNum) {
242 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
243 return (opNum == 0);
244 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
245 return (opNum == 1);
246 else
247 return false;
248}
249
250
251#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000252 printOneOperand(Op1); \
253 toAsm << "+"; \
254 printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000255
256unsigned int
257SparcAsmPrinter::printOperands(const MachineInstr *MI,
258 unsigned int opNum)
259{
260 const MachineOperand& Op = MI->getOperand(opNum);
261
262 if (OpIsBranchTargetLabel(MI, opNum))
263 {
264 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
265 return 2;
266 }
267 else if (OpIsMemoryAddressBase(MI, opNum))
268 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000269 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000270 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000271 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000272 return 2;
273 }
274 else
275 {
276 printOneOperand(Op);
277 return 1;
278 }
279}
280
281
282void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000283SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000284{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000285 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000286 {
287 case MachineOperand::MO_VirtualRegister:
288 case MachineOperand::MO_CCRegister:
289 case MachineOperand::MO_MachineRegister:
290 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000291 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000292
293 // ****this code is temporary till NULL Values are fixed
Ruchira Sasanka5f98aca2001-11-13 23:08:19 +0000294 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000295 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000296 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000297 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000298 }
299 break;
300 }
301
302 case MachineOperand::MO_PCRelativeDisp:
303 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000304 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000305 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000306 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000307 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000308 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000309 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000310 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000311 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000312 toAsm << getID(GV);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 else if (const Constant *CV = dyn_cast<const Constant>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000314 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000315 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000316 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000317 break;
318 }
319
320 case MachineOperand::MO_SignExtendedImmed:
321 case MachineOperand::MO_UnextendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000322 toAsm << (long)op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000323 break;
324
325 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000326 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000327 break;
328 }
329}
330
331
332void
333SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
334{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000335 unsigned Opcode = MI->getOpCode();
336
337 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
338 return; // IGNORE PHI NODES
339
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000340 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000341
342 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000343
Chris Lattnere88f78c2001-09-19 13:47:27 +0000344 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000345 unsigned N = 1;
346 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
347 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000348 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000349 NeedComma = true;
350 N = printOperands(MI, OpNum);
351 }
352 else
353 N = 1;
354
Chris Lattner697954c2002-01-20 22:54:45 +0000355 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000356}
357
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000358void
359SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
360{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000361 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000362 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000363
364 // Get the vector of machine instructions corresponding to this bb.
365 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
366 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
367
368 // Loop over all of the instructions in the basic block...
369 for (; MII != MIE; ++MII)
370 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000371 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000372}
373
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000374void
375SparcAsmPrinter::emitMethod(const Method *M)
376{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000377 if (M->isExternal()) return;
378
379 // Make sure the slot table has information about this method...
380 Table.incorporateMethod(M);
381
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000382 string methName = getID(M);
383 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000384 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000385 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
386 //toAsm << "\t.type\t" << methName << ",#function\n";
387 toAsm << "\t.type\t" << methName << ", 2\n";
388 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000389
390 // Output code for all of the basic blocks in the method...
391 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
392 emitBasicBlock(*I);
393
394 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000395 toAsm << ".EndOf_" << methName << ":\n\t.size "
396 << methName << ", .EndOf_"
Chris Lattner697954c2002-01-20 22:54:45 +0000397 << methName << "-" << methName << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000398
399 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000400 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000401
402 // Forget all about M.
403 Table.purgeMethod();
404}
405
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000406inline bool
407ArrayTypeIsString(ArrayType* arrayType)
408{
409 return (arrayType->getElementType() == Type::UByteTy ||
410 arrayType->getElementType() == Type::SByteTy);
411}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000412
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000413inline const string
414TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000415{
416 switch(type->getPrimitiveID())
417 {
418 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
419 return ".byte";
420 case Type::UShortTyID: case Type::ShortTyID:
421 return ".half";
422 case Type::UIntTyID: case Type::IntTyID:
423 return ".word";
424 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
425 return ".xword";
426 case Type::FloatTyID:
427 return ".single";
428 case Type::DoubleTyID:
429 return ".double";
430 case Type::ArrayTyID:
431 if (ArrayTypeIsString((ArrayType*) type))
432 return ".ascii";
433 else
434 return "<InvaliDataTypeForPrinting>";
435 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000436 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000437 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000438}
439
Vikram S. Adve21447222001-11-10 02:03:06 +0000440// Get the size of the constant for the given target.
441// If this is an unsized array, return 0.
442//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000443inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000444ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000445{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000446 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000447 {
448 ArrayType *aty = cast<ArrayType>(CPA->getType());
449 if (ArrayTypeIsString(aty))
450 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000451 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000452
453 return target.findOptimalStorageSize(CV->getType());
454}
455
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000456inline
457unsigned int TypeToSize(const Type* type, const TargetMachine& target)
458{
459 return target.findOptimalStorageSize(type);
460}
461
462
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000463// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000464// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
465//
466inline unsigned int
467SizeToAlignment(unsigned int size, const TargetMachine& target)
468{
469 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
470 if (size > (unsigned) cacheLineSize / 2)
471 return cacheLineSize;
472 else
473 for (unsigned sz=1; /*no condition*/; sz *= 2)
474 if (sz >= size)
475 return sz;
476}
477
478// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000479//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000480inline unsigned int
481TypeToAlignment(const Type* type, const TargetMachine& target)
482{
Vikram S. Adve21447222001-11-10 02:03:06 +0000483 return SizeToAlignment(target.findOptimalStorageSize(type), target);
484}
485
486// Get the size of the constant and then use SizeToAlignment.
487// Handles strings as a special case;
488inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000489ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000490{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000491 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000492 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
493 return SizeToAlignment(1 + CPA->getNumOperands(), target);
494
495 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000496}
497
498
Vikram S. Adve21447222001-11-10 02:03:06 +0000499// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000500void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000501SparcAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000502{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000503 assert(CV->getType() != Type::VoidTy &&
504 CV->getType() != Type::TypeTy &&
505 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000506 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000507
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000508 assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000509 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000510
511 toAsm << "\t"
512 << TypeToDataDirective(CV->getType()) << "\t";
513
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000514 if (CV->getType()->isPrimitiveType())
515 {
516 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
517 toAsm << "0r"; // FP constants must have this prefix
Chris Lattner697954c2002-01-20 22:54:45 +0000518 toAsm << CV->getStrValue() << "\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000519 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000520 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000521 {
Chris Lattner697954c2002-01-20 22:54:45 +0000522 assert(CPP->isNullValue() &&
523 "Cannot yet print non-null pointer constants to assembly");
524 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000525 }
Chris Lattner697954c2002-01-20 22:54:45 +0000526 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000527 {
528 assert(0 && "Cannot yet initialize pointer refs in assembly");
529 }
530 else
531 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000532 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000533 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000534}
535
Vikram S. Adve21447222001-11-10 02:03:06 +0000536// Print a constant value or values (it may be an aggregate).
537// Uses printSingleConstant() to print each individual value.
538void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000539SparcAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000540{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000541 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000542
543 if (CPA && isStringCompatible(CPA))
544 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000545 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000546 }
547 else if (CPA)
548 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000549 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000550 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000551 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000552 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000553 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000554 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000555 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000556 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000557 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000558 }
559 else
560 this->printSingleConstant(CV);
561}
562
563// Print a constant (which may be an aggregate) prefixed by all the
564// appropriate directives. Uses printConstantValueOnly() to print the
565// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000566void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000567SparcAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000568{
569 if (valID.length() == 0)
570 valID = getID(CV);
571
Vikram S. Adve21447222001-11-10 02:03:06 +0000572 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000573 << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000574
575 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000576 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000577 if (CPA && isStringCompatible(CPA))
578 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000579 toAsm << valID << ":\n";
580 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000581 return;
582 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000583
Chris Lattner697954c2002-01-20 22:54:45 +0000584 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000585
586 unsigned int constSize = ConstantToSize(CV, Target);
587 if (constSize)
588 toAsm << "\t.size" << "\t" << valID << ","
Chris Lattner697954c2002-01-20 22:54:45 +0000589 << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000590
Chris Lattner697954c2002-01-20 22:54:45 +0000591 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000592
Vikram S. Adve21447222001-11-10 02:03:06 +0000593 this->printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000594}
595
596
597void
598SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
599{
Chris Lattner697954c2002-01-20 22:54:45 +0000600 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000601
602 if (GV->hasInitializer())
603 printConstant(GV->getInitializer(), getID(GV));
604 else {
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000605 toAsm << "\t.align\t"
Chris Lattner697954c2002-01-20 22:54:45 +0000606 << TypeToAlignment(GV->getType()->getElementType(), Target) << "\n";
607 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000608 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattner7a176752001-12-04 00:03:30 +0000609 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000610 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000611 }
612}
613
614
615static void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000616FoldConstants(const Module *M,
Chris Lattner697954c2002-01-20 22:54:45 +0000617 std::hash_set<const Constant*>& moduleConstants)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000618{
619 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
620 if (! (*I)->isExternal())
621 {
Chris Lattner697954c2002-01-20 22:54:45 +0000622 const std::hash_set<const Constant*>& pool =
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000623 MachineCodeForMethod::get(*I).getConstantPoolValues();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000624 moduleConstants.insert(pool.begin(), pool.end());
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000625 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000626}
627
628
629void
630SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
631{
632 // First, get the constants there were marked by the code generator for
633 // inclusion in the assembly code data area and fold them all into a
634 // single constant pool since there may be lots of duplicates. Also,
635 // lets force these constants into the slot table so that we can get
636 // unique names for unnamed constants also.
637 //
Chris Lattner697954c2002-01-20 22:54:45 +0000638 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000639 FoldConstants(M, moduleConstants);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000640
641 // Now, emit the three data sections separately; the cost of I/O should
642 // make up for the cost of extra passes over the globals list!
643 //
644 // Read-only data section (implies initialized)
645 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
646 {
647 const GlobalVariable* GV = *GI;
648 if (GV->hasInitializer() && GV->isConstant())
649 {
650 if (GI == M->gbegin())
651 enterSection(ReadOnlyData);
652 printGlobalVariable(GV);
653 }
654 }
655
Chris Lattner697954c2002-01-20 22:54:45 +0000656 for (std::hash_set<const Constant*>::const_iterator
657 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000658 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000659 printConstant(*I);
660
661 // Initialized read-write data section
662 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
663 {
664 const GlobalVariable* GV = *GI;
665 if (GV->hasInitializer() && ! GV->isConstant())
666 {
667 if (GI == M->gbegin())
668 enterSection(InitRWData);
669 printGlobalVariable(GV);
670 }
671 }
672
673 // Uninitialized read-write data section
674 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
675 {
676 const GlobalVariable* GV = *GI;
677 if (! GV->hasInitializer())
678 {
679 if (GI == M->gbegin())
680 enterSection(UninitRWData);
681 printGlobalVariable(GV);
682 }
683 }
684
Chris Lattner697954c2002-01-20 22:54:45 +0000685 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000686}
687
688
689void
690SparcAsmPrinter::emitModule(const Module *M)
691{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000692 // TODO: Look for a filename annotation on M to emit a .file directive
693 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
694 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000695
696 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000697}
698
699} // End anonymous namespace
700
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000701
Chris Lattnere88f78c2001-09-19 13:47:27 +0000702//
703// emitAssembly - Output assembly language code (a .s file) for the specified
704// method. The specified method must have been compiled before this may be
705// used.
706//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707void
Chris Lattner697954c2002-01-20 22:54:45 +0000708UltraSparc::emitAssembly(const Module *M, std::ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000710 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000711}