blob: 5f73a8d5fc6d33560039d76a49720cb943294e68 [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"
142
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000143 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adved198c472002-03-18 03:07:26 +0000144
145 // 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);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000156 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000157
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000158 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000159 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000160
Chris Lattnere88f78c2001-09-19 13:47:27 +0000161 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000162 string getID(const Function *F) {
163 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000164 }
165 string getID(const BasicBlock *BB) {
166 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
167 }
168 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000169 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000170 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000171 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000172 return getID(CV, "LLVMConst_", ".C_");
173 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000174 string getID(const GlobalValue *GV) {
175 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
176 return getID(V);
177 else if (const Function *F = dyn_cast<Function>(GV))
178 return getID(F);
179 assert(0 && "Unexpected type of GlobalValue!");
180 return "";
181 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000182
Vikram S. Adve537a8772002-09-05 18:28:10 +0000183 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
184 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000185 string ConstantExprToString(const ConstantExpr* CE,
186 const TargetMachine& target) {
187 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000188 switch(CE->getOpcode()) {
189 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000190 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000191 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000192 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000193 S += "(" + valToExprString(ptrVal, target) + ") + ("
194 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
195 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000196 break;
197 }
198
Vikram S. Adve537a8772002-09-05 18:28:10 +0000199 case Instruction::Cast:
200 // Support only non-converting casts for now, i.e., a no-op.
201 // This assertion is not a complete check.
202 assert(target.DataLayout.getTypeSize(CE->getType()) ==
203 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
204 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
205 break;
206
207 case Instruction::Add:
208 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
209 + valToExprString(CE->getOperand(1), target) + ")";
210 break;
211
Vikram S. Advee99941a2002-08-22 02:58:36 +0000212 default:
213 assert(0 && "Unsupported operator in ConstantExprToString()");
214 break;
215 }
216
217 return S;
218 }
219
220 // valToExprString - Helper function for ConstantExprToString().
221 // Appends result to argument string S.
222 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000223 string valToExprString(const Value* V, const TargetMachine& target) {
224 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000225 bool failed = false;
226 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
227
228 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000229 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000230 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
231 S += itostr(CI->getValue());
232 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
233 S += utostr(CI->getValue());
234 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
235 S += ftostr(CFP->getValue());
236 else if (isa<ConstantPointerNull>(CV))
237 S += "0";
238 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000239 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000240 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
241 S += ConstantExprToString(CE, target);
242 else
243 failed = true;
244
245 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
246 S += getID(GV);
247 }
248 else
249 failed = true;
250
251 if (failed) {
252 assert(0 && "Cannot convert value to string");
253 S += "<illegal-value>";
254 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000255 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000256 }
257
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000258};
259
260
261
262//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000263// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000264//===----------------------------------------------------------------------===//
265
Chris Lattnerf57b8452002-04-27 06:56:12 +0000266struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000268 : AsmPrinter(os, t) {}
269
Chris Lattner96c466b2002-04-29 14:57:45 +0000270 const char *getPassName() const {
271 return "Output Sparc Assembly for Functions";
272 }
273
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000274 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000275 startModule(M);
276 return false;
277 }
278
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000279 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000280 startFunction(F);
281 emitFunction(F);
282 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000283 return false;
284 }
285
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000286 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000287 endModule();
288 return false;
289 }
290
Chris Lattner97e52e42002-04-28 21:27:06 +0000291 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
292 AU.setPreservesAll();
293 }
294
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000295 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000296private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000297 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000298 void emitMachineInst(const MachineInstr *MI);
299
300 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
301 void printOneOperand(const MachineOperand &Op);
302
303 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
304 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000305
Chris Lattnere88f78c2001-09-19 13:47:27 +0000306 unsigned getOperandMask(unsigned Opcode) {
307 switch (Opcode) {
308 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000309 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000310 default: return 0; // By default, don't hack operands...
311 }
312 }
313};
314
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000315inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000316SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
317 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318 switch (MI->getOpCode()) {
319 case JMPLCALL:
320 case JMPLRET: return (opNum == 0);
321 default: return false;
322 }
323}
324
325
326inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000327SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
328 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000329 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
330 return (opNum == 0);
331 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
332 return (opNum == 1);
333 else
334 return false;
335}
336
337
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000338#define PrintOp1PlusOp2(mop1, mop2) \
339 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000340 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000341 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000342
343unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000344SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345 unsigned int opNum)
346{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000347 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348
349 if (OpIsBranchTargetLabel(MI, opNum))
350 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000351 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000352 return 2;
353 }
354 else if (OpIsMemoryAddressBase(MI, opNum))
355 {
356 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000357 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000358 toAsm << "]";
359 return 2;
360 }
361 else
362 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000363 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000364 return 1;
365 }
366}
367
368
369void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000370SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000371{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000372 bool needBitsFlag = true;
373
374 if (mop.opHiBits32())
375 toAsm << "%lm(";
376 else if (mop.opLoBits32())
377 toAsm << "%lo(";
378 else if (mop.opHiBits64())
379 toAsm << "%hh(";
380 else if (mop.opLoBits64())
381 toAsm << "%hm(";
382 else
383 needBitsFlag = false;
384
Chris Lattner133f0792002-10-28 04:45:29 +0000385 switch (mop.getType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000386 {
387 case MachineOperand::MO_VirtualRegister:
388 case MachineOperand::MO_CCRegister:
389 case MachineOperand::MO_MachineRegister:
390 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000391 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392
Vikram S. Advefbd21612002-03-31 19:03:58 +0000393 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000394 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
395 toAsm << "<NULL VALUE>";
396 } else {
397 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
398 }
399 break;
400 }
401
402 case MachineOperand::MO_PCRelativeDisp:
403 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000404 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000405 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
406
407 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000408 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000409 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000410 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000411 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000412 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000413 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000414 toAsm << getID(CV);
415 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000416 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 break;
418 }
419
420 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000421 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000422 break;
423
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000424 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000425 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000426 break;
427
428 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000429 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000430 break;
431 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000432
433 if (needBitsFlag)
434 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000435}
436
437
438void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000439SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000440{
441 unsigned Opcode = MI->getOpCode();
442
443 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
444 return; // IGNORE PHI NODES
445
446 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
447
448 unsigned Mask = getOperandMask(Opcode);
449
450 bool NeedComma = false;
451 unsigned N = 1;
452 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
453 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
454 if (NeedComma) toAsm << ", "; // Handle comma outputing
455 NeedComma = true;
456 N = printOperands(MI, OpNum);
457 }
458 else
459 N = 1;
460
461 toAsm << "\n";
462}
463
464void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000465SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000466{
467 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000468 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000469
470 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000471 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000472 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000473 emitMachineInst(*MII);
474 toAsm << "\n"; // Seperate BB's with newlines
475}
476
477void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000478SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000479{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000480 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000481 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000482 enterSection(AsmPrinter::Text);
483 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
484 //toAsm << "\t.type\t" << methName << ",#function\n";
485 toAsm << "\t.type\t" << methName << ", 2\n";
486 toAsm << methName << ":\n";
487
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000488 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000489 MachineFunction &MF = MachineFunction::get(&F);
490 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E; ++I)
491 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000492
493 // Output a .size directive so the debugger knows the extents of the function
494 toAsm << ".EndOf_" << methName << ":\n\t.size "
495 << methName << ", .EndOf_"
496 << methName << "-" << methName << "\n";
497
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000498 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000499 toAsm << "\n\n";
500}
501
502} // End anonymous namespace
503
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000504Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000505 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000506}
507
508
509
510
511
512//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000513// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000514//===----------------------------------------------------------------------===//
515
516namespace {
517
518class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
519public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000520 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
521 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000522
Chris Lattner96c466b2002-04-29 14:57:45 +0000523 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
524
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000525 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000526 startModule(M);
527 emitGlobalsAndConstants(M);
528 endModule();
529 return false;
530 }
531
Chris Lattner97e52e42002-04-28 21:27:06 +0000532 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
533 AU.setPreservesAll();
534 }
535
536private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000537 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000538
Vikram S. Advefee76262002-10-13 00:32:18 +0000539 void printGlobalVariable (const GlobalVariable *GV);
540 void PrintZeroBytesToPad (int numBytes);
541 void printSingleConstantValue (const Constant* CV);
542 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000543 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000544
Vikram S. Advefee76262002-10-13 00:32:18 +0000545 static void FoldConstants (const Module &M,
546 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000547};
548
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000549
550// Can we treat the specified array as a string? Only if it is an array of
551// ubytes or non-negative sbytes.
552//
Vikram S. Advefee76262002-10-13 00:32:18 +0000553static bool isStringCompatible(const ConstantArray *CVA) {
554 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000555 if (ETy == Type::UByteTy) return true;
556 if (ETy != Type::SByteTy) return false;
557
Vikram S. Advefee76262002-10-13 00:32:18 +0000558 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
559 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000560 return false;
561
562 return true;
563}
564
565// toOctal - Convert the low order bits of X into an octal letter
566static inline char toOctal(int X) {
567 return (X&7)+'0';
568}
569
570// getAsCString - Return the specified array as a C compatible string, only if
571// the predicate isStringCompatible is true.
572//
Vikram S. Advefee76262002-10-13 00:32:18 +0000573static string getAsCString(const ConstantArray *CVA) {
574 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000575
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000576 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000577 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000578 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000579 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000580 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000581 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
582 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000583
Vikram S. Adve242a8082002-05-19 15:25:51 +0000584 if (C == '"') {
585 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000586 } else if (C == '\\') {
587 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000588 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000589 Result += C;
590 } else {
591 switch(C) {
592 case '\a': Result += "\\a"; break;
593 case '\b': Result += "\\b"; break;
594 case '\f': Result += "\\f"; break;
595 case '\n': Result += "\\n"; break;
596 case '\r': Result += "\\r"; break;
597 case '\t': Result += "\\t"; break;
598 case '\v': Result += "\\v"; break;
599 default:
600 Result += '\\';
601 Result += toOctal(C >> 6);
602 Result += toOctal(C >> 3);
603 Result += toOctal(C >> 0);
604 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000605 }
606 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000607 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000608 Result += "\"";
609
610 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000611}
612
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000613inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000614ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000615{
616 return (arrayType->getElementType() == Type::UByteTy ||
617 arrayType->getElementType() == Type::SByteTy);
618}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000619
Vikram S. Advee99941a2002-08-22 02:58:36 +0000620
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000621inline const string
622TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000623{
624 switch(type->getPrimitiveID())
625 {
626 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
627 return ".byte";
628 case Type::UShortTyID: case Type::ShortTyID:
629 return ".half";
630 case Type::UIntTyID: case Type::IntTyID:
631 return ".word";
632 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
633 return ".xword";
634 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000635 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000636 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000637 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000638 case Type::ArrayTyID:
639 if (ArrayTypeIsString((ArrayType*) type))
640 return ".ascii";
641 else
642 return "<InvaliDataTypeForPrinting>";
643 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000644 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000645 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000646}
647
Vikram S. Advefee76262002-10-13 00:32:18 +0000648// Get the size of the type
649//
650inline unsigned int
651TypeToSize(const Type* type, const TargetMachine& target)
652{
653 return target.findOptimalStorageSize(type);
654}
655
Vikram S. Adve21447222001-11-10 02:03:06 +0000656// Get the size of the constant for the given target.
657// If this is an unsized array, return 0.
658//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000659inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000660ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000661{
Vikram S. Advefee76262002-10-13 00:32:18 +0000662 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000663 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000664 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000665 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000666 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000667 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000668
Vikram S. Advefee76262002-10-13 00:32:18 +0000669 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000670}
671
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000672// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000673// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
674//
675inline unsigned int
676SizeToAlignment(unsigned int size, const TargetMachine& target)
677{
678 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
679 if (size > (unsigned) cacheLineSize / 2)
680 return cacheLineSize;
681 else
682 for (unsigned sz=1; /*no condition*/; sz *= 2)
683 if (sz >= size)
684 return sz;
685}
686
687// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000688//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000689inline unsigned int
690TypeToAlignment(const Type* type, const TargetMachine& target)
691{
Vikram S. Advefee76262002-10-13 00:32:18 +0000692 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000693}
694
695// Get the size of the constant and then use SizeToAlignment.
696// Handles strings as a special case;
697inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000698ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000699{
Vikram S. Advefee76262002-10-13 00:32:18 +0000700 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
701 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
702 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000703
704 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000705}
706
707
Vikram S. Adve21447222001-11-10 02:03:06 +0000708// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709void
Vikram S. Advefee76262002-10-13 00:32:18 +0000710SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000711{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000712 assert(CV->getType() != Type::VoidTy &&
713 CV->getType() != Type::TypeTy &&
714 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000715 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000716
Chris Lattnerf678dc62002-04-11 21:44:02 +0000717 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
718 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000719
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000720 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000721
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000722 if (CV->getType()->isPrimitiveType())
723 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000724 if (CV->getType()->isFloatingPoint()) {
725 // FP Constants are printed as integer constants to avoid losing
726 // precision...
727 double Val = cast<ConstantFP>(CV)->getValue();
728 if (CV->getType() == Type::FloatTy) {
729 float FVal = (float)Val;
730 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
731 toAsm << *(unsigned int*)ProxyPtr;
732 } else if (CV->getType() == Type::DoubleTy) {
733 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
734 toAsm << *(uint64_t*)ProxyPtr;
735 } else {
736 assert(0 && "Unknown floating point type!");
737 }
738
739 toAsm << "\t! " << CV->getType()->getDescription()
740 << " value: " << Val << "\n";
741 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000742 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000743 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000744 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000745 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
746 { // This is a constant address for a global variable or method.
747 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000748 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000749 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000750 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000751 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000752 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000753 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000754 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
755 { // Constant expression built from operators, constants, and symbolic addrs
756 toAsm << ConstantExprToString(CE, Target) << "\n";
757 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000758 else
759 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000760 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000761 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000762}
763
Vikram S. Adve21447222001-11-10 02:03:06 +0000764void
Vikram S. Advefee76262002-10-13 00:32:18 +0000765SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000766{
Vikram S. Advefee76262002-10-13 00:32:18 +0000767 for ( ; numBytes >= 8; numBytes -= 8)
768 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
769
770 if (numBytes >= 4)
771 {
772 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
773 numBytes -= 4;
774 }
775
776 while (numBytes--)
777 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
778}
779
780// Print a constant value or values (it may be an aggregate).
781// Uses printSingleConstantValue() to print each individual value.
782void
783SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
784 int numPadBytes /* = 0*/)
785{
786 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
787
788 if (numPadBytes)
789 PrintZeroBytesToPad(numPadBytes);
790
791 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000792 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000793 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000794 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000795 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000796 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000797 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000798 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000799 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000800 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000801 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
802 { // Print the fields in successive locations. Pad to align if needed!
803 const StructLayout *cvsLayout =
804 Target.DataLayout.getStructLayout(CVS->getType());
805 const std::vector<Use>& constValues = CVS->getValues();
806 unsigned sizeSoFar = 0;
807 for (unsigned i=0, N = constValues.size(); i < N; i++)
808 {
809 const Constant* field = cast<Constant>(constValues[i].get());
810
811 // Check if padding is needed and insert one or more 0s.
812 unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
813 int padSize = ((i == N-1? cvsLayout->StructSize
814 : cvsLayout->MemberOffsets[i+1])
815 - cvsLayout->MemberOffsets[i]) - fieldSize;
816 sizeSoFar += (fieldSize + padSize);
817
818 // Now print the actual field value
819 printConstantValueOnly(field, padSize);
820 }
821 assert(sizeSoFar == cvsLayout->StructSize &&
822 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000823 }
824 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000825 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000826}
827
828// Print a constant (which may be an aggregate) prefixed by all the
829// appropriate directives. Uses printConstantValueOnly() to print the
830// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000831void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000832SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000833{
834 if (valID.length() == 0)
835 valID = getID(CV);
836
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000837 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000838
839 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000840 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
841 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000842 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000843 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000844 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000845 return;
846 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000847
Chris Lattner697954c2002-01-20 22:54:45 +0000848 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000849
850 unsigned int constSize = ConstantToSize(CV, Target);
851 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000852 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000853
Chris Lattner697954c2002-01-20 22:54:45 +0000854 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000855
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000856 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000857}
858
859
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000860void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000861 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000862 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
863 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000864 const hash_set<const Constant*> &pool =
Misha Brukmanfce11432002-10-28 00:28:31 +0000865 MachineFunction::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000866 MC.insert(pool.begin(), pool.end());
867 }
868}
869
870void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000871{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000872 if (GV->hasExternalLinkage())
873 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000874
Vikram S. Advefee76262002-10-13 00:32:18 +0000875 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000876 printConstant(GV->getInitializer(), getID(GV));
877 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000878 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
879 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000880 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000881 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000882 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000883 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000884 }
885}
886
887
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000888void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000889 // First, get the constants there were marked by the code generator for
890 // inclusion in the assembly code data area and fold them all into a
891 // single constant pool since there may be lots of duplicates. Also,
892 // lets force these constants into the slot table so that we can get
893 // unique names for unnamed constants also.
894 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000895 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000896 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000897
Chris Lattner637ed862002-08-07 21:39:48 +0000898 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000899 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000900 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000901 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000902 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000903
904 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000905 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
906 if (! GI->isExternal()) {
907 assert(GI->hasInitializer());
908 if (GI->isConstant())
909 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
910 else if (GI->getInitializer()->isNullValue())
911 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
912 else
913 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
914
915 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000916 }
Chris Lattner637ed862002-08-07 21:39:48 +0000917
Chris Lattner697954c2002-01-20 22:54:45 +0000918 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000919}
920
Chris Lattnere88f78c2001-09-19 13:47:27 +0000921} // End anonymous namespace
922
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000923Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000924 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000925}