blob: 6001046b2fa5816fd95284327bec98f13ac92250 [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 Lattnerd0fe5f52002-12-28 20:15:01 +000017#include "llvm/CodeGen/MachineFunctionInfo.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)
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000101 {
102 default: assert(0 && "Bad section name!");
103 case Text: toAsm << "\".text\""; break;
104 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
105 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
106 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
107 }
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
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000121 for (unsigned i = 0; i < S.size(); ++i) {
122 char C = S[i];
123 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
124 Result += C;
125 else {
126 Result += '_';
127 Result += char('0' + ((unsigned char)C >> 4));
128 Result += char('0' + (C & 0xF));
Chris Lattnerc56d7792001-09-28 15:07:24 +0000129 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000130 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000131 return Result;
132 }
133
Chris Lattnere88f78c2001-09-19 13:47:27 +0000134 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000135 // the name of the identifier if possible (qualified by the type), and
136 // use a numbered value based on prefix otherwise.
137 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000138 //
139 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000140 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000141
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000142 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000143
Vikram S. Adved198c472002-03-18 03:07:26 +0000144 // Qualify all internal names with a unique id.
145 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000146 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000147 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000148 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
149 if (I == idTable->valToIdMap.end())
150 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000151 else
152 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000153 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000154 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000155
156 // Replace or prefix problem characters in the name
157 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000158 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000159
160 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000161 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000162
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000164 string getID(const Function *F) {
165 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000166 }
167 string getID(const BasicBlock *BB) {
168 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
169 }
170 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000171 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000172 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000173 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174 return getID(CV, "LLVMConst_", ".C_");
175 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000176 string getID(const GlobalValue *GV) {
177 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
178 return getID(V);
179 else if (const Function *F = dyn_cast<Function>(GV))
180 return getID(F);
181 assert(0 && "Unexpected type of GlobalValue!");
182 return "";
183 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000184
Vikram S. Adve537a8772002-09-05 18:28:10 +0000185 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
186 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000187 string ConstantExprToString(const ConstantExpr* CE,
188 const TargetMachine& target) {
189 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000190 switch(CE->getOpcode()) {
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000191 case Instruction::GetElementPtr: {
192 // generate a symbolic expression for the byte address
193 const Value* ptrVal = CE->getOperand(0);
194 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
195 const TargetData &TD = target.getTargetData();
196 S += "(" + valToExprString(ptrVal, target) + ") + ("
197 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
198 break;
199 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000200
Vikram S. Adve537a8772002-09-05 18:28:10 +0000201 case Instruction::Cast:
202 // Support only non-converting casts for now, i.e., a no-op.
203 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000204 assert(target.getTargetData().getTypeSize(CE->getType()) ==
205 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000206 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
207 break;
208
209 case Instruction::Add:
210 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
211 + valToExprString(CE->getOperand(1), target) + ")";
212 break;
213
Vikram S. Advee99941a2002-08-22 02:58:36 +0000214 default:
215 assert(0 && "Unsupported operator in ConstantExprToString()");
216 break;
217 }
218
219 return S;
220 }
221
222 // valToExprString - Helper function for ConstantExprToString().
223 // Appends result to argument string S.
224 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000225 string valToExprString(const Value* V, const TargetMachine& target) {
226 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000227 bool failed = false;
228 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
229
230 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000231 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000232 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
233 S += itostr(CI->getValue());
234 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
235 S += utostr(CI->getValue());
236 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
237 S += ftostr(CFP->getValue());
238 else if (isa<ConstantPointerNull>(CV))
239 S += "0";
240 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000241 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000242 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
243 S += ConstantExprToString(CE, target);
244 else
245 failed = true;
246
247 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
248 S += getID(GV);
249 }
250 else
251 failed = true;
252
253 if (failed) {
254 assert(0 && "Cannot convert value to string");
255 S += "<illegal-value>";
256 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000257 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000258 }
259
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000260};
261
262
263
264//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000265// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000266//===----------------------------------------------------------------------===//
267
Chris Lattnerf57b8452002-04-27 06:56:12 +0000268struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000269 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000270 : AsmPrinter(os, t) {}
271
Chris Lattner96c466b2002-04-29 14:57:45 +0000272 const char *getPassName() const {
273 return "Output Sparc Assembly for Functions";
274 }
275
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000276 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000277 startModule(M);
278 return false;
279 }
280
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000281 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000282 startFunction(F);
283 emitFunction(F);
284 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000285 return false;
286 }
287
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000288 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000289 endModule();
290 return false;
291 }
292
Chris Lattner97e52e42002-04-28 21:27:06 +0000293 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
294 AU.setPreservesAll();
295 }
296
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000297 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000298private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000299 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000300 void emitMachineInst(const MachineInstr *MI);
301
302 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000303 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000304
305 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
306 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000307
Chris Lattnere88f78c2001-09-19 13:47:27 +0000308 unsigned getOperandMask(unsigned Opcode) {
309 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000310 case V9::SUBccr:
311 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000312 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000313 default: return 0; // By default, don't hack operands...
314 }
315 }
316};
317
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000319SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
320 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000321 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000322 case V9::JMPLCALLr:
323 case V9::JMPLCALLi:
324 case V9::JMPLRETr:
325 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000326 return (opNum == 0);
327 default:
328 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000329 }
330}
331
332
333inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
335 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000336 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
337 return (opNum == 0);
338 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
339 return (opNum == 1);
340 else
341 return false;
342}
343
344
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000345#define PrintOp1PlusOp2(mop1, mop2, opCode) \
346 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000348 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000349
350unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000351SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000352 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000353{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000354 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000355
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000356 if (OpIsBranchTargetLabel(MI, opNum)) {
357 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
358 return 2;
359 } else if (OpIsMemoryAddressBase(MI, opNum)) {
360 toAsm << "[";
361 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
362 toAsm << "]";
363 return 2;
364 } else {
365 printOneOperand(mop, MI->getOpCode());
366 return 1;
367 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000368}
369
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000370void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000371SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
372 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000373{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000374 bool needBitsFlag = true;
375
376 if (mop.opHiBits32())
377 toAsm << "%lm(";
378 else if (mop.opLoBits32())
379 toAsm << "%lo(";
380 else if (mop.opHiBits64())
381 toAsm << "%hh(";
382 else if (mop.opLoBits64())
383 toAsm << "%hm(";
384 else
385 needBitsFlag = false;
386
Chris Lattner133f0792002-10-28 04:45:29 +0000387 switch (mop.getType())
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000388 {
389 case MachineOperand::MO_VirtualRegister:
390 case MachineOperand::MO_CCRegister:
391 case MachineOperand::MO_MachineRegister: {
392 int regNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000393
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000394 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
395 // better to print code with NULL registers than to die
396 toAsm << "<NULL VALUE>";
397 } else {
398 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000400 break;
401 }
402
403 case MachineOperand::MO_PCRelativeDisp: {
404 const Value *Val = mop.getVRegValue();
405 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
406
407 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
408 toAsm << getID(BB);
409 else if (const Function *M = dyn_cast<Function>(Val))
410 toAsm << getID(M);
411 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
412 toAsm << getID(GV);
413 else if (const Constant *CV = dyn_cast<Constant>(Val))
414 toAsm << getID(CV);
415 else
416 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
417 break;
418 }
419
420 case MachineOperand::MO_SignExtendedImmed:
421 toAsm << mop.getImmedValue();
422 break;
423
424 case MachineOperand::MO_UnextendedImmed:
425 toAsm << (uint64_t) mop.getImmedValue();
426 break;
427
428 default:
429 toAsm << mop; // use dump field
430 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
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000443 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000444 return; // Ignore Phi nodes
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000445
Chris Lattnerf44f9052002-10-29 17:35:41 +0000446 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000447
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?
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000454 if (NeedComma) toAsm << ", "; // Handle comma outputing
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000455 NeedComma = true;
456 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000457 } else
458 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000459
460 toAsm << "\n";
461}
462
463void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000464SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000465{
466 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000467 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000468
469 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000470 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000471 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000472 emitMachineInst(*MII);
473 toAsm << "\n"; // Seperate BB's with newlines
474}
475
476void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000477SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000478{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000479 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000480 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000481 enterSection(AsmPrinter::Text);
482 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
483 //toAsm << "\t.type\t" << methName << ",#function\n";
484 toAsm << "\t.type\t" << methName << ", 2\n";
485 toAsm << methName << ":\n";
486
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000487 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000488 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000489 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000490 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000491
492 // Output a .size directive so the debugger knows the extents of the function
493 toAsm << ".EndOf_" << methName << ":\n\t.size "
494 << methName << ", .EndOf_"
495 << methName << "-" << methName << "\n";
496
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000497 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000498 toAsm << "\n\n";
499}
500
501} // End anonymous namespace
502
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000503Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000504 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000505}
506
507
508
509
510
511//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000512// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000513//===----------------------------------------------------------------------===//
514
515namespace {
516
517class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
518public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000519 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
520 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000521
Chris Lattner96c466b2002-04-29 14:57:45 +0000522 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
523
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000524 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000525 startModule(M);
526 emitGlobalsAndConstants(M);
527 endModule();
528 return false;
529 }
530
Chris Lattner97e52e42002-04-28 21:27:06 +0000531 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
532 AU.setPreservesAll();
533 }
534
535private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000536 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000537
Vikram S. Advefee76262002-10-13 00:32:18 +0000538 void printGlobalVariable (const GlobalVariable *GV);
539 void PrintZeroBytesToPad (int numBytes);
540 void printSingleConstantValue (const Constant* CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000541 void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000542 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000543
Vikram S. Advefee76262002-10-13 00:32:18 +0000544 static void FoldConstants (const Module &M,
545 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000546};
547
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000548
549// Can we treat the specified array as a string? Only if it is an array of
550// ubytes or non-negative sbytes.
551//
Vikram S. Advefee76262002-10-13 00:32:18 +0000552static bool isStringCompatible(const ConstantArray *CVA) {
553 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000554 if (ETy == Type::UByteTy) return true;
555 if (ETy != Type::SByteTy) return false;
556
Vikram S. Advefee76262002-10-13 00:32:18 +0000557 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
558 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000559 return false;
560
561 return true;
562}
563
564// toOctal - Convert the low order bits of X into an octal letter
565static inline char toOctal(int X) {
566 return (X&7)+'0';
567}
568
569// getAsCString - Return the specified array as a C compatible string, only if
570// the predicate isStringCompatible is true.
571//
Vikram S. Advefee76262002-10-13 00:32:18 +0000572static string getAsCString(const ConstantArray *CVA) {
573 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000574
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000575 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000576 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000577 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000578 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000579 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000580 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
581 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000582
Vikram S. Adve242a8082002-05-19 15:25:51 +0000583 if (C == '"') {
584 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000585 } else if (C == '\\') {
586 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000587 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000588 Result += C;
589 } else {
590 switch(C) {
591 case '\a': Result += "\\a"; break;
592 case '\b': Result += "\\b"; break;
593 case '\f': Result += "\\f"; break;
594 case '\n': Result += "\\n"; break;
595 case '\r': Result += "\\r"; break;
596 case '\t': Result += "\\t"; break;
597 case '\v': Result += "\\v"; break;
598 default:
599 Result += '\\';
600 Result += toOctal(C >> 6);
601 Result += toOctal(C >> 3);
602 Result += toOctal(C >> 0);
603 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000604 }
605 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000606 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000607 Result += "\"";
608
609 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000610}
611
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000612inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000613ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000614{
615 return (arrayType->getElementType() == Type::UByteTy ||
616 arrayType->getElementType() == Type::SByteTy);
617}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000618
Vikram S. Advee99941a2002-08-22 02:58:36 +0000619
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000620inline const string
621TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000622{
623 switch(type->getPrimitiveID())
624 {
625 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
626 return ".byte";
627 case Type::UShortTyID: case Type::ShortTyID:
628 return ".half";
629 case Type::UIntTyID: case Type::IntTyID:
630 return ".word";
631 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
632 return ".xword";
633 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000634 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000635 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000636 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000637 case Type::ArrayTyID:
638 if (ArrayTypeIsString((ArrayType*) type))
639 return ".ascii";
640 else
641 return "<InvaliDataTypeForPrinting>";
642 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000643 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000645}
646
Vikram S. Advefee76262002-10-13 00:32:18 +0000647// Get the size of the type
648//
649inline unsigned int
650TypeToSize(const Type* type, const TargetMachine& target)
651{
652 return target.findOptimalStorageSize(type);
653}
654
Vikram S. Adve21447222001-11-10 02:03:06 +0000655// Get the size of the constant for the given target.
656// If this is an unsized array, return 0.
657//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000658inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000659ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000660{
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000661 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) {
662 const ArrayType *aty = cast<ArrayType>(CVA->getType());
663 if (ArrayTypeIsString(aty))
664 return 1 + CVA->getNumOperands();
665 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000666
Vikram S. Advefee76262002-10-13 00:32:18 +0000667 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000668}
669
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000670// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000671// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
672//
673inline unsigned int
674SizeToAlignment(unsigned int size, const TargetMachine& target)
675{
676 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
677 if (size > (unsigned) cacheLineSize / 2)
678 return cacheLineSize;
679 else
680 for (unsigned sz=1; /*no condition*/; sz *= 2)
681 if (sz >= size)
682 return sz;
683}
684
685// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000686//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000687inline unsigned int
688TypeToAlignment(const Type* type, const TargetMachine& target)
689{
Vikram S. Advefee76262002-10-13 00:32:18 +0000690 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000691}
692
693// Get the size of the constant and then use SizeToAlignment.
694// Handles strings as a special case;
695inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000696ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000697{
Vikram S. Advefee76262002-10-13 00:32:18 +0000698 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
699 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
700 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000701
702 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000703}
704
705
Vikram S. Adve21447222001-11-10 02:03:06 +0000706// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707void
Vikram S. Advefee76262002-10-13 00:32:18 +0000708SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000710 assert(CV->getType() != Type::VoidTy &&
711 CV->getType() != Type::TypeTy &&
712 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000713 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714
Chris Lattnerf678dc62002-04-11 21:44:02 +0000715 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
716 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000717
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000718 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000719
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000720 if (CV->getType()->isPrimitiveType()) {
721 if (CV->getType()->isFloatingPoint()) {
722 // FP Constants are printed as integer constants to avoid losing
723 // precision...
724 double Val = cast<ConstantFP>(CV)->getValue();
725 if (CV->getType() == Type::FloatTy) {
726 float FVal = (float)Val;
727 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
728 toAsm << *(unsigned int*)ProxyPtr;
729 } else if (CV->getType() == Type::DoubleTy) {
730 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
731 toAsm << *(uint64_t*)ProxyPtr;
Chris Lattnerf678dc62002-04-11 21:44:02 +0000732 } else {
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000733 assert(0 && "Unknown floating point type!");
Chris Lattnerf678dc62002-04-11 21:44:02 +0000734 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000735
736 toAsm << "\t! " << CV->getType()->getDescription()
737 << " value: " << Val << "\n";
738 } else {
739 WriteAsOperand(toAsm, CV, false, false) << "\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000740 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000741 } else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV)) {
742 // This is a constant address for a global variable or method.
743 // Use the name of the variable or method as the address value.
744 toAsm << getID(CPR->getValue()) << "\n";
745 } else if (isa<ConstantPointerNull>(CV)) {
746 // Null pointer value
747 toAsm << "0\n";
748 } else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
749 // Constant expression built from operators, constants, and symbolic addrs
750 toAsm << ConstantExprToString(CE, Target) << "\n";
751 } else {
752 assert(0 && "Unknown elementary type for constant");
753 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000754}
755
Vikram S. Adve21447222001-11-10 02:03:06 +0000756void
Vikram S. Advefee76262002-10-13 00:32:18 +0000757SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000758{
Vikram S. Advefee76262002-10-13 00:32:18 +0000759 for ( ; numBytes >= 8; numBytes -= 8)
760 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
761
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000762 if (numBytes >= 4) {
763 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
764 numBytes -= 4;
765 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000766
767 while (numBytes--)
768 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
769}
770
771// Print a constant value or values (it may be an aggregate).
772// Uses printSingleConstantValue() to print each individual value.
773void
774SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
Vikram S. Adve9e498242003-05-25 21:59:09 +0000775 int numPadBytesAfter /* = 0*/)
Vikram S. Advefee76262002-10-13 00:32:18 +0000776{
777 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
778
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000779 if (CVA && isStringCompatible(CVA)) {
780 // print the string alone and return
781 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
782 } else if (CVA) {
783 // Not a string. Print the values in successive locations
784 const std::vector<Use> &constValues = CVA->getValues();
785 for (unsigned i=0; i < constValues.size(); i++)
786 printConstantValueOnly(cast<Constant>(constValues[i].get()));
787 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
788 // Print the fields in successive locations. Pad to align if needed!
789 const StructLayout *cvsLayout =
790 Target.getTargetData().getStructLayout(CVS->getType());
791 const std::vector<Use>& constValues = CVS->getValues();
792 unsigned sizeSoFar = 0;
793 for (unsigned i=0, N = constValues.size(); i < N; i++) {
794 const Constant* field = cast<Constant>(constValues[i].get());
Vikram S. Advefee76262002-10-13 00:32:18 +0000795
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000796 // Check if padding is needed and insert one or more 0s.
797 unsigned fieldSize =
798 Target.getTargetData().getTypeSize(field->getType());
799 int padSize = ((i == N-1? cvsLayout->StructSize
800 : cvsLayout->MemberOffsets[i+1])
801 - cvsLayout->MemberOffsets[i]) - fieldSize;
802 sizeSoFar += (fieldSize + padSize);
Vikram S. Advefee76262002-10-13 00:32:18 +0000803
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000804 // Now print the actual field value
805 printConstantValueOnly(field, padSize);
Vikram S. Adve21447222001-11-10 02:03:06 +0000806 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000807 assert(sizeSoFar == cvsLayout->StructSize &&
808 "Layout of constant struct may be incorrect!");
809 } else
Vikram S. Advefee76262002-10-13 00:32:18 +0000810 printSingleConstantValue(CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000811
812 if (numPadBytesAfter)
813 PrintZeroBytesToPad(numPadBytesAfter);
Vikram S. Adve21447222001-11-10 02:03:06 +0000814}
815
816// Print a constant (which may be an aggregate) prefixed by all the
817// appropriate directives. Uses printConstantValueOnly() to print the
818// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000819void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000820SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000821{
822 if (valID.length() == 0)
823 valID = getID(CV);
824
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000825 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000826
827 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000828 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000829 if (CVA && isStringCompatible(CVA)) {
830 // print it as a string and return
831 toAsm << valID << ":\n";
832 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
833 return;
834 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000835
Chris Lattner697954c2002-01-20 22:54:45 +0000836 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000837
838 unsigned int constSize = ConstantToSize(CV, Target);
839 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000840 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000841
Chris Lattner697954c2002-01-20 22:54:45 +0000842 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000843
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000844 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000845}
846
847
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000848void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000849 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000850 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
851 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000852 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000853 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000854 MC.insert(pool.begin(), pool.end());
855 }
856}
857
858void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000859{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000860 if (GV->hasExternalLinkage())
861 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000862
Vikram S. Advefee76262002-10-13 00:32:18 +0000863 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000864 printConstant(GV->getInitializer(), getID(GV));
865 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000866 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
867 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000868 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000869 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000870 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000871 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000872 }
873}
874
875
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000876void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000877 // First, get the constants there were marked by the code generator for
878 // inclusion in the assembly code data area and fold them all into a
879 // single constant pool since there may be lots of duplicates. Also,
880 // lets force these constants into the slot table so that we can get
881 // unique names for unnamed constants also.
882 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000883 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000884 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000885
Chris Lattner637ed862002-08-07 21:39:48 +0000886 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000887 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000888 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000889 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000890 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000891
892 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000893 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
894 if (! GI->isExternal()) {
895 assert(GI->hasInitializer());
896 if (GI->isConstant())
897 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
898 else if (GI->getInitializer()->isNullValue())
899 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
900 else
901 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
902
903 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000904 }
Chris Lattner637ed862002-08-07 21:39:48 +0000905
Chris Lattner697954c2002-01-20 22:54:45 +0000906 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000907}
908
Chris Lattnere88f78c2001-09-19 13:47:27 +0000909} // End anonymous namespace
910
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000911Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000912 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000913}