blob: f3de79d10164ad3fe13e2e95ba1ae1da97e42c37 [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 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000188
189 // ConstantExprToString() - Convert a ConstantExpr to a C expression, and
190 // return this as a string.
191 //
192 std::string ConstantExprToString(const ConstantExpr* CE,
193 const TargetMachine& target) {
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000194 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000195
196 switch(CE->getOpcode()) {
197 case Instruction::GetElementPtr:
198 {
199 const Value* ptrVal = CE->getOperand(0);
200 valToExprString(ptrVal, target, S);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000201 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
202 uint64_t byteOffset =
203 target.DataLayout.getIndexedOffset(ptrVal->getType(), idxVec);
204
205 const Type *PtrElTy =
206 cast<PointerType>(ptrVal->getType())->getElementType();
207 uint64_t eltSize = target.DataLayout.getTypeSize(PtrElTy);
208
Vikram S. Advee99941a2002-08-22 02:58:36 +0000209 S += " + " + utostr(byteOffset / eltSize);
210 break;
211 }
212
213 default:
214 assert(0 && "Unsupported operator in ConstantExprToString()");
215 break;
216 }
217
218 return S;
219 }
220
221 // valToExprString - Helper function for ConstantExprToString().
222 // Appends result to argument string S.
223 //
224 void valToExprString(const Value* V, const TargetMachine& target,
225 std::string& S) {
226 bool failed = false;
227 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
228
229 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
230 S += std::string(CB == ConstantBool::True ? "1" : "0");
231 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
232 S += itostr(CI->getValue());
233 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
234 S += utostr(CI->getValue());
235 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
236 S += ftostr(CFP->getValue());
237 else if (isa<ConstantPointerNull>(CV))
238 S += "0";
239 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
240 valToExprString(CPR->getValue(), target, S);
241 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
242 S += ConstantExprToString(CE, target);
243 else
244 failed = true;
245
246 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
247 S += getID(GV);
248 }
249 else
250 failed = true;
251
252 if (failed) {
253 assert(0 && "Cannot convert value to string");
254 S += "<illegal-value>";
255 }
256 }
257
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000258};
259
260
261
262//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000263// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000264//===----------------------------------------------------------------------===//
265
Chris Lattnerf57b8452002-04-27 06:56:12 +0000266struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000268 : AsmPrinter(os, t) {}
269
Chris Lattner96c466b2002-04-29 14:57:45 +0000270 const char *getPassName() const {
271 return "Output Sparc Assembly for Functions";
272 }
273
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000274 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000275 startModule(M);
276 return false;
277 }
278
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000279 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000280 startFunction(F);
281 emitFunction(F);
282 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000283 return false;
284 }
285
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000286 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000287 endModule();
288 return false;
289 }
290
Chris Lattner97e52e42002-04-28 21:27:06 +0000291 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
292 AU.setPreservesAll();
293 }
294
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000295 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000296private :
297 void emitBasicBlock(const BasicBlock *BB);
298 void emitMachineInst(const MachineInstr *MI);
299
300 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
301 void printOneOperand(const MachineOperand &Op);
302
303 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
304 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000305
Chris Lattnere88f78c2001-09-19 13:47:27 +0000306 unsigned getOperandMask(unsigned Opcode) {
307 switch (Opcode) {
308 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000309 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000310 default: return 0; // By default, don't hack operands...
311 }
312 }
313};
314
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000315inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000316SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
317 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318 switch (MI->getOpCode()) {
319 case JMPLCALL:
320 case JMPLRET: return (opNum == 0);
321 default: return false;
322 }
323}
324
325
326inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000327SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
328 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000329 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
330 return (opNum == 0);
331 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
332 return (opNum == 1);
333 else
334 return false;
335}
336
337
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000338#define PrintOp1PlusOp2(mop1, mop2) \
339 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000340 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000341 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000342
343unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000344SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345 unsigned int opNum)
346{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000347 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348
349 if (OpIsBranchTargetLabel(MI, opNum))
350 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000351 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000352 return 2;
353 }
354 else if (OpIsMemoryAddressBase(MI, opNum))
355 {
356 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000357 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000358 toAsm << "]";
359 return 2;
360 }
361 else
362 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000363 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000364 return 1;
365 }
366}
367
368
369void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000370SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000371{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000372 bool needBitsFlag = true;
373
374 if (mop.opHiBits32())
375 toAsm << "%lm(";
376 else if (mop.opLoBits32())
377 toAsm << "%lo(";
378 else if (mop.opHiBits64())
379 toAsm << "%hh(";
380 else if (mop.opLoBits64())
381 toAsm << "%hm(";
382 else
383 needBitsFlag = false;
384
385 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000386 {
387 case MachineOperand::MO_VirtualRegister:
388 case MachineOperand::MO_CCRegister:
389 case MachineOperand::MO_MachineRegister:
390 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000391 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392
Vikram S. Advefbd21612002-03-31 19:03:58 +0000393 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000394 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
395 toAsm << "<NULL VALUE>";
396 } else {
397 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
398 }
399 break;
400 }
401
402 case MachineOperand::MO_PCRelativeDisp:
403 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000404 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000405 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
406
407 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000408 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000409 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000410 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000411 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000412 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000413 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000414 toAsm << getID(CV);
415 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000416 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 break;
418 }
419
420 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000421 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000422 break;
423
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000424 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000425 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000426 break;
427
428 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000429 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000430 break;
431 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000432
433 if (needBitsFlag)
434 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000435}
436
437
438void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000439SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000440{
441 unsigned Opcode = MI->getOpCode();
442
443 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
444 return; // IGNORE PHI NODES
445
446 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
447
448 unsigned Mask = getOperandMask(Opcode);
449
450 bool NeedComma = false;
451 unsigned N = 1;
452 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
453 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
454 if (NeedComma) toAsm << ", "; // Handle comma outputing
455 NeedComma = true;
456 N = printOperands(MI, OpNum);
457 }
458 else
459 N = 1;
460
461 toAsm << "\n";
462}
463
464void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000465SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000466{
467 // Emit a label for the basic block
468 toAsm << getID(BB) << ":\n";
469
470 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000471 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000472 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
473
474 // Loop over all of the instructions in the basic block...
475 for (; MII != MIE; ++MII)
476 emitMachineInst(*MII);
477 toAsm << "\n"; // Seperate BB's with newlines
478}
479
480void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000481SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000482{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000483 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000484 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000485 enterSection(AsmPrinter::Text);
486 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
487 //toAsm << "\t.type\t" << methName << ",#function\n";
488 toAsm << "\t.type\t" << methName << ", 2\n";
489 toAsm << methName << ":\n";
490
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000491 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000492 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
493 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000494
495 // Output a .size directive so the debugger knows the extents of the function
496 toAsm << ".EndOf_" << methName << ":\n\t.size "
497 << methName << ", .EndOf_"
498 << methName << "-" << methName << "\n";
499
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000500 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000501 toAsm << "\n\n";
502}
503
504} // End anonymous namespace
505
Chris Lattnerf57b8452002-04-27 06:56:12 +0000506Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000507 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000508}
509
510
511
512
513
514//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000515// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000516//===----------------------------------------------------------------------===//
517
518namespace {
519
520class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
521public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000522 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
523 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000524
Chris Lattner96c466b2002-04-29 14:57:45 +0000525 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
526
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000527 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000528 startModule(M);
529 emitGlobalsAndConstants(M);
530 endModule();
531 return false;
532 }
533
Chris Lattner97e52e42002-04-28 21:27:06 +0000534 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
535 AU.setPreservesAll();
536 }
537
538private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000539 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000540
541 void printGlobalVariable(const GlobalVariable *GV);
542 void printSingleConstant( const Constant* CV);
543 void printConstantValueOnly(const Constant* CV);
544 void printConstant( const Constant* CV, std::string valID = "");
545
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000546 static void FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000547 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000548};
549
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000550
551// Can we treat the specified array as a string? Only if it is an array of
552// ubytes or non-negative sbytes.
553//
Chris Lattner122787b2002-06-05 18:08:26 +0000554static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000555 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
556 if (ETy == Type::UByteTy) return true;
557 if (ETy != Type::SByteTy) return false;
558
559 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000560 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000561 return false;
562
563 return true;
564}
565
566// toOctal - Convert the low order bits of X into an octal letter
567static inline char toOctal(int X) {
568 return (X&7)+'0';
569}
570
571// getAsCString - Return the specified array as a C compatible string, only if
572// the predicate isStringCompatible is true.
573//
Chris Lattner122787b2002-06-05 18:08:26 +0000574static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000575 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000576
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000577 string Result;
578 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
579 Result = "\"";
580 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
581 unsigned char C = (ETy == Type::SByteTy) ?
582 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
583 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
584
Vikram S. Adve242a8082002-05-19 15:25:51 +0000585 if (C == '"') {
586 Result += "\\\"";
587 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000588 Result += C;
589 } else {
590 switch(C) {
591 case '\a': Result += "\\a"; break;
592 case '\b': Result += "\\b"; break;
593 case '\f': Result += "\\f"; break;
594 case '\n': Result += "\\n"; break;
595 case '\r': Result += "\\r"; break;
596 case '\t': Result += "\\t"; break;
597 case '\v': Result += "\\v"; break;
598 default:
599 Result += '\\';
600 Result += toOctal(C >> 6);
601 Result += toOctal(C >> 3);
602 Result += toOctal(C >> 0);
603 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000604 }
605 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000606 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000607 Result += "\"";
608
609 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000610}
611
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000612inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000613ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000614{
615 return (arrayType->getElementType() == Type::UByteTy ||
616 arrayType->getElementType() == Type::SByteTy);
617}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000618
Vikram S. Advee99941a2002-08-22 02:58:36 +0000619
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000620inline const string
621TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000622{
623 switch(type->getPrimitiveID())
624 {
625 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
626 return ".byte";
627 case Type::UShortTyID: case Type::ShortTyID:
628 return ".half";
629 case Type::UIntTyID: case Type::IntTyID:
630 return ".word";
631 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
632 return ".xword";
633 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000634 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000635 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000636 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000637 case Type::ArrayTyID:
638 if (ArrayTypeIsString((ArrayType*) type))
639 return ".ascii";
640 else
641 return "<InvaliDataTypeForPrinting>";
642 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000643 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000645}
646
Vikram S. Adve21447222001-11-10 02:03:06 +0000647// Get the size of the constant for the given target.
648// If this is an unsized array, return 0.
649//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000650inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000651ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000652{
Chris Lattner122787b2002-06-05 18:08:26 +0000653 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000654 {
Chris Lattner122787b2002-06-05 18:08:26 +0000655 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000656 if (ArrayTypeIsString(aty))
657 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000658 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000659
660 return target.findOptimalStorageSize(CV->getType());
661}
662
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000663
664
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000665// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000666// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
667//
668inline unsigned int
669SizeToAlignment(unsigned int size, const TargetMachine& target)
670{
671 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
672 if (size > (unsigned) cacheLineSize / 2)
673 return cacheLineSize;
674 else
675 for (unsigned sz=1; /*no condition*/; sz *= 2)
676 if (sz >= size)
677 return sz;
678}
679
680// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000681//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000682inline unsigned int
683TypeToAlignment(const Type* type, const TargetMachine& target)
684{
Vikram S. Adve21447222001-11-10 02:03:06 +0000685 return SizeToAlignment(target.findOptimalStorageSize(type), target);
686}
687
688// Get the size of the constant and then use SizeToAlignment.
689// Handles strings as a special case;
690inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000691ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000692{
Chris Lattner122787b2002-06-05 18:08:26 +0000693 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000694 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
695 return SizeToAlignment(1 + CPA->getNumOperands(), target);
696
697 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000698}
699
700
Vikram S. Adve21447222001-11-10 02:03:06 +0000701// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000702void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000703SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000704{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000705 assert(CV->getType() != Type::VoidTy &&
706 CV->getType() != Type::TypeTy &&
707 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000708 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709
Chris Lattnerf678dc62002-04-11 21:44:02 +0000710 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
711 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000712
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000713 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000714
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000715 if (CV->getType()->isPrimitiveType())
716 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000717 if (CV->getType()->isFloatingPoint()) {
718 // FP Constants are printed as integer constants to avoid losing
719 // precision...
720 double Val = cast<ConstantFP>(CV)->getValue();
721 if (CV->getType() == Type::FloatTy) {
722 float FVal = (float)Val;
723 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
724 toAsm << *(unsigned int*)ProxyPtr;
725 } else if (CV->getType() == Type::DoubleTy) {
726 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
727 toAsm << *(uint64_t*)ProxyPtr;
728 } else {
729 assert(0 && "Unknown floating point type!");
730 }
731
732 toAsm << "\t! " << CV->getType()->getDescription()
733 << " value: " << Val << "\n";
734 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000735 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000736 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000737 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000738 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
739 { // This is a constant address for a global variable or method.
740 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000741 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000742 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000743 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000744 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000745 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000746 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000747 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
748 { // Constant expression built from operators, constants, and symbolic addrs
749 toAsm << ConstantExprToString(CE, Target) << "\n";
750 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000751 else
752 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000753 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000754 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000755}
756
Vikram S. Adve21447222001-11-10 02:03:06 +0000757// Print a constant value or values (it may be an aggregate).
758// Uses printSingleConstant() to print each individual value.
759void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000760SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000761{
Chris Lattner122787b2002-06-05 18:08:26 +0000762 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000763
764 if (CPA && isStringCompatible(CPA))
765 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000766 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000767 }
768 else if (CPA)
769 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000770 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000771 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000772 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000773 }
Chris Lattner122787b2002-06-05 18:08:26 +0000774 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000775 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000776 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000777 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000778 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000779 }
780 else
Chris Lattner122787b2002-06-05 18:08:26 +0000781 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000782}
783
784// Print a constant (which may be an aggregate) prefixed by all the
785// appropriate directives. Uses printConstantValueOnly() to print the
786// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000787void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000788SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000789{
790 if (valID.length() == 0)
791 valID = getID(CV);
792
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000793 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000794
795 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000796 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000797 if (CPA && isStringCompatible(CPA))
798 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000799 toAsm << valID << ":\n";
800 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000801 return;
802 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000803
Chris Lattner697954c2002-01-20 22:54:45 +0000804 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000805
806 unsigned int constSize = ConstantToSize(CV, Target);
807 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000808 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000809
Chris Lattner697954c2002-01-20 22:54:45 +0000810 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000811
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000812 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000813}
814
815
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000816void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000817 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000818 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
819 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000820 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000821 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000822 MC.insert(pool.begin(), pool.end());
823 }
824}
825
826void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000827{
Chris Lattner697954c2002-01-20 22:54:45 +0000828 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000829
830 if (GV->hasInitializer())
831 printConstant(GV->getInitializer(), getID(GV));
832 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000833 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
834 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000835 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000836 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000837 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000838 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000839 }
840}
841
842
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000843void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000844 // First, get the constants there were marked by the code generator for
845 // inclusion in the assembly code data area and fold them all into a
846 // single constant pool since there may be lots of duplicates. Also,
847 // lets force these constants into the slot table so that we can get
848 // unique names for unnamed constants also.
849 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000850 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000851 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000852
Chris Lattner637ed862002-08-07 21:39:48 +0000853 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000854 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000855 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000856 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000857 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000858
859 // Output global variables...
860 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) {
861 if (GI->hasInitializer() && GI->isConstant()) {
862 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
863 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
864 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
865 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
866 enterSection(AsmPrinter::InitRWData);
867 } else {
868 assert (!GI->hasInitializer() && "Unexpected global variable type found");
869 enterSection(AsmPrinter::UninitRWData); // Uninitialized data
870 }
871 printGlobalVariable(GI);
872 }
873
Chris Lattner697954c2002-01-20 22:54:45 +0000874 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000875}
876
Chris Lattnere88f78c2001-09-19 13:47:27 +0000877} // End anonymous namespace
878
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000879Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
880 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000881}