blob: 18c552a69b26d4c31ad31d125183c1b562f369fc [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 Lattnerc19b8b12002-02-03 23:41:08 +00007// This code largely consists of two LLVM Pass's: a MethodPass and a Pass. The
8// MethodPass is pipelined together with all of the rest of the code generation
9// stages, and the Pass runs at the end to emit code for global variables and
10// such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInternals.h"
15#include "llvm/Analysis/SlotCalculator.h"
16#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerc019a172002-02-03 07:48:06 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/GlobalVariable.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000019#include "llvm/ConstantVals.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000020#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000021#include "llvm/BasicBlock.h"
22#include "llvm/Method.h"
23#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
25#include "Support/HashExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000026#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000028
29namespace {
30
Chris Lattnerc19b8b12002-02-03 23:41:08 +000031//===----------------------------------------------------------------------===//
32// Code Shared By the two printer passes, as a mixin
33//===----------------------------------------------------------------------===//
Chris Lattnere88f78c2001-09-19 13:47:27 +000034
Chris Lattnerc19b8b12002-02-03 23:41:08 +000035class AsmPrinter {
Chris Lattner697954c2002-01-20 22:54:45 +000036 typedef std::hash_map<const Value*, int> ValIdMap;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000037 typedef ValIdMap:: iterator ValIdMapIterator;
38 typedef ValIdMap::const_iterator ValIdMapConstIterator;
39
Chris Lattnerc19b8b12002-02-03 23:41:08 +000040 SlotCalculator *Table; // map anonymous values to unique integer IDs
41 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
42public:
Chris Lattner697954c2002-01-20 22:54:45 +000043 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000044 const TargetMachine &Target;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000045
Chris Lattnere88f78c2001-09-19 13:47:27 +000046 enum Sections {
47 Unknown,
48 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000049 ReadOnlyData,
50 InitRWData,
51 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000052 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000053
Chris Lattner59ba1092002-02-04 15:53:23 +000054 AsmPrinter(std::ostream &os, const TargetMachine &T)
Chris Lattnerc19b8b12002-02-03 23:41:08 +000055 : Table(0), toAsm(os), Target(T), CurSection(Unknown) {}
56
57
58 // (start|end)(Module|Method) - Callback methods to be invoked by subclasses
59 void startModule(Module *M) {
60 Table = new SlotCalculator(M, true);
61 }
62 void startMethod(Method *M) {
63 // Make sure the slot table has information about this method...
64 Table->incorporateMethod(M);
65 }
66 void endMethod(Method *M) {
67 Table->purgeMethod(); // Forget all about M.
68 }
69 void endModule() {
70 delete Table; Table = 0;
71 valToIdMap.clear();
Chris Lattnere88f78c2001-09-19 13:47:27 +000072 }
73
Vikram S. Adve953c83e2001-10-28 21:38:52 +000074
Chris Lattnere88f78c2001-09-19 13:47:27 +000075 // enterSection - Use this method to enter a different section of the output
76 // executable. This is used to only output neccesary section transitions.
77 //
78 void enterSection(enum Sections S) {
79 if (S == CurSection) return; // Only switch section if neccesary
80 CurSection = S;
81
Vikram S. Adve29ff8732001-11-08 05:12:37 +000082 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000083 switch (S)
84 {
85 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000086 case Text: toAsm << "\".text\""; break;
87 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
88 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
89 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000090 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000091 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000092 }
93
Chris Lattnerc19b8b12002-02-03 23:41:08 +000094 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000095 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000096
97 // Symbol names in Sparc assembly language have these rules:
98 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
99 // (b) A name beginning in "." is treated as a local name.
100 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
101 //
102 if (S[0] == '_' || isdigit(S[0]))
103 Result += "ll";
104
105 for (unsigned i = 0; i < S.size(); ++i)
106 {
107 char C = S[i];
108 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
109 Result += C;
110 else
111 {
112 Result += '_';
113 Result += char('0' + ((unsigned char)C >> 4));
114 Result += char('0' + (C & 0xF));
115 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000116 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000117 return Result;
118 }
119
Chris Lattnere88f78c2001-09-19 13:47:27 +0000120 // getID - Return a valid identifier for the specified value. Base it on
121 // the name of the identifier if possible, use a numbered value based on
122 // prefix otherwise. FPrefix is always prepended to the output identifier.
123 //
124 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000125 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000126 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
127 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000128 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000129 } else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000130 int valId = Table->getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000131 if (valId == -1) {
132 ValIdMapConstIterator I = valToIdMap.find(V);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000133 if (I == valToIdMap.end())
134 valId = valToIdMap[V] = valToIdMap.size();
135 else
136 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000137 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000138 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000140 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000141 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000142
Chris Lattnere88f78c2001-09-19 13:47:27 +0000143 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000144 string getID(const Module *M) {
145 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000146 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000147 string getID(const Method *M) {
148 return getID(M, "LLVMMethod_");
149 }
150 string getID(const BasicBlock *BB) {
151 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
152 }
153 string getID(const GlobalVariable *GV) {
154 return getID(GV, "LLVMGlobal_", ".G_");
155 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000156 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000157 return getID(CV, "LLVMConst_", ".C_");
158 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000159};
160
161
162
163//===----------------------------------------------------------------------===//
164// SparcMethodAsmPrinter Code
165//===----------------------------------------------------------------------===//
166
167struct SparcMethodAsmPrinter : public MethodPass, public AsmPrinter {
Chris Lattner59ba1092002-02-04 15:53:23 +0000168 inline SparcMethodAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000169 : AsmPrinter(os, t) {}
170
171 virtual bool doInitialization(Module *M) {
172 startModule(M);
173 return false;
174 }
175
176 virtual bool runOnMethod(Method *M) {
177 startMethod(M);
178 emitMethod(M);
179 endMethod(M);
180 return false;
181 }
182
183 virtual bool doFinalization(Module *M) {
184 endModule();
185 return false;
186 }
187
188 void emitMethod(const Method *M);
189private :
190 void emitBasicBlock(const BasicBlock *BB);
191 void emitMachineInst(const MachineInstr *MI);
192
193 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
194 void printOneOperand(const MachineOperand &Op);
195
196 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
197 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000198
Chris Lattnere88f78c2001-09-19 13:47:27 +0000199 unsigned getOperandMask(unsigned Opcode) {
200 switch (Opcode) {
201 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000202 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000203 default: return 0; // By default, don't hack operands...
204 }
205 }
206};
207
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000208inline bool
209SparcMethodAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
210 unsigned int opNum) {
211 switch (MI->getOpCode()) {
212 case JMPLCALL:
213 case JMPLRET: return (opNum == 0);
214 default: return false;
215 }
216}
217
218
219inline bool
220SparcMethodAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
221 unsigned int opNum) {
222 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
223 return (opNum == 0);
224 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
225 return (opNum == 1);
226 else
227 return false;
228}
229
230
231#define PrintOp1PlusOp2(Op1, Op2) \
232 printOneOperand(Op1); \
233 toAsm << "+"; \
234 printOneOperand(Op2);
235
236unsigned int
237SparcMethodAsmPrinter::printOperands(const MachineInstr *MI,
238 unsigned int opNum)
239{
240 const MachineOperand& Op = MI->getOperand(opNum);
241
242 if (OpIsBranchTargetLabel(MI, opNum))
243 {
244 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
245 return 2;
246 }
247 else if (OpIsMemoryAddressBase(MI, opNum))
248 {
249 toAsm << "[";
250 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
251 toAsm << "]";
252 return 2;
253 }
254 else
255 {
256 printOneOperand(Op);
257 return 1;
258 }
259}
260
261
262void
263SparcMethodAsmPrinter::printOneOperand(const MachineOperand &op)
264{
265 switch (op.getOperandType())
266 {
267 case MachineOperand::MO_VirtualRegister:
268 case MachineOperand::MO_CCRegister:
269 case MachineOperand::MO_MachineRegister:
270 {
271 int RegNum = (int)op.getAllocatedRegNum();
272
273 // ****this code is temporary till NULL Values are fixed
274 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
275 toAsm << "<NULL VALUE>";
276 } else {
277 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
278 }
279 break;
280 }
281
282 case MachineOperand::MO_PCRelativeDisp:
283 {
284 const Value *Val = op.getVRegValue();
285 if (!Val)
286 toAsm << "\t<*NULL Value*>";
287 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
288 toAsm << getID(BB);
289 else if (const Method *M = dyn_cast<const Method>(Val))
290 toAsm << getID(M);
291 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
292 toAsm << getID(GV);
293 else if (const Constant *CV = dyn_cast<const Constant>(Val))
294 toAsm << getID(CV);
295 else
296 toAsm << "<unknown value=" << Val << ">";
297 break;
298 }
299
300 case MachineOperand::MO_SignExtendedImmed:
301 case MachineOperand::MO_UnextendedImmed:
302 toAsm << (long)op.getImmedValue();
303 break;
304
305 default:
306 toAsm << op; // use dump field
307 break;
308 }
309}
310
311
312void
313SparcMethodAsmPrinter::emitMachineInst(const MachineInstr *MI)
314{
315 unsigned Opcode = MI->getOpCode();
316
317 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
318 return; // IGNORE PHI NODES
319
320 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
321
322 unsigned Mask = getOperandMask(Opcode);
323
324 bool NeedComma = false;
325 unsigned N = 1;
326 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
327 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
328 if (NeedComma) toAsm << ", "; // Handle comma outputing
329 NeedComma = true;
330 N = printOperands(MI, OpNum);
331 }
332 else
333 N = 1;
334
335 toAsm << "\n";
336}
337
338void
339SparcMethodAsmPrinter::emitBasicBlock(const BasicBlock *BB)
340{
341 // Emit a label for the basic block
342 toAsm << getID(BB) << ":\n";
343
344 // Get the vector of machine instructions corresponding to this bb.
345 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
346 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
347
348 // Loop over all of the instructions in the basic block...
349 for (; MII != MIE; ++MII)
350 emitMachineInst(*MII);
351 toAsm << "\n"; // Seperate BB's with newlines
352}
353
354void
355SparcMethodAsmPrinter::emitMethod(const Method *M)
356{
357 string methName = getID(M);
358 toAsm << "!****** Outputing Method: " << methName << " ******\n";
359 enterSection(AsmPrinter::Text);
360 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
361 //toAsm << "\t.type\t" << methName << ",#function\n";
362 toAsm << "\t.type\t" << methName << ", 2\n";
363 toAsm << methName << ":\n";
364
365 // Output code for all of the basic blocks in the method...
366 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
367 emitBasicBlock(*I);
368
369 // Output a .size directive so the debugger knows the extents of the function
370 toAsm << ".EndOf_" << methName << ":\n\t.size "
371 << methName << ", .EndOf_"
372 << methName << "-" << methName << "\n";
373
374 // Put some spaces between the methods
375 toAsm << "\n\n";
376}
377
378} // End anonymous namespace
379
380Pass *UltraSparc::getMethodAsmPrinterPass(PassManager &PM, std::ostream &Out) {
381 return new SparcMethodAsmPrinter(Out, *this);
382}
383
384
385
386
387
388//===----------------------------------------------------------------------===//
389// SparcMethodAsmPrinter Code
390//===----------------------------------------------------------------------===//
391
392namespace {
393
394class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
395public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000396 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
397 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000398
399 virtual bool run(Module *M) {
400 startModule(M);
401 emitGlobalsAndConstants(M);
402 endModule();
403 return false;
404 }
405
406 void emitGlobalsAndConstants(const Module *M);
407
408 void printGlobalVariable(const GlobalVariable *GV);
409 void printSingleConstant( const Constant* CV);
410 void printConstantValueOnly(const Constant* CV);
411 void printConstant( const Constant* CV, std::string valID = "");
412
413 static void FoldConstants(const Module *M,
414 std::hash_set<const Constant*> &moduleConstants);
415
416};
417
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000418
419// Can we treat the specified array as a string? Only if it is an array of
420// ubytes or non-negative sbytes.
421//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000422static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000423 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
424 if (ETy == Type::UByteTy) return true;
425 if (ETy != Type::SByteTy) return false;
426
427 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000428 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000429 return false;
430
431 return true;
432}
433
434// toOctal - Convert the low order bits of X into an octal letter
435static inline char toOctal(int X) {
436 return (X&7)+'0';
437}
438
439// getAsCString - Return the specified array as a C compatible string, only if
440// the predicate isStringCompatible is true.
441//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000442static string getAsCString(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000443 if (isStringCompatible(CPA)) {
444 string Result;
445 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
446 Result = "\"";
447 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
448 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000449 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
450 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000451
452 if (isprint(C)) {
453 Result += C;
454 } else {
455 switch(C) {
456 case '\a': Result += "\\a"; break;
457 case '\b': Result += "\\b"; break;
458 case '\f': Result += "\\f"; break;
459 case '\n': Result += "\\n"; break;
460 case '\r': Result += "\\r"; break;
461 case '\t': Result += "\\t"; break;
462 case '\v': Result += "\\v"; break;
463 default:
464 Result += '\\';
465 Result += toOctal(C >> 6);
466 Result += toOctal(C >> 3);
467 Result += toOctal(C >> 0);
468 break;
469 }
470 }
471 }
472 Result += "\"";
473
474 return Result;
475 } else {
476 return CPA->getStrValue();
477 }
478}
479
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000480inline bool
481ArrayTypeIsString(ArrayType* arrayType)
482{
483 return (arrayType->getElementType() == Type::UByteTy ||
484 arrayType->getElementType() == Type::SByteTy);
485}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000486
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000487inline const string
488TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000489{
490 switch(type->getPrimitiveID())
491 {
492 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
493 return ".byte";
494 case Type::UShortTyID: case Type::ShortTyID:
495 return ".half";
496 case Type::UIntTyID: case Type::IntTyID:
497 return ".word";
498 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
499 return ".xword";
500 case Type::FloatTyID:
501 return ".single";
502 case Type::DoubleTyID:
503 return ".double";
504 case Type::ArrayTyID:
505 if (ArrayTypeIsString((ArrayType*) type))
506 return ".ascii";
507 else
508 return "<InvaliDataTypeForPrinting>";
509 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000510 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000511 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000512}
513
Vikram S. Adve21447222001-11-10 02:03:06 +0000514// Get the size of the constant for the given target.
515// If this is an unsized array, return 0.
516//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000517inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000518ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000519{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000520 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000521 {
522 ArrayType *aty = cast<ArrayType>(CPA->getType());
523 if (ArrayTypeIsString(aty))
524 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000525 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000526
527 return target.findOptimalStorageSize(CV->getType());
528}
529
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000530
531
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000532// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000533// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
534//
535inline unsigned int
536SizeToAlignment(unsigned int size, const TargetMachine& target)
537{
538 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
539 if (size > (unsigned) cacheLineSize / 2)
540 return cacheLineSize;
541 else
542 for (unsigned sz=1; /*no condition*/; sz *= 2)
543 if (sz >= size)
544 return sz;
545}
546
547// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000548//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000549inline unsigned int
550TypeToAlignment(const Type* type, const TargetMachine& target)
551{
Vikram S. Adve21447222001-11-10 02:03:06 +0000552 return SizeToAlignment(target.findOptimalStorageSize(type), target);
553}
554
555// Get the size of the constant and then use SizeToAlignment.
556// Handles strings as a special case;
557inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000558ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000559{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000560 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000561 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
562 return SizeToAlignment(1 + CPA->getNumOperands(), target);
563
564 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000565}
566
567
Vikram S. Adve21447222001-11-10 02:03:06 +0000568// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000569void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000570SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000571{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000572 assert(CV->getType() != Type::VoidTy &&
573 CV->getType() != Type::TypeTy &&
574 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000575 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000576
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000577 assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000578 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000579
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000580 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000581
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000582 if (CV->getType()->isPrimitiveType())
583 {
584 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
585 toAsm << "0r"; // FP constants must have this prefix
Chris Lattner697954c2002-01-20 22:54:45 +0000586 toAsm << CV->getStrValue() << "\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000587 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000588 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000589 {
Chris Lattner697954c2002-01-20 22:54:45 +0000590 assert(CPP->isNullValue() &&
591 "Cannot yet print non-null pointer constants to assembly");
592 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000593 }
Chris Lattner697954c2002-01-20 22:54:45 +0000594 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000595 {
596 assert(0 && "Cannot yet initialize pointer refs in assembly");
597 }
598 else
599 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000600 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000601 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000602}
603
Vikram S. Adve21447222001-11-10 02:03:06 +0000604// Print a constant value or values (it may be an aggregate).
605// Uses printSingleConstant() to print each individual value.
606void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000607SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000608{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000609 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000610
611 if (CPA && isStringCompatible(CPA))
612 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000613 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000614 }
615 else if (CPA)
616 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000617 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000618 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000619 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000620 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000621 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000622 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000623 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000624 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000625 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000626 }
627 else
628 this->printSingleConstant(CV);
629}
630
631// Print a constant (which may be an aggregate) prefixed by all the
632// appropriate directives. Uses printConstantValueOnly() to print the
633// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000634void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000635SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000636{
637 if (valID.length() == 0)
638 valID = getID(CV);
639
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000640 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000641
642 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000643 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000644 if (CPA && isStringCompatible(CPA))
645 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000646 toAsm << valID << ":\n";
647 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000648 return;
649 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000650
Chris Lattner697954c2002-01-20 22:54:45 +0000651 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000652
653 unsigned int constSize = ConstantToSize(CV, Target);
654 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000655 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000656
Chris Lattner697954c2002-01-20 22:54:45 +0000657 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000658
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000659 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000660}
661
662
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000663void SparcModuleAsmPrinter::FoldConstants(const Module *M,
664 std::hash_set<const Constant*> &MC) {
665 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
666 if (!(*I)->isExternal()) {
667 const std::hash_set<const Constant*> &pool =
668 MachineCodeForMethod::get(*I).getConstantPoolValues();
669 MC.insert(pool.begin(), pool.end());
670 }
671}
672
673void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000674{
Chris Lattner697954c2002-01-20 22:54:45 +0000675 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000676
677 if (GV->hasInitializer())
678 printConstant(GV->getInitializer(), getID(GV));
679 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000680 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
681 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000682 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000683 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000684 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000685 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000686 }
687}
688
689
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000690void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000691 // First, get the constants there were marked by the code generator for
692 // inclusion in the assembly code data area and fold them all into a
693 // single constant pool since there may be lots of duplicates. Also,
694 // lets force these constants into the slot table so that we can get
695 // unique names for unnamed constants also.
696 //
Chris Lattner697954c2002-01-20 22:54:45 +0000697 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000698 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000699
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000700 // Now, emit the three data sections separately; the cost of I/O should
701 // make up for the cost of extra passes over the globals list!
702 //
703 // Read-only data section (implies initialized)
704 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
705 {
706 const GlobalVariable* GV = *GI;
707 if (GV->hasInitializer() && GV->isConstant())
708 {
709 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000710 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000711 printGlobalVariable(GV);
712 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000713 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714
Chris Lattner697954c2002-01-20 22:54:45 +0000715 for (std::hash_set<const Constant*>::const_iterator
716 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000717 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000718 printConstant(*I);
719
720 // Initialized read-write data section
721 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
722 {
723 const GlobalVariable* GV = *GI;
724 if (GV->hasInitializer() && ! GV->isConstant())
725 {
726 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000727 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000728 printGlobalVariable(GV);
729 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000730 }
731
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000732 // Uninitialized read-write data section
733 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
734 {
735 const GlobalVariable* GV = *GI;
736 if (! GV->hasInitializer())
737 {
738 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000739 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000740 printGlobalVariable(GV);
741 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000742 }
743
Chris Lattner697954c2002-01-20 22:54:45 +0000744 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000745}
746
Chris Lattnere88f78c2001-09-19 13:47:27 +0000747} // End anonymous namespace
748
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000749Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
750 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000751}