blob: c823e91d1c99bcb99ea8b3b69d44cbf56496adfc [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 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 Lattnerfd63f25f2002-04-28 04:50:54 +000041 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000042 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000043
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000044 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000045};
46
47AnnotationID GlobalIdTable::AnnotId =
48 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
49
50//===---------------------------------------------------------------------===//
51// Code Shared By the two printer passes, as a mixin
52//===---------------------------------------------------------------------===//
53
54class AsmPrinter {
55 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000056public:
Chris Lattner697954c2002-01-20 22:54:45 +000057 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000058 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000059
Chris Lattnere88f78c2001-09-19 13:47:27 +000060 enum Sections {
61 Unknown,
62 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000063 ReadOnlyData,
64 InitRWData,
65 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000066 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000067
Chris Lattner59ba1092002-02-04 15:53:23 +000068 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000069 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
70
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000071 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattnerc19b8b12002-02-03 23:41:08 +000072 void startModule(Module *M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000073 // Create the global id table if it does not already exist
74 idTable = (GlobalIdTable*) M->getAnnotation(GlobalIdTable::AnnotId);
75 if (idTable == NULL) {
76 idTable = new GlobalIdTable(M);
77 M->addAnnotation(idTable);
78 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080 void startFunction(Function *F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000081 // Make sure the slot table has information about this function...
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000082 idTable->Table.incorporateFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000083 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084 void endFunction(Function *F) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000085 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000086 }
87 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000088 }
89
Vikram S. Adved198c472002-03-18 03:07:26 +000090 // Check if a name is external or accessible from external code.
91 // Only functions can currently be external. "main" is the only name
92 // that is visible externally.
93 bool isExternal(const Value* V) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000094 const Function *F = dyn_cast<Function>(V);
95 return F && (F->isExternal() || F->getName() == "main");
Vikram S. Adved198c472002-03-18 03:07:26 +000096 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000097
Chris Lattnere88f78c2001-09-19 13:47:27 +000098 // enterSection - Use this method to enter a different section of the output
99 // executable. This is used to only output neccesary section transitions.
100 //
101 void enterSection(enum Sections S) {
102 if (S == CurSection) return; // Only switch section if neccesary
103 CurSection = S;
104
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000105 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000106 switch (S)
107 {
108 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000109 case Text: toAsm << "\".text\""; break;
110 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
111 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
112 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000113 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000114 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000115 }
116
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000117 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000118 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000119
120 // Symbol names in Sparc assembly language have these rules:
121 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
122 // (b) A name beginning in "." is treated as a local name.
123 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
124 //
125 if (S[0] == '_' || isdigit(S[0]))
126 Result += "ll";
127
128 for (unsigned i = 0; i < S.size(); ++i)
129 {
130 char C = S[i];
131 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
132 Result += C;
133 else
134 {
135 Result += '_';
136 Result += char('0' + ((unsigned char)C >> 4));
137 Result += char('0' + (C & 0xF));
138 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000139 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000140 return Result;
141 }
142
Chris Lattnere88f78c2001-09-19 13:47:27 +0000143 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000144 // the name of the identifier if possible (qualified by the type), and
145 // use a numbered value based on prefix otherwise.
146 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000147 //
148 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000149 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
150
151 Result = Result + (V->hasName()? V->getName() : string(Prefix));
152
153 // Qualify all internal names with a unique id.
154 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000155 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000156 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000157 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
158 if (I == idTable->valToIdMap.end())
159 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000160 else
161 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000162 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000163 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000164 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000165
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000166 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000167 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168
Chris Lattnere88f78c2001-09-19 13:47:27 +0000169 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000170 string getID(const Function *F) {
171 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000172 }
173 string getID(const BasicBlock *BB) {
174 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
175 }
176 string getID(const GlobalVariable *GV) {
177 return getID(GV, "LLVMGlobal_", ".G_");
178 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000179 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000180 return getID(CV, "LLVMConst_", ".C_");
181 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000182};
183
184
185
186//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000187// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000188//===----------------------------------------------------------------------===//
189
Chris Lattnerf57b8452002-04-27 06:56:12 +0000190struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000191 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000192 : AsmPrinter(os, t) {}
193
194 virtual bool doInitialization(Module *M) {
195 startModule(M);
196 return false;
197 }
198
Chris Lattnerf57b8452002-04-27 06:56:12 +0000199 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000200 startFunction(F);
201 emitFunction(F);
202 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000203 return false;
204 }
205
206 virtual bool doFinalization(Module *M) {
207 endModule();
208 return false;
209 }
210
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000211 void emitFunction(const Function *F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000212private :
213 void emitBasicBlock(const BasicBlock *BB);
214 void emitMachineInst(const MachineInstr *MI);
215
216 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
217 void printOneOperand(const MachineOperand &Op);
218
219 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
220 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000221
Chris Lattnere88f78c2001-09-19 13:47:27 +0000222 unsigned getOperandMask(unsigned Opcode) {
223 switch (Opcode) {
224 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000225 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000226 default: return 0; // By default, don't hack operands...
227 }
228 }
229};
230
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000231inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000232SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
233 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000234 switch (MI->getOpCode()) {
235 case JMPLCALL:
236 case JMPLRET: return (opNum == 0);
237 default: return false;
238 }
239}
240
241
242inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000243SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
244 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000245 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
246 return (opNum == 0);
247 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
248 return (opNum == 1);
249 else
250 return false;
251}
252
253
254#define PrintOp1PlusOp2(Op1, Op2) \
255 printOneOperand(Op1); \
256 toAsm << "+"; \
257 printOneOperand(Op2);
258
259unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000260SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000261 unsigned int opNum)
262{
263 const MachineOperand& Op = MI->getOperand(opNum);
264
265 if (OpIsBranchTargetLabel(MI, opNum))
266 {
267 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
268 return 2;
269 }
270 else if (OpIsMemoryAddressBase(MI, opNum))
271 {
272 toAsm << "[";
273 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
274 toAsm << "]";
275 return 2;
276 }
277 else
278 {
279 printOneOperand(Op);
280 return 1;
281 }
282}
283
284
285void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000286SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &op)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000287{
288 switch (op.getOperandType())
289 {
290 case MachineOperand::MO_VirtualRegister:
291 case MachineOperand::MO_CCRegister:
292 case MachineOperand::MO_MachineRegister:
293 {
294 int RegNum = (int)op.getAllocatedRegNum();
295
Vikram S. Advefbd21612002-03-31 19:03:58 +0000296 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000297 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
298 toAsm << "<NULL VALUE>";
299 } else {
300 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
301 }
302 break;
303 }
304
305 case MachineOperand::MO_PCRelativeDisp:
306 {
307 const Value *Val = op.getVRegValue();
308 if (!Val)
309 toAsm << "\t<*NULL Value*>";
Chris Lattner31bcdb82002-04-28 19:55:58 +0000310 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000311 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000312 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000313 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000314 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000315 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000316 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000317 toAsm << getID(CV);
318 else
319 toAsm << "<unknown value=" << Val << ">";
320 break;
321 }
322
323 case MachineOperand::MO_SignExtendedImmed:
324 case MachineOperand::MO_UnextendedImmed:
325 toAsm << (long)op.getImmedValue();
326 break;
327
328 default:
329 toAsm << op; // use dump field
330 break;
331 }
332}
333
334
335void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000336SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000337{
338 unsigned Opcode = MI->getOpCode();
339
340 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
341 return; // IGNORE PHI NODES
342
343 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
344
345 unsigned Mask = getOperandMask(Opcode);
346
347 bool NeedComma = false;
348 unsigned N = 1;
349 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
350 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
351 if (NeedComma) toAsm << ", "; // Handle comma outputing
352 NeedComma = true;
353 N = printOperands(MI, OpNum);
354 }
355 else
356 N = 1;
357
358 toAsm << "\n";
359}
360
361void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000362SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000363{
364 // Emit a label for the basic block
365 toAsm << getID(BB) << ":\n";
366
367 // Get the vector of machine instructions corresponding to this bb.
368 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
369 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
370
371 // Loop over all of the instructions in the basic block...
372 for (; MII != MIE; ++MII)
373 emitMachineInst(*MII);
374 toAsm << "\n"; // Seperate BB's with newlines
375}
376
377void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000378SparcFunctionAsmPrinter::emitFunction(const Function *M)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000379{
380 string methName = getID(M);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000381 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000382 enterSection(AsmPrinter::Text);
383 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
384 //toAsm << "\t.type\t" << methName << ",#function\n";
385 toAsm << "\t.type\t" << methName << ", 2\n";
386 toAsm << methName << ":\n";
387
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000388 // Output code for all of the basic blocks in the function...
389 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000390 emitBasicBlock(*I);
391
392 // Output a .size directive so the debugger knows the extents of the function
393 toAsm << ".EndOf_" << methName << ":\n\t.size "
394 << methName << ", .EndOf_"
395 << methName << "-" << methName << "\n";
396
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000397 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000398 toAsm << "\n\n";
399}
400
401} // End anonymous namespace
402
Chris Lattnerf57b8452002-04-27 06:56:12 +0000403Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000404 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000405}
406
407
408
409
410
411//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000412// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000413//===----------------------------------------------------------------------===//
414
415namespace {
416
417class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
418public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000419 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
420 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000421
422 virtual bool run(Module *M) {
423 startModule(M);
424 emitGlobalsAndConstants(M);
425 endModule();
426 return false;
427 }
428
429 void emitGlobalsAndConstants(const Module *M);
430
431 void printGlobalVariable(const GlobalVariable *GV);
432 void printSingleConstant( const Constant* CV);
433 void printConstantValueOnly(const Constant* CV);
434 void printConstant( const Constant* CV, std::string valID = "");
435
436 static void FoldConstants(const Module *M,
437 std::hash_set<const Constant*> &moduleConstants);
438
439};
440
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000441
442// Can we treat the specified array as a string? Only if it is an array of
443// ubytes or non-negative sbytes.
444//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000445static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000446 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
447 if (ETy == Type::UByteTy) return true;
448 if (ETy != Type::SByteTy) return false;
449
450 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000451 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000452 return false;
453
454 return true;
455}
456
457// toOctal - Convert the low order bits of X into an octal letter
458static inline char toOctal(int X) {
459 return (X&7)+'0';
460}
461
462// getAsCString - Return the specified array as a C compatible string, only if
463// the predicate isStringCompatible is true.
464//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000465static string getAsCString(ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000466 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000467
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000468 string Result;
469 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
470 Result = "\"";
471 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
472 unsigned char C = (ETy == Type::SByteTy) ?
473 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
474 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
475
476 if (isprint(C)) {
477 Result += C;
478 } else {
479 switch(C) {
480 case '\a': Result += "\\a"; break;
481 case '\b': Result += "\\b"; break;
482 case '\f': Result += "\\f"; break;
483 case '\n': Result += "\\n"; break;
484 case '\r': Result += "\\r"; break;
485 case '\t': Result += "\\t"; break;
486 case '\v': Result += "\\v"; break;
487 default:
488 Result += '\\';
489 Result += toOctal(C >> 6);
490 Result += toOctal(C >> 3);
491 Result += toOctal(C >> 0);
492 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000493 }
494 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000495 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000496 Result += "\"";
497
498 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000499}
500
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000501inline bool
502ArrayTypeIsString(ArrayType* arrayType)
503{
504 return (arrayType->getElementType() == Type::UByteTy ||
505 arrayType->getElementType() == Type::SByteTy);
506}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000507
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000508inline const string
509TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000510{
511 switch(type->getPrimitiveID())
512 {
513 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
514 return ".byte";
515 case Type::UShortTyID: case Type::ShortTyID:
516 return ".half";
517 case Type::UIntTyID: case Type::IntTyID:
518 return ".word";
519 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
520 return ".xword";
521 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000522 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000523 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000524 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000525 case Type::ArrayTyID:
526 if (ArrayTypeIsString((ArrayType*) type))
527 return ".ascii";
528 else
529 return "<InvaliDataTypeForPrinting>";
530 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000531 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000532 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000533}
534
Vikram S. Adve21447222001-11-10 02:03:06 +0000535// Get the size of the constant for the given target.
536// If this is an unsized array, return 0.
537//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000538inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000539ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000540{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000541 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000542 {
543 ArrayType *aty = cast<ArrayType>(CPA->getType());
544 if (ArrayTypeIsString(aty))
545 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000546 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000547
548 return target.findOptimalStorageSize(CV->getType());
549}
550
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000551
552
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000553// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000554// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
555//
556inline unsigned int
557SizeToAlignment(unsigned int size, const TargetMachine& target)
558{
559 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
560 if (size > (unsigned) cacheLineSize / 2)
561 return cacheLineSize;
562 else
563 for (unsigned sz=1; /*no condition*/; sz *= 2)
564 if (sz >= size)
565 return sz;
566}
567
568// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000569//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000570inline unsigned int
571TypeToAlignment(const Type* type, const TargetMachine& target)
572{
Vikram S. Adve21447222001-11-10 02:03:06 +0000573 return SizeToAlignment(target.findOptimalStorageSize(type), target);
574}
575
576// Get the size of the constant and then use SizeToAlignment.
577// Handles strings as a special case;
578inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000579ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000580{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000581 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000582 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
583 return SizeToAlignment(1 + CPA->getNumOperands(), target);
584
585 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000586}
587
588
Vikram S. Adve21447222001-11-10 02:03:06 +0000589// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000590void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000591SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000592{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000593 assert(CV->getType() != Type::VoidTy &&
594 CV->getType() != Type::TypeTy &&
595 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000596 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000597
Chris Lattnerf678dc62002-04-11 21:44:02 +0000598 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
599 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000600
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000601 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000602
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000603 if (CV->getType()->isPrimitiveType())
604 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000605 if (CV->getType()->isFloatingPoint()) {
606 // FP Constants are printed as integer constants to avoid losing
607 // precision...
608 double Val = cast<ConstantFP>(CV)->getValue();
609 if (CV->getType() == Type::FloatTy) {
610 float FVal = (float)Val;
611 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
612 toAsm << *(unsigned int*)ProxyPtr;
613 } else if (CV->getType() == Type::DoubleTy) {
614 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
615 toAsm << *(uint64_t*)ProxyPtr;
616 } else {
617 assert(0 && "Unknown floating point type!");
618 }
619
620 toAsm << "\t! " << CV->getType()->getDescription()
621 << " value: " << Val << "\n";
622 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000623 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000624 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000625 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000626 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000627 {
Chris Lattner697954c2002-01-20 22:54:45 +0000628 assert(CPP->isNullValue() &&
629 "Cannot yet print non-null pointer constants to assembly");
630 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000631 }
Chris Lattner697954c2002-01-20 22:54:45 +0000632 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000633 {
634 assert(0 && "Cannot yet initialize pointer refs in assembly");
635 }
636 else
637 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000638 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000639 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000640}
641
Vikram S. Adve21447222001-11-10 02:03:06 +0000642// Print a constant value or values (it may be an aggregate).
643// Uses printSingleConstant() to print each individual value.
644void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000645SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000646{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000647 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000648
649 if (CPA && isStringCompatible(CPA))
650 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000651 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000652 }
653 else if (CPA)
654 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000655 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000656 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000657 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000658 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000659 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000660 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000661 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000662 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000663 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000664 }
665 else
666 this->printSingleConstant(CV);
667}
668
669// Print a constant (which may be an aggregate) prefixed by all the
670// appropriate directives. Uses printConstantValueOnly() to print the
671// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000672void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000673SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000674{
675 if (valID.length() == 0)
676 valID = getID(CV);
677
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000678 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000679
680 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000681 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000682 if (CPA && isStringCompatible(CPA))
683 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000684 toAsm << valID << ":\n";
685 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000686 return;
687 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000688
Chris Lattner697954c2002-01-20 22:54:45 +0000689 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000690
691 unsigned int constSize = ConstantToSize(CV, Target);
692 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000693 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000694
Chris Lattner697954c2002-01-20 22:54:45 +0000695 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000696
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000697 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000698}
699
700
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000701void SparcModuleAsmPrinter::FoldConstants(const Module *M,
702 std::hash_set<const Constant*> &MC) {
703 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
704 if (!(*I)->isExternal()) {
705 const std::hash_set<const Constant*> &pool =
706 MachineCodeForMethod::get(*I).getConstantPoolValues();
707 MC.insert(pool.begin(), pool.end());
708 }
709}
710
711void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000712{
Chris Lattner697954c2002-01-20 22:54:45 +0000713 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714
715 if (GV->hasInitializer())
716 printConstant(GV->getInitializer(), getID(GV));
717 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000718 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
719 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000720 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000721 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000722 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000723 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000724 }
725}
726
727
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000728void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000729 // First, get the constants there were marked by the code generator for
730 // inclusion in the assembly code data area and fold them all into a
731 // single constant pool since there may be lots of duplicates. Also,
732 // lets force these constants into the slot table so that we can get
733 // unique names for unnamed constants also.
734 //
Chris Lattner697954c2002-01-20 22:54:45 +0000735 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000736 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000737
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000738 // Now, emit the three data sections separately; the cost of I/O should
739 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000740
741 // Section 1 : Read-only data section (implies initialized)
742 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000743 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000744 if ((*GI)->hasInitializer() && (*GI)->isConstant())
745 printGlobalVariable(*GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000746
Chris Lattner697954c2002-01-20 22:54:45 +0000747 for (std::hash_set<const Constant*>::const_iterator
748 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000749 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000750 printConstant(*I);
751
Vikram S. Advefbd21612002-03-31 19:03:58 +0000752 // Section 2 : Initialized read-write data section
753 enterSection(AsmPrinter::InitRWData);
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);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000757
Vikram S. Advefbd21612002-03-31 19:03:58 +0000758 // Section 3 : Uninitialized read-write data section
759 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000760 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000761 if (! (*GI)->hasInitializer())
762 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000763
Chris Lattner697954c2002-01-20 22:54:45 +0000764 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765}
766
Chris Lattnere88f78c2001-09-19 13:47:27 +0000767} // End anonymous namespace
768
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000769Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
770 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000771}