blob: 940b72205feade884a02817e31363f407548115d [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
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 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
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
Chris Lattner96c466b2002-04-29 14:57:45 +0000194 const char *getPassName() const {
195 return "Output Sparc Assembly for Functions";
196 }
197
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000198 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000199 startModule(M);
200 return false;
201 }
202
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000203 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000204 startFunction(F);
205 emitFunction(F);
206 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000207 return false;
208 }
209
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000210 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000211 endModule();
212 return false;
213 }
214
Chris Lattner97e52e42002-04-28 21:27:06 +0000215 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
216 AU.setPreservesAll();
217 }
218
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000219 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000220private :
221 void emitBasicBlock(const BasicBlock *BB);
222 void emitMachineInst(const MachineInstr *MI);
223
224 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
225 void printOneOperand(const MachineOperand &Op);
226
227 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
228 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000229
Chris Lattnere88f78c2001-09-19 13:47:27 +0000230 unsigned getOperandMask(unsigned Opcode) {
231 switch (Opcode) {
232 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000233 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000234 default: return 0; // By default, don't hack operands...
235 }
236 }
237};
238
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000239inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000240SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
241 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000242 switch (MI->getOpCode()) {
243 case JMPLCALL:
244 case JMPLRET: return (opNum == 0);
245 default: return false;
246 }
247}
248
249
250inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000251SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
252 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000253 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
254 return (opNum == 0);
255 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
256 return (opNum == 1);
257 else
258 return false;
259}
260
261
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000262#define PrintOp1PlusOp2(mop1, mop2) \
263 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000264 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000265 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000266
267unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000268SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000269 unsigned int opNum)
270{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000271 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000272
273 if (OpIsBranchTargetLabel(MI, opNum))
274 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000275 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000276 return 2;
277 }
278 else if (OpIsMemoryAddressBase(MI, opNum))
279 {
280 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000281 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000282 toAsm << "]";
283 return 2;
284 }
285 else
286 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000287 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000288 return 1;
289 }
290}
291
292
293void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000294SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000295{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000296 bool needBitsFlag = true;
297
298 if (mop.opHiBits32())
299 toAsm << "%lm(";
300 else if (mop.opLoBits32())
301 toAsm << "%lo(";
302 else if (mop.opHiBits64())
303 toAsm << "%hh(";
304 else if (mop.opLoBits64())
305 toAsm << "%hm(";
306 else
307 needBitsFlag = false;
308
309 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000310 {
311 case MachineOperand::MO_VirtualRegister:
312 case MachineOperand::MO_CCRegister:
313 case MachineOperand::MO_MachineRegister:
314 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000315 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000316
Vikram S. Advefbd21612002-03-31 19:03:58 +0000317 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
319 toAsm << "<NULL VALUE>";
320 } else {
321 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
322 }
323 break;
324 }
325
326 case MachineOperand::MO_PCRelativeDisp:
327 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000328 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000329 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
330
331 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000332 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000333 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000334 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000335 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000336 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000337 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000338 toAsm << getID(CV);
339 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000340 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000341 break;
342 }
343
344 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000345 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000346 break;
347
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000349 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000350 break;
351
352 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000353 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000354 break;
355 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000356
357 if (needBitsFlag)
358 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000359}
360
361
362void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000363SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000364{
365 unsigned Opcode = MI->getOpCode();
366
367 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
368 return; // IGNORE PHI NODES
369
370 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
371
372 unsigned Mask = getOperandMask(Opcode);
373
374 bool NeedComma = false;
375 unsigned N = 1;
376 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
377 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
378 if (NeedComma) toAsm << ", "; // Handle comma outputing
379 NeedComma = true;
380 N = printOperands(MI, OpNum);
381 }
382 else
383 N = 1;
384
385 toAsm << "\n";
386}
387
388void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000389SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000390{
391 // Emit a label for the basic block
392 toAsm << getID(BB) << ":\n";
393
394 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000395 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000396 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
397
398 // Loop over all of the instructions in the basic block...
399 for (; MII != MIE; ++MII)
400 emitMachineInst(*MII);
401 toAsm << "\n"; // Seperate BB's with newlines
402}
403
404void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000405SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000406{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000407 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000408 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000409 enterSection(AsmPrinter::Text);
410 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
411 //toAsm << "\t.type\t" << methName << ",#function\n";
412 toAsm << "\t.type\t" << methName << ", 2\n";
413 toAsm << methName << ":\n";
414
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000415 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000416 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
417 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000418
419 // Output a .size directive so the debugger knows the extents of the function
420 toAsm << ".EndOf_" << methName << ":\n\t.size "
421 << methName << ", .EndOf_"
422 << methName << "-" << methName << "\n";
423
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000424 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000425 toAsm << "\n\n";
426}
427
428} // End anonymous namespace
429
Chris Lattnerf57b8452002-04-27 06:56:12 +0000430Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000431 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000432}
433
434
435
436
437
438//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000439// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000440//===----------------------------------------------------------------------===//
441
442namespace {
443
444class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
445public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000446 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
447 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000448
Chris Lattner96c466b2002-04-29 14:57:45 +0000449 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
450
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000451 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000452 startModule(M);
453 emitGlobalsAndConstants(M);
454 endModule();
455 return false;
456 }
457
Chris Lattner97e52e42002-04-28 21:27:06 +0000458 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
459 AU.setPreservesAll();
460 }
461
462private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000463 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000464
465 void printGlobalVariable(const GlobalVariable *GV);
466 void printSingleConstant( const Constant* CV);
467 void printConstantValueOnly(const Constant* CV);
468 void printConstant( const Constant* CV, std::string valID = "");
469
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000470 static void FoldConstants(const Module &M,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000471 std::hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000472};
473
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000474
475// Can we treat the specified array as a string? Only if it is an array of
476// ubytes or non-negative sbytes.
477//
Chris Lattner122787b2002-06-05 18:08:26 +0000478static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000479 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
480 if (ETy == Type::UByteTy) return true;
481 if (ETy != Type::SByteTy) return false;
482
483 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000484 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000485 return false;
486
487 return true;
488}
489
490// toOctal - Convert the low order bits of X into an octal letter
491static inline char toOctal(int X) {
492 return (X&7)+'0';
493}
494
495// getAsCString - Return the specified array as a C compatible string, only if
496// the predicate isStringCompatible is true.
497//
Chris Lattner122787b2002-06-05 18:08:26 +0000498static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000499 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000500
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000501 string Result;
502 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
503 Result = "\"";
504 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
505 unsigned char C = (ETy == Type::SByteTy) ?
506 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
507 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
508
Vikram S. Adve242a8082002-05-19 15:25:51 +0000509 if (C == '"') {
510 Result += "\\\"";
511 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000512 Result += C;
513 } else {
514 switch(C) {
515 case '\a': Result += "\\a"; break;
516 case '\b': Result += "\\b"; break;
517 case '\f': Result += "\\f"; break;
518 case '\n': Result += "\\n"; break;
519 case '\r': Result += "\\r"; break;
520 case '\t': Result += "\\t"; break;
521 case '\v': Result += "\\v"; break;
522 default:
523 Result += '\\';
524 Result += toOctal(C >> 6);
525 Result += toOctal(C >> 3);
526 Result += toOctal(C >> 0);
527 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000528 }
529 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000530 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000531 Result += "\"";
532
533 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000534}
535
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000536inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000537ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000538{
539 return (arrayType->getElementType() == Type::UByteTy ||
540 arrayType->getElementType() == Type::SByteTy);
541}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000542
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000543inline const string
544TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000545{
546 switch(type->getPrimitiveID())
547 {
548 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
549 return ".byte";
550 case Type::UShortTyID: case Type::ShortTyID:
551 return ".half";
552 case Type::UIntTyID: case Type::IntTyID:
553 return ".word";
554 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
555 return ".xword";
556 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000557 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000558 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000559 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000560 case Type::ArrayTyID:
561 if (ArrayTypeIsString((ArrayType*) type))
562 return ".ascii";
563 else
564 return "<InvaliDataTypeForPrinting>";
565 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000566 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000567 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000568}
569
Vikram S. Adve21447222001-11-10 02:03:06 +0000570// Get the size of the constant for the given target.
571// If this is an unsized array, return 0.
572//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000573inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000574ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000575{
Chris Lattner122787b2002-06-05 18:08:26 +0000576 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000577 {
Chris Lattner122787b2002-06-05 18:08:26 +0000578 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000579 if (ArrayTypeIsString(aty))
580 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000581 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000582
583 return target.findOptimalStorageSize(CV->getType());
584}
585
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000586
587
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000588// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000589// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
590//
591inline unsigned int
592SizeToAlignment(unsigned int size, const TargetMachine& target)
593{
594 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
595 if (size > (unsigned) cacheLineSize / 2)
596 return cacheLineSize;
597 else
598 for (unsigned sz=1; /*no condition*/; sz *= 2)
599 if (sz >= size)
600 return sz;
601}
602
603// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000604//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000605inline unsigned int
606TypeToAlignment(const Type* type, const TargetMachine& target)
607{
Vikram S. Adve21447222001-11-10 02:03:06 +0000608 return SizeToAlignment(target.findOptimalStorageSize(type), target);
609}
610
611// Get the size of the constant and then use SizeToAlignment.
612// Handles strings as a special case;
613inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000614ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000615{
Chris Lattner122787b2002-06-05 18:08:26 +0000616 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000617 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
618 return SizeToAlignment(1 + CPA->getNumOperands(), target);
619
620 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000621}
622
623
Vikram S. Adve21447222001-11-10 02:03:06 +0000624// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000625void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000626SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000627{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000628 assert(CV->getType() != Type::VoidTy &&
629 CV->getType() != Type::TypeTy &&
630 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000631 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000632
Chris Lattnerf678dc62002-04-11 21:44:02 +0000633 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
634 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000635
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000636 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000637
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000638 if (CV->getType()->isPrimitiveType())
639 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000640 if (CV->getType()->isFloatingPoint()) {
641 // FP Constants are printed as integer constants to avoid losing
642 // precision...
643 double Val = cast<ConstantFP>(CV)->getValue();
644 if (CV->getType() == Type::FloatTy) {
645 float FVal = (float)Val;
646 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
647 toAsm << *(unsigned int*)ProxyPtr;
648 } else if (CV->getType() == Type::DoubleTy) {
649 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
650 toAsm << *(uint64_t*)ProxyPtr;
651 } else {
652 assert(0 && "Unknown floating point type!");
653 }
654
655 toAsm << "\t! " << CV->getType()->getDescription()
656 << " value: " << Val << "\n";
657 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000658 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000659 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000660 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000661 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
662 { // This is a constant address for a global variable or method.
663 // Use the name of the variable or method as the address value.
664 if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(CPR->getValue()))
665 toAsm << getID(GV);
666 else if (const Function* F = dyn_cast<Function>(CPR->getValue()))
667 toAsm << getID(F);
668 else
669 assert(0 && "Unexpected constant reference type");
670 }
Chris Lattner122787b2002-06-05 18:08:26 +0000671 else if (const ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000672 {
Chris Lattner697954c2002-01-20 22:54:45 +0000673 assert(CPP->isNullValue() &&
674 "Cannot yet print non-null pointer constants to assembly");
675 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000676 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000677 else
678 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000679 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000680 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000681}
682
Vikram S. Adve21447222001-11-10 02:03:06 +0000683// Print a constant value or values (it may be an aggregate).
684// Uses printSingleConstant() to print each individual value.
685void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000686SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000687{
Chris Lattner122787b2002-06-05 18:08:26 +0000688 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000689
690 if (CPA && isStringCompatible(CPA))
691 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000692 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000693 }
694 else if (CPA)
695 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000696 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000697 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000698 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000699 }
Chris Lattner122787b2002-06-05 18:08:26 +0000700 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000701 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000702 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000703 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000704 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000705 }
706 else
Chris Lattner122787b2002-06-05 18:08:26 +0000707 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000708}
709
710// Print a constant (which may be an aggregate) prefixed by all the
711// appropriate directives. Uses printConstantValueOnly() to print the
712// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000713void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000714SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000715{
716 if (valID.length() == 0)
717 valID = getID(CV);
718
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000719 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000720
721 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000722 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000723 if (CPA && isStringCompatible(CPA))
724 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000725 toAsm << valID << ":\n";
726 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000727 return;
728 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000729
Chris Lattner697954c2002-01-20 22:54:45 +0000730 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000731
732 unsigned int constSize = ConstantToSize(CV, Target);
733 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000734 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000735
Chris Lattner697954c2002-01-20 22:54:45 +0000736 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000737
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000738 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000739}
740
741
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000742void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000743 std::hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000744 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
745 if (!I->isExternal()) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000746 const std::hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000747 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000748 MC.insert(pool.begin(), pool.end());
749 }
750}
751
752void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000753{
Chris Lattner697954c2002-01-20 22:54:45 +0000754 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000755
756 if (GV->hasInitializer())
757 printConstant(GV->getInitializer(), getID(GV));
758 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000759 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
760 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000761 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000762 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000763 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000764 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000765 }
766}
767
768
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000769void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000770 // First, get the constants there were marked by the code generator for
771 // inclusion in the assembly code data area and fold them all into a
772 // single constant pool since there may be lots of duplicates. Also,
773 // lets force these constants into the slot table so that we can get
774 // unique names for unnamed constants also.
775 //
Chris Lattner697954c2002-01-20 22:54:45 +0000776 std::hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000777 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000778
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000779 // Now, emit the three data sections separately; the cost of I/O should
780 // make up for the cost of extra passes over the globals list!
Vikram S. Advefbd21612002-03-31 19:03:58 +0000781
782 // Section 1 : Read-only data section (implies initialized)
783 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000784 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
785 if (GI->hasInitializer() && GI->isConstant())
786 printGlobalVariable(GI);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000787
Chris Lattner697954c2002-01-20 22:54:45 +0000788 for (std::hash_set<const Constant*>::const_iterator
789 I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000790 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000791 printConstant(*I);
792
Vikram S. Advefbd21612002-03-31 19:03:58 +0000793 // Section 2 : Initialized read-write data section
794 enterSection(AsmPrinter::InitRWData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000795 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
796 if (GI->hasInitializer() && !GI->isConstant())
797 printGlobalVariable(GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000798
Vikram S. Advefbd21612002-03-31 19:03:58 +0000799 // Section 3 : Uninitialized read-write data section
800 enterSection(AsmPrinter::UninitRWData);
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000801 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
802 if (!GI->hasInitializer())
803 printGlobalVariable(GI);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000804
Chris Lattner697954c2002-01-20 22:54:45 +0000805 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000806}
807
Chris Lattnere88f78c2001-09-19 13:47:27 +0000808} // End anonymous namespace
809
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000810Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
811 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000812}