blob: 0a7f5ea7bf861253562c978d9a9835683ebfc7c7 [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"
Vikram S. Adveb2debdc2002-07-08 23:30:59 +000016#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattnerc019a172002-02-03 07:48:06 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000019#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000020#include "llvm/DerivedTypes.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"
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
Chris Lattner09ff1122002-07-24 21:21:32 +000037 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000038 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 Lattner0b12b5f2002-06-25 16:13:21 +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
Chris Lattner0b12b5f2002-06-25 16:13:21 +000074 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000075 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000076 idTable = new GlobalIdTable(&M);
77 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000078 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000081 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000082 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000083 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000084 void endFunction(Function &) {
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
Chris Lattner8b1b4e22002-07-16 18:35:16 +000090 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000091 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000092 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
93 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000094 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000095
Chris Lattnere88f78c2001-09-19 13:47:27 +000096 // enterSection - Use this method to enter a different section of the output
97 // executable. This is used to only output neccesary section transitions.
98 //
99 void enterSection(enum Sections S) {
100 if (S == CurSection) return; // Only switch section if neccesary
101 CurSection = S;
102
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000103 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000104 switch (S)
105 {
106 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000107 case Text: toAsm << "\".text\""; break;
108 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
109 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
110 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000111 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000112 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000113 }
114
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000115 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000116 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000117
118 // Symbol names in Sparc assembly language have these rules:
119 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
120 // (b) A name beginning in "." is treated as a local name.
121 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
122 //
123 if (S[0] == '_' || isdigit(S[0]))
124 Result += "ll";
125
126 for (unsigned i = 0; i < S.size(); ++i)
127 {
128 char C = S[i];
129 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
130 Result += C;
131 else
132 {
133 Result += '_';
134 Result += char('0' + ((unsigned char)C >> 4));
135 Result += char('0' + (C & 0xF));
136 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000137 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000138 return Result;
139 }
140
Chris Lattnere88f78c2001-09-19 13:47:27 +0000141 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000142 // the name of the identifier if possible (qualified by the type), and
143 // use a numbered value based on prefix otherwise.
144 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000145 //
146 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000147 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
148
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000149 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adved198c472002-03-18 03:07:26 +0000150
151 // Qualify all internal names with a unique id.
152 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000153 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000154 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000155 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
156 if (I == idTable->valToIdMap.end())
157 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000158 else
159 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000160 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000161 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000163
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000164 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000166
Chris Lattnere88f78c2001-09-19 13:47:27 +0000167 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000168 string getID(const Function *F) {
169 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000170 }
171 string getID(const BasicBlock *BB) {
172 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
173 }
174 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000175 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000177 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000178 return getID(CV, "LLVMConst_", ".C_");
179 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000180 string getID(const GlobalValue *GV) {
181 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
182 return getID(V);
183 else if (const Function *F = dyn_cast<Function>(GV))
184 return getID(F);
185 assert(0 && "Unexpected type of GlobalValue!");
186 return "";
187 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000188};
189
190
191
192//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000193// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000194//===----------------------------------------------------------------------===//
195
Chris Lattnerf57b8452002-04-27 06:56:12 +0000196struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000197 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000198 : AsmPrinter(os, t) {}
199
Chris Lattner96c466b2002-04-29 14:57:45 +0000200 const char *getPassName() const {
201 return "Output Sparc Assembly for Functions";
202 }
203
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000204 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000205 startModule(M);
206 return false;
207 }
208
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000209 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000210 startFunction(F);
211 emitFunction(F);
212 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000213 return false;
214 }
215
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000216 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000217 endModule();
218 return false;
219 }
220
Chris Lattner97e52e42002-04-28 21:27:06 +0000221 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
222 AU.setPreservesAll();
223 }
224
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000225 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000226private :
227 void emitBasicBlock(const BasicBlock *BB);
228 void emitMachineInst(const MachineInstr *MI);
229
230 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
231 void printOneOperand(const MachineOperand &Op);
232
233 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
234 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000235
Chris Lattnere88f78c2001-09-19 13:47:27 +0000236 unsigned getOperandMask(unsigned Opcode) {
237 switch (Opcode) {
238 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000239 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000240 default: return 0; // By default, don't hack operands...
241 }
242 }
243};
244
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000245inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000246SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
247 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000248 switch (MI->getOpCode()) {
249 case JMPLCALL:
250 case JMPLRET: return (opNum == 0);
251 default: return false;
252 }
253}
254
255
256inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000257SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
258 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000259 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
260 return (opNum == 0);
261 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
262 return (opNum == 1);
263 else
264 return false;
265}
266
267
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000268#define PrintOp1PlusOp2(mop1, mop2) \
269 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000270 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000271 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000272
273unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000274SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000275 unsigned int opNum)
276{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000277 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000278
279 if (OpIsBranchTargetLabel(MI, opNum))
280 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000281 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000282 return 2;
283 }
284 else if (OpIsMemoryAddressBase(MI, opNum))
285 {
286 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000287 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000288 toAsm << "]";
289 return 2;
290 }
291 else
292 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000293 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000294 return 1;
295 }
296}
297
298
299void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000300SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000301{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000302 bool needBitsFlag = true;
303
304 if (mop.opHiBits32())
305 toAsm << "%lm(";
306 else if (mop.opLoBits32())
307 toAsm << "%lo(";
308 else if (mop.opHiBits64())
309 toAsm << "%hh(";
310 else if (mop.opLoBits64())
311 toAsm << "%hm(";
312 else
313 needBitsFlag = false;
314
315 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000316 {
317 case MachineOperand::MO_VirtualRegister:
318 case MachineOperand::MO_CCRegister:
319 case MachineOperand::MO_MachineRegister:
320 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000321 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322
Vikram S. Advefbd21612002-03-31 19:03:58 +0000323 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000324 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
325 toAsm << "<NULL VALUE>";
326 } else {
327 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
328 }
329 break;
330 }
331
332 case MachineOperand::MO_PCRelativeDisp:
333 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000334 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000335 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
336
337 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000338 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000339 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000340 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000341 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000342 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000343 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000344 toAsm << getID(CV);
345 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000346 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347 break;
348 }
349
350 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000351 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000352 break;
353
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000354 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000355 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000356 break;
357
358 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000359 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000360 break;
361 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000362
363 if (needBitsFlag)
364 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000365}
366
367
368void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000369SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000370{
371 unsigned Opcode = MI->getOpCode();
372
373 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
374 return; // IGNORE PHI NODES
375
376 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
377
378 unsigned Mask = getOperandMask(Opcode);
379
380 bool NeedComma = false;
381 unsigned N = 1;
382 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
383 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
384 if (NeedComma) toAsm << ", "; // Handle comma outputing
385 NeedComma = true;
386 N = printOperands(MI, OpNum);
387 }
388 else
389 N = 1;
390
391 toAsm << "\n";
392}
393
394void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000395SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000396{
397 // Emit a label for the basic block
398 toAsm << getID(BB) << ":\n";
399
400 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000401 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000402 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
403
404 // Loop over all of the instructions in the basic block...
405 for (; MII != MIE; ++MII)
406 emitMachineInst(*MII);
407 toAsm << "\n"; // Seperate BB's with newlines
408}
409
410void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000411SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000412{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000413 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000414 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 enterSection(AsmPrinter::Text);
416 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
417 //toAsm << "\t.type\t" << methName << ",#function\n";
418 toAsm << "\t.type\t" << methName << ", 2\n";
419 toAsm << methName << ":\n";
420
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000421 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000422 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
423 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000424
425 // Output a .size directive so the debugger knows the extents of the function
426 toAsm << ".EndOf_" << methName << ":\n\t.size "
427 << methName << ", .EndOf_"
428 << methName << "-" << methName << "\n";
429
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000430 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431 toAsm << "\n\n";
432}
433
434} // End anonymous namespace
435
Chris Lattnerf57b8452002-04-27 06:56:12 +0000436Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000437 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000438}
439
440
441
442
443
444//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000445// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000446//===----------------------------------------------------------------------===//
447
448namespace {
449
450class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
451public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000452 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
453 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000454
Chris Lattner96c466b2002-04-29 14:57:45 +0000455 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
456
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000457 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000458 startModule(M);
459 emitGlobalsAndConstants(M);
460 endModule();
461 return false;
462 }
463
Chris Lattner97e52e42002-04-28 21:27:06 +0000464 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
465 AU.setPreservesAll();
466 }
467
468private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000469 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000470
471 void printGlobalVariable(const GlobalVariable *GV);
472 void printSingleConstant( const Constant* CV);
473 void printConstantValueOnly(const Constant* CV);
474 void printConstant( const Constant* CV, std::string valID = "");
475
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000476 static void FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000477 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000478};
479
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000480
481// Can we treat the specified array as a string? Only if it is an array of
482// ubytes or non-negative sbytes.
483//
Chris Lattner122787b2002-06-05 18:08:26 +0000484static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000485 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
486 if (ETy == Type::UByteTy) return true;
487 if (ETy != Type::SByteTy) return false;
488
489 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000490 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000491 return false;
492
493 return true;
494}
495
496// toOctal - Convert the low order bits of X into an octal letter
497static inline char toOctal(int X) {
498 return (X&7)+'0';
499}
500
501// getAsCString - Return the specified array as a C compatible string, only if
502// the predicate isStringCompatible is true.
503//
Chris Lattner122787b2002-06-05 18:08:26 +0000504static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000505 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000506
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000507 string Result;
508 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
509 Result = "\"";
510 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
511 unsigned char C = (ETy == Type::SByteTy) ?
512 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
513 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
514
Vikram S. Adve242a8082002-05-19 15:25:51 +0000515 if (C == '"') {
516 Result += "\\\"";
517 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000518 Result += C;
519 } else {
520 switch(C) {
521 case '\a': Result += "\\a"; break;
522 case '\b': Result += "\\b"; break;
523 case '\f': Result += "\\f"; break;
524 case '\n': Result += "\\n"; break;
525 case '\r': Result += "\\r"; break;
526 case '\t': Result += "\\t"; break;
527 case '\v': Result += "\\v"; break;
528 default:
529 Result += '\\';
530 Result += toOctal(C >> 6);
531 Result += toOctal(C >> 3);
532 Result += toOctal(C >> 0);
533 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000534 }
535 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000536 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000537 Result += "\"";
538
539 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000540}
541
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000542inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000543ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000544{
545 return (arrayType->getElementType() == Type::UByteTy ||
546 arrayType->getElementType() == Type::SByteTy);
547}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000548
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000549inline const string
550TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000551{
552 switch(type->getPrimitiveID())
553 {
554 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
555 return ".byte";
556 case Type::UShortTyID: case Type::ShortTyID:
557 return ".half";
558 case Type::UIntTyID: case Type::IntTyID:
559 return ".word";
560 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
561 return ".xword";
562 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000563 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000564 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000565 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000566 case Type::ArrayTyID:
567 if (ArrayTypeIsString((ArrayType*) type))
568 return ".ascii";
569 else
570 return "<InvaliDataTypeForPrinting>";
571 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000572 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000573 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000574}
575
Vikram S. Adve21447222001-11-10 02:03:06 +0000576// Get the size of the constant for the given target.
577// If this is an unsized array, return 0.
578//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000579inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000580ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000581{
Chris Lattner122787b2002-06-05 18:08:26 +0000582 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000583 {
Chris Lattner122787b2002-06-05 18:08:26 +0000584 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000585 if (ArrayTypeIsString(aty))
586 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000587 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000588
589 return target.findOptimalStorageSize(CV->getType());
590}
591
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000592
593
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000594// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000595// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
596//
597inline unsigned int
598SizeToAlignment(unsigned int size, const TargetMachine& target)
599{
600 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
601 if (size > (unsigned) cacheLineSize / 2)
602 return cacheLineSize;
603 else
604 for (unsigned sz=1; /*no condition*/; sz *= 2)
605 if (sz >= size)
606 return sz;
607}
608
609// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000610//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000611inline unsigned int
612TypeToAlignment(const Type* type, const TargetMachine& target)
613{
Vikram S. Adve21447222001-11-10 02:03:06 +0000614 return SizeToAlignment(target.findOptimalStorageSize(type), target);
615}
616
617// Get the size of the constant and then use SizeToAlignment.
618// Handles strings as a special case;
619inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000620ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000621{
Chris Lattner122787b2002-06-05 18:08:26 +0000622 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000623 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
624 return SizeToAlignment(1 + CPA->getNumOperands(), target);
625
626 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000627}
628
629
Vikram S. Adve21447222001-11-10 02:03:06 +0000630// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000631void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000632SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000633{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000634 assert(CV->getType() != Type::VoidTy &&
635 CV->getType() != Type::TypeTy &&
636 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000637 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000638
Chris Lattnerf678dc62002-04-11 21:44:02 +0000639 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
640 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000641
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000642 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000643
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 if (CV->getType()->isPrimitiveType())
645 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000646 if (CV->getType()->isFloatingPoint()) {
647 // FP Constants are printed as integer constants to avoid losing
648 // precision...
649 double Val = cast<ConstantFP>(CV)->getValue();
650 if (CV->getType() == Type::FloatTy) {
651 float FVal = (float)Val;
652 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
653 toAsm << *(unsigned int*)ProxyPtr;
654 } else if (CV->getType() == Type::DoubleTy) {
655 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
656 toAsm << *(uint64_t*)ProxyPtr;
657 } else {
658 assert(0 && "Unknown floating point type!");
659 }
660
661 toAsm << "\t! " << CV->getType()->getDescription()
662 << " value: " << Val << "\n";
663 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000664 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000665 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000666 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000667 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
668 { // This is a constant address for a global variable or method.
669 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000670 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000671 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000672 else if (isa<ConstantPointerNull>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000673 {
Chris Lattner697954c2002-01-20 22:54:45 +0000674 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000675 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000676 else
677 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000678 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000679 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000680}
681
Vikram S. Adve21447222001-11-10 02:03:06 +0000682// Print a constant value or values (it may be an aggregate).
683// Uses printSingleConstant() to print each individual value.
684void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000685SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000686{
Chris Lattner122787b2002-06-05 18:08:26 +0000687 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000688
689 if (CPA && isStringCompatible(CPA))
690 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000691 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000692 }
693 else if (CPA)
694 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000695 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000696 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000697 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000698 }
Chris Lattner122787b2002-06-05 18:08:26 +0000699 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000700 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000701 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000702 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000703 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000704 }
705 else
Chris Lattner122787b2002-06-05 18:08:26 +0000706 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000707}
708
709// Print a constant (which may be an aggregate) prefixed by all the
710// appropriate directives. Uses printConstantValueOnly() to print the
711// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000712void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000713SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000714{
715 if (valID.length() == 0)
716 valID = getID(CV);
717
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000718 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000719
720 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000721 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000722 if (CPA && isStringCompatible(CPA))
723 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000724 toAsm << valID << ":\n";
725 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000726 return;
727 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000728
Chris Lattner697954c2002-01-20 22:54:45 +0000729 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000730
731 unsigned int constSize = ConstantToSize(CV, Target);
732 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000733 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000734
Chris Lattner697954c2002-01-20 22:54:45 +0000735 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000736
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000737 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000738}
739
740
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000741void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000742 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000743 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
744 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000745 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000746 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000747 MC.insert(pool.begin(), pool.end());
748 }
749}
750
751void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000752{
Chris Lattner697954c2002-01-20 22:54:45 +0000753 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000754
755 if (GV->hasInitializer())
756 printConstant(GV->getInitializer(), getID(GV));
757 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000758 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
759 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000760 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000761 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000762 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000763 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000764 }
765}
766
767
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000768void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000769 // First, get the constants there were marked by the code generator for
770 // inclusion in the assembly code data area and fold them all into a
771 // single constant pool since there may be lots of duplicates. Also,
772 // lets force these constants into the slot table so that we can get
773 // unique names for unnamed constants also.
774 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000775 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000776 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000777
Chris Lattner637ed862002-08-07 21:39:48 +0000778 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000779 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000780 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000781 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000782 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000783
784 // Output global variables...
785 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) {
786 if (GI->hasInitializer() && GI->isConstant()) {
787 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
788 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
789 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
790 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
791 enterSection(AsmPrinter::InitRWData);
792 } else {
793 assert (!GI->hasInitializer() && "Unexpected global variable type found");
794 enterSection(AsmPrinter::UninitRWData); // Uninitialized data
795 }
796 printGlobalVariable(GI);
797 }
798
Chris Lattner697954c2002-01-20 22:54:45 +0000799 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000800}
801
Chris Lattnere88f78c2001-09-19 13:47:27 +0000802} // End anonymous namespace
803
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000804Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
805 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000806}