blob: 58a60b0977517b3b755cf9ece2adf4874e8ef77a [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"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000019#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000020#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000021#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000022#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000023#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000026
27namespace {
28
Vikram S. Adved198c472002-03-18 03:07:26 +000029class GlobalIdTable: public Annotation {
30 static AnnotationID AnnotId;
31 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000032
Chris Lattner09ff1122002-07-24 21:21:32 +000033 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000034 typedef ValIdMap::const_iterator ValIdMapConstIterator;
35 typedef ValIdMap:: iterator ValIdMapIterator;
36public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000037 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000038 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000039
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000040 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000041};
42
43AnnotationID GlobalIdTable::AnnotId =
44 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
45
46//===---------------------------------------------------------------------===//
47// Code Shared By the two printer passes, as a mixin
48//===---------------------------------------------------------------------===//
49
50class AsmPrinter {
51 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000052public:
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000054 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000055
Chris Lattnere88f78c2001-09-19 13:47:27 +000056 enum Sections {
57 Unknown,
58 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000059 ReadOnlyData,
60 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000061 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000062 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000063
Chris Lattner59ba1092002-02-04 15:53:23 +000064 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000065 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
66
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000067 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000068 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000069 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000070 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000071 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000072 idTable = new GlobalIdTable(&M);
73 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000074 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000075 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000076 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000077 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000078 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000081 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000082 }
83 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000084 }
85
Chris Lattner8b1b4e22002-07-16 18:35:16 +000086 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000087 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000088 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
89 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000090 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000091
Chris Lattnere88f78c2001-09-19 13:47:27 +000092 // enterSection - Use this method to enter a different section of the output
93 // executable. This is used to only output neccesary section transitions.
94 //
95 void enterSection(enum Sections S) {
96 if (S == CurSection) return; // Only switch section if neccesary
97 CurSection = S;
98
Vikram S. Adve29ff8732001-11-08 05:12:37 +000099 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000100 switch (S)
101 {
102 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000103 case Text: toAsm << "\".text\""; break;
104 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
105 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Advefee76262002-10-13 00:32:18 +0000106 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000107 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000108 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000109 }
110
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000111 static string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000112 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000113
114 // Symbol names in Sparc assembly language have these rules:
115 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
116 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000117 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000118 if (isdigit(S[0]))
119 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120
121 for (unsigned i = 0; i < S.size(); ++i)
122 {
123 char C = S[i];
124 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
125 Result += C;
126 else
127 {
128 Result += '_';
129 Result += char('0' + ((unsigned char)C >> 4));
130 Result += char('0' + (C & 0xF));
131 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000132 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000133 return Result;
134 }
135
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000137 // the name of the identifier if possible (qualified by the type), and
138 // use a numbered value based on prefix otherwise.
139 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000140 //
141 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000142 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
143
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000144 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adved198c472002-03-18 03:07:26 +0000145
146 // Qualify all internal names with a unique id.
147 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000148 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000149 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000150 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
151 if (I == idTable->valToIdMap.end())
152 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000153 else
154 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000155 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000157 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000158
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000159 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000160 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000161
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000163 string getID(const Function *F) {
164 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000165 }
166 string getID(const BasicBlock *BB) {
167 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
168 }
169 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000170 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000171 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000172 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000173 return getID(CV, "LLVMConst_", ".C_");
174 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000175 string getID(const GlobalValue *GV) {
176 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
177 return getID(V);
178 else if (const Function *F = dyn_cast<Function>(GV))
179 return getID(F);
180 assert(0 && "Unexpected type of GlobalValue!");
181 return "";
182 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000183
Vikram S. Adve537a8772002-09-05 18:28:10 +0000184 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
185 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000186 string ConstantExprToString(const ConstantExpr* CE,
187 const TargetMachine& target) {
188 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000189 switch(CE->getOpcode()) {
190 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000191 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000192 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000193 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000194 S += "(" + valToExprString(ptrVal, target) + ") + ("
195 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
196 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000197 break;
198 }
199
Vikram S. Adve537a8772002-09-05 18:28:10 +0000200 case Instruction::Cast:
201 // Support only non-converting casts for now, i.e., a no-op.
202 // This assertion is not a complete check.
203 assert(target.DataLayout.getTypeSize(CE->getType()) ==
204 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
205 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
206 break;
207
208 case Instruction::Add:
209 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
210 + valToExprString(CE->getOperand(1), target) + ")";
211 break;
212
Vikram S. Advee99941a2002-08-22 02:58:36 +0000213 default:
214 assert(0 && "Unsupported operator in ConstantExprToString()");
215 break;
216 }
217
218 return S;
219 }
220
221 // valToExprString - Helper function for ConstantExprToString().
222 // Appends result to argument string S.
223 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000224 string valToExprString(const Value* V, const TargetMachine& target) {
225 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000226 bool failed = false;
227 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
228
229 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000230 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000231 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
232 S += itostr(CI->getValue());
233 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
234 S += utostr(CI->getValue());
235 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
236 S += ftostr(CFP->getValue());
237 else if (isa<ConstantPointerNull>(CV))
238 S += "0";
239 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000240 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000241 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
242 S += ConstantExprToString(CE, target);
243 else
244 failed = true;
245
246 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
247 S += getID(GV);
248 }
249 else
250 failed = true;
251
252 if (failed) {
253 assert(0 && "Cannot convert value to string");
254 S += "<illegal-value>";
255 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000256 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000257 }
258
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000259};
260
261
262
263//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000264// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000265//===----------------------------------------------------------------------===//
266
Chris Lattnerf57b8452002-04-27 06:56:12 +0000267struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000268 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000269 : AsmPrinter(os, t) {}
270
Chris Lattner96c466b2002-04-29 14:57:45 +0000271 const char *getPassName() const {
272 return "Output Sparc Assembly for Functions";
273 }
274
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000275 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000276 startModule(M);
277 return false;
278 }
279
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000280 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000281 startFunction(F);
282 emitFunction(F);
283 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000284 return false;
285 }
286
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000287 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000288 endModule();
289 return false;
290 }
291
Chris Lattner97e52e42002-04-28 21:27:06 +0000292 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
293 AU.setPreservesAll();
294 }
295
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000296 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000297private :
298 void emitBasicBlock(const BasicBlock *BB);
299 void emitMachineInst(const MachineInstr *MI);
300
301 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
302 void printOneOperand(const MachineOperand &Op);
303
304 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
305 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000306
Chris Lattnere88f78c2001-09-19 13:47:27 +0000307 unsigned getOperandMask(unsigned Opcode) {
308 switch (Opcode) {
309 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000310 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000311 default: return 0; // By default, don't hack operands...
312 }
313 }
314};
315
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000316inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000317SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
318 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000319 switch (MI->getOpCode()) {
320 case JMPLCALL:
321 case JMPLRET: return (opNum == 0);
322 default: return false;
323 }
324}
325
326
327inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000328SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
329 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000330 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
331 return (opNum == 0);
332 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
333 return (opNum == 1);
334 else
335 return false;
336}
337
338
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000339#define PrintOp1PlusOp2(mop1, mop2) \
340 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000341 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000342 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000343
344unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000345SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000346 unsigned int opNum)
347{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000348 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000349
350 if (OpIsBranchTargetLabel(MI, opNum))
351 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000352 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000353 return 2;
354 }
355 else if (OpIsMemoryAddressBase(MI, opNum))
356 {
357 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000358 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000359 toAsm << "]";
360 return 2;
361 }
362 else
363 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000364 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000365 return 1;
366 }
367}
368
369
370void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000371SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000372{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000373 bool needBitsFlag = true;
374
375 if (mop.opHiBits32())
376 toAsm << "%lm(";
377 else if (mop.opLoBits32())
378 toAsm << "%lo(";
379 else if (mop.opHiBits64())
380 toAsm << "%hh(";
381 else if (mop.opLoBits64())
382 toAsm << "%hm(";
383 else
384 needBitsFlag = false;
385
386 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000387 {
388 case MachineOperand::MO_VirtualRegister:
389 case MachineOperand::MO_CCRegister:
390 case MachineOperand::MO_MachineRegister:
391 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000392 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000393
Vikram S. Advefbd21612002-03-31 19:03:58 +0000394 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000395 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
396 toAsm << "<NULL VALUE>";
397 } else {
398 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
399 }
400 break;
401 }
402
403 case MachineOperand::MO_PCRelativeDisp:
404 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000405 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000406 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
407
408 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000409 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000410 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000411 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000412 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000413 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000414 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 toAsm << getID(CV);
416 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000417 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000418 break;
419 }
420
421 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000422 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000423 break;
424
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000425 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000426 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000427 break;
428
429 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000430 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431 break;
432 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000433
434 if (needBitsFlag)
435 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000436}
437
438
439void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000440SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000441{
442 unsigned Opcode = MI->getOpCode();
443
444 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
445 return; // IGNORE PHI NODES
446
447 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
448
449 unsigned Mask = getOperandMask(Opcode);
450
451 bool NeedComma = false;
452 unsigned N = 1;
453 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
454 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
455 if (NeedComma) toAsm << ", "; // Handle comma outputing
456 NeedComma = true;
457 N = printOperands(MI, OpNum);
458 }
459 else
460 N = 1;
461
462 toAsm << "\n";
463}
464
465void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000466SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000467{
468 // Emit a label for the basic block
469 toAsm << getID(BB) << ":\n";
470
471 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000472 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000473 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
474
475 // Loop over all of the instructions in the basic block...
476 for (; MII != MIE; ++MII)
477 emitMachineInst(*MII);
478 toAsm << "\n"; // Seperate BB's with newlines
479}
480
481void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000482SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000483{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000484 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000485 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000486 enterSection(AsmPrinter::Text);
487 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
488 //toAsm << "\t.type\t" << methName << ",#function\n";
489 toAsm << "\t.type\t" << methName << ", 2\n";
490 toAsm << methName << ":\n";
491
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000492 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000493 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
494 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000495
496 // Output a .size directive so the debugger knows the extents of the function
497 toAsm << ".EndOf_" << methName << ":\n\t.size "
498 << methName << ", .EndOf_"
499 << methName << "-" << methName << "\n";
500
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000501 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000502 toAsm << "\n\n";
503}
504
505} // End anonymous namespace
506
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000507Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000508 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000509}
510
511
512
513
514
515//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000516// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000517//===----------------------------------------------------------------------===//
518
519namespace {
520
521class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
522public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000523 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
524 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000525
Chris Lattner96c466b2002-04-29 14:57:45 +0000526 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
527
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000528 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000529 startModule(M);
530 emitGlobalsAndConstants(M);
531 endModule();
532 return false;
533 }
534
Chris Lattner97e52e42002-04-28 21:27:06 +0000535 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
536 AU.setPreservesAll();
537 }
538
539private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000540 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000541
Vikram S. Advefee76262002-10-13 00:32:18 +0000542 void printGlobalVariable (const GlobalVariable *GV);
543 void PrintZeroBytesToPad (int numBytes);
544 void printSingleConstantValue (const Constant* CV);
545 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000546 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000547
Vikram S. Advefee76262002-10-13 00:32:18 +0000548 static void FoldConstants (const Module &M,
549 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000550};
551
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000552
553// Can we treat the specified array as a string? Only if it is an array of
554// ubytes or non-negative sbytes.
555//
Vikram S. Advefee76262002-10-13 00:32:18 +0000556static bool isStringCompatible(const ConstantArray *CVA) {
557 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000558 if (ETy == Type::UByteTy) return true;
559 if (ETy != Type::SByteTy) return false;
560
Vikram S. Advefee76262002-10-13 00:32:18 +0000561 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
562 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000563 return false;
564
565 return true;
566}
567
568// toOctal - Convert the low order bits of X into an octal letter
569static inline char toOctal(int X) {
570 return (X&7)+'0';
571}
572
573// getAsCString - Return the specified array as a C compatible string, only if
574// the predicate isStringCompatible is true.
575//
Vikram S. Advefee76262002-10-13 00:32:18 +0000576static string getAsCString(const ConstantArray *CVA) {
577 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000578
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000579 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000580 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000581 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000582 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000583 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000584 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
585 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000586
Vikram S. Adve242a8082002-05-19 15:25:51 +0000587 if (C == '"') {
588 Result += "\\\"";
589 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000590 Result += C;
591 } else {
592 switch(C) {
593 case '\a': Result += "\\a"; break;
594 case '\b': Result += "\\b"; break;
595 case '\f': Result += "\\f"; break;
596 case '\n': Result += "\\n"; break;
597 case '\r': Result += "\\r"; break;
598 case '\t': Result += "\\t"; break;
599 case '\v': Result += "\\v"; break;
600 default:
601 Result += '\\';
602 Result += toOctal(C >> 6);
603 Result += toOctal(C >> 3);
604 Result += toOctal(C >> 0);
605 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000606 }
607 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000608 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000609 Result += "\"";
610
611 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000612}
613
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000614inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000615ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000616{
617 return (arrayType->getElementType() == Type::UByteTy ||
618 arrayType->getElementType() == Type::SByteTy);
619}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000620
Vikram S. Advee99941a2002-08-22 02:58:36 +0000621
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000622inline const string
623TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000624{
625 switch(type->getPrimitiveID())
626 {
627 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
628 return ".byte";
629 case Type::UShortTyID: case Type::ShortTyID:
630 return ".half";
631 case Type::UIntTyID: case Type::IntTyID:
632 return ".word";
633 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
634 return ".xword";
635 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000636 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000637 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000638 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000639 case Type::ArrayTyID:
640 if (ArrayTypeIsString((ArrayType*) type))
641 return ".ascii";
642 else
643 return "<InvaliDataTypeForPrinting>";
644 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000645 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000646 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000647}
648
Vikram S. Advefee76262002-10-13 00:32:18 +0000649// Get the size of the type
650//
651inline unsigned int
652TypeToSize(const Type* type, const TargetMachine& target)
653{
654 return target.findOptimalStorageSize(type);
655}
656
Vikram S. Adve21447222001-11-10 02:03:06 +0000657// Get the size of the constant for the given target.
658// If this is an unsized array, return 0.
659//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000660inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000661ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000662{
Vikram S. Advefee76262002-10-13 00:32:18 +0000663 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000664 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000665 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000666 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000667 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000668 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000669
Vikram S. Advefee76262002-10-13 00:32:18 +0000670 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000671}
672
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000673// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000674// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
675//
676inline unsigned int
677SizeToAlignment(unsigned int size, const TargetMachine& target)
678{
679 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
680 if (size > (unsigned) cacheLineSize / 2)
681 return cacheLineSize;
682 else
683 for (unsigned sz=1; /*no condition*/; sz *= 2)
684 if (sz >= size)
685 return sz;
686}
687
688// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000689//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000690inline unsigned int
691TypeToAlignment(const Type* type, const TargetMachine& target)
692{
Vikram S. Advefee76262002-10-13 00:32:18 +0000693 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000694}
695
696// Get the size of the constant and then use SizeToAlignment.
697// Handles strings as a special case;
698inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000699ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000700{
Vikram S. Advefee76262002-10-13 00:32:18 +0000701 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
702 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
703 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000704
705 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000706}
707
708
Vikram S. Adve21447222001-11-10 02:03:06 +0000709// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000710void
Vikram S. Advefee76262002-10-13 00:32:18 +0000711SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000712{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000713 assert(CV->getType() != Type::VoidTy &&
714 CV->getType() != Type::TypeTy &&
715 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000716 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000717
Chris Lattnerf678dc62002-04-11 21:44:02 +0000718 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
719 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000720
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000721 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000722
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000723 if (CV->getType()->isPrimitiveType())
724 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000725 if (CV->getType()->isFloatingPoint()) {
726 // FP Constants are printed as integer constants to avoid losing
727 // precision...
728 double Val = cast<ConstantFP>(CV)->getValue();
729 if (CV->getType() == Type::FloatTy) {
730 float FVal = (float)Val;
731 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
732 toAsm << *(unsigned int*)ProxyPtr;
733 } else if (CV->getType() == Type::DoubleTy) {
734 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
735 toAsm << *(uint64_t*)ProxyPtr;
736 } else {
737 assert(0 && "Unknown floating point type!");
738 }
739
740 toAsm << "\t! " << CV->getType()->getDescription()
741 << " value: " << Val << "\n";
742 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000743 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000744 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000745 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000746 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
747 { // This is a constant address for a global variable or method.
748 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000749 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000750 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000751 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000752 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000753 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000754 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000755 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
756 { // Constant expression built from operators, constants, and symbolic addrs
757 toAsm << ConstantExprToString(CE, Target) << "\n";
758 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000759 else
760 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000761 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000762 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000763}
764
Vikram S. Adve21447222001-11-10 02:03:06 +0000765void
Vikram S. Advefee76262002-10-13 00:32:18 +0000766SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000767{
Vikram S. Advefee76262002-10-13 00:32:18 +0000768 for ( ; numBytes >= 8; numBytes -= 8)
769 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
770
771 if (numBytes >= 4)
772 {
773 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
774 numBytes -= 4;
775 }
776
777 while (numBytes--)
778 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
779}
780
781// Print a constant value or values (it may be an aggregate).
782// Uses printSingleConstantValue() to print each individual value.
783void
784SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
785 int numPadBytes /* = 0*/)
786{
787 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
788
789 if (numPadBytes)
790 PrintZeroBytesToPad(numPadBytes);
791
792 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000793 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000794 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000795 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000796 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000797 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000798 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000799 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000800 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000801 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000802 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
803 { // Print the fields in successive locations. Pad to align if needed!
804 const StructLayout *cvsLayout =
805 Target.DataLayout.getStructLayout(CVS->getType());
806 const std::vector<Use>& constValues = CVS->getValues();
807 unsigned sizeSoFar = 0;
808 for (unsigned i=0, N = constValues.size(); i < N; i++)
809 {
810 const Constant* field = cast<Constant>(constValues[i].get());
811
812 // Check if padding is needed and insert one or more 0s.
813 unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
814 int padSize = ((i == N-1? cvsLayout->StructSize
815 : cvsLayout->MemberOffsets[i+1])
816 - cvsLayout->MemberOffsets[i]) - fieldSize;
817 sizeSoFar += (fieldSize + padSize);
818
819 // Now print the actual field value
820 printConstantValueOnly(field, padSize);
821 }
822 assert(sizeSoFar == cvsLayout->StructSize &&
823 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000824 }
825 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000826 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000827}
828
829// Print a constant (which may be an aggregate) prefixed by all the
830// appropriate directives. Uses printConstantValueOnly() to print the
831// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000832void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000833SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000834{
835 if (valID.length() == 0)
836 valID = getID(CV);
837
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000838 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000839
840 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000841 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
842 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000843 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000844 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000845 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000846 return;
847 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000848
Chris Lattner697954c2002-01-20 22:54:45 +0000849 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000850
851 unsigned int constSize = ConstantToSize(CV, Target);
852 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000853 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000854
Chris Lattner697954c2002-01-20 22:54:45 +0000855 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000856
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000857 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000858}
859
860
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000861void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000862 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000863 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
864 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000865 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000866 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000867 MC.insert(pool.begin(), pool.end());
868 }
869}
870
871void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000872{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000873 if (GV->hasExternalLinkage())
874 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000875
Vikram S. Advefee76262002-10-13 00:32:18 +0000876 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000877 printConstant(GV->getInitializer(), getID(GV));
878 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000879 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
880 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000881 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000882 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000883 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000884 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000885 }
886}
887
888
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000889void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000890 // First, get the constants there were marked by the code generator for
891 // inclusion in the assembly code data area and fold them all into a
892 // single constant pool since there may be lots of duplicates. Also,
893 // lets force these constants into the slot table so that we can get
894 // unique names for unnamed constants also.
895 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000896 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000897 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000898
Chris Lattner637ed862002-08-07 21:39:48 +0000899 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000900 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000901 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000902 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000903 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000904
905 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000906 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
907 if (! GI->isExternal()) {
908 assert(GI->hasInitializer());
909 if (GI->isConstant())
910 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
911 else if (GI->getInitializer()->isNullValue())
912 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
913 else
914 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
915
916 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000917 }
Chris Lattner637ed862002-08-07 21:39:48 +0000918
Chris Lattner697954c2002-01-20 22:54:45 +0000919 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000920}
921
Chris Lattnere88f78c2001-09-19 13:47:27 +0000922} // End anonymous namespace
923
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000924Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000925 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000926}