blob: 79b7b003c71b379efb8e15855734f72d1231adff [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"
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 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 Lattner2fbfdcf2002-04-07 20:49:59 +000077 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattnerc19b8b12002-02-03 23:41:08 +000078 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 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000086 void startFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000087 // Make sure the slot table has information about this function...
88 idTable->Table->incorporateFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000089 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 void endFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000091 idTable->Table->purgeFunction(); // Forget all about F
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) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000100 const Function *F = dyn_cast<Function>(V);
101 return F && (F->isExternal() || F->getName() == "main");
Vikram S. Adved198c472002-03-18 03:07:26 +0000102 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000103
Chris Lattnere88f78c2001-09-19 13:47:27 +0000104 // enterSection - Use this method to enter a different section of the output
105 // executable. This is used to only output neccesary section transitions.
106 //
107 void enterSection(enum Sections S) {
108 if (S == CurSection) return; // Only switch section if neccesary
109 CurSection = S;
110
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000111 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000112 switch (S)
113 {
114 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000115 case Text: toAsm << "\".text\""; break;
116 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
117 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
118 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000119 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000121 }
122
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000123 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000124 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000125
126 // Symbol names in Sparc assembly language have these rules:
127 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
128 // (b) A name beginning in "." is treated as a local name.
129 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
130 //
131 if (S[0] == '_' || isdigit(S[0]))
132 Result += "ll";
133
134 for (unsigned i = 0; i < S.size(); ++i)
135 {
136 char C = S[i];
137 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
138 Result += C;
139 else
140 {
141 Result += '_';
142 Result += char('0' + ((unsigned char)C >> 4));
143 Result += char('0' + (C & 0xF));
144 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000145 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000146 return Result;
147 }
148
Chris Lattnere88f78c2001-09-19 13:47:27 +0000149 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000150 // the name of the identifier if possible (qualified by the type), and
151 // use a numbered value based on prefix otherwise.
152 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000153 //
154 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000155 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
156
157 Result = Result + (V->hasName()? V->getName() : string(Prefix));
158
159 // Qualify all internal names with a unique id.
160 if (!isExternal(V)) {
161 int valId = idTable->Table->getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000162 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000163 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
164 if (I == idTable->valToIdMap.end())
165 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000166 else
167 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000169 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000170 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000171
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000172 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000173 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174
Chris Lattnere88f78c2001-09-19 13:47:27 +0000175 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 string getID(const Module *M) {
177 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000178 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000179 string getID(const Function *F) {
180 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000181 }
182 string getID(const BasicBlock *BB) {
183 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
184 }
185 string getID(const GlobalVariable *GV) {
186 return getID(GV, "LLVMGlobal_", ".G_");
187 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000189 return getID(CV, "LLVMConst_", ".C_");
190 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000191};
192
193
194
195//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000196// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000197//===----------------------------------------------------------------------===//
198
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000199struct SparcFunctionAsmPrinter : public MethodPass, public AsmPrinter {
200 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000201 : AsmPrinter(os, t) {}
202
203 virtual bool doInitialization(Module *M) {
204 startModule(M);
205 return false;
206 }
207
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000208 virtual bool runOnMethod(Function *F) {
209 startFunction(F);
210 emitFunction(F);
211 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000212 return false;
213 }
214
215 virtual bool doFinalization(Module *M) {
216 endModule();
217 return false;
218 }
219
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000220 void emitFunction(const Function *F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000221private :
222 void emitBasicBlock(const BasicBlock *BB);
223 void emitMachineInst(const MachineInstr *MI);
224
225 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
226 void printOneOperand(const MachineOperand &Op);
227
228 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
229 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000230
Chris Lattnere88f78c2001-09-19 13:47:27 +0000231 unsigned getOperandMask(unsigned Opcode) {
232 switch (Opcode) {
233 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000234 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000235 default: return 0; // By default, don't hack operands...
236 }
237 }
238};
239
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000240inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000241SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
242 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000243 switch (MI->getOpCode()) {
244 case JMPLCALL:
245 case JMPLRET: return (opNum == 0);
246 default: return false;
247 }
248}
249
250
251inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000252SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
253 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000254 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
255 return (opNum == 0);
256 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
257 return (opNum == 1);
258 else
259 return false;
260}
261
262
263#define PrintOp1PlusOp2(Op1, Op2) \
264 printOneOperand(Op1); \
265 toAsm << "+"; \
266 printOneOperand(Op2);
267
268unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000270 unsigned int opNum)
271{
272 const MachineOperand& Op = MI->getOperand(opNum);
273
274 if (OpIsBranchTargetLabel(MI, opNum))
275 {
276 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
277 return 2;
278 }
279 else if (OpIsMemoryAddressBase(MI, opNum))
280 {
281 toAsm << "[";
282 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
283 toAsm << "]";
284 return 2;
285 }
286 else
287 {
288 printOneOperand(Op);
289 return 1;
290 }
291}
292
293
294void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000295SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &op)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000296{
297 switch (op.getOperandType())
298 {
299 case MachineOperand::MO_VirtualRegister:
300 case MachineOperand::MO_CCRegister:
301 case MachineOperand::MO_MachineRegister:
302 {
303 int RegNum = (int)op.getAllocatedRegNum();
304
Vikram S. Advefbd21612002-03-31 19:03:58 +0000305 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000306 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
307 toAsm << "<NULL VALUE>";
308 } else {
309 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
310 }
311 break;
312 }
313
314 case MachineOperand::MO_PCRelativeDisp:
315 {
316 const Value *Val = op.getVRegValue();
317 if (!Val)
318 toAsm << "\t<*NULL Value*>";
319 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
320 toAsm << getID(BB);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000321 else if (const Function *M = dyn_cast<const Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 toAsm << getID(M);
323 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
324 toAsm << getID(GV);
325 else if (const Constant *CV = dyn_cast<const Constant>(Val))
326 toAsm << getID(CV);
327 else
328 toAsm << "<unknown value=" << Val << ">";
329 break;
330 }
331
332 case MachineOperand::MO_SignExtendedImmed:
333 case MachineOperand::MO_UnextendedImmed:
334 toAsm << (long)op.getImmedValue();
335 break;
336
337 default:
338 toAsm << op; // use dump field
339 break;
340 }
341}
342
343
344void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000345SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000346{
347 unsigned Opcode = MI->getOpCode();
348
349 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
350 return; // IGNORE PHI NODES
351
352 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
353
354 unsigned Mask = getOperandMask(Opcode);
355
356 bool NeedComma = false;
357 unsigned N = 1;
358 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
359 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
360 if (NeedComma) toAsm << ", "; // Handle comma outputing
361 NeedComma = true;
362 N = printOperands(MI, OpNum);
363 }
364 else
365 N = 1;
366
367 toAsm << "\n";
368}
369
370void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000371SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000372{
373 // Emit a label for the basic block
374 toAsm << getID(BB) << ":\n";
375
376 // Get the vector of machine instructions corresponding to this bb.
377 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
378 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
379
380 // Loop over all of the instructions in the basic block...
381 for (; MII != MIE; ++MII)
382 emitMachineInst(*MII);
383 toAsm << "\n"; // Seperate BB's with newlines
384}
385
386void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000387SparcFunctionAsmPrinter::emitFunction(const Function *M)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000388{
389 string methName = getID(M);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000390 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000391 enterSection(AsmPrinter::Text);
392 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
393 //toAsm << "\t.type\t" << methName << ",#function\n";
394 toAsm << "\t.type\t" << methName << ", 2\n";
395 toAsm << methName << ":\n";
396
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000397 // Output code for all of the basic blocks in the function...
398 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399 emitBasicBlock(*I);
400
401 // Output a .size directive so the debugger knows the extents of the function
402 toAsm << ".EndOf_" << methName << ":\n\t.size "
403 << methName << ", .EndOf_"
404 << methName << "-" << methName << "\n";
405
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000406 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000407 toAsm << "\n\n";
408}
409
410} // End anonymous namespace
411
412Pass *UltraSparc::getMethodAsmPrinterPass(PassManager &PM, std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000413 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000414}
415
416
417
418
419
420//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000421// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000422//===----------------------------------------------------------------------===//
423
424namespace {
425
426class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
427public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000428 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
429 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000430
431 virtual bool run(Module *M) {
432 startModule(M);
433 emitGlobalsAndConstants(M);
434 endModule();
435 return false;
436 }
437
438 void emitGlobalsAndConstants(const Module *M);
439
440 void printGlobalVariable(const GlobalVariable *GV);
441 void printSingleConstant( const Constant* CV);
442 void printConstantValueOnly(const Constant* CV);
443 void printConstant( const Constant* CV, std::string valID = "");
444
445 static void FoldConstants(const Module *M,
446 std::hash_set<const Constant*> &moduleConstants);
447
448};
449
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000450
451// Can we treat the specified array as a string? Only if it is an array of
452// ubytes or non-negative sbytes.
453//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000454static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000455 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
456 if (ETy == Type::UByteTy) return true;
457 if (ETy != Type::SByteTy) return false;
458
459 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000460 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000461 return false;
462
463 return true;
464}
465
466// toOctal - Convert the low order bits of X into an octal letter
467static inline char toOctal(int X) {
468 return (X&7)+'0';
469}
470
471// getAsCString - Return the specified array as a C compatible string, only if
472// the predicate isStringCompatible is true.
473//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000474static string getAsCString(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000475 if (isStringCompatible(CPA)) {
476 string Result;
477 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
478 Result = "\"";
479 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
480 unsigned char C = (ETy == Type::SByteTy) ?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000481 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
482 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000483
484 if (isprint(C)) {
485 Result += C;
486 } else {
487 switch(C) {
488 case '\a': Result += "\\a"; break;
489 case '\b': Result += "\\b"; break;
490 case '\f': Result += "\\f"; break;
491 case '\n': Result += "\\n"; break;
492 case '\r': Result += "\\r"; break;
493 case '\t': Result += "\\t"; break;
494 case '\v': Result += "\\v"; break;
495 default:
496 Result += '\\';
497 Result += toOctal(C >> 6);
498 Result += toOctal(C >> 3);
499 Result += toOctal(C >> 0);
500 break;
501 }
502 }
503 }
504 Result += "\"";
505
506 return Result;
507 } else {
508 return CPA->getStrValue();
509 }
510}
511
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000512inline bool
513ArrayTypeIsString(ArrayType* arrayType)
514{
515 return (arrayType->getElementType() == Type::UByteTy ||
516 arrayType->getElementType() == Type::SByteTy);
517}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000518
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000519inline const string
520TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000521{
522 switch(type->getPrimitiveID())
523 {
524 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
525 return ".byte";
526 case Type::UShortTyID: case Type::ShortTyID:
527 return ".half";
528 case Type::UIntTyID: case Type::IntTyID:
529 return ".word";
530 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
531 return ".xword";
532 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000533 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000534 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000535 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000536 case Type::ArrayTyID:
537 if (ArrayTypeIsString((ArrayType*) type))
538 return ".ascii";
539 else
540 return "<InvaliDataTypeForPrinting>";
541 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000542 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000543 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000544}
545
Vikram S. Adve21447222001-11-10 02:03:06 +0000546// Get the size of the constant for the given target.
547// If this is an unsized array, return 0.
548//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000549inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000550ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000551{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000552 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000553 {
554 ArrayType *aty = cast<ArrayType>(CPA->getType());
555 if (ArrayTypeIsString(aty))
556 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000557 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000558
559 return target.findOptimalStorageSize(CV->getType());
560}
561
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000562
563
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000564// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000565// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
566//
567inline unsigned int
568SizeToAlignment(unsigned int size, const TargetMachine& target)
569{
570 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
571 if (size > (unsigned) cacheLineSize / 2)
572 return cacheLineSize;
573 else
574 for (unsigned sz=1; /*no condition*/; sz *= 2)
575 if (sz >= size)
576 return sz;
577}
578
579// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000580//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000581inline unsigned int
582TypeToAlignment(const Type* type, const TargetMachine& target)
583{
Vikram S. Adve21447222001-11-10 02:03:06 +0000584 return SizeToAlignment(target.findOptimalStorageSize(type), target);
585}
586
587// Get the size of the constant and then use SizeToAlignment.
588// Handles strings as a special case;
589inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000590ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000591{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000592 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000593 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
594 return SizeToAlignment(1 + CPA->getNumOperands(), target);
595
596 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000597}
598
599
Vikram S. Adve21447222001-11-10 02:03:06 +0000600// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000601void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000602SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000603{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000604 assert(CV->getType() != Type::VoidTy &&
605 CV->getType() != Type::TypeTy &&
606 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000607 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000608
Chris Lattnerf678dc62002-04-11 21:44:02 +0000609 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
610 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000611
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000612 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000613
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000614 if (CV->getType()->isPrimitiveType())
615 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000616 if (CV->getType()->isFloatingPoint()) {
617 // FP Constants are printed as integer constants to avoid losing
618 // precision...
619 double Val = cast<ConstantFP>(CV)->getValue();
620 if (CV->getType() == Type::FloatTy) {
621 float FVal = (float)Val;
622 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
623 toAsm << *(unsigned int*)ProxyPtr;
624 } else if (CV->getType() == Type::DoubleTy) {
625 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
626 toAsm << *(uint64_t*)ProxyPtr;
627 } else {
628 assert(0 && "Unknown floating point type!");
629 }
630
631 toAsm << "\t! " << CV->getType()->getDescription()
632 << " value: " << Val << "\n";
633 } else {
634 toAsm << CV->getStrValue() << "\n";
635 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000636 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000637 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000638 {
Chris Lattner697954c2002-01-20 22:54:45 +0000639 assert(CPP->isNullValue() &&
640 "Cannot yet print non-null pointer constants to assembly");
641 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000642 }
Chris Lattner697954c2002-01-20 22:54:45 +0000643 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 {
645 assert(0 && "Cannot yet initialize pointer refs in assembly");
646 }
647 else
648 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000649 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000650 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000651}
652
Vikram S. Adve21447222001-11-10 02:03:06 +0000653// Print a constant value or values (it may be an aggregate).
654// Uses printSingleConstant() to print each individual value.
655void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000656SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000657{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000658 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000659
660 if (CPA && isStringCompatible(CPA))
661 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000662 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000663 }
664 else if (CPA)
665 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000666 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000667 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000668 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000669 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000670 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000671 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000672 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000673 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000674 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000675 }
676 else
677 this->printSingleConstant(CV);
678}
679
680// Print a constant (which may be an aggregate) prefixed by all the
681// appropriate directives. Uses printConstantValueOnly() to print the
682// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000683void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000684SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000685{
686 if (valID.length() == 0)
687 valID = getID(CV);
688
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000689 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000690
691 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000692 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000693 if (CPA && isStringCompatible(CPA))
694 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000695 toAsm << valID << ":\n";
696 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000697 return;
698 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000699
Chris Lattner697954c2002-01-20 22:54:45 +0000700 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000701
702 unsigned int constSize = ConstantToSize(CV, Target);
703 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000704 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000705
Chris Lattner697954c2002-01-20 22:54:45 +0000706 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000707
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000708 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709}
710
711
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000712void SparcModuleAsmPrinter::FoldConstants(const Module *M,
713 std::hash_set<const Constant*> &MC) {
714 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
715 if (!(*I)->isExternal()) {
716 const std::hash_set<const Constant*> &pool =
717 MachineCodeForMethod::get(*I).getConstantPoolValues();
718 MC.insert(pool.begin(), pool.end());
719 }
720}
721
722void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000723{
Chris Lattner697954c2002-01-20 22:54:45 +0000724 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000725
726 if (GV->hasInitializer())
727 printConstant(GV->getInitializer(), getID(GV));
728 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000729 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
730 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000731 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000732 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000733 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000734 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000735 }
736}
737
738
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000739void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000740 // First, get the constants there were marked by the code generator for
741 // inclusion in the assembly code data area and fold them all into a
742 // single constant pool since there may be lots of duplicates. Also,
743 // lets force these constants into the slot table so that we can get
744 // unique names for unnamed constants also.
745 //
Chris Lattner697954c2002-01-20 22:54:45 +0000746 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000747 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000748
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000749 // Now, emit the three data sections separately; the cost of I/O should
750 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000751
752 // Section 1 : Read-only data section (implies initialized)
753 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000754 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000755 if ((*GI)->hasInitializer() && (*GI)->isConstant())
756 printGlobalVariable(*GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000757
Chris Lattner697954c2002-01-20 22:54:45 +0000758 for (std::hash_set<const Constant*>::const_iterator
759 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000760 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000761 printConstant(*I);
762
Vikram S. Advefbd21612002-03-31 19:03:58 +0000763 // Section 2 : Initialized read-write data section
764 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000766 if ((*GI)->hasInitializer() && ! (*GI)->isConstant())
767 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000768
Vikram S. Advefbd21612002-03-31 19:03:58 +0000769 // Section 3 : Uninitialized read-write data section
770 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000771 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000772 if (! (*GI)->hasInitializer())
773 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000774
Chris Lattner697954c2002-01-20 22:54:45 +0000775 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000776}
777
Chris Lattnere88f78c2001-09-19 13:47:27 +0000778} // End anonymous namespace
779
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000780Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
781 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000782}