blob: fa2aeaa3b1176b0b7850dad507ae7e934d5712aa [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 Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.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 Lattnerd50b6712002-04-28 20:40:59 +000025#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000026#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/StringExtras.h"
28#include "Support/HashExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000029#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000030using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000031
32namespace {
33
Vikram S. Adved198c472002-03-18 03:07:26 +000034class GlobalIdTable: public Annotation {
35 static AnnotationID AnnotId;
36 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000037
Vikram S. Adved198c472002-03-18 03:07:26 +000038 typedef std::hash_map<const Value*, int> ValIdMap;
39 typedef ValIdMap::const_iterator ValIdMapConstIterator;
40 typedef ValIdMap:: iterator ValIdMapIterator;
41public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000042 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000043 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000044
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000045 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000046};
47
48AnnotationID GlobalIdTable::AnnotId =
49 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
50
51//===---------------------------------------------------------------------===//
52// Code Shared By the two printer passes, as a mixin
53//===---------------------------------------------------------------------===//
54
55class AsmPrinter {
56 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000057public:
Chris Lattner697954c2002-01-20 22:54:45 +000058 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000059 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000060
Chris Lattnere88f78c2001-09-19 13:47:27 +000061 enum Sections {
62 Unknown,
63 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000064 ReadOnlyData,
65 InitRWData,
66 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000067 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000068
Chris Lattner59ba1092002-02-04 15:53:23 +000069 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000070 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
71
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000072 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattnerc19b8b12002-02-03 23:41:08 +000073 void startModule(Module *M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000074 // Create the global id table if it does not already exist
75 idTable = (GlobalIdTable*) M->getAnnotation(GlobalIdTable::AnnotId);
76 if (idTable == NULL) {
77 idTable = new GlobalIdTable(M);
78 M->addAnnotation(idTable);
79 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000080 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081 void startFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000082 // Make sure the slot table has information about this function...
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000083 idTable->Table.incorporateFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000084 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000085 void endFunction(Function *F) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000086 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000087 }
88 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000089 }
90
Vikram S. Adved198c472002-03-18 03:07:26 +000091 // Check if a name is external or accessible from external code.
92 // Only functions can currently be external. "main" is the only name
93 // that is visible externally.
94 bool isExternal(const Value* V) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000095 const Function *F = dyn_cast<Function>(V);
96 return F && (F->isExternal() || F->getName() == "main");
Vikram S. Adved198c472002-03-18 03:07:26 +000097 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000098
Chris Lattnere88f78c2001-09-19 13:47:27 +000099 // enterSection - Use this method to enter a different section of the output
100 // executable. This is used to only output neccesary section transitions.
101 //
102 void enterSection(enum Sections S) {
103 if (S == CurSection) return; // Only switch section if neccesary
104 CurSection = S;
105
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000106 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000107 switch (S)
108 {
109 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000110 case Text: toAsm << "\".text\""; break;
111 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
112 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
113 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000114 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000115 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000116 }
117
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000118 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000119 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120
121 // Symbol names in Sparc assembly language have these rules:
122 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
123 // (b) A name beginning in "." is treated as a local name.
124 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
125 //
126 if (S[0] == '_' || isdigit(S[0]))
127 Result += "ll";
128
129 for (unsigned i = 0; i < S.size(); ++i)
130 {
131 char C = S[i];
132 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
133 Result += C;
134 else
135 {
136 Result += '_';
137 Result += char('0' + ((unsigned char)C >> 4));
138 Result += char('0' + (C & 0xF));
139 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000140 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000141 return Result;
142 }
143
Chris Lattnere88f78c2001-09-19 13:47:27 +0000144 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000145 // the name of the identifier if possible (qualified by the type), and
146 // use a numbered value based on prefix otherwise.
147 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000148 //
149 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000150 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
151
152 Result = Result + (V->hasName()? V->getName() : string(Prefix));
153
154 // Qualify all internal names with a unique id.
155 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000156 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000157 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000158 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
159 if (I == idTable->valToIdMap.end())
160 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000161 else
162 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000163 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000164 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000166
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000167 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000168 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000169
Chris Lattnere88f78c2001-09-19 13:47:27 +0000170 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000171 string getID(const Function *F) {
172 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000173 }
174 string getID(const BasicBlock *BB) {
175 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
176 }
177 string getID(const GlobalVariable *GV) {
178 return getID(GV, "LLVMGlobal_", ".G_");
179 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000180 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000181 return getID(CV, "LLVMConst_", ".C_");
182 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000183};
184
185
186
187//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000188// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000189//===----------------------------------------------------------------------===//
190
Chris Lattnerf57b8452002-04-27 06:56:12 +0000191struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000192 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000193 : AsmPrinter(os, t) {}
194
195 virtual bool doInitialization(Module *M) {
196 startModule(M);
197 return false;
198 }
199
Chris Lattnerf57b8452002-04-27 06:56:12 +0000200 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000201 startFunction(F);
202 emitFunction(F);
203 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000204 return false;
205 }
206
207 virtual bool doFinalization(Module *M) {
208 endModule();
209 return false;
210 }
211
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000212 void emitFunction(const Function *F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000213private :
214 void emitBasicBlock(const BasicBlock *BB);
215 void emitMachineInst(const MachineInstr *MI);
216
217 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
218 void printOneOperand(const MachineOperand &Op);
219
220 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
221 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000222
Chris Lattnere88f78c2001-09-19 13:47:27 +0000223 unsigned getOperandMask(unsigned Opcode) {
224 switch (Opcode) {
225 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000226 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000227 default: return 0; // By default, don't hack operands...
228 }
229 }
230};
231
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000232inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000233SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
234 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000235 switch (MI->getOpCode()) {
236 case JMPLCALL:
237 case JMPLRET: return (opNum == 0);
238 default: return false;
239 }
240}
241
242
243inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000244SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
245 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000246 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
247 return (opNum == 0);
248 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
249 return (opNum == 1);
250 else
251 return false;
252}
253
254
255#define PrintOp1PlusOp2(Op1, Op2) \
256 printOneOperand(Op1); \
257 toAsm << "+"; \
258 printOneOperand(Op2);
259
260unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000261SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000262 unsigned int opNum)
263{
264 const MachineOperand& Op = MI->getOperand(opNum);
265
266 if (OpIsBranchTargetLabel(MI, opNum))
267 {
268 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
269 return 2;
270 }
271 else if (OpIsMemoryAddressBase(MI, opNum))
272 {
273 toAsm << "[";
274 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
275 toAsm << "]";
276 return 2;
277 }
278 else
279 {
280 printOneOperand(Op);
281 return 1;
282 }
283}
284
285
286void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000287SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &op)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000288{
289 switch (op.getOperandType())
290 {
291 case MachineOperand::MO_VirtualRegister:
292 case MachineOperand::MO_CCRegister:
293 case MachineOperand::MO_MachineRegister:
294 {
295 int RegNum = (int)op.getAllocatedRegNum();
296
Vikram S. Advefbd21612002-03-31 19:03:58 +0000297 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000298 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
299 toAsm << "<NULL VALUE>";
300 } else {
301 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
302 }
303 break;
304 }
305
306 case MachineOperand::MO_PCRelativeDisp:
307 {
308 const Value *Val = op.getVRegValue();
309 if (!Val)
310 toAsm << "\t<*NULL Value*>";
Chris Lattner31bcdb82002-04-28 19:55:58 +0000311 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000312 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000313 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000314 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000315 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000316 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000317 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318 toAsm << getID(CV);
319 else
320 toAsm << "<unknown value=" << Val << ">";
321 break;
322 }
323
324 case MachineOperand::MO_SignExtendedImmed:
325 case MachineOperand::MO_UnextendedImmed:
326 toAsm << (long)op.getImmedValue();
327 break;
328
329 default:
330 toAsm << op; // use dump field
331 break;
332 }
333}
334
335
336void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000337SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000338{
339 unsigned Opcode = MI->getOpCode();
340
341 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
342 return; // IGNORE PHI NODES
343
344 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
345
346 unsigned Mask = getOperandMask(Opcode);
347
348 bool NeedComma = false;
349 unsigned N = 1;
350 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
351 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
352 if (NeedComma) toAsm << ", "; // Handle comma outputing
353 NeedComma = true;
354 N = printOperands(MI, OpNum);
355 }
356 else
357 N = 1;
358
359 toAsm << "\n";
360}
361
362void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000363SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000364{
365 // Emit a label for the basic block
366 toAsm << getID(BB) << ":\n";
367
368 // Get the vector of machine instructions corresponding to this bb.
369 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
370 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
371
372 // Loop over all of the instructions in the basic block...
373 for (; MII != MIE; ++MII)
374 emitMachineInst(*MII);
375 toAsm << "\n"; // Seperate BB's with newlines
376}
377
378void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000379SparcFunctionAsmPrinter::emitFunction(const Function *M)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000380{
381 string methName = getID(M);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000382 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000383 enterSection(AsmPrinter::Text);
384 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
385 //toAsm << "\t.type\t" << methName << ",#function\n";
386 toAsm << "\t.type\t" << methName << ", 2\n";
387 toAsm << methName << ":\n";
388
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000389 // Output code for all of the basic blocks in the function...
390 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000391 emitBasicBlock(*I);
392
393 // Output a .size directive so the debugger knows the extents of the function
394 toAsm << ".EndOf_" << methName << ":\n\t.size "
395 << methName << ", .EndOf_"
396 << methName << "-" << methName << "\n";
397
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000398 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399 toAsm << "\n\n";
400}
401
402} // End anonymous namespace
403
Chris Lattnerf57b8452002-04-27 06:56:12 +0000404Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000405 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000406}
407
408
409
410
411
412//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000413// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000414//===----------------------------------------------------------------------===//
415
416namespace {
417
418class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
419public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000420 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
421 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000422
423 virtual bool run(Module *M) {
424 startModule(M);
425 emitGlobalsAndConstants(M);
426 endModule();
427 return false;
428 }
429
430 void emitGlobalsAndConstants(const Module *M);
431
432 void printGlobalVariable(const GlobalVariable *GV);
433 void printSingleConstant( const Constant* CV);
434 void printConstantValueOnly(const Constant* CV);
435 void printConstant( const Constant* CV, std::string valID = "");
436
437 static void FoldConstants(const Module *M,
438 std::hash_set<const Constant*> &moduleConstants);
439
440};
441
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000442
443// Can we treat the specified array as a string? Only if it is an array of
444// ubytes or non-negative sbytes.
445//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000446static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000447 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
448 if (ETy == Type::UByteTy) return true;
449 if (ETy != Type::SByteTy) return false;
450
451 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000452 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000453 return false;
454
455 return true;
456}
457
458// toOctal - Convert the low order bits of X into an octal letter
459static inline char toOctal(int X) {
460 return (X&7)+'0';
461}
462
463// getAsCString - Return the specified array as a C compatible string, only if
464// the predicate isStringCompatible is true.
465//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000466static string getAsCString(ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000467 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000468
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000469 string Result;
470 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
471 Result = "\"";
472 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
473 unsigned char C = (ETy == Type::SByteTy) ?
474 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
475 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
476
477 if (isprint(C)) {
478 Result += C;
479 } else {
480 switch(C) {
481 case '\a': Result += "\\a"; break;
482 case '\b': Result += "\\b"; break;
483 case '\f': Result += "\\f"; break;
484 case '\n': Result += "\\n"; break;
485 case '\r': Result += "\\r"; break;
486 case '\t': Result += "\\t"; break;
487 case '\v': Result += "\\v"; break;
488 default:
489 Result += '\\';
490 Result += toOctal(C >> 6);
491 Result += toOctal(C >> 3);
492 Result += toOctal(C >> 0);
493 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000494 }
495 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000496 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000497 Result += "\"";
498
499 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000500}
501
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000502inline bool
503ArrayTypeIsString(ArrayType* arrayType)
504{
505 return (arrayType->getElementType() == Type::UByteTy ||
506 arrayType->getElementType() == Type::SByteTy);
507}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000508
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000509inline const string
510TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000511{
512 switch(type->getPrimitiveID())
513 {
514 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
515 return ".byte";
516 case Type::UShortTyID: case Type::ShortTyID:
517 return ".half";
518 case Type::UIntTyID: case Type::IntTyID:
519 return ".word";
520 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
521 return ".xword";
522 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000523 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000524 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000525 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000526 case Type::ArrayTyID:
527 if (ArrayTypeIsString((ArrayType*) type))
528 return ".ascii";
529 else
530 return "<InvaliDataTypeForPrinting>";
531 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000532 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000533 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000534}
535
Vikram S. Adve21447222001-11-10 02:03:06 +0000536// Get the size of the constant for the given target.
537// If this is an unsized array, return 0.
538//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000539inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000540ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000541{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000542 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000543 {
544 ArrayType *aty = cast<ArrayType>(CPA->getType());
545 if (ArrayTypeIsString(aty))
546 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000547 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000548
549 return target.findOptimalStorageSize(CV->getType());
550}
551
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000552
553
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000554// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000555// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
556//
557inline unsigned int
558SizeToAlignment(unsigned int size, const TargetMachine& target)
559{
560 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
561 if (size > (unsigned) cacheLineSize / 2)
562 return cacheLineSize;
563 else
564 for (unsigned sz=1; /*no condition*/; sz *= 2)
565 if (sz >= size)
566 return sz;
567}
568
569// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000570//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000571inline unsigned int
572TypeToAlignment(const Type* type, const TargetMachine& target)
573{
Vikram S. Adve21447222001-11-10 02:03:06 +0000574 return SizeToAlignment(target.findOptimalStorageSize(type), target);
575}
576
577// Get the size of the constant and then use SizeToAlignment.
578// Handles strings as a special case;
579inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000580ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000581{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000582 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000583 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
584 return SizeToAlignment(1 + CPA->getNumOperands(), target);
585
586 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000587}
588
589
Vikram S. Adve21447222001-11-10 02:03:06 +0000590// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000591void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000592SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000593{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000594 assert(CV->getType() != Type::VoidTy &&
595 CV->getType() != Type::TypeTy &&
596 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000597 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000598
Chris Lattnerf678dc62002-04-11 21:44:02 +0000599 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
600 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000601
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000602 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000603
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000604 if (CV->getType()->isPrimitiveType())
605 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000606 if (CV->getType()->isFloatingPoint()) {
607 // FP Constants are printed as integer constants to avoid losing
608 // precision...
609 double Val = cast<ConstantFP>(CV)->getValue();
610 if (CV->getType() == Type::FloatTy) {
611 float FVal = (float)Val;
612 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
613 toAsm << *(unsigned int*)ProxyPtr;
614 } else if (CV->getType() == Type::DoubleTy) {
615 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
616 toAsm << *(uint64_t*)ProxyPtr;
617 } else {
618 assert(0 && "Unknown floating point type!");
619 }
620
621 toAsm << "\t! " << CV->getType()->getDescription()
622 << " value: " << Val << "\n";
623 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000624 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000625 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000626 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000627 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000628 {
Chris Lattner697954c2002-01-20 22:54:45 +0000629 assert(CPP->isNullValue() &&
630 "Cannot yet print non-null pointer constants to assembly");
631 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000632 }
Chris Lattner697954c2002-01-20 22:54:45 +0000633 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000634 {
635 assert(0 && "Cannot yet initialize pointer refs in assembly");
636 }
637 else
638 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000639 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000640 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000641}
642
Vikram S. Adve21447222001-11-10 02:03:06 +0000643// Print a constant value or values (it may be an aggregate).
644// Uses printSingleConstant() to print each individual value.
645void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000646SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000647{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000648 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000649
650 if (CPA && isStringCompatible(CPA))
651 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000652 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000653 }
654 else if (CPA)
655 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000656 const std::vector<Use> &constValues = CPA->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 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000660 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000661 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000662 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000663 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000664 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000665 }
666 else
667 this->printSingleConstant(CV);
668}
669
670// Print a constant (which may be an aggregate) prefixed by all the
671// appropriate directives. Uses printConstantValueOnly() to print the
672// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000673void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000674SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000675{
676 if (valID.length() == 0)
677 valID = getID(CV);
678
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000679 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000680
681 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000682 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000683 if (CPA && isStringCompatible(CPA))
684 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000685 toAsm << valID << ":\n";
686 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000687 return;
688 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000689
Chris Lattner697954c2002-01-20 22:54:45 +0000690 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000691
692 unsigned int constSize = ConstantToSize(CV, Target);
693 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000694 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000695
Chris Lattner697954c2002-01-20 22:54:45 +0000696 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000697
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000698 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000699}
700
701
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000702void SparcModuleAsmPrinter::FoldConstants(const Module *M,
703 std::hash_set<const Constant*> &MC) {
704 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
705 if (!(*I)->isExternal()) {
706 const std::hash_set<const Constant*> &pool =
707 MachineCodeForMethod::get(*I).getConstantPoolValues();
708 MC.insert(pool.begin(), pool.end());
709 }
710}
711
712void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000713{
Chris Lattner697954c2002-01-20 22:54:45 +0000714 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000715
716 if (GV->hasInitializer())
717 printConstant(GV->getInitializer(), getID(GV));
718 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000719 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
720 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000721 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000722 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000723 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000724 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000725 }
726}
727
728
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000729void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000730 // First, get the constants there were marked by the code generator for
731 // inclusion in the assembly code data area and fold them all into a
732 // single constant pool since there may be lots of duplicates. Also,
733 // lets force these constants into the slot table so that we can get
734 // unique names for unnamed constants also.
735 //
Chris Lattner697954c2002-01-20 22:54:45 +0000736 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000737 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000738
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000739 // Now, emit the three data sections separately; the cost of I/O should
740 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000741
742 // Section 1 : Read-only data section (implies initialized)
743 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000744 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000745 if ((*GI)->hasInitializer() && (*GI)->isConstant())
746 printGlobalVariable(*GI);
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
Vikram S. Advefbd21612002-03-31 19:03:58 +0000753 // Section 2 : Initialized read-write data section
754 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000755 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000756 if ((*GI)->hasInitializer() && ! (*GI)->isConstant())
757 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000758
Vikram S. Advefbd21612002-03-31 19:03:58 +0000759 // Section 3 : Uninitialized read-write data section
760 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000761 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000762 if (! (*GI)->hasInitializer())
763 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000764
Chris Lattner697954c2002-01-20 22:54:45 +0000765 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000766}
767
Chris Lattnere88f78c2001-09-19 13:47:27 +0000768} // End anonymous namespace
769
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000770Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
771 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000772}