blob: 38518eef41c2c9ebd23df1ffa645047112387351 [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"
Misha Brukmanfce11432002-10-28 00:28:31 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000019#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000020#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000021#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000022#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000025
26namespace {
27
Vikram S. Adved198c472002-03-18 03:07:26 +000028class GlobalIdTable: public Annotation {
29 static AnnotationID AnnotId;
30 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000031
Chris Lattner09ff1122002-07-24 21:21:32 +000032 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000033 typedef ValIdMap::const_iterator ValIdMapConstIterator;
34 typedef ValIdMap:: iterator ValIdMapIterator;
35public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000036 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000037 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000038
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000039 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000040};
41
42AnnotationID GlobalIdTable::AnnotId =
43 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
44
45//===---------------------------------------------------------------------===//
46// Code Shared By the two printer passes, as a mixin
47//===---------------------------------------------------------------------===//
48
49class AsmPrinter {
50 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000051public:
Chris Lattner697954c2002-01-20 22:54:45 +000052 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000053 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000054
Chris Lattnere88f78c2001-09-19 13:47:27 +000055 enum Sections {
56 Unknown,
57 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000058 ReadOnlyData,
59 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000060 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000061 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000062
Chris Lattner59ba1092002-02-04 15:53:23 +000063 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000064 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
65
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000066 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000067 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000068 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000069 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000070 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000071 idTable = new GlobalIdTable(&M);
72 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000073 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000074 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000075 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000076 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000077 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000078 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000079 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000080 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000081 }
82 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000083 }
84
Chris Lattner8b1b4e22002-07-16 18:35:16 +000085 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000086 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000087 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
88 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000089 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000090
Chris Lattnere88f78c2001-09-19 13:47:27 +000091 // enterSection - Use this method to enter a different section of the output
92 // executable. This is used to only output neccesary section transitions.
93 //
94 void enterSection(enum Sections S) {
95 if (S == CurSection) return; // Only switch section if neccesary
96 CurSection = S;
97
Vikram S. Adve29ff8732001-11-08 05:12:37 +000098 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000099 switch (S)
100 {
101 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000102 case Text: toAsm << "\".text\""; break;
103 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
104 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Advefee76262002-10-13 00:32:18 +0000105 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000106 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000107 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000108 }
109
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000110 static string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000111 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000112
113 // Symbol names in Sparc assembly language have these rules:
114 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
115 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000116 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000117 if (isdigit(S[0]))
118 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000119
120 for (unsigned i = 0; i < S.size(); ++i)
121 {
122 char C = S[i];
123 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
124 Result += C;
125 else
126 {
127 Result += '_';
128 Result += char('0' + ((unsigned char)C >> 4));
129 Result += char('0' + (C & 0xF));
130 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000131 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000132 return Result;
133 }
134
Chris Lattnere88f78c2001-09-19 13:47:27 +0000135 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000136 // the name of the identifier if possible (qualified by the type), and
137 // use a numbered value based on prefix otherwise.
138 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 //
140 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000141 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000142
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000143 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000144
Vikram S. Adved198c472002-03-18 03:07:26 +0000145 // Qualify all internal names with a unique id.
146 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000147 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000148 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000149 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
150 if (I == idTable->valToIdMap.end())
151 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000152 else
153 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000154 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000155 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000156
157 // Replace or prefix problem characters in the name
158 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000159 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000160
161 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000163
Chris Lattnere88f78c2001-09-19 13:47:27 +0000164 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000165 string getID(const Function *F) {
166 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000167 }
168 string getID(const BasicBlock *BB) {
169 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
170 }
171 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000172 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000173 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000174 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000175 return getID(CV, "LLVMConst_", ".C_");
176 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000177 string getID(const GlobalValue *GV) {
178 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
179 return getID(V);
180 else if (const Function *F = dyn_cast<Function>(GV))
181 return getID(F);
182 assert(0 && "Unexpected type of GlobalValue!");
183 return "";
184 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000185
Vikram S. Adve537a8772002-09-05 18:28:10 +0000186 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
187 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000188 string ConstantExprToString(const ConstantExpr* CE,
189 const TargetMachine& target) {
190 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000191 switch(CE->getOpcode()) {
192 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000193 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000194 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000195 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000196 S += "(" + valToExprString(ptrVal, target) + ") + ("
197 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
198 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000199 break;
200 }
201
Vikram S. Adve537a8772002-09-05 18:28:10 +0000202 case Instruction::Cast:
203 // Support only non-converting casts for now, i.e., a no-op.
204 // This assertion is not a complete check.
205 assert(target.DataLayout.getTypeSize(CE->getType()) ==
206 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
207 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
208 break;
209
210 case Instruction::Add:
211 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
212 + valToExprString(CE->getOperand(1), target) + ")";
213 break;
214
Vikram S. Advee99941a2002-08-22 02:58:36 +0000215 default:
216 assert(0 && "Unsupported operator in ConstantExprToString()");
217 break;
218 }
219
220 return S;
221 }
222
223 // valToExprString - Helper function for ConstantExprToString().
224 // Appends result to argument string S.
225 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000226 string valToExprString(const Value* V, const TargetMachine& target) {
227 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000228 bool failed = false;
229 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
230
231 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000232 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000233 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
234 S += itostr(CI->getValue());
235 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
236 S += utostr(CI->getValue());
237 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
238 S += ftostr(CFP->getValue());
239 else if (isa<ConstantPointerNull>(CV))
240 S += "0";
241 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000242 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000243 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
244 S += ConstantExprToString(CE, target);
245 else
246 failed = true;
247
248 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
249 S += getID(GV);
250 }
251 else
252 failed = true;
253
254 if (failed) {
255 assert(0 && "Cannot convert value to string");
256 S += "<illegal-value>";
257 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000258 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000259 }
260
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000261};
262
263
264
265//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000266// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000267//===----------------------------------------------------------------------===//
268
Chris Lattnerf57b8452002-04-27 06:56:12 +0000269struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000271 : AsmPrinter(os, t) {}
272
Chris Lattner96c466b2002-04-29 14:57:45 +0000273 const char *getPassName() const {
274 return "Output Sparc Assembly for Functions";
275 }
276
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000277 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000278 startModule(M);
279 return false;
280 }
281
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000282 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000283 startFunction(F);
284 emitFunction(F);
285 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000286 return false;
287 }
288
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000289 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000290 endModule();
291 return false;
292 }
293
Chris Lattner97e52e42002-04-28 21:27:06 +0000294 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
295 AU.setPreservesAll();
296 }
297
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000298 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000299private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000300 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000301 void emitMachineInst(const MachineInstr *MI);
302
303 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
304 void printOneOperand(const MachineOperand &Op);
305
306 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
307 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000308
Chris Lattnere88f78c2001-09-19 13:47:27 +0000309 unsigned getOperandMask(unsigned Opcode) {
310 switch (Opcode) {
311 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000312 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000313 default: return 0; // By default, don't hack operands...
314 }
315 }
316};
317
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000319SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
320 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000321 switch (MI->getOpCode()) {
322 case JMPLCALL:
323 case JMPLRET: return (opNum == 0);
324 default: return false;
325 }
326}
327
328
329inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000330SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
331 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000332 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
333 return (opNum == 0);
334 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
335 return (opNum == 1);
336 else
337 return false;
338}
339
340
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000341#define PrintOp1PlusOp2(mop1, mop2) \
342 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000343 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000344 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345
346unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000347SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348 unsigned int opNum)
349{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000350 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000351
352 if (OpIsBranchTargetLabel(MI, opNum))
353 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000354 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000355 return 2;
356 }
357 else if (OpIsMemoryAddressBase(MI, opNum))
358 {
359 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000360 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000361 toAsm << "]";
362 return 2;
363 }
364 else
365 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000366 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000367 return 1;
368 }
369}
370
371
372void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000373SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000374{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000375 bool needBitsFlag = true;
376
377 if (mop.opHiBits32())
378 toAsm << "%lm(";
379 else if (mop.opLoBits32())
380 toAsm << "%lo(";
381 else if (mop.opHiBits64())
382 toAsm << "%hh(";
383 else if (mop.opLoBits64())
384 toAsm << "%hm(";
385 else
386 needBitsFlag = false;
387
Chris Lattner133f0792002-10-28 04:45:29 +0000388 switch (mop.getType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389 {
390 case MachineOperand::MO_VirtualRegister:
391 case MachineOperand::MO_CCRegister:
392 case MachineOperand::MO_MachineRegister:
393 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000394 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000395
Vikram S. Advefbd21612002-03-31 19:03:58 +0000396 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000397 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
398 toAsm << "<NULL VALUE>";
399 } else {
400 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
401 }
402 break;
403 }
404
405 case MachineOperand::MO_PCRelativeDisp:
406 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000407 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000408 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
409
410 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000411 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000412 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000413 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000414 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000416 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 toAsm << getID(CV);
418 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000419 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000420 break;
421 }
422
423 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000424 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000425 break;
426
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000427 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000428 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000429 break;
430
431 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000432 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000433 break;
434 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000435
436 if (needBitsFlag)
437 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000438}
439
440
441void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000442SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000443{
444 unsigned Opcode = MI->getOpCode();
445
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000446 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000447 return; // IGNORE PHI NODES
448
Chris Lattnerf44f9052002-10-29 17:35:41 +0000449 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000450
451 unsigned Mask = getOperandMask(Opcode);
452
453 bool NeedComma = false;
454 unsigned N = 1;
455 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
456 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
457 if (NeedComma) toAsm << ", "; // Handle comma outputing
458 NeedComma = true;
459 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000460 } else
461 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000462
463 toAsm << "\n";
464}
465
466void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000467SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000468{
469 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000470 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000471
472 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000473 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000474 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000475 emitMachineInst(*MII);
476 toAsm << "\n"; // Seperate BB's with newlines
477}
478
479void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000480SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000481{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000482 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000483 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000484 enterSection(AsmPrinter::Text);
485 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
486 //toAsm << "\t.type\t" << methName << ",#function\n";
487 toAsm << "\t.type\t" << methName << ", 2\n";
488 toAsm << methName << ":\n";
489
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000490 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000491 MachineFunction &MF = MachineFunction::get(&F);
492 for (MachineFunction::const_iterator I = MF.begin(), E = MF.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
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000506Pass *UltraSparc::getFunctionAsmPrinterPass(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:
Vikram S. Advefee76262002-10-13 00:32:18 +0000539 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000540
Vikram S. Advefee76262002-10-13 00:32:18 +0000541 void printGlobalVariable (const GlobalVariable *GV);
542 void PrintZeroBytesToPad (int numBytes);
543 void printSingleConstantValue (const Constant* CV);
544 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000545 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000546
Vikram S. Advefee76262002-10-13 00:32:18 +0000547 static void FoldConstants (const Module &M,
548 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000549};
550
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000551
552// Can we treat the specified array as a string? Only if it is an array of
553// ubytes or non-negative sbytes.
554//
Vikram S. Advefee76262002-10-13 00:32:18 +0000555static bool isStringCompatible(const ConstantArray *CVA) {
556 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000557 if (ETy == Type::UByteTy) return true;
558 if (ETy != Type::SByteTy) return false;
559
Vikram S. Advefee76262002-10-13 00:32:18 +0000560 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
561 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000562 return false;
563
564 return true;
565}
566
567// toOctal - Convert the low order bits of X into an octal letter
568static inline char toOctal(int X) {
569 return (X&7)+'0';
570}
571
572// getAsCString - Return the specified array as a C compatible string, only if
573// the predicate isStringCompatible is true.
574//
Vikram S. Advefee76262002-10-13 00:32:18 +0000575static string getAsCString(const ConstantArray *CVA) {
576 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000577
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000578 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000579 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000580 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000581 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000582 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000583 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
584 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000585
Vikram S. Adve242a8082002-05-19 15:25:51 +0000586 if (C == '"') {
587 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000588 } else if (C == '\\') {
589 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000590 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000591 Result += C;
592 } else {
593 switch(C) {
594 case '\a': Result += "\\a"; break;
595 case '\b': Result += "\\b"; break;
596 case '\f': Result += "\\f"; break;
597 case '\n': Result += "\\n"; break;
598 case '\r': Result += "\\r"; break;
599 case '\t': Result += "\\t"; break;
600 case '\v': Result += "\\v"; break;
601 default:
602 Result += '\\';
603 Result += toOctal(C >> 6);
604 Result += toOctal(C >> 3);
605 Result += toOctal(C >> 0);
606 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000607 }
608 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000609 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000610 Result += "\"";
611
612 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000613}
614
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000615inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000616ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000617{
618 return (arrayType->getElementType() == Type::UByteTy ||
619 arrayType->getElementType() == Type::SByteTy);
620}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000621
Vikram S. Advee99941a2002-08-22 02:58:36 +0000622
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000623inline const string
624TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000625{
626 switch(type->getPrimitiveID())
627 {
628 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
629 return ".byte";
630 case Type::UShortTyID: case Type::ShortTyID:
631 return ".half";
632 case Type::UIntTyID: case Type::IntTyID:
633 return ".word";
634 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
635 return ".xword";
636 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000637 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000638 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000639 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000640 case Type::ArrayTyID:
641 if (ArrayTypeIsString((ArrayType*) type))
642 return ".ascii";
643 else
644 return "<InvaliDataTypeForPrinting>";
645 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000646 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000647 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000648}
649
Vikram S. Advefee76262002-10-13 00:32:18 +0000650// Get the size of the type
651//
652inline unsigned int
653TypeToSize(const Type* type, const TargetMachine& target)
654{
655 return target.findOptimalStorageSize(type);
656}
657
Vikram S. Adve21447222001-11-10 02:03:06 +0000658// Get the size of the constant for the given target.
659// If this is an unsized array, return 0.
660//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000661inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000662ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000663{
Vikram S. Advefee76262002-10-13 00:32:18 +0000664 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000665 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000666 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000667 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000668 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000669 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000670
Vikram S. Advefee76262002-10-13 00:32:18 +0000671 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000672}
673
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000674// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000675// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
676//
677inline unsigned int
678SizeToAlignment(unsigned int size, const TargetMachine& target)
679{
680 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
681 if (size > (unsigned) cacheLineSize / 2)
682 return cacheLineSize;
683 else
684 for (unsigned sz=1; /*no condition*/; sz *= 2)
685 if (sz >= size)
686 return sz;
687}
688
689// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000690//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000691inline unsigned int
692TypeToAlignment(const Type* type, const TargetMachine& target)
693{
Vikram S. Advefee76262002-10-13 00:32:18 +0000694 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000695}
696
697// Get the size of the constant and then use SizeToAlignment.
698// Handles strings as a special case;
699inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000700ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000701{
Vikram S. Advefee76262002-10-13 00:32:18 +0000702 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
703 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
704 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000705
706 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707}
708
709
Vikram S. Adve21447222001-11-10 02:03:06 +0000710// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000711void
Vikram S. Advefee76262002-10-13 00:32:18 +0000712SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000713{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000714 assert(CV->getType() != Type::VoidTy &&
715 CV->getType() != Type::TypeTy &&
716 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000717 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000718
Chris Lattnerf678dc62002-04-11 21:44:02 +0000719 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
720 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000721
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000722 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000723
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000724 if (CV->getType()->isPrimitiveType())
725 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000726 if (CV->getType()->isFloatingPoint()) {
727 // FP Constants are printed as integer constants to avoid losing
728 // precision...
729 double Val = cast<ConstantFP>(CV)->getValue();
730 if (CV->getType() == Type::FloatTy) {
731 float FVal = (float)Val;
732 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
733 toAsm << *(unsigned int*)ProxyPtr;
734 } else if (CV->getType() == Type::DoubleTy) {
735 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
736 toAsm << *(uint64_t*)ProxyPtr;
737 } else {
738 assert(0 && "Unknown floating point type!");
739 }
740
741 toAsm << "\t! " << CV->getType()->getDescription()
742 << " value: " << Val << "\n";
743 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000744 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000745 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000746 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000747 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
748 { // This is a constant address for a global variable or method.
749 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000750 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000751 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000752 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000753 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000754 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000755 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000756 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
757 { // Constant expression built from operators, constants, and symbolic addrs
758 toAsm << ConstantExprToString(CE, Target) << "\n";
759 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000760 else
761 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000762 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000763 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000764}
765
Vikram S. Adve21447222001-11-10 02:03:06 +0000766void
Vikram S. Advefee76262002-10-13 00:32:18 +0000767SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000768{
Vikram S. Advefee76262002-10-13 00:32:18 +0000769 for ( ; numBytes >= 8; numBytes -= 8)
770 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
771
772 if (numBytes >= 4)
773 {
774 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
775 numBytes -= 4;
776 }
777
778 while (numBytes--)
779 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
780}
781
782// Print a constant value or values (it may be an aggregate).
783// Uses printSingleConstantValue() to print each individual value.
784void
785SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
786 int numPadBytes /* = 0*/)
787{
788 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
789
790 if (numPadBytes)
791 PrintZeroBytesToPad(numPadBytes);
792
793 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000794 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000795 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000796 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000797 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000798 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000799 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000800 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000801 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000802 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000803 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
804 { // Print the fields in successive locations. Pad to align if needed!
805 const StructLayout *cvsLayout =
806 Target.DataLayout.getStructLayout(CVS->getType());
807 const std::vector<Use>& constValues = CVS->getValues();
808 unsigned sizeSoFar = 0;
809 for (unsigned i=0, N = constValues.size(); i < N; i++)
810 {
811 const Constant* field = cast<Constant>(constValues[i].get());
812
813 // Check if padding is needed and insert one or more 0s.
814 unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
815 int padSize = ((i == N-1? cvsLayout->StructSize
816 : cvsLayout->MemberOffsets[i+1])
817 - cvsLayout->MemberOffsets[i]) - fieldSize;
818 sizeSoFar += (fieldSize + padSize);
819
820 // Now print the actual field value
821 printConstantValueOnly(field, padSize);
822 }
823 assert(sizeSoFar == cvsLayout->StructSize &&
824 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000825 }
826 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000827 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000828}
829
830// Print a constant (which may be an aggregate) prefixed by all the
831// appropriate directives. Uses printConstantValueOnly() to print the
832// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000833void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000834SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000835{
836 if (valID.length() == 0)
837 valID = getID(CV);
838
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000839 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000840
841 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000842 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
843 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000844 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000845 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000846 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000847 return;
848 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000849
Chris Lattner697954c2002-01-20 22:54:45 +0000850 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000851
852 unsigned int constSize = ConstantToSize(CV, Target);
853 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000854 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000855
Chris Lattner697954c2002-01-20 22:54:45 +0000856 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000857
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000858 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000859}
860
861
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000862void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000863 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000864 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
865 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000866 const hash_set<const Constant*> &pool =
Misha Brukmanfce11432002-10-28 00:28:31 +0000867 MachineFunction::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000868 MC.insert(pool.begin(), pool.end());
869 }
870}
871
872void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000873{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000874 if (GV->hasExternalLinkage())
875 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000876
Vikram S. Advefee76262002-10-13 00:32:18 +0000877 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000878 printConstant(GV->getInitializer(), getID(GV));
879 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000880 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
881 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000882 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000883 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000884 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000885 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000886 }
887}
888
889
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000890void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000891 // First, get the constants there were marked by the code generator for
892 // inclusion in the assembly code data area and fold them all into a
893 // single constant pool since there may be lots of duplicates. Also,
894 // lets force these constants into the slot table so that we can get
895 // unique names for unnamed constants also.
896 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000897 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000898 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000899
Chris Lattner637ed862002-08-07 21:39:48 +0000900 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000901 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000902 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000903 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000904 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000905
906 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000907 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
908 if (! GI->isExternal()) {
909 assert(GI->hasInitializer());
910 if (GI->isConstant())
911 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
912 else if (GI->getInitializer()->isNullValue())
913 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
914 else
915 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
916
917 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000918 }
Chris Lattner637ed862002-08-07 21:39:48 +0000919
Chris Lattner697954c2002-01-20 22:54:45 +0000920 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000921}
922
Chris Lattnere88f78c2001-09-19 13:47:27 +0000923} // End anonymous namespace
924
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000925Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000926 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000927}