blob: ae61b8d8e19acd7f1f113ee77937901c0993a847 [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"
Vikram S. Adved198c472002-03-18 03:07:26 +000021#include "llvm/Annotation.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000022#include "llvm/BasicBlock.h"
23#include "llvm/Method.h"
24#include "llvm/Module.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
26#include "Support/HashExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000027#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000028using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000029
30namespace {
31
Vikram S. Adved198c472002-03-18 03:07:26 +000032class GlobalIdTable: public Annotation {
33 static AnnotationID AnnotId;
34 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000035
Vikram S. Adved198c472002-03-18 03:07:26 +000036 typedef std::hash_map<const Value*, int> ValIdMap;
37 typedef ValIdMap::const_iterator ValIdMapConstIterator;
38 typedef ValIdMap:: iterator ValIdMapIterator;
39public:
Chris 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
Vikram S. Adved198c472002-03-18 03:07:26 +000042
43 GlobalIdTable(Module* M) : Annotation(AnnotId) {
44 Table = new SlotCalculator(M, true);
45 }
46 ~GlobalIdTable() {
47 delete Table;
48 Table = NULL;
49 valToIdMap.clear();
50 }
51};
52
53AnnotationID GlobalIdTable::AnnotId =
54 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
55
56//===---------------------------------------------------------------------===//
57// Code Shared By the two printer passes, as a mixin
58//===---------------------------------------------------------------------===//
59
60class AsmPrinter {
61 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000062public:
Chris Lattner697954c2002-01-20 22:54:45 +000063 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000064 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000065
Chris Lattnere88f78c2001-09-19 13:47:27 +000066 enum Sections {
67 Unknown,
68 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000069 ReadOnlyData,
70 InitRWData,
71 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000072 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000073
Chris Lattner59ba1092002-02-04 15:53:23 +000074 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000075 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
76
Chris Lattnerc19b8b12002-02-03 23:41:08 +000077 // (start|end)(Module|Method) - Callback methods to be invoked by subclasses
78 void startModule(Module *M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000079 // Create the global id table if it does not already exist
80 idTable = (GlobalIdTable*) M->getAnnotation(GlobalIdTable::AnnotId);
81 if (idTable == NULL) {
82 idTable = new GlobalIdTable(M);
83 M->addAnnotation(idTable);
84 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000085 }
86 void startMethod(Method *M) {
87 // Make sure the slot table has information about this method...
Vikram S. Adved198c472002-03-18 03:07:26 +000088 idTable->Table->incorporateMethod(M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000089 }
90 void endMethod(Method *M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000091 idTable->Table->purgeMethod(); // Forget all about M.
Chris Lattnerc19b8b12002-02-03 23:41:08 +000092 }
93 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000094 }
95
Vikram S. Adved198c472002-03-18 03:07:26 +000096 // Check if a name is external or accessible from external code.
97 // Only functions can currently be external. "main" is the only name
98 // that is visible externally.
99 bool isExternal(const Value* V) {
100 const Method* meth = dyn_cast<Method>(V);
101 return bool(meth != NULL
102 && (meth->isExternal() || meth->getName() == "main"));
103 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000104
Chris Lattnere88f78c2001-09-19 13:47:27 +0000105 // enterSection - Use this method to enter a different section of the output
106 // executable. This is used to only output neccesary section transitions.
107 //
108 void enterSection(enum Sections S) {
109 if (S == CurSection) return; // Only switch section if neccesary
110 CurSection = S;
111
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000112 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000113 switch (S)
114 {
115 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000116 case Text: toAsm << "\".text\""; break;
117 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
118 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
119 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000120 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000121 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000122 }
123
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000124 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000125 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000126
127 // Symbol names in Sparc assembly language have these rules:
128 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
129 // (b) A name beginning in "." is treated as a local name.
130 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
131 //
132 if (S[0] == '_' || isdigit(S[0]))
133 Result += "ll";
134
135 for (unsigned i = 0; i < S.size(); ++i)
136 {
137 char C = S[i];
138 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
139 Result += C;
140 else
141 {
142 Result += '_';
143 Result += char('0' + ((unsigned char)C >> 4));
144 Result += char('0' + (C & 0xF));
145 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000146 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000147 return Result;
148 }
149
Chris Lattnere88f78c2001-09-19 13:47:27 +0000150 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000151 // the name of the identifier if possible (qualified by the type), and
152 // use a numbered value based on prefix otherwise.
153 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000154 //
155 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
157
158 Result = Result + (V->hasName()? V->getName() : string(Prefix));
159
160 // Qualify all internal names with a unique id.
161 if (!isExternal(V)) {
162 int valId = idTable->Table->getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000163 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000164 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
165 if (I == idTable->valToIdMap.end())
166 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000167 else
168 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000169 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000170 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000171 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000172
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000173 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000174 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000175
Chris Lattnere88f78c2001-09-19 13:47:27 +0000176 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000177 string getID(const Module *M) {
178 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000179 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000180 string getID(const Method *M) {
181 return getID(M, "LLVMMethod_");
182 }
183 string getID(const BasicBlock *BB) {
184 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
185 }
186 string getID(const GlobalVariable *GV) {
187 return getID(GV, "LLVMGlobal_", ".G_");
188 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000189 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000190 return getID(CV, "LLVMConst_", ".C_");
191 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000192};
193
194
195
196//===----------------------------------------------------------------------===//
197// SparcMethodAsmPrinter Code
198//===----------------------------------------------------------------------===//
199
200struct SparcMethodAsmPrinter : public MethodPass, public AsmPrinter {
Chris Lattner59ba1092002-02-04 15:53:23 +0000201 inline SparcMethodAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000202 : AsmPrinter(os, t) {}
203
204 virtual bool doInitialization(Module *M) {
205 startModule(M);
206 return false;
207 }
208
209 virtual bool runOnMethod(Method *M) {
210 startMethod(M);
211 emitMethod(M);
212 endMethod(M);
213 return false;
214 }
215
216 virtual bool doFinalization(Module *M) {
217 endModule();
218 return false;
219 }
220
221 void emitMethod(const Method *M);
222private :
223 void emitBasicBlock(const BasicBlock *BB);
224 void emitMachineInst(const MachineInstr *MI);
225
226 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
227 void printOneOperand(const MachineOperand &Op);
228
229 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
230 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000231
Chris Lattnere88f78c2001-09-19 13:47:27 +0000232 unsigned getOperandMask(unsigned Opcode) {
233 switch (Opcode) {
234 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000235 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000236 default: return 0; // By default, don't hack operands...
237 }
238 }
239};
240
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000241inline bool
242SparcMethodAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
243 unsigned int opNum) {
244 switch (MI->getOpCode()) {
245 case JMPLCALL:
246 case JMPLRET: return (opNum == 0);
247 default: return false;
248 }
249}
250
251
252inline bool
253SparcMethodAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
254 unsigned int opNum) {
255 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
256 return (opNum == 0);
257 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
258 return (opNum == 1);
259 else
260 return false;
261}
262
263
264#define PrintOp1PlusOp2(Op1, Op2) \
265 printOneOperand(Op1); \
266 toAsm << "+"; \
267 printOneOperand(Op2);
268
269unsigned int
270SparcMethodAsmPrinter::printOperands(const MachineInstr *MI,
271 unsigned int opNum)
272{
273 const MachineOperand& Op = MI->getOperand(opNum);
274
275 if (OpIsBranchTargetLabel(MI, opNum))
276 {
277 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
278 return 2;
279 }
280 else if (OpIsMemoryAddressBase(MI, opNum))
281 {
282 toAsm << "[";
283 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
284 toAsm << "]";
285 return 2;
286 }
287 else
288 {
289 printOneOperand(Op);
290 return 1;
291 }
292}
293
294
295void
296SparcMethodAsmPrinter::printOneOperand(const MachineOperand &op)
297{
298 switch (op.getOperandType())
299 {
300 case MachineOperand::MO_VirtualRegister:
301 case MachineOperand::MO_CCRegister:
302 case MachineOperand::MO_MachineRegister:
303 {
304 int RegNum = (int)op.getAllocatedRegNum();
305
306 // ****this code is temporary till NULL Values are fixed
307 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
308 toAsm << "<NULL VALUE>";
309 } else {
310 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
311 }
312 break;
313 }
314
315 case MachineOperand::MO_PCRelativeDisp:
316 {
317 const Value *Val = op.getVRegValue();
318 if (!Val)
319 toAsm << "\t<*NULL Value*>";
320 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
321 toAsm << getID(BB);
322 else if (const Method *M = dyn_cast<const Method>(Val))
323 toAsm << getID(M);
324 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
325 toAsm << getID(GV);
326 else if (const Constant *CV = dyn_cast<const Constant>(Val))
327 toAsm << getID(CV);
328 else
329 toAsm << "<unknown value=" << Val << ">";
330 break;
331 }
332
333 case MachineOperand::MO_SignExtendedImmed:
334 case MachineOperand::MO_UnextendedImmed:
335 toAsm << (long)op.getImmedValue();
336 break;
337
338 default:
339 toAsm << op; // use dump field
340 break;
341 }
342}
343
344
345void
346SparcMethodAsmPrinter::emitMachineInst(const MachineInstr *MI)
347{
348 unsigned Opcode = MI->getOpCode();
349
350 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
351 return; // IGNORE PHI NODES
352
353 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
354
355 unsigned Mask = getOperandMask(Opcode);
356
357 bool NeedComma = false;
358 unsigned N = 1;
359 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
360 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
361 if (NeedComma) toAsm << ", "; // Handle comma outputing
362 NeedComma = true;
363 N = printOperands(MI, OpNum);
364 }
365 else
366 N = 1;
367
368 toAsm << "\n";
369}
370
371void
372SparcMethodAsmPrinter::emitBasicBlock(const BasicBlock *BB)
373{
374 // Emit a label for the basic block
375 toAsm << getID(BB) << ":\n";
376
377 // Get the vector of machine instructions corresponding to this bb.
378 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
379 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
380
381 // Loop over all of the instructions in the basic block...
382 for (; MII != MIE; ++MII)
383 emitMachineInst(*MII);
384 toAsm << "\n"; // Seperate BB's with newlines
385}
386
387void
388SparcMethodAsmPrinter::emitMethod(const Method *M)
389{
390 string methName = getID(M);
391 toAsm << "!****** Outputing Method: " << methName << " ******\n";
392 enterSection(AsmPrinter::Text);
393 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
394 //toAsm << "\t.type\t" << methName << ",#function\n";
395 toAsm << "\t.type\t" << methName << ", 2\n";
396 toAsm << methName << ":\n";
397
398 // Output code for all of the basic blocks in the method...
399 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
400 emitBasicBlock(*I);
401
402 // Output a .size directive so the debugger knows the extents of the function
403 toAsm << ".EndOf_" << methName << ":\n\t.size "
404 << methName << ", .EndOf_"
405 << methName << "-" << methName << "\n";
406
407 // Put some spaces between the methods
408 toAsm << "\n\n";
409}
410
411} // End anonymous namespace
412
413Pass *UltraSparc::getMethodAsmPrinterPass(PassManager &PM, std::ostream &Out) {
414 return new SparcMethodAsmPrinter(Out, *this);
415}
416
417
418
419
420
421//===----------------------------------------------------------------------===//
422// SparcMethodAsmPrinter Code
423//===----------------------------------------------------------------------===//
424
425namespace {
426
427class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
428public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000429 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
430 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431
432 virtual bool run(Module *M) {
433 startModule(M);
434 emitGlobalsAndConstants(M);
435 endModule();
436 return false;
437 }
438
439 void emitGlobalsAndConstants(const Module *M);
440
441 void printGlobalVariable(const GlobalVariable *GV);
442 void printSingleConstant( const Constant* CV);
443 void printConstantValueOnly(const Constant* CV);
444 void printConstant( const Constant* CV, std::string valID = "");
445
446 static void FoldConstants(const Module *M,
447 std::hash_set<const Constant*> &moduleConstants);
448
449};
450
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000451
452// Can we treat the specified array as a string? Only if it is an array of
453// ubytes or non-negative sbytes.
454//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000455static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000456 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
457 if (ETy == Type::UByteTy) return true;
458 if (ETy != Type::SByteTy) return false;
459
460 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000461 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000462 return false;
463
464 return true;
465}
466
467// toOctal - Convert the low order bits of X into an octal letter
468static inline char toOctal(int X) {
469 return (X&7)+'0';
470}
471
472// getAsCString - Return the specified array as a C compatible string, only if
473// the predicate isStringCompatible is true.
474//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000475static string getAsCString(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000476 if (isStringCompatible(CPA)) {
477 string Result;
478 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
479 Result = "\"";
480 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
481 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000482 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
483 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000484
485 if (isprint(C)) {
486 Result += C;
487 } else {
488 switch(C) {
489 case '\a': Result += "\\a"; break;
490 case '\b': Result += "\\b"; break;
491 case '\f': Result += "\\f"; break;
492 case '\n': Result += "\\n"; break;
493 case '\r': Result += "\\r"; break;
494 case '\t': Result += "\\t"; break;
495 case '\v': Result += "\\v"; break;
496 default:
497 Result += '\\';
498 Result += toOctal(C >> 6);
499 Result += toOctal(C >> 3);
500 Result += toOctal(C >> 0);
501 break;
502 }
503 }
504 }
505 Result += "\"";
506
507 return Result;
508 } else {
509 return CPA->getStrValue();
510 }
511}
512
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000513inline bool
514ArrayTypeIsString(ArrayType* arrayType)
515{
516 return (arrayType->getElementType() == Type::UByteTy ||
517 arrayType->getElementType() == Type::SByteTy);
518}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000519
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000520inline const string
521TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000522{
523 switch(type->getPrimitiveID())
524 {
525 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
526 return ".byte";
527 case Type::UShortTyID: case Type::ShortTyID:
528 return ".half";
529 case Type::UIntTyID: case Type::IntTyID:
530 return ".word";
531 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
532 return ".xword";
533 case Type::FloatTyID:
534 return ".single";
535 case Type::DoubleTyID:
536 return ".double";
537 case Type::ArrayTyID:
538 if (ArrayTypeIsString((ArrayType*) type))
539 return ".ascii";
540 else
541 return "<InvaliDataTypeForPrinting>";
542 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000543 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000544 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000545}
546
Vikram S. Adve21447222001-11-10 02:03:06 +0000547// Get the size of the constant for the given target.
548// If this is an unsized array, return 0.
549//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000550inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000551ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000552{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000553 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000554 {
555 ArrayType *aty = cast<ArrayType>(CPA->getType());
556 if (ArrayTypeIsString(aty))
557 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000558 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000559
560 return target.findOptimalStorageSize(CV->getType());
561}
562
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000563
564
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000565// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000566// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
567//
568inline unsigned int
569SizeToAlignment(unsigned int size, const TargetMachine& target)
570{
571 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
572 if (size > (unsigned) cacheLineSize / 2)
573 return cacheLineSize;
574 else
575 for (unsigned sz=1; /*no condition*/; sz *= 2)
576 if (sz >= size)
577 return sz;
578}
579
580// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000581//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000582inline unsigned int
583TypeToAlignment(const Type* type, const TargetMachine& target)
584{
Vikram S. Adve21447222001-11-10 02:03:06 +0000585 return SizeToAlignment(target.findOptimalStorageSize(type), target);
586}
587
588// Get the size of the constant and then use SizeToAlignment.
589// Handles strings as a special case;
590inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000591ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000592{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000593 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000594 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
595 return SizeToAlignment(1 + CPA->getNumOperands(), target);
596
597 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000598}
599
600
Vikram S. Adve21447222001-11-10 02:03:06 +0000601// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000602void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000603SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000604{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000605 assert(CV->getType() != Type::VoidTy &&
606 CV->getType() != Type::TypeTy &&
607 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000608 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000609
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000610 assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000611 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000612
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000613 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000614
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000615 if (CV->getType()->isPrimitiveType())
616 {
617 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
618 toAsm << "0r"; // FP constants must have this prefix
Chris Lattner697954c2002-01-20 22:54:45 +0000619 toAsm << CV->getStrValue() << "\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000620 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000621 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000622 {
Chris Lattner697954c2002-01-20 22:54:45 +0000623 assert(CPP->isNullValue() &&
624 "Cannot yet print non-null pointer constants to assembly");
625 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000626 }
Chris Lattner697954c2002-01-20 22:54:45 +0000627 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000628 {
629 assert(0 && "Cannot yet initialize pointer refs in assembly");
630 }
631 else
632 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000633 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000634 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000635}
636
Vikram S. Adve21447222001-11-10 02:03:06 +0000637// Print a constant value or values (it may be an aggregate).
638// Uses printSingleConstant() to print each individual value.
639void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000640SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000641{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000642 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000643
644 if (CPA && isStringCompatible(CPA))
645 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000646 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000647 }
648 else if (CPA)
649 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000650 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000651 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000652 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000653 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000654 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000655 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000656 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000657 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000658 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000659 }
660 else
661 this->printSingleConstant(CV);
662}
663
664// Print a constant (which may be an aggregate) prefixed by all the
665// appropriate directives. Uses printConstantValueOnly() to print the
666// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000667void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000668SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000669{
670 if (valID.length() == 0)
671 valID = getID(CV);
672
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000673 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000674
675 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000676 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000677 if (CPA && isStringCompatible(CPA))
678 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000679 toAsm << valID << ":\n";
680 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000681 return;
682 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000683
Chris Lattner697954c2002-01-20 22:54:45 +0000684 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000685
686 unsigned int constSize = ConstantToSize(CV, Target);
687 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000688 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000689
Chris Lattner697954c2002-01-20 22:54:45 +0000690 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000691
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000692 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000693}
694
695
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000696void SparcModuleAsmPrinter::FoldConstants(const Module *M,
697 std::hash_set<const Constant*> &MC) {
698 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
699 if (!(*I)->isExternal()) {
700 const std::hash_set<const Constant*> &pool =
701 MachineCodeForMethod::get(*I).getConstantPoolValues();
702 MC.insert(pool.begin(), pool.end());
703 }
704}
705
706void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707{
Chris Lattner697954c2002-01-20 22:54:45 +0000708 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709
710 if (GV->hasInitializer())
711 printConstant(GV->getInitializer(), getID(GV));
712 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000713 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
714 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000715 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000716 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000717 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000718 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000719 }
720}
721
722
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000723void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000724 // First, get the constants there were marked by the code generator for
725 // inclusion in the assembly code data area and fold them all into a
726 // single constant pool since there may be lots of duplicates. Also,
727 // lets force these constants into the slot table so that we can get
728 // unique names for unnamed constants also.
729 //
Chris Lattner697954c2002-01-20 22:54:45 +0000730 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000731 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000732
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000733 // Now, emit the three data sections separately; the cost of I/O should
734 // make up for the cost of extra passes over the globals list!
735 //
736 // Read-only data section (implies initialized)
737 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
738 {
739 const GlobalVariable* GV = *GI;
740 if (GV->hasInitializer() && GV->isConstant())
741 {
742 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000743 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000744 printGlobalVariable(GV);
745 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000746 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000747
Chris Lattner697954c2002-01-20 22:54:45 +0000748 for (std::hash_set<const Constant*>::const_iterator
749 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000750 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000751 printConstant(*I);
752
753 // Initialized read-write data section
754 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
755 {
756 const GlobalVariable* GV = *GI;
757 if (GV->hasInitializer() && ! GV->isConstant())
758 {
759 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000760 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000761 printGlobalVariable(GV);
762 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000763 }
764
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765 // Uninitialized read-write data section
766 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
767 {
768 const GlobalVariable* GV = *GI;
769 if (! GV->hasInitializer())
770 {
771 if (GI == M->gbegin())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000772 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000773 printGlobalVariable(GV);
774 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000775 }
776
Chris Lattner697954c2002-01-20 22:54:45 +0000777 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000778}
779
Chris Lattnere88f78c2001-09-19 13:47:27 +0000780} // End anonymous namespace
781
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000782Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
783 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000784}