blob: 385ffffe56f120a0db15b27421514c252dfe3160 [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 Lattnere88f78c2001-09-19 13:47:27 +000025
26namespace {
27
Chris Lattnere88f78c2001-09-19 13:47:27 +000028
Vikram S. Adve953c83e2001-10-28 21:38:52 +000029class SparcAsmPrinter {
30 typedef hash_map<const Value*, int> ValIdMap;
31 typedef ValIdMap:: iterator ValIdMapIterator;
32 typedef ValIdMap::const_iterator ValIdMapConstIterator;
33
Vikram S. Adve29ff8732001-11-08 05:12:37 +000034 ostream &toAsm;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000035 SlotCalculator Table; // map anonymous values to unique integer IDs
36 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
37 const UltraSparc &Target;
38
Chris Lattnere88f78c2001-09-19 13:47:27 +000039 enum Sections {
40 Unknown,
41 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000042 ReadOnlyData,
43 InitRWData,
44 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000045 } CurSection;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000046
Chris Lattnere88f78c2001-09-19 13:47:27 +000047public:
Chris Lattnerec0a95f2001-10-15 15:54:43 +000048 inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
Vikram S. Adve29ff8732001-11-08 05:12:37 +000049 : toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
Chris Lattnere88f78c2001-09-19 13:47:27 +000050 emitModule(M);
51 }
52
53private :
54 void emitModule(const Module *M);
Chris Lattnere88f78c2001-09-19 13:47:27 +000055 void emitMethod(const Method *M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +000056 void emitGlobalsAndConstants(const Module* module);
Chris Lattnere88f78c2001-09-19 13:47:27 +000057 //void processMethodArgument(const MethodArgument *MA);
58 void emitBasicBlock(const BasicBlock *BB);
59 void emitMachineInst(const MachineInstr *MI);
Chris Lattnere88f78c2001-09-19 13:47:27 +000060
Vikram S. Adve21447222001-11-10 02:03:06 +000061 void printGlobalVariable( const GlobalVariable* GV);
Chris Lattnere9bb2df2001-12-03 22:26:30 +000062 void printSingleConstant( const Constant* CV);
63 void printConstantValueOnly(const Constant* CV);
64 void printConstant( const Constant* CV, string valID=string(""));
Vikram S. Adve953c83e2001-10-28 21:38:52 +000065
66 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
67 void printOneOperand(const MachineOperand &Op);
Chris Lattnere88f78c2001-09-19 13:47:27 +000068
Vikram S. Adve953c83e2001-10-28 21:38:52 +000069 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
70 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
71
Chris Lattnere88f78c2001-09-19 13:47:27 +000072 // enterSection - Use this method to enter a different section of the output
73 // executable. This is used to only output neccesary section transitions.
74 //
75 void enterSection(enum Sections S) {
76 if (S == CurSection) return; // Only switch section if neccesary
77 CurSection = S;
78
Vikram S. Adve29ff8732001-11-08 05:12:37 +000079 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000080 switch (S)
81 {
82 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000083 case Text: toAsm << "\".text\""; break;
84 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
85 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
86 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000087 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000088 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000089 }
90
Vikram S. Adve29ff8732001-11-08 05:12:37 +000091 string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000092 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000093
94 // Symbol names in Sparc assembly language have these rules:
95 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
96 // (b) A name beginning in "." is treated as a local name.
97 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
98 //
99 if (S[0] == '_' || isdigit(S[0]))
100 Result += "ll";
101
102 for (unsigned i = 0; i < S.size(); ++i)
103 {
104 char C = S[i];
105 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
106 Result += C;
107 else
108 {
109 Result += '_';
110 Result += char('0' + ((unsigned char)C >> 4));
111 Result += char('0' + (C & 0xF));
112 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000113 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000114 return Result;
115 }
116
Chris Lattnere88f78c2001-09-19 13:47:27 +0000117 // getID - Return a valid identifier for the specified value. Base it on
118 // the name of the identifier if possible, use a numbered value based on
119 // prefix otherwise. FPrefix is always prepended to the output identifier.
120 //
121 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000122 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000123 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
124 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000125 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000126 } else {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000127 int valId = Table.getValSlot(V);
128 if (valId == -1) {
129 ValIdMapConstIterator I = valToIdMap.find(V);
130 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
131 : (*I).second;
132 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000133 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000134 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000135 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000137
Chris Lattnere88f78c2001-09-19 13:47:27 +0000138 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000139 string getID(const Module *M) {
140 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000141 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000142 string getID(const Method *M) {
143 return getID(M, "LLVMMethod_");
144 }
145 string getID(const BasicBlock *BB) {
146 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
147 }
148 string getID(const GlobalVariable *GV) {
149 return getID(GV, "LLVMGlobal_", ".G_");
150 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000151 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000152 return getID(CV, "LLVMConst_", ".C_");
153 }
154
Chris Lattnere88f78c2001-09-19 13:47:27 +0000155 unsigned getOperandMask(unsigned Opcode) {
156 switch (Opcode) {
157 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000158 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000159 default: return 0; // By default, don't hack operands...
160 }
161 }
162};
163
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000164
165// Can we treat the specified array as a string? Only if it is an array of
166// ubytes or non-negative sbytes.
167//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000168static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000169 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
170 if (ETy == Type::UByteTy) return true;
171 if (ETy != Type::SByteTy) return false;
172
173 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000174 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000175 return false;
176
177 return true;
178}
179
180// toOctal - Convert the low order bits of X into an octal letter
181static inline char toOctal(int X) {
182 return (X&7)+'0';
183}
184
185// getAsCString - Return the specified array as a C compatible string, only if
186// the predicate isStringCompatible is true.
187//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188static string getAsCString(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000189 if (isStringCompatible(CPA)) {
190 string Result;
191 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
192 Result = "\"";
193 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
194 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000195 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
196 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000197
198 if (isprint(C)) {
199 Result += C;
200 } else {
201 switch(C) {
202 case '\a': Result += "\\a"; break;
203 case '\b': Result += "\\b"; break;
204 case '\f': Result += "\\f"; break;
205 case '\n': Result += "\\n"; break;
206 case '\r': Result += "\\r"; break;
207 case '\t': Result += "\\t"; break;
208 case '\v': Result += "\\v"; break;
209 default:
210 Result += '\\';
211 Result += toOctal(C >> 6);
212 Result += toOctal(C >> 3);
213 Result += toOctal(C >> 0);
214 break;
215 }
216 }
217 }
218 Result += "\"";
219
220 return Result;
221 } else {
222 return CPA->getStrValue();
223 }
224}
225
226
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000227inline bool
228SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
229 unsigned int opNum) {
230 switch (MI->getOpCode()) {
231 case JMPLCALL:
232 case JMPLRET: return (opNum == 0);
233 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000234 }
235}
236
237
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000238inline bool
239SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
240 unsigned int opNum) {
241 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
242 return (opNum == 0);
243 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
244 return (opNum == 1);
245 else
246 return false;
247}
248
249
250#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000251 printOneOperand(Op1); \
252 toAsm << "+"; \
253 printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000254
255unsigned int
256SparcAsmPrinter::printOperands(const MachineInstr *MI,
257 unsigned int opNum)
258{
259 const MachineOperand& Op = MI->getOperand(opNum);
260
261 if (OpIsBranchTargetLabel(MI, opNum))
262 {
263 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
264 return 2;
265 }
266 else if (OpIsMemoryAddressBase(MI, opNum))
267 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000268 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000269 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000270 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000271 return 2;
272 }
273 else
274 {
275 printOneOperand(Op);
276 return 1;
277 }
278}
279
280
281void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000282SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000283{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000284 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000285 {
286 case MachineOperand::MO_VirtualRegister:
287 case MachineOperand::MO_CCRegister:
288 case MachineOperand::MO_MachineRegister:
289 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000290 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000291
292 // ****this code is temporary till NULL Values are fixed
Ruchira Sasanka5f98aca2001-11-13 23:08:19 +0000293 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000294 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000295 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000296 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000297 }
298 break;
299 }
300
301 case MachineOperand::MO_PCRelativeDisp:
302 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000303 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000304 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000305 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000306 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000307 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000308 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000309 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000310 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000311 toAsm << getID(GV);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 else if (const Constant *CV = dyn_cast<const Constant>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000313 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000314 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000315 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000316 break;
317 }
318
319 case MachineOperand::MO_SignExtendedImmed:
320 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000321 toAsm << op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000322 break;
323
324 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000325 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000326 break;
327 }
328}
329
330
331void
332SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
333{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000334 unsigned Opcode = MI->getOpCode();
335
336 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
337 return; // IGNORE PHI NODES
338
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000339 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000340
341 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000342
Chris Lattnere88f78c2001-09-19 13:47:27 +0000343 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000344 unsigned N = 1;
345 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
346 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000347 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000348 NeedComma = true;
349 N = printOperands(MI, OpNum);
350 }
351 else
352 N = 1;
353
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000354 toAsm << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000355}
356
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000357void
358SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
359{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000360 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000361 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000362
363 // Get the vector of machine instructions corresponding to this bb.
364 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
365 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
366
367 // Loop over all of the instructions in the basic block...
368 for (; MII != MIE; ++MII)
369 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000370 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000371}
372
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000373void
374SparcAsmPrinter::emitMethod(const Method *M)
375{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000376 if (M->isExternal()) return;
377
378 // Make sure the slot table has information about this method...
379 Table.incorporateMethod(M);
380
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000381 string methName = getID(M);
382 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000383 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000384 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
385 //toAsm << "\t.type\t" << methName << ",#function\n";
386 toAsm << "\t.type\t" << methName << ", 2\n";
387 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000388
389 // Output code for all of the basic blocks in the method...
390 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
391 emitBasicBlock(*I);
392
393 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000394 toAsm << ".EndOf_" << methName << ":\n\t.size "
395 << methName << ", .EndOf_"
396 << methName << "-" << methName << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000397
398 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000399 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000400
401 // Forget all about M.
402 Table.purgeMethod();
403}
404
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000405inline bool
406ArrayTypeIsString(ArrayType* arrayType)
407{
408 return (arrayType->getElementType() == Type::UByteTy ||
409 arrayType->getElementType() == Type::SByteTy);
410}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000411
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000412inline const string
413TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000414{
415 switch(type->getPrimitiveID())
416 {
417 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
418 return ".byte";
419 case Type::UShortTyID: case Type::ShortTyID:
420 return ".half";
421 case Type::UIntTyID: case Type::IntTyID:
422 return ".word";
423 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
424 return ".xword";
425 case Type::FloatTyID:
426 return ".single";
427 case Type::DoubleTyID:
428 return ".double";
429 case Type::ArrayTyID:
430 if (ArrayTypeIsString((ArrayType*) type))
431 return ".ascii";
432 else
433 return "<InvaliDataTypeForPrinting>";
434 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000435 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000436 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000437}
438
Vikram S. Adve21447222001-11-10 02:03:06 +0000439// Get the size of the constant for the given target.
440// If this is an unsized array, return 0.
441//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000442inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000443ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000444{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000445 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000446 {
447 ArrayType *aty = cast<ArrayType>(CPA->getType());
448 if (ArrayTypeIsString(aty))
449 return 1 + CPA->getNumOperands();
450 else if (! aty->isSized())
451 return 0;
452 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000453
454 return target.findOptimalStorageSize(CV->getType());
455}
456
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000457inline
458unsigned int TypeToSize(const Type* type, const TargetMachine& target)
459{
460 return target.findOptimalStorageSize(type);
461}
462
463
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000464// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000465// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
466//
467inline unsigned int
468SizeToAlignment(unsigned int size, const TargetMachine& target)
469{
470 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
471 if (size > (unsigned) cacheLineSize / 2)
472 return cacheLineSize;
473 else
474 for (unsigned sz=1; /*no condition*/; sz *= 2)
475 if (sz >= size)
476 return sz;
477}
478
479// Get the size of the type and then use SizeToAlignment.
480// If this is an unsized array, just return the L1 cache line size
481// (viz., the default behavior for large global objects).
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000482//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000483inline unsigned int
484TypeToAlignment(const Type* type, const TargetMachine& target)
485{
Vikram S. Adve21447222001-11-10 02:03:06 +0000486 if (ArrayType* aty = dyn_cast<ArrayType>(type))
487 if (! aty->isSized())
488 return target.getCacheInfo().getCacheLineSize(1);
489
490 return SizeToAlignment(target.findOptimalStorageSize(type), target);
491}
492
493// Get the size of the constant and then use SizeToAlignment.
494// Handles strings as a special case;
495inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000496ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000497{
498 unsigned int constantSize;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000499 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000500 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
501 return SizeToAlignment(1 + CPA->getNumOperands(), target);
502
503 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000504}
505
506
Vikram S. Adve21447222001-11-10 02:03:06 +0000507// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000508void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000509SparcAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000510{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000511 assert(CV->getType() != Type::VoidTy &&
512 CV->getType() != Type::TypeTy &&
513 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000514 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000515
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000516 assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000517 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000518
519 toAsm << "\t"
520 << TypeToDataDirective(CV->getType()) << "\t";
521
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000522 if (CV->getType()->isPrimitiveType())
523 {
524 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
525 toAsm << "0r"; // FP constants must have this prefix
526 toAsm << CV->getStrValue() << endl;
527 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000528 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000529 {
530 if (! CPP->isNullValue())
531 assert(0 && "Cannot yet print non-null pointer constants to assembly");
532 else
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000533 toAsm << (void*) NULL << endl;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000534 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000535 else if (ConstantPointerRef* CPRef = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000536 {
537 assert(0 && "Cannot yet initialize pointer refs in assembly");
538 }
539 else
540 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000541 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000542 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000543}
544
Vikram S. Adve21447222001-11-10 02:03:06 +0000545// Print a constant value or values (it may be an aggregate).
546// Uses printSingleConstant() to print each individual value.
547void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000548SparcAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000549{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000550 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000551
552 if (CPA && isStringCompatible(CPA))
553 { // print the string alone and return
554 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
555 }
556 else if (CPA)
557 { // Not a string. Print the values in successive locations
558 const vector<Use>& constValues = CPA->getValues();
559 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000560 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000561 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000562 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000563 { // Print the fields in successive locations
564 const vector<Use>& constValues = CPS->getValues();
565 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000566 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000567 }
568 else
569 this->printSingleConstant(CV);
570}
571
572// Print a constant (which may be an aggregate) prefixed by all the
573// appropriate directives. Uses printConstantValueOnly() to print the
574// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000575void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000576SparcAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000577{
578 if (valID.length() == 0)
579 valID = getID(CV);
580
Vikram S. Adve21447222001-11-10 02:03:06 +0000581 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000582 << endl;
583
584 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000585 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000586 if (CPA && isStringCompatible(CPA))
587 { // print it as a string and return
588 toAsm << valID << ":" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000589 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000590 return;
591 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000592
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000593 toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000594
595 unsigned int constSize = ConstantToSize(CV, Target);
596 if (constSize)
597 toAsm << "\t.size" << "\t" << valID << ","
598 << constSize << endl;
599
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000600 toAsm << valID << ":" << endl;
601
Vikram S. Adve21447222001-11-10 02:03:06 +0000602 this->printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000603}
604
605
606void
607SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
608{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000609 toAsm << "\t.global\t" << getID(GV) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000610
611 if (GV->hasInitializer())
612 printConstant(GV->getInitializer(), getID(GV));
613 else {
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000614 toAsm << "\t.align\t"
615 << TypeToAlignment(GV->getType()->getValueType(), Target) << endl;
616 toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
617 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000618 << TypeToSize(GV->getType()->getValueType(), Target)
619 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000620 }
621}
622
623
624static void
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000625FoldConstants(const Module *M,
626 hash_set<const Constant*>& moduleConstants)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000627{
628 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
629 if (! (*I)->isExternal())
630 {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000631 const hash_set<const Constant*>& pool =
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000632 MachineCodeForMethod::get(*I).getConstantPoolValues();
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000633 moduleConstants.insert(pool.begin(), pool.end());
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000634 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000635}
636
637
638void
639SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
640{
641 // First, get the constants there were marked by the code generator for
642 // inclusion in the assembly code data area and fold them all into a
643 // single constant pool since there may be lots of duplicates. Also,
644 // lets force these constants into the slot table so that we can get
645 // unique names for unnamed constants also.
646 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000647 hash_set<const Constant*> moduleConstants;
648 FoldConstants(M, moduleConstants);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000649
650 // Now, emit the three data sections separately; the cost of I/O should
651 // make up for the cost of extra passes over the globals list!
652 //
653 // Read-only data section (implies initialized)
654 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
655 {
656 const GlobalVariable* GV = *GI;
657 if (GV->hasInitializer() && GV->isConstant())
658 {
659 if (GI == M->gbegin())
660 enterSection(ReadOnlyData);
661 printGlobalVariable(GV);
662 }
663 }
664
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000665 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
666 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000667 printConstant(*I);
668
669 // Initialized read-write data section
670 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
671 {
672 const GlobalVariable* GV = *GI;
673 if (GV->hasInitializer() && ! GV->isConstant())
674 {
675 if (GI == M->gbegin())
676 enterSection(InitRWData);
677 printGlobalVariable(GV);
678 }
679 }
680
681 // Uninitialized read-write data section
682 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
683 {
684 const GlobalVariable* GV = *GI;
685 if (! GV->hasInitializer())
686 {
687 if (GI == M->gbegin())
688 enterSection(UninitRWData);
689 printGlobalVariable(GV);
690 }
691 }
692
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000693 toAsm << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000694}
695
696
697void
698SparcAsmPrinter::emitModule(const Module *M)
699{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000700 // TODO: Look for a filename annotation on M to emit a .file directive
701 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
702 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000703
704 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000705}
706
707} // End anonymous namespace
708
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709
Chris Lattnere88f78c2001-09-19 13:47:27 +0000710//
711// emitAssembly - Output assembly language code (a .s file) for the specified
712// method. The specified method must have been compiled before this may be
713// used.
714//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000715void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000716UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000717{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000718 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000719}