blob: 9520d58b30101ab79c57efbb2dd8195b2c538e9e [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/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000022#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000023#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000024#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000026#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000027using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000028
29namespace {
30
Vikram S. Adved198c472002-03-18 03:07:26 +000031class GlobalIdTable: public Annotation {
32 static AnnotationID AnnotId;
33 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000034
Chris Lattner09ff1122002-07-24 21:21:32 +000035 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000036 typedef ValIdMap::const_iterator ValIdMapConstIterator;
37 typedef ValIdMap:: iterator ValIdMapIterator;
38public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000039 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000040 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000041
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000042 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000043};
44
45AnnotationID GlobalIdTable::AnnotId =
46 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
47
48//===---------------------------------------------------------------------===//
49// Code Shared By the two printer passes, as a mixin
50//===---------------------------------------------------------------------===//
51
52class AsmPrinter {
53 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000054public:
Chris Lattner697954c2002-01-20 22:54:45 +000055 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000056 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000057
Chris Lattnere88f78c2001-09-19 13:47:27 +000058 enum Sections {
59 Unknown,
60 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000061 ReadOnlyData,
62 InitRWData,
63 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000064 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000065
Chris Lattner59ba1092002-02-04 15:53:23 +000066 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000067 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
68
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000069 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000070 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000071 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000072 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000073 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000074 idTable = new GlobalIdTable(&M);
75 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000076 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000077 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000078 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000079 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000081 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000082 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000083 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000084 }
85 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000086 }
87
Chris Lattner8b1b4e22002-07-16 18:35:16 +000088 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000089 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000090 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
91 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000092 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000093
Chris Lattnere88f78c2001-09-19 13:47:27 +000094 // enterSection - Use this method to enter a different section of the output
95 // executable. This is used to only output neccesary section transitions.
96 //
97 void enterSection(enum Sections S) {
98 if (S == CurSection) return; // Only switch section if neccesary
99 CurSection = S;
100
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000101 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000102 switch (S)
103 {
104 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000105 case Text: toAsm << "\".text\""; break;
106 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
107 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Adve537a8772002-09-05 18:28:10 +0000108 case UninitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000109 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000110 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000111 }
112
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000113 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000114 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000115
116 // Symbol names in Sparc assembly language have these rules:
117 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
118 // (b) A name beginning in "." is treated as a local name.
119 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
120 //
121 if (S[0] == '_' || isdigit(S[0]))
122 Result += "ll";
123
124 for (unsigned i = 0; i < S.size(); ++i)
125 {
126 char C = S[i];
127 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
128 Result += C;
129 else
130 {
131 Result += '_';
132 Result += char('0' + ((unsigned char)C >> 4));
133 Result += char('0' + (C & 0xF));
134 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000135 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000136 return Result;
137 }
138
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000140 // the name of the identifier if possible (qualified by the type), and
141 // use a numbered value based on prefix otherwise.
142 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000143 //
144 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000145 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
146
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000147 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adved198c472002-03-18 03:07:26 +0000148
149 // Qualify all internal names with a unique id.
150 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000151 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000152 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000153 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
154 if (I == idTable->valToIdMap.end())
155 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000156 else
157 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000158 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000159 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000160 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000161
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000162 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000164
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000166 string getID(const Function *F) {
167 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168 }
169 string getID(const BasicBlock *BB) {
170 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
171 }
172 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000173 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000175 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 return getID(CV, "LLVMConst_", ".C_");
177 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000178 string getID(const GlobalValue *GV) {
179 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
180 return getID(V);
181 else if (const Function *F = dyn_cast<Function>(GV))
182 return getID(F);
183 assert(0 && "Unexpected type of GlobalValue!");
184 return "";
185 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000186
Vikram S. Adve537a8772002-09-05 18:28:10 +0000187 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
188 // and return this as a string.
Vikram S. Advee99941a2002-08-22 02:58:36 +0000189 std::string ConstantExprToString(const ConstantExpr* CE,
190 const TargetMachine& target) {
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000191 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000192
193 switch(CE->getOpcode()) {
194 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000195 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000196 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000197 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000198 S += "(" + valToExprString(ptrVal, target) + ") + ("
199 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
200 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000201 break;
202 }
203
Vikram S. Adve537a8772002-09-05 18:28:10 +0000204 case Instruction::Cast:
205 // Support only non-converting casts for now, i.e., a no-op.
206 // This assertion is not a complete check.
207 assert(target.DataLayout.getTypeSize(CE->getType()) ==
208 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
209 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
210 break;
211
212 case Instruction::Add:
213 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
214 + valToExprString(CE->getOperand(1), target) + ")";
215 break;
216
Vikram S. Advee99941a2002-08-22 02:58:36 +0000217 default:
218 assert(0 && "Unsupported operator in ConstantExprToString()");
219 break;
220 }
221
222 return S;
223 }
224
225 // valToExprString - Helper function for ConstantExprToString().
226 // Appends result to argument string S.
227 //
Vikram S. Adve537a8772002-09-05 18:28:10 +0000228 std::string valToExprString(const Value* V, const TargetMachine& target) {
229 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000230 bool failed = false;
231 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
232
233 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
234 S += std::string(CB == ConstantBool::True ? "1" : "0");
235 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
236 S += itostr(CI->getValue());
237 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
238 S += utostr(CI->getValue());
239 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
240 S += ftostr(CFP->getValue());
241 else if (isa<ConstantPointerNull>(CV))
242 S += "0";
243 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000244 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000245 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
246 S += ConstantExprToString(CE, target);
247 else
248 failed = true;
249
250 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
251 S += getID(GV);
252 }
253 else
254 failed = true;
255
256 if (failed) {
257 assert(0 && "Cannot convert value to string");
258 S += "<illegal-value>";
259 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000260 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000261 }
262
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000263};
264
265
266
267//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000268// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000269//===----------------------------------------------------------------------===//
270
Chris Lattnerf57b8452002-04-27 06:56:12 +0000271struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000272 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000273 : AsmPrinter(os, t) {}
274
Chris Lattner96c466b2002-04-29 14:57:45 +0000275 const char *getPassName() const {
276 return "Output Sparc Assembly for Functions";
277 }
278
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000279 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000280 startModule(M);
281 return false;
282 }
283
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000284 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000285 startFunction(F);
286 emitFunction(F);
287 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000288 return false;
289 }
290
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000291 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000292 endModule();
293 return false;
294 }
295
Chris Lattner97e52e42002-04-28 21:27:06 +0000296 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
297 AU.setPreservesAll();
298 }
299
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000300 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000301private :
302 void emitBasicBlock(const BasicBlock *BB);
303 void emitMachineInst(const MachineInstr *MI);
304
305 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
306 void printOneOperand(const MachineOperand &Op);
307
308 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
309 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000310
Chris Lattnere88f78c2001-09-19 13:47:27 +0000311 unsigned getOperandMask(unsigned Opcode) {
312 switch (Opcode) {
313 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000314 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000315 default: return 0; // By default, don't hack operands...
316 }
317 }
318};
319
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000320inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000321SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
322 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000323 switch (MI->getOpCode()) {
324 case JMPLCALL:
325 case JMPLRET: return (opNum == 0);
326 default: return false;
327 }
328}
329
330
331inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000332SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
333 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000334 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
335 return (opNum == 0);
336 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
337 return (opNum == 1);
338 else
339 return false;
340}
341
342
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000343#define PrintOp1PlusOp2(mop1, mop2) \
344 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000346 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347
348unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000349SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000350 unsigned int opNum)
351{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000352 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000353
354 if (OpIsBranchTargetLabel(MI, opNum))
355 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000356 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000357 return 2;
358 }
359 else if (OpIsMemoryAddressBase(MI, opNum))
360 {
361 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000362 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000363 toAsm << "]";
364 return 2;
365 }
366 else
367 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000368 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000369 return 1;
370 }
371}
372
373
374void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000375SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000376{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000377 bool needBitsFlag = true;
378
379 if (mop.opHiBits32())
380 toAsm << "%lm(";
381 else if (mop.opLoBits32())
382 toAsm << "%lo(";
383 else if (mop.opHiBits64())
384 toAsm << "%hh(";
385 else if (mop.opLoBits64())
386 toAsm << "%hm(";
387 else
388 needBitsFlag = false;
389
390 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000391 {
392 case MachineOperand::MO_VirtualRegister:
393 case MachineOperand::MO_CCRegister:
394 case MachineOperand::MO_MachineRegister:
395 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000396 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000397
Vikram S. Advefbd21612002-03-31 19:03:58 +0000398 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
400 toAsm << "<NULL VALUE>";
401 } else {
402 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
403 }
404 break;
405 }
406
407 case MachineOperand::MO_PCRelativeDisp:
408 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000409 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000410 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
411
412 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000413 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000414 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000416 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000418 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000419 toAsm << getID(CV);
420 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000421 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000422 break;
423 }
424
425 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000426 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000427 break;
428
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000429 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000430 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431 break;
432
433 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000434 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000435 break;
436 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000437
438 if (needBitsFlag)
439 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000440}
441
442
443void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000444SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000445{
446 unsigned Opcode = MI->getOpCode();
447
448 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
449 return; // IGNORE PHI NODES
450
451 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
452
453 unsigned Mask = getOperandMask(Opcode);
454
455 bool NeedComma = false;
456 unsigned N = 1;
457 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
458 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
459 if (NeedComma) toAsm << ", "; // Handle comma outputing
460 NeedComma = true;
461 N = printOperands(MI, OpNum);
462 }
463 else
464 N = 1;
465
466 toAsm << "\n";
467}
468
469void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000470SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000471{
472 // Emit a label for the basic block
473 toAsm << getID(BB) << ":\n";
474
475 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000476 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000477 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
478
479 // Loop over all of the instructions in the basic block...
480 for (; MII != MIE; ++MII)
481 emitMachineInst(*MII);
482 toAsm << "\n"; // Seperate BB's with newlines
483}
484
485void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000486SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000487{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000488 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000489 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000490 enterSection(AsmPrinter::Text);
491 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
492 //toAsm << "\t.type\t" << methName << ",#function\n";
493 toAsm << "\t.type\t" << methName << ", 2\n";
494 toAsm << methName << ":\n";
495
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000496 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000497 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
498 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000499
500 // Output a .size directive so the debugger knows the extents of the function
501 toAsm << ".EndOf_" << methName << ":\n\t.size "
502 << methName << ", .EndOf_"
503 << methName << "-" << methName << "\n";
504
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000505 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000506 toAsm << "\n\n";
507}
508
509} // End anonymous namespace
510
Chris Lattnerf57b8452002-04-27 06:56:12 +0000511Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000512 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000513}
514
515
516
517
518
519//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000520// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000521//===----------------------------------------------------------------------===//
522
523namespace {
524
525class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
526public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000527 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
528 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000529
Chris Lattner96c466b2002-04-29 14:57:45 +0000530 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
531
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000532 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000533 startModule(M);
534 emitGlobalsAndConstants(M);
535 endModule();
536 return false;
537 }
538
Chris Lattner97e52e42002-04-28 21:27:06 +0000539 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
540 AU.setPreservesAll();
541 }
542
543private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000544 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000545
546 void printGlobalVariable(const GlobalVariable *GV);
547 void printSingleConstant( const Constant* CV);
548 void printConstantValueOnly(const Constant* CV);
549 void printConstant( const Constant* CV, std::string valID = "");
550
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000551 static void FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000552 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000553};
554
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000555
556// Can we treat the specified array as a string? Only if it is an array of
557// ubytes or non-negative sbytes.
558//
Chris Lattner122787b2002-06-05 18:08:26 +0000559static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000560 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
561 if (ETy == Type::UByteTy) return true;
562 if (ETy != Type::SByteTy) return false;
563
564 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000565 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000566 return false;
567
568 return true;
569}
570
571// toOctal - Convert the low order bits of X into an octal letter
572static inline char toOctal(int X) {
573 return (X&7)+'0';
574}
575
576// getAsCString - Return the specified array as a C compatible string, only if
577// the predicate isStringCompatible is true.
578//
Chris Lattner122787b2002-06-05 18:08:26 +0000579static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000580 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000581
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000582 string Result;
583 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
584 Result = "\"";
585 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
586 unsigned char C = (ETy == Type::SByteTy) ?
587 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
588 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
589
Vikram S. Adve242a8082002-05-19 15:25:51 +0000590 if (C == '"') {
591 Result += "\\\"";
592 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000593 Result += C;
594 } else {
595 switch(C) {
596 case '\a': Result += "\\a"; break;
597 case '\b': Result += "\\b"; break;
598 case '\f': Result += "\\f"; break;
599 case '\n': Result += "\\n"; break;
600 case '\r': Result += "\\r"; break;
601 case '\t': Result += "\\t"; break;
602 case '\v': Result += "\\v"; break;
603 default:
604 Result += '\\';
605 Result += toOctal(C >> 6);
606 Result += toOctal(C >> 3);
607 Result += toOctal(C >> 0);
608 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000609 }
610 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000611 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000612 Result += "\"";
613
614 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000615}
616
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000617inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000618ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000619{
620 return (arrayType->getElementType() == Type::UByteTy ||
621 arrayType->getElementType() == Type::SByteTy);
622}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000623
Vikram S. Advee99941a2002-08-22 02:58:36 +0000624
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000625inline const string
626TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000627{
628 switch(type->getPrimitiveID())
629 {
630 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
631 return ".byte";
632 case Type::UShortTyID: case Type::ShortTyID:
633 return ".half";
634 case Type::UIntTyID: case Type::IntTyID:
635 return ".word";
636 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
637 return ".xword";
638 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000639 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000640 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000641 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000642 case Type::ArrayTyID:
643 if (ArrayTypeIsString((ArrayType*) type))
644 return ".ascii";
645 else
646 return "<InvaliDataTypeForPrinting>";
647 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000648 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000649 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000650}
651
Vikram S. Adve21447222001-11-10 02:03:06 +0000652// Get the size of the constant for the given target.
653// If this is an unsized array, return 0.
654//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000655inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000656ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000657{
Chris Lattner122787b2002-06-05 18:08:26 +0000658 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000659 {
Chris Lattner122787b2002-06-05 18:08:26 +0000660 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000661 if (ArrayTypeIsString(aty))
662 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000663 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000664
665 return target.findOptimalStorageSize(CV->getType());
666}
667
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000668
669
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000670// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000671// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
672//
673inline unsigned int
674SizeToAlignment(unsigned int size, const TargetMachine& target)
675{
676 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
677 if (size > (unsigned) cacheLineSize / 2)
678 return cacheLineSize;
679 else
680 for (unsigned sz=1; /*no condition*/; sz *= 2)
681 if (sz >= size)
682 return sz;
683}
684
685// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000686//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000687inline unsigned int
688TypeToAlignment(const Type* type, const TargetMachine& target)
689{
Vikram S. Adve21447222001-11-10 02:03:06 +0000690 return SizeToAlignment(target.findOptimalStorageSize(type), target);
691}
692
693// Get the size of the constant and then use SizeToAlignment.
694// Handles strings as a special case;
695inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000696ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000697{
Chris Lattner122787b2002-06-05 18:08:26 +0000698 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000699 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
700 return SizeToAlignment(1 + CPA->getNumOperands(), target);
701
702 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000703}
704
705
Vikram S. Adve21447222001-11-10 02:03:06 +0000706// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000708SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000710 assert(CV->getType() != Type::VoidTy &&
711 CV->getType() != Type::TypeTy &&
712 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000713 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714
Chris Lattnerf678dc62002-04-11 21:44:02 +0000715 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
716 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000717
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000718 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000719
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000720 if (CV->getType()->isPrimitiveType())
721 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000722 if (CV->getType()->isFloatingPoint()) {
723 // FP Constants are printed as integer constants to avoid losing
724 // precision...
725 double Val = cast<ConstantFP>(CV)->getValue();
726 if (CV->getType() == Type::FloatTy) {
727 float FVal = (float)Val;
728 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
729 toAsm << *(unsigned int*)ProxyPtr;
730 } else if (CV->getType() == Type::DoubleTy) {
731 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
732 toAsm << *(uint64_t*)ProxyPtr;
733 } else {
734 assert(0 && "Unknown floating point type!");
735 }
736
737 toAsm << "\t! " << CV->getType()->getDescription()
738 << " value: " << Val << "\n";
739 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000740 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000741 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000742 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000743 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
744 { // This is a constant address for a global variable or method.
745 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000746 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000747 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000748 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000749 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000750 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000751 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000752 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
753 { // Constant expression built from operators, constants, and symbolic addrs
754 toAsm << ConstantExprToString(CE, Target) << "\n";
755 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000756 else
757 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000758 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000759 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000760}
761
Vikram S. Adve21447222001-11-10 02:03:06 +0000762// Print a constant value or values (it may be an aggregate).
763// Uses printSingleConstant() to print each individual value.
764void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000765SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000766{
Chris Lattner122787b2002-06-05 18:08:26 +0000767 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000768
769 if (CPA && isStringCompatible(CPA))
770 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000771 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000772 }
773 else if (CPA)
774 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000775 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000776 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000777 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000778 }
Chris Lattner122787b2002-06-05 18:08:26 +0000779 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000780 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000781 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000782 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000783 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000784 }
785 else
Chris Lattner122787b2002-06-05 18:08:26 +0000786 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000787}
788
789// Print a constant (which may be an aggregate) prefixed by all the
790// appropriate directives. Uses printConstantValueOnly() to print the
791// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000792void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000793SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000794{
795 if (valID.length() == 0)
796 valID = getID(CV);
797
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000798 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000799
800 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000801 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000802 if (CPA && isStringCompatible(CPA))
803 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000804 toAsm << valID << ":\n";
805 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000806 return;
807 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000808
Chris Lattner697954c2002-01-20 22:54:45 +0000809 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000810
811 unsigned int constSize = ConstantToSize(CV, Target);
812 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000813 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000814
Chris Lattner697954c2002-01-20 22:54:45 +0000815 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000816
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000817 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000818}
819
820
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000821void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000822 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000823 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
824 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000825 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000826 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000827 MC.insert(pool.begin(), pool.end());
828 }
829}
830
831void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000832{
Chris Lattner697954c2002-01-20 22:54:45 +0000833 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000834
835 if (GV->hasInitializer())
836 printConstant(GV->getInitializer(), getID(GV));
837 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000838 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
839 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000840 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000841 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000842 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000843 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000844 }
845}
846
847
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000848void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000849 // First, get the constants there were marked by the code generator for
850 // inclusion in the assembly code data area and fold them all into a
851 // single constant pool since there may be lots of duplicates. Also,
852 // lets force these constants into the slot table so that we can get
853 // unique names for unnamed constants also.
854 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000855 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000856 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000857
Chris Lattner637ed862002-08-07 21:39:48 +0000858 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000859 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000860 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000861 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000862 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000863
864 // Output global variables...
865 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) {
866 if (GI->hasInitializer() && GI->isConstant()) {
867 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
868 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
Chris Lattner637ed862002-08-07 21:39:48 +0000869 enterSection(AsmPrinter::InitRWData);
870 } else {
871 assert (!GI->hasInitializer() && "Unexpected global variable type found");
872 enterSection(AsmPrinter::UninitRWData); // Uninitialized data
873 }
874 printGlobalVariable(GI);
875 }
876
Chris Lattner697954c2002-01-20 22:54:45 +0000877 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000878}
879
Chris Lattnere88f78c2001-09-19 13:47:27 +0000880} // End anonymous namespace
881
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000882Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
883 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000884}