blob: 8d87bfec6beda57dbca0bcdfa8bc5d37e109f574 [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
Chris Lattner96c466b2002-04-29 14:57:45 +0000195 const char *getPassName() const {
196 return "Output Sparc Assembly for Functions";
197 }
198
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000199 virtual bool doInitialization(Module *M) {
200 startModule(M);
201 return false;
202 }
203
Chris Lattnerf57b8452002-04-27 06:56:12 +0000204 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000205 startFunction(F);
206 emitFunction(F);
207 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000208 return false;
209 }
210
211 virtual bool doFinalization(Module *M) {
212 endModule();
213 return false;
214 }
215
Chris Lattner97e52e42002-04-28 21:27:06 +0000216 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
217 AU.setPreservesAll();
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*>";
Chris Lattner31bcdb82002-04-28 19:55:58 +0000319 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000320 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000321 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000323 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000324 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000325 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000326 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
Chris Lattnerf57b8452002-04-27 06:56:12 +0000412Pass *UltraSparc::getFunctionAsmPrinterPass(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
Chris Lattner96c466b2002-04-29 14:57:45 +0000431 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
432
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000433 virtual bool run(Module *M) {
434 startModule(M);
435 emitGlobalsAndConstants(M);
436 endModule();
437 return false;
438 }
439
Chris Lattner97e52e42002-04-28 21:27:06 +0000440 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
441 AU.setPreservesAll();
442 }
443
444private:
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000445 void emitGlobalsAndConstants(const Module *M);
446
447 void printGlobalVariable(const GlobalVariable *GV);
448 void printSingleConstant( const Constant* CV);
449 void printConstantValueOnly(const Constant* CV);
450 void printConstant( const Constant* CV, std::string valID = "");
451
452 static void FoldConstants(const Module *M,
453 std::hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000454};
455
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000456
457// Can we treat the specified array as a string? Only if it is an array of
458// ubytes or non-negative sbytes.
459//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000460static bool isStringCompatible(ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000461 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
462 if (ETy == Type::UByteTy) return true;
463 if (ETy != Type::SByteTy) return false;
464
465 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000466 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000467 return false;
468
469 return true;
470}
471
472// toOctal - Convert the low order bits of X into an octal letter
473static inline char toOctal(int X) {
474 return (X&7)+'0';
475}
476
477// getAsCString - Return the specified array as a C compatible string, only if
478// the predicate isStringCompatible is true.
479//
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000480static string getAsCString(ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000481 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000482
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000483 string Result;
484 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
485 Result = "\"";
486 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
487 unsigned char C = (ETy == Type::SByteTy) ?
488 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
489 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
490
491 if (isprint(C)) {
492 Result += C;
493 } else {
494 switch(C) {
495 case '\a': Result += "\\a"; break;
496 case '\b': Result += "\\b"; break;
497 case '\f': Result += "\\f"; break;
498 case '\n': Result += "\\n"; break;
499 case '\r': Result += "\\r"; break;
500 case '\t': Result += "\\t"; break;
501 case '\v': Result += "\\v"; break;
502 default:
503 Result += '\\';
504 Result += toOctal(C >> 6);
505 Result += toOctal(C >> 3);
506 Result += toOctal(C >> 0);
507 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000508 }
509 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000510 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000511 Result += "\"";
512
513 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000514}
515
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000516inline bool
517ArrayTypeIsString(ArrayType* arrayType)
518{
519 return (arrayType->getElementType() == Type::UByteTy ||
520 arrayType->getElementType() == Type::SByteTy);
521}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000522
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000523inline const string
524TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000525{
526 switch(type->getPrimitiveID())
527 {
528 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
529 return ".byte";
530 case Type::UShortTyID: case Type::ShortTyID:
531 return ".half";
532 case Type::UIntTyID: case Type::IntTyID:
533 return ".word";
534 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
535 return ".xword";
536 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000537 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000538 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000539 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000540 case Type::ArrayTyID:
541 if (ArrayTypeIsString((ArrayType*) type))
542 return ".ascii";
543 else
544 return "<InvaliDataTypeForPrinting>";
545 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000546 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000547 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000548}
549
Vikram S. Adve21447222001-11-10 02:03:06 +0000550// Get the size of the constant for the given target.
551// If this is an unsized array, return 0.
552//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000553inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000554ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000555{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000556 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000557 {
558 ArrayType *aty = cast<ArrayType>(CPA->getType());
559 if (ArrayTypeIsString(aty))
560 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000561 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000562
563 return target.findOptimalStorageSize(CV->getType());
564}
565
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000566
567
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000568// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000569// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
570//
571inline unsigned int
572SizeToAlignment(unsigned int size, const TargetMachine& target)
573{
574 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
575 if (size > (unsigned) cacheLineSize / 2)
576 return cacheLineSize;
577 else
578 for (unsigned sz=1; /*no condition*/; sz *= 2)
579 if (sz >= size)
580 return sz;
581}
582
583// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000584//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000585inline unsigned int
586TypeToAlignment(const Type* type, const TargetMachine& target)
587{
Vikram S. Adve21447222001-11-10 02:03:06 +0000588 return SizeToAlignment(target.findOptimalStorageSize(type), target);
589}
590
591// Get the size of the constant and then use SizeToAlignment.
592// Handles strings as a special case;
593inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000594ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000595{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000596 if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000597 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
598 return SizeToAlignment(1 + CPA->getNumOperands(), target);
599
600 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000601}
602
603
Vikram S. Adve21447222001-11-10 02:03:06 +0000604// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000605void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000606SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000607{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000608 assert(CV->getType() != Type::VoidTy &&
609 CV->getType() != Type::TypeTy &&
610 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000611 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000612
Chris Lattnerf678dc62002-04-11 21:44:02 +0000613 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
614 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000615
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000616 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000617
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000618 if (CV->getType()->isPrimitiveType())
619 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000620 if (CV->getType()->isFloatingPoint()) {
621 // FP Constants are printed as integer constants to avoid losing
622 // precision...
623 double Val = cast<ConstantFP>(CV)->getValue();
624 if (CV->getType() == Type::FloatTy) {
625 float FVal = (float)Val;
626 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
627 toAsm << *(unsigned int*)ProxyPtr;
628 } else if (CV->getType() == Type::DoubleTy) {
629 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
630 toAsm << *(uint64_t*)ProxyPtr;
631 } else {
632 assert(0 && "Unknown floating point type!");
633 }
634
635 toAsm << "\t! " << CV->getType()->getDescription()
636 << " value: " << Val << "\n";
637 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000638 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000639 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000640 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000641 else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000642 {
Chris Lattner697954c2002-01-20 22:54:45 +0000643 assert(CPP->isNullValue() &&
644 "Cannot yet print non-null pointer constants to assembly");
645 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000646 }
Chris Lattner697954c2002-01-20 22:54:45 +0000647 else if (isa<ConstantPointerRef>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000648 {
649 assert(0 && "Cannot yet initialize pointer refs in assembly");
650 }
651 else
652 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000653 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000654 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000655}
656
Vikram S. Adve21447222001-11-10 02:03:06 +0000657// Print a constant value or values (it may be an aggregate).
658// Uses printSingleConstant() to print each individual value.
659void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000660SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000661{
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000662 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000663
664 if (CPA && isStringCompatible(CPA))
665 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000666 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000667 }
668 else if (CPA)
669 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000670 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000671 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000672 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000673 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000674 else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000675 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000676 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve21447222001-11-10 02:03:06 +0000677 for (unsigned i=1; i < constValues.size(); i++)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000678 this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000679 }
680 else
681 this->printSingleConstant(CV);
682}
683
684// Print a constant (which may be an aggregate) prefixed by all the
685// appropriate directives. Uses printConstantValueOnly() to print the
686// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000687void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000688SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000689{
690 if (valID.length() == 0)
691 valID = getID(CV);
692
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000693 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000694
695 // Print .size and .type only if it is not a string.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000696 ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000697 if (CPA && isStringCompatible(CPA))
698 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000699 toAsm << valID << ":\n";
700 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000701 return;
702 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000703
Chris Lattner697954c2002-01-20 22:54:45 +0000704 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000705
706 unsigned int constSize = ConstantToSize(CV, Target);
707 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000708 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000709
Chris Lattner697954c2002-01-20 22:54:45 +0000710 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000711
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000712 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000713}
714
715
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000716void SparcModuleAsmPrinter::FoldConstants(const Module *M,
717 std::hash_set<const Constant*> &MC) {
718 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
719 if (!(*I)->isExternal()) {
720 const std::hash_set<const Constant*> &pool =
721 MachineCodeForMethod::get(*I).getConstantPoolValues();
722 MC.insert(pool.begin(), pool.end());
723 }
724}
725
726void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000727{
Chris Lattner697954c2002-01-20 22:54:45 +0000728 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000729
730 if (GV->hasInitializer())
731 printConstant(GV->getInitializer(), getID(GV));
732 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000733 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
734 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000735 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000736 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000737 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000738 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000739 }
740}
741
742
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000743void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000744 // First, get the constants there were marked by the code generator for
745 // inclusion in the assembly code data area and fold them all into a
746 // single constant pool since there may be lots of duplicates. Also,
747 // lets force these constants into the slot table so that we can get
748 // unique names for unnamed constants also.
749 //
Chris Lattner697954c2002-01-20 22:54:45 +0000750 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000751 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000752
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000753 // Now, emit the three data sections separately; the cost of I/O should
754 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000755
756 // Section 1 : Read-only data section (implies initialized)
757 enterSection(AsmPrinter::ReadOnlyData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000758 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000759 if ((*GI)->hasInitializer() && (*GI)->isConstant())
760 printGlobalVariable(*GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000761
Chris Lattner697954c2002-01-20 22:54:45 +0000762 for (std::hash_set<const Constant*>::const_iterator
763 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000764 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765 printConstant(*I);
766
Vikram S. Advefbd21612002-03-31 19:03:58 +0000767 // Section 2 : Initialized read-write data section
768 enterSection(AsmPrinter::InitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000769 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000770 if ((*GI)->hasInitializer() && ! (*GI)->isConstant())
771 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000772
Vikram S. Advefbd21612002-03-31 19:03:58 +0000773 // Section 3 : Uninitialized read-write data section
774 enterSection(AsmPrinter::UninitRWData);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000775 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
Vikram S. Advefbd21612002-03-31 19:03:58 +0000776 if (! (*GI)->hasInitializer())
777 printGlobalVariable(*GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000778
Chris Lattner697954c2002-01-20 22:54:45 +0000779 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000780}
781
Chris Lattnere88f78c2001-09-19 13:47:27 +0000782} // End anonymous namespace
783
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000784Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
785 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000786}