blob: 7342baba973a47385b6e8000b3e24e371e502e3b [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,
Vikram S. Advefee76262002-10-13 00:32:18 +000063 ZeroInitRWData,
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. Advefee76262002-10-13 00:32:18 +0000108 case ZeroInitRWData: 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
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000511Pass *UltraSparc::getFunctionAsmPrinterPass(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:
Vikram S. Advefee76262002-10-13 00:32:18 +0000544 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000545
Vikram S. Advefee76262002-10-13 00:32:18 +0000546 void printGlobalVariable (const GlobalVariable *GV);
547 void PrintZeroBytesToPad (int numBytes);
548 void printSingleConstantValue (const Constant* CV);
549 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
550 void printConstant (const Constant* CV, std::string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000551
Vikram S. Advefee76262002-10-13 00:32:18 +0000552 static void FoldConstants (const Module &M,
553 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000554};
555
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000556
557// Can we treat the specified array as a string? Only if it is an array of
558// ubytes or non-negative sbytes.
559//
Vikram S. Advefee76262002-10-13 00:32:18 +0000560static bool isStringCompatible(const ConstantArray *CVA) {
561 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000562 if (ETy == Type::UByteTy) return true;
563 if (ETy != Type::SByteTy) return false;
564
Vikram S. Advefee76262002-10-13 00:32:18 +0000565 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
566 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000567 return false;
568
569 return true;
570}
571
572// toOctal - Convert the low order bits of X into an octal letter
573static inline char toOctal(int X) {
574 return (X&7)+'0';
575}
576
577// getAsCString - Return the specified array as a C compatible string, only if
578// the predicate isStringCompatible is true.
579//
Vikram S. Advefee76262002-10-13 00:32:18 +0000580static string getAsCString(const ConstantArray *CVA) {
581 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000582
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000583 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000584 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000585 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000586 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000587 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000588 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
589 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000590
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 if (C == '"') {
592 Result += "\\\"";
593 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000594 Result += C;
595 } else {
596 switch(C) {
597 case '\a': Result += "\\a"; break;
598 case '\b': Result += "\\b"; break;
599 case '\f': Result += "\\f"; break;
600 case '\n': Result += "\\n"; break;
601 case '\r': Result += "\\r"; break;
602 case '\t': Result += "\\t"; break;
603 case '\v': Result += "\\v"; break;
604 default:
605 Result += '\\';
606 Result += toOctal(C >> 6);
607 Result += toOctal(C >> 3);
608 Result += toOctal(C >> 0);
609 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000610 }
611 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000612 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000613 Result += "\"";
614
615 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000616}
617
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000618inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000619ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000620{
621 return (arrayType->getElementType() == Type::UByteTy ||
622 arrayType->getElementType() == Type::SByteTy);
623}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000624
Vikram S. Advee99941a2002-08-22 02:58:36 +0000625
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000626inline const string
627TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000628{
629 switch(type->getPrimitiveID())
630 {
631 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
632 return ".byte";
633 case Type::UShortTyID: case Type::ShortTyID:
634 return ".half";
635 case Type::UIntTyID: case Type::IntTyID:
636 return ".word";
637 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
638 return ".xword";
639 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000640 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000641 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000642 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000643 case Type::ArrayTyID:
644 if (ArrayTypeIsString((ArrayType*) type))
645 return ".ascii";
646 else
647 return "<InvaliDataTypeForPrinting>";
648 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000649 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000650 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000651}
652
Vikram S. Advefee76262002-10-13 00:32:18 +0000653// Get the size of the type
654//
655inline unsigned int
656TypeToSize(const Type* type, const TargetMachine& target)
657{
658 return target.findOptimalStorageSize(type);
659}
660
Vikram S. Adve21447222001-11-10 02:03:06 +0000661// Get the size of the constant for the given target.
662// If this is an unsized array, return 0.
663//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000664inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000665ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000666{
Vikram S. Advefee76262002-10-13 00:32:18 +0000667 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000668 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000669 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000670 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000671 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000672 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000673
Vikram S. Advefee76262002-10-13 00:32:18 +0000674 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000675}
676
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000677// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000678// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
679//
680inline unsigned int
681SizeToAlignment(unsigned int size, const TargetMachine& target)
682{
683 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
684 if (size > (unsigned) cacheLineSize / 2)
685 return cacheLineSize;
686 else
687 for (unsigned sz=1; /*no condition*/; sz *= 2)
688 if (sz >= size)
689 return sz;
690}
691
692// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000693//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000694inline unsigned int
695TypeToAlignment(const Type* type, const TargetMachine& target)
696{
Vikram S. Advefee76262002-10-13 00:32:18 +0000697 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000698}
699
700// Get the size of the constant and then use SizeToAlignment.
701// Handles strings as a special case;
702inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000703ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000704{
Vikram S. Advefee76262002-10-13 00:32:18 +0000705 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
706 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
707 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000708
709 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000710}
711
712
Vikram S. Adve21447222001-11-10 02:03:06 +0000713// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714void
Vikram S. Advefee76262002-10-13 00:32:18 +0000715SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000716{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000717 assert(CV->getType() != Type::VoidTy &&
718 CV->getType() != Type::TypeTy &&
719 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000720 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000721
Chris Lattnerf678dc62002-04-11 21:44:02 +0000722 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
723 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000724
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000725 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000726
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000727 if (CV->getType()->isPrimitiveType())
728 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000729 if (CV->getType()->isFloatingPoint()) {
730 // FP Constants are printed as integer constants to avoid losing
731 // precision...
732 double Val = cast<ConstantFP>(CV)->getValue();
733 if (CV->getType() == Type::FloatTy) {
734 float FVal = (float)Val;
735 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
736 toAsm << *(unsigned int*)ProxyPtr;
737 } else if (CV->getType() == Type::DoubleTy) {
738 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
739 toAsm << *(uint64_t*)ProxyPtr;
740 } else {
741 assert(0 && "Unknown floating point type!");
742 }
743
744 toAsm << "\t! " << CV->getType()->getDescription()
745 << " value: " << Val << "\n";
746 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000747 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000748 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000749 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000750 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
751 { // This is a constant address for a global variable or method.
752 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000753 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000754 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000755 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000756 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000757 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000758 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000759 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
760 { // Constant expression built from operators, constants, and symbolic addrs
761 toAsm << ConstantExprToString(CE, Target) << "\n";
762 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000763 else
764 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000765 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000766 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000767}
768
Vikram S. Adve21447222001-11-10 02:03:06 +0000769void
Vikram S. Advefee76262002-10-13 00:32:18 +0000770SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000771{
Vikram S. Advefee76262002-10-13 00:32:18 +0000772 for ( ; numBytes >= 8; numBytes -= 8)
773 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
774
775 if (numBytes >= 4)
776 {
777 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
778 numBytes -= 4;
779 }
780
781 while (numBytes--)
782 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
783}
784
785// Print a constant value or values (it may be an aggregate).
786// Uses printSingleConstantValue() to print each individual value.
787void
788SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
789 int numPadBytes /* = 0*/)
790{
791 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
792
793 if (numPadBytes)
794 PrintZeroBytesToPad(numPadBytes);
795
796 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000797 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000798 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000799 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000800 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000801 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000802 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000803 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000804 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000805 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000806 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
807 { // Print the fields in successive locations. Pad to align if needed!
808 const StructLayout *cvsLayout =
809 Target.DataLayout.getStructLayout(CVS->getType());
810 const std::vector<Use>& constValues = CVS->getValues();
811 unsigned sizeSoFar = 0;
812 for (unsigned i=0, N = constValues.size(); i < N; i++)
813 {
814 const Constant* field = cast<Constant>(constValues[i].get());
815
816 // Check if padding is needed and insert one or more 0s.
817 unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
818 int padSize = ((i == N-1? cvsLayout->StructSize
819 : cvsLayout->MemberOffsets[i+1])
820 - cvsLayout->MemberOffsets[i]) - fieldSize;
821 sizeSoFar += (fieldSize + padSize);
822
823 // Now print the actual field value
824 printConstantValueOnly(field, padSize);
825 }
826 assert(sizeSoFar == cvsLayout->StructSize &&
827 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000828 }
829 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000830 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000831}
832
833// Print a constant (which may be an aggregate) prefixed by all the
834// appropriate directives. Uses printConstantValueOnly() to print the
835// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000836void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000837SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000838{
839 if (valID.length() == 0)
840 valID = getID(CV);
841
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000842 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000843
844 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000845 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
846 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000847 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000848 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000849 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000850 return;
851 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000852
Chris Lattner697954c2002-01-20 22:54:45 +0000853 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000854
855 unsigned int constSize = ConstantToSize(CV, Target);
856 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000857 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000858
Chris Lattner697954c2002-01-20 22:54:45 +0000859 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000860
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000861 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000862}
863
864
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000865void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000866 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000867 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
868 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000869 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000870 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000871 MC.insert(pool.begin(), pool.end());
872 }
873}
874
875void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000876{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000877 if (GV->hasExternalLinkage())
878 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000879
Vikram S. Advefee76262002-10-13 00:32:18 +0000880 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000881 printConstant(GV->getInitializer(), getID(GV));
882 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000883 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
884 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000885 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000886 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000887 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000888 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000889 }
890}
891
892
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000893void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000894 // First, get the constants there were marked by the code generator for
895 // inclusion in the assembly code data area and fold them all into a
896 // single constant pool since there may be lots of duplicates. Also,
897 // lets force these constants into the slot table so that we can get
898 // unique names for unnamed constants also.
899 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000900 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000901 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000902
Chris Lattner637ed862002-08-07 21:39:48 +0000903 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000904 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000905 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000906 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000907 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000908
909 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000910 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
911 if (! GI->isExternal()) {
912 assert(GI->hasInitializer());
913 if (GI->isConstant())
914 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
915 else if (GI->getInitializer()->isNullValue())
916 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
917 else
918 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
919
920 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000921 }
Chris Lattner637ed862002-08-07 21:39:48 +0000922
Chris Lattner697954c2002-01-20 22:54:45 +0000923 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000924}
925
Chris Lattnere88f78c2001-09-19 13:47:27 +0000926} // End anonymous namespace
927
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000928Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000929 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000930}