blob: 83e54871652926d4ba0338103259cc3ca0f71d9d [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2//
3// This file implements all of the stuff neccesary to output a .s file from
4// LLVM. The code in this file assumes that the specified module has already
5// been compiled into the internal data structures of the Module.
6//
Chris Lattnerf57b8452002-04-27 06:56:12 +00007// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
8// The FunctionPass is pipelined together with all of the rest of the code
9// generation stages, and the Pass runs at the end to emit code for global
10// variables and such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInternals.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerc019a172002-02-03 07:48:06 +000016#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000017#include "llvm/GlobalVariable.h"
Chris Lattnere9bb2df2001-12-03 22:26:30 +000018#include "llvm/ConstantVals.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000019#include "llvm/DerivedTypes.h"
Vikram S. Adved198c472002-03-18 03:07:26 +000020#include "llvm/Annotation.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000021#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000023#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000024#include "llvm/SlotCalculator.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000025#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000026#include "Support/StringExtras.h"
27#include "Support/HashExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000028#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000029using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000030
31namespace {
32
Vikram S. Adved198c472002-03-18 03:07:26 +000033class GlobalIdTable: public Annotation {
34 static AnnotationID AnnotId;
35 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000036
Vikram S. Adved198c472002-03-18 03:07:26 +000037 typedef std::hash_map<const Value*, int> ValIdMap;
38 typedef ValIdMap::const_iterator ValIdMapConstIterator;
39 typedef ValIdMap:: iterator ValIdMapIterator;
40public:
Chris Lattnerc19b8b12002-02-03 23:41:08 +000041 SlotCalculator *Table; // map anonymous values to unique integer IDs
42 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000043
44 GlobalIdTable(Module* M) : Annotation(AnnotId) {
45 Table = new SlotCalculator(M, true);
46 }
47 ~GlobalIdTable() {
48 delete Table;
49 Table = NULL;
50 valToIdMap.clear();
51 }
52};
53
54AnnotationID GlobalIdTable::AnnotId =
55 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
56
57//===---------------------------------------------------------------------===//
58// Code Shared By the two printer passes, as a mixin
59//===---------------------------------------------------------------------===//
60
61class AsmPrinter {
62 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000063public:
Chris Lattner697954c2002-01-20 22:54:45 +000064 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000065 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000066
Chris Lattnere88f78c2001-09-19 13:47:27 +000067 enum Sections {
68 Unknown,
69 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000070 ReadOnlyData,
71 InitRWData,
72 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000073 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000074
Chris Lattner59ba1092002-02-04 15:53:23 +000075 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000076 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
77
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000078 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 void startModule(Module *M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000080 // Create the global id table if it does not already exist
81 idTable = (GlobalIdTable*) M->getAnnotation(GlobalIdTable::AnnotId);
82 if (idTable == NULL) {
83 idTable = new GlobalIdTable(M);
84 M->addAnnotation(idTable);
85 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000086 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000087 void startFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000088 // Make sure the slot table has information about this function...
89 idTable->Table->incorporateFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000090 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 void endFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000092 idTable->Table->purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000093 }
94 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000095 }
96
Vikram S. Adved198c472002-03-18 03:07:26 +000097 // Check if a name is external or accessible from external code.
98 // Only functions can currently be external. "main" is the only name
99 // that is visible externally.
100 bool isExternal(const Value* V) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000101 const Function *F = dyn_cast<Function>(V);
102 return F && (F->isExternal() || F->getName() == "main");
Vikram S. Adved198c472002-03-18 03:07:26 +0000103 }
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 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000180 string getID(const Function *F) {
181 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000182 }
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//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000197// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000198//===----------------------------------------------------------------------===//
199
Chris Lattnerf57b8452002-04-27 06:56:12 +0000200struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000201 inline SparcFunctionAsmPrinter(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
Chris Lattnerf57b8452002-04-27 06:56:12 +0000209 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000210 startFunction(F);
211 emitFunction(F);
212 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000213 return false;
214 }
215
216 virtual bool doFinalization(Module *M) {
217 endModule();
218 return false;
219 }
220
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000221 void emitFunction(const Function *F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000222private :
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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000242SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
243 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000244 switch (MI->getOpCode()) {
245 case JMPLCALL:
246 case JMPLRET: return (opNum == 0);
247 default: return false;
248 }
249}
250
251
252inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000253SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
254 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000255 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000271 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000296SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &op)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000297{
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
Vikram S. Advefbd21612002-03-31 19:03:58 +0000306 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000307 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);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000322 else if (const Function *M = dyn_cast<const Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000323 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000346SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347{
348 unsigned Opcode = MI->getOpCode();
349
350 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
351 return; // IGNORE PHI NODES
352
353 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
354
355 unsigned Mask = getOperandMask(Opcode);
356
357 bool NeedComma = false;
358 unsigned N = 1;
359 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
360 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
361 if (NeedComma) toAsm << ", "; // Handle comma outputing
362 NeedComma = true;
363 N = printOperands(MI, OpNum);
364 }
365 else
366 N = 1;
367
368 toAsm << "\n";
369}
370
371void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000372SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000373{
374 // Emit a label for the basic block
375 toAsm << getID(BB) << ":\n";
376
377 // Get the vector of machine instructions corresponding to this bb.
378 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
379 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
380
381 // Loop over all of the instructions in the basic block...
382 for (; MII != MIE; ++MII)
383 emitMachineInst(*MII);
384 toAsm << "\n"; // Seperate BB's with newlines
385}
386
387void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000388SparcFunctionAsmPrinter::emitFunction(const Function *M)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389{
390 string methName = getID(M);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000391 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392 enterSection(AsmPrinter::Text);
393 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
394 //toAsm << "\t.type\t" << methName << ",#function\n";
395 toAsm << "\t.type\t" << methName << ", 2\n";
396 toAsm << methName << ":\n";
397
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000398 // Output code for all of the basic blocks in the function...
399 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000400 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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000407 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000408 toAsm << "\n\n";
409}
410
411} // End anonymous namespace
412
Chris Lattnerf57b8452002-04-27 06:56:12 +0000413Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000414 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415}
416
417
418
419
420
421//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000422// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000423//===----------------------------------------------------------------------===//
424
425namespace {
426
427class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
428public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000429 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
430 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431
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 Lattner4b1de8e2002-04-18 18:15:38 +0000476 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000477
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000478 string Result;
479 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
480 Result = "\"";
481 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
482 unsigned char C = (ETy == Type::SByteTy) ?
483 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
484 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
485
486 if (isprint(C)) {
487 Result += C;
488 } else {
489 switch(C) {
490 case '\a': Result += "\\a"; break;
491 case '\b': Result += "\\b"; break;
492 case '\f': Result += "\\f"; break;
493 case '\n': Result += "\\n"; break;
494 case '\r': Result += "\\r"; break;
495 case '\t': Result += "\\t"; break;
496 case '\v': Result += "\\v"; break;
497 default:
498 Result += '\\';
499 Result += toOctal(C >> 6);
500 Result += toOctal(C >> 3);
501 Result += toOctal(C >> 0);
502 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000503 }
504 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000505 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000506 Result += "\"";
507
508 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000509}
510
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000511inline bool
512ArrayTypeIsString(ArrayType* arrayType)
513{
514 return (arrayType->getElementType() == Type::UByteTy ||
515 arrayType->getElementType() == Type::SByteTy);
516}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000517
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000518inline const string
519TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000520{
521 switch(type->getPrimitiveID())
522 {
523 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
524 return ".byte";
525 case Type::UShortTyID: case Type::ShortTyID:
526 return ".half";
527 case Type::UIntTyID: case Type::IntTyID:
528 return ".word";
529 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
530 return ".xword";
531 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000532 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000533 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000534 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000535 case Type::ArrayTyID:
536 if (ArrayTypeIsString((ArrayType*) type))
537 return ".ascii";
538 else
539 return "<InvaliDataTypeForPrinting>";
540 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000541 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000542 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000543}
544
Vikram S. Adve21447222001-11-10 02:03:06 +0000545// Get the size of the constant for the given target.
546// If this is an unsized array, return 0.
547//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000548inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000549ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000550{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000551 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000552 {
553 ArrayType *aty = cast<ArrayType>(CPA->getType());
554 if (ArrayTypeIsString(aty))
555 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000556 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000557
558 return target.findOptimalStorageSize(CV->getType());
559}
560
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000561
562
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000563// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000564// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
565//
566inline unsigned int
567SizeToAlignment(unsigned int size, const TargetMachine& target)
568{
569 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
570 if (size > (unsigned) cacheLineSize / 2)
571 return cacheLineSize;
572 else
573 for (unsigned sz=1; /*no condition*/; sz *= 2)
574 if (sz >= size)
575 return sz;
576}
577
578// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000579//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000580inline unsigned int
581TypeToAlignment(const Type* type, const TargetMachine& target)
582{
Vikram S. Adve21447222001-11-10 02:03:06 +0000583 return SizeToAlignment(target.findOptimalStorageSize(type), target);
584}
585
586// Get the size of the constant and then use SizeToAlignment.
587// Handles strings as a special case;
588inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000589ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000590{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000591 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000592 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
593 return SizeToAlignment(1 + CPA->getNumOperands(), target);
594
595 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000596}
597
598
Vikram S. Adve21447222001-11-10 02:03:06 +0000599// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000600void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000601SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000602{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000603 assert(CV->getType() != Type::VoidTy &&
604 CV->getType() != Type::TypeTy &&
605 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000606 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000607
Chris Lattnerf678dc62002-04-11 21:44:02 +0000608 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
609 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000610
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000611 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000612
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000613 if (CV->getType()->isPrimitiveType())
614 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000615 if (CV->getType()->isFloatingPoint()) {
616 // FP Constants are printed as integer constants to avoid losing
617 // precision...
618 double Val = cast<ConstantFP>(CV)->getValue();
619 if (CV->getType() == Type::FloatTy) {
620 float FVal = (float)Val;
621 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
622 toAsm << *(unsigned int*)ProxyPtr;
623 } else if (CV->getType() == Type::DoubleTy) {
624 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
625 toAsm << *(uint64_t*)ProxyPtr;
626 } else {
627 assert(0 && "Unknown floating point type!");
628 }
629
630 toAsm << "\t! " << CV->getType()->getDescription()
631 << " value: " << Val << "\n";
632 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000633 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000634 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000635 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000636 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000637 {
Chris Lattner697954c2002-01-20 22:54:45 +0000638 assert(CPP->isNullValue() &&
639 "Cannot yet print non-null pointer constants to assembly");
640 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000641 }
Chris Lattner697954c2002-01-20 22:54:45 +0000642 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000643 {
644 assert(0 && "Cannot yet initialize pointer refs in assembly");
645 }
646 else
647 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000648 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000649 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000650}
651
Vikram S. Adve21447222001-11-10 02:03:06 +0000652// Print a constant value or values (it may be an aggregate).
653// Uses printSingleConstant() to print each individual value.
654void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000655SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000656{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000657 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000658
659 if (CPA && isStringCompatible(CPA))
660 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000661 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000662 }
663 else if (CPA)
664 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000665 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000666 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000667 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000668 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000669 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000670 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000671 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000672 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000673 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000674 }
675 else
676 this->printSingleConstant(CV);
677}
678
679// Print a constant (which may be an aggregate) prefixed by all the
680// appropriate directives. Uses printConstantValueOnly() to print the
681// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000682void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000683SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000684{
685 if (valID.length() == 0)
686 valID = getID(CV);
687
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000688 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000689
690 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000691 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000692 if (CPA && isStringCompatible(CPA))
693 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000694 toAsm << valID << ":\n";
695 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000696 return;
697 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000698
Chris Lattner697954c2002-01-20 22:54:45 +0000699 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000700
701 unsigned int constSize = ConstantToSize(CV, Target);
702 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000703 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000704
Chris Lattner697954c2002-01-20 22:54:45 +0000705 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000706
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000707 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000708}
709
710
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000711void SparcModuleAsmPrinter::FoldConstants(const Module *M,
712 std::hash_set<const Constant*> &MC) {
713 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
714 if (!(*I)->isExternal()) {
715 const std::hash_set<const Constant*> &pool =
716 MachineCodeForMethod::get(*I).getConstantPoolValues();
717 MC.insert(pool.begin(), pool.end());
718 }
719}
720
721void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000722{
Chris Lattner697954c2002-01-20 22:54:45 +0000723 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000724
725 if (GV->hasInitializer())
726 printConstant(GV->getInitializer(), getID(GV));
727 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000728 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
729 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000730 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000731 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000732 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000733 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000734 }
735}
736
737
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000738void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000739 // First, get the constants there were marked by the code generator for
740 // inclusion in the assembly code data area and fold them all into a
741 // single constant pool since there may be lots of duplicates. Also,
742 // lets force these constants into the slot table so that we can get
743 // unique names for unnamed constants also.
744 //
Chris Lattner697954c2002-01-20 22:54:45 +0000745 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000746 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000747
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000748 // Now, emit the three data sections separately; the cost of I/O should
749 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000750
751 // Section 1 : Read-only data section (implies initialized)
752 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000753 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000754 if ((*GI)->hasInitializer() && (*GI)->isConstant())
755 printGlobalVariable(*GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000756
Chris Lattner697954c2002-01-20 22:54:45 +0000757 for (std::hash_set<const Constant*>::const_iterator
758 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000759 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000760 printConstant(*I);
761
Vikram S. Advefbd21612002-03-31 19:03:58 +0000762 // Section 2 : Initialized read-write data section
763 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000764 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000765 if ((*GI)->hasInitializer() && ! (*GI)->isConstant())
766 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000767
Vikram S. Advefbd21612002-03-31 19:03:58 +0000768 // Section 3 : Uninitialized read-write data section
769 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000770 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000771 if (! (*GI)->hasInitializer())
772 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000773
Chris Lattner697954c2002-01-20 22:54:45 +0000774 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000775}
776
Chris Lattnere88f78c2001-09-19 13:47:27 +0000777} // End anonymous namespace
778
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000779Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
780 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000781}