blob: 523e21e16425b3fb3e2427ad2c4de62a75aa52c0 [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)
101 {
102 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000103 case Text: toAsm << "\".text\""; break;
104 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
105 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Advefee76262002-10-13 00:32:18 +0000106 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000107 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000108 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000109 }
110
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000111 static string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000112 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000113
114 // Symbol names in Sparc assembly language have these rules:
115 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
116 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000117 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000118 if (isdigit(S[0]))
119 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120
121 for (unsigned i = 0; i < S.size(); ++i)
122 {
123 char C = S[i];
124 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
125 Result += C;
126 else
127 {
128 Result += '_';
129 Result += char('0' + ((unsigned char)C >> 4));
130 Result += char('0' + (C & 0xF));
131 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000132 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000133 return Result;
134 }
135
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000137 // the name of the identifier if possible (qualified by the type), and
138 // use a numbered value based on prefix otherwise.
139 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000140 //
141 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000142 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000143
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000144 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000145
Vikram S. Adved198c472002-03-18 03:07:26 +0000146 // Qualify all internal names with a unique id.
147 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000148 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000149 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000150 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
151 if (I == idTable->valToIdMap.end())
152 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000153 else
154 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000155 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000157
158 // Replace or prefix problem characters in the name
159 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000160 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000161
162 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000164
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000166 string getID(const Function *F) {
167 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168 }
169 string getID(const BasicBlock *BB) {
170 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
171 }
172 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000173 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000175 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 return getID(CV, "LLVMConst_", ".C_");
177 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000178 string getID(const GlobalValue *GV) {
179 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
180 return getID(V);
181 else if (const Function *F = dyn_cast<Function>(GV))
182 return getID(F);
183 assert(0 && "Unexpected type of GlobalValue!");
184 return "";
185 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000186
Vikram S. Adve537a8772002-09-05 18:28:10 +0000187 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
188 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000189 string ConstantExprToString(const ConstantExpr* CE,
190 const TargetMachine& target) {
191 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000192 switch(CE->getOpcode()) {
193 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000194 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000195 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000196 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000197 const TargetData &TD = target.getTargetData();
Vikram S. Adve537a8772002-09-05 18:28:10 +0000198 S += "(" + valToExprString(ptrVal, target) + ") + ("
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000199 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000200 break;
201 }
202
Vikram S. Adve537a8772002-09-05 18:28:10 +0000203 case Instruction::Cast:
204 // Support only non-converting casts for now, i.e., a no-op.
205 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000206 assert(target.getTargetData().getTypeSize(CE->getType()) ==
207 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000208 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
209 break;
210
211 case Instruction::Add:
212 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
213 + valToExprString(CE->getOperand(1), target) + ")";
214 break;
215
Vikram S. Advee99941a2002-08-22 02:58:36 +0000216 default:
217 assert(0 && "Unsupported operator in ConstantExprToString()");
218 break;
219 }
220
221 return S;
222 }
223
224 // valToExprString - Helper function for ConstantExprToString().
225 // Appends result to argument string S.
226 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000227 string valToExprString(const Value* V, const TargetMachine& target) {
228 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000229 bool failed = false;
230 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
231
232 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000233 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000234 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
235 S += itostr(CI->getValue());
236 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
237 S += utostr(CI->getValue());
238 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
239 S += ftostr(CFP->getValue());
240 else if (isa<ConstantPointerNull>(CV))
241 S += "0";
242 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000243 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000244 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
245 S += ConstantExprToString(CE, target);
246 else
247 failed = true;
248
249 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
250 S += getID(GV);
251 }
252 else
253 failed = true;
254
255 if (failed) {
256 assert(0 && "Cannot convert value to string");
257 S += "<illegal-value>";
258 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000259 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000260 }
261
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000262};
263
264
265
266//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000268//===----------------------------------------------------------------------===//
269
Chris Lattnerf57b8452002-04-27 06:56:12 +0000270struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000271 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000272 : AsmPrinter(os, t) {}
273
Chris Lattner96c466b2002-04-29 14:57:45 +0000274 const char *getPassName() const {
275 return "Output Sparc Assembly for Functions";
276 }
277
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000278 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000279 startModule(M);
280 return false;
281 }
282
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000283 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000284 startFunction(F);
285 emitFunction(F);
286 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000287 return false;
288 }
289
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000290 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000291 endModule();
292 return false;
293 }
294
Chris Lattner97e52e42002-04-28 21:27:06 +0000295 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
296 AU.setPreservesAll();
297 }
298
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000299 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000300private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000301 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000302 void emitMachineInst(const MachineInstr *MI);
303
304 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
305 void printOneOperand(const MachineOperand &Op);
306
307 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
308 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000309
Chris Lattnere88f78c2001-09-19 13:47:27 +0000310 unsigned getOperandMask(unsigned Opcode) {
311 switch (Opcode) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000312 case V9::SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000313 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000314 default: return 0; // By default, don't hack operands...
315 }
316 }
317};
318
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000319inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000320SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
321 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 switch (MI->getOpCode()) {
Misha Brukmana98cd452003-05-20 20:32:24 +0000323 case V9::JMPLCALL:
324 case V9::JMPLRET:
325 return (opNum == 0);
326 default:
327 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000328 }
329}
330
331
332inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000333SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
334 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000335 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
336 return (opNum == 0);
337 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
338 return (opNum == 1);
339 else
340 return false;
341}
342
343
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000344#define PrintOp1PlusOp2(mop1, mop2) \
345 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000346 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000347 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348
349unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000350SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000351 unsigned int opNum)
352{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000353 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000354
355 if (OpIsBranchTargetLabel(MI, opNum))
356 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000357 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000358 return 2;
359 }
360 else if (OpIsMemoryAddressBase(MI, opNum))
361 {
362 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000363 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000364 toAsm << "]";
365 return 2;
366 }
367 else
368 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000369 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000370 return 1;
371 }
372}
373
374
375void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000376SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000377{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000378 bool needBitsFlag = true;
379
380 if (mop.opHiBits32())
381 toAsm << "%lm(";
382 else if (mop.opLoBits32())
383 toAsm << "%lo(";
384 else if (mop.opHiBits64())
385 toAsm << "%hh(";
386 else if (mop.opLoBits64())
387 toAsm << "%hm(";
388 else
389 needBitsFlag = false;
390
Chris Lattner133f0792002-10-28 04:45:29 +0000391 switch (mop.getType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392 {
393 case MachineOperand::MO_VirtualRegister:
394 case MachineOperand::MO_CCRegister:
395 case MachineOperand::MO_MachineRegister:
396 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000397 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000398
Vikram S. Advefbd21612002-03-31 19:03:58 +0000399 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000400 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
401 toAsm << "<NULL VALUE>";
402 } else {
403 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
404 }
405 break;
406 }
407
408 case MachineOperand::MO_PCRelativeDisp:
409 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000410 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000411 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
412
413 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000414 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000415 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000416 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000417 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000418 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000419 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000420 toAsm << getID(CV);
421 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000422 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000423 break;
424 }
425
426 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000427 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000428 break;
429
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000430 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000431 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000432 break;
433
434 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000435 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000436 break;
437 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000438
439 if (needBitsFlag)
440 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000441}
442
443
444void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000445SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000446{
447 unsigned Opcode = MI->getOpCode();
448
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000449 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000450 return; // IGNORE PHI NODES
451
Chris Lattnerf44f9052002-10-29 17:35:41 +0000452 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000453
454 unsigned Mask = getOperandMask(Opcode);
455
456 bool NeedComma = false;
457 unsigned N = 1;
458 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
459 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
460 if (NeedComma) toAsm << ", "; // Handle comma outputing
461 NeedComma = true;
462 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000463 } else
464 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000465
466 toAsm << "\n";
467}
468
469void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000470SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000471{
472 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000473 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000474
475 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000476 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000477 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000478 emitMachineInst(*MII);
479 toAsm << "\n"; // Seperate BB's with newlines
480}
481
482void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000483SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000484{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000485 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000486 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000487 enterSection(AsmPrinter::Text);
488 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
489 //toAsm << "\t.type\t" << methName << ",#function\n";
490 toAsm << "\t.type\t" << methName << ", 2\n";
491 toAsm << methName << ":\n";
492
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000493 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000494 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000495 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000496 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000497
498 // Output a .size directive so the debugger knows the extents of the function
499 toAsm << ".EndOf_" << methName << ":\n\t.size "
500 << methName << ", .EndOf_"
501 << methName << "-" << methName << "\n";
502
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000503 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000504 toAsm << "\n\n";
505}
506
507} // End anonymous namespace
508
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000509Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000510 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000511}
512
513
514
515
516
517//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000518// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000519//===----------------------------------------------------------------------===//
520
521namespace {
522
523class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
524public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000525 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
526 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000527
Chris Lattner96c466b2002-04-29 14:57:45 +0000528 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
529
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000530 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000531 startModule(M);
532 emitGlobalsAndConstants(M);
533 endModule();
534 return false;
535 }
536
Chris Lattner97e52e42002-04-28 21:27:06 +0000537 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
538 AU.setPreservesAll();
539 }
540
541private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000542 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000543
Vikram S. Advefee76262002-10-13 00:32:18 +0000544 void printGlobalVariable (const GlobalVariable *GV);
545 void PrintZeroBytesToPad (int numBytes);
546 void printSingleConstantValue (const Constant* CV);
547 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000548 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000549
Vikram S. Advefee76262002-10-13 00:32:18 +0000550 static void FoldConstants (const Module &M,
551 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000552};
553
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000554
555// Can we treat the specified array as a string? Only if it is an array of
556// ubytes or non-negative sbytes.
557//
Vikram S. Advefee76262002-10-13 00:32:18 +0000558static bool isStringCompatible(const ConstantArray *CVA) {
559 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000560 if (ETy == Type::UByteTy) return true;
561 if (ETy != Type::SByteTy) return false;
562
Vikram S. Advefee76262002-10-13 00:32:18 +0000563 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
564 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000565 return false;
566
567 return true;
568}
569
570// toOctal - Convert the low order bits of X into an octal letter
571static inline char toOctal(int X) {
572 return (X&7)+'0';
573}
574
575// getAsCString - Return the specified array as a C compatible string, only if
576// the predicate isStringCompatible is true.
577//
Vikram S. Advefee76262002-10-13 00:32:18 +0000578static string getAsCString(const ConstantArray *CVA) {
579 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000580
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000581 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000582 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000583 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000584 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000585 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000586 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
587 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000588
Vikram S. Adve242a8082002-05-19 15:25:51 +0000589 if (C == '"') {
590 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000591 } else if (C == '\\') {
592 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000593 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000594 Result += C;
595 } else {
596 switch(C) {
597 case '\a': Result += "\\a"; break;
598 case '\b': Result += "\\b"; break;
599 case '\f': Result += "\\f"; break;
600 case '\n': Result += "\\n"; break;
601 case '\r': Result += "\\r"; break;
602 case '\t': Result += "\\t"; break;
603 case '\v': Result += "\\v"; break;
604 default:
605 Result += '\\';
606 Result += toOctal(C >> 6);
607 Result += toOctal(C >> 3);
608 Result += toOctal(C >> 0);
609 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000610 }
611 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000612 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000613 Result += "\"";
614
615 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000616}
617
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000618inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000619ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000620{
621 return (arrayType->getElementType() == Type::UByteTy ||
622 arrayType->getElementType() == Type::SByteTy);
623}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000624
Vikram S. Advee99941a2002-08-22 02:58:36 +0000625
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000626inline const string
627TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000628{
629 switch(type->getPrimitiveID())
630 {
631 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
632 return ".byte";
633 case Type::UShortTyID: case Type::ShortTyID:
634 return ".half";
635 case Type::UIntTyID: case Type::IntTyID:
636 return ".word";
637 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
638 return ".xword";
639 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000640 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000641 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000642 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000643 case Type::ArrayTyID:
644 if (ArrayTypeIsString((ArrayType*) type))
645 return ".ascii";
646 else
647 return "<InvaliDataTypeForPrinting>";
648 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000649 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000650 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000651}
652
Vikram S. Advefee76262002-10-13 00:32:18 +0000653// Get the size of the type
654//
655inline unsigned int
656TypeToSize(const Type* type, const TargetMachine& target)
657{
658 return target.findOptimalStorageSize(type);
659}
660
Vikram S. Adve21447222001-11-10 02:03:06 +0000661// Get the size of the constant for the given target.
662// If this is an unsized array, return 0.
663//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000664inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000665ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000666{
Vikram S. Advefee76262002-10-13 00:32:18 +0000667 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000668 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000669 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000670 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000671 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000672 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000673
Vikram S. Advefee76262002-10-13 00:32:18 +0000674 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000675}
676
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000677// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000678// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
679//
680inline unsigned int
681SizeToAlignment(unsigned int size, const TargetMachine& target)
682{
683 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
684 if (size > (unsigned) cacheLineSize / 2)
685 return cacheLineSize;
686 else
687 for (unsigned sz=1; /*no condition*/; sz *= 2)
688 if (sz >= size)
689 return sz;
690}
691
692// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000693//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000694inline unsigned int
695TypeToAlignment(const Type* type, const TargetMachine& target)
696{
Vikram S. Advefee76262002-10-13 00:32:18 +0000697 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000698}
699
700// Get the size of the constant and then use SizeToAlignment.
701// Handles strings as a special case;
702inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000703ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000704{
Vikram S. Advefee76262002-10-13 00:32:18 +0000705 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
706 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
707 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000708
709 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000710}
711
712
Vikram S. Adve21447222001-11-10 02:03:06 +0000713// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714void
Vikram S. Advefee76262002-10-13 00:32:18 +0000715SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000716{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000717 assert(CV->getType() != Type::VoidTy &&
718 CV->getType() != Type::TypeTy &&
719 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000720 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000721
Chris Lattnerf678dc62002-04-11 21:44:02 +0000722 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
723 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000724
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000725 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000726
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000727 if (CV->getType()->isPrimitiveType())
728 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000729 if (CV->getType()->isFloatingPoint()) {
730 // FP Constants are printed as integer constants to avoid losing
731 // precision...
732 double Val = cast<ConstantFP>(CV)->getValue();
733 if (CV->getType() == Type::FloatTy) {
734 float FVal = (float)Val;
735 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
736 toAsm << *(unsigned int*)ProxyPtr;
737 } else if (CV->getType() == Type::DoubleTy) {
738 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
739 toAsm << *(uint64_t*)ProxyPtr;
740 } else {
741 assert(0 && "Unknown floating point type!");
742 }
743
744 toAsm << "\t! " << CV->getType()->getDescription()
745 << " value: " << Val << "\n";
746 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000747 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000748 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000749 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000750 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
751 { // This is a constant address for a global variable or method.
752 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000753 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000754 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000755 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000756 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000757 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000758 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000759 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
760 { // Constant expression built from operators, constants, and symbolic addrs
761 toAsm << ConstantExprToString(CE, Target) << "\n";
762 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000763 else
764 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000765 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000766 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000767}
768
Vikram S. Adve21447222001-11-10 02:03:06 +0000769void
Vikram S. Advefee76262002-10-13 00:32:18 +0000770SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000771{
Vikram S. Advefee76262002-10-13 00:32:18 +0000772 for ( ; numBytes >= 8; numBytes -= 8)
773 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
774
775 if (numBytes >= 4)
776 {
777 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
778 numBytes -= 4;
779 }
780
781 while (numBytes--)
782 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
783}
784
785// Print a constant value or values (it may be an aggregate).
786// Uses printSingleConstantValue() to print each individual value.
787void
788SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
789 int numPadBytes /* = 0*/)
790{
791 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
792
793 if (numPadBytes)
794 PrintZeroBytesToPad(numPadBytes);
795
796 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000797 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000798 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000799 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000800 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000801 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000802 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000803 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000804 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000805 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000806 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
807 { // Print the fields in successive locations. Pad to align if needed!
808 const StructLayout *cvsLayout =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000809 Target.getTargetData().getStructLayout(CVS->getType());
Vikram S. Advefee76262002-10-13 00:32:18 +0000810 const std::vector<Use>& constValues = CVS->getValues();
811 unsigned sizeSoFar = 0;
812 for (unsigned i=0, N = constValues.size(); i < N; i++)
813 {
814 const Constant* field = cast<Constant>(constValues[i].get());
815
816 // Check if padding is needed and insert one or more 0s.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000817 unsigned fieldSize =
818 Target.getTargetData().getTypeSize(field->getType());
Vikram S. Advefee76262002-10-13 00:32:18 +0000819 int padSize = ((i == N-1? cvsLayout->StructSize
820 : cvsLayout->MemberOffsets[i+1])
821 - cvsLayout->MemberOffsets[i]) - fieldSize;
822 sizeSoFar += (fieldSize + padSize);
823
824 // Now print the actual field value
825 printConstantValueOnly(field, padSize);
826 }
827 assert(sizeSoFar == cvsLayout->StructSize &&
828 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000829 }
830 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000831 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000832}
833
834// Print a constant (which may be an aggregate) prefixed by all the
835// appropriate directives. Uses printConstantValueOnly() to print the
836// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000837void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000838SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000839{
840 if (valID.length() == 0)
841 valID = getID(CV);
842
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000843 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000844
845 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000846 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
847 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000848 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000849 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000850 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000851 return;
852 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000853
Chris Lattner697954c2002-01-20 22:54:45 +0000854 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000855
856 unsigned int constSize = ConstantToSize(CV, Target);
857 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000858 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000859
Chris Lattner697954c2002-01-20 22:54:45 +0000860 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000861
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000862 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000863}
864
865
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000866void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000867 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000868 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
869 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000870 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000871 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000872 MC.insert(pool.begin(), pool.end());
873 }
874}
875
876void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000877{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000878 if (GV->hasExternalLinkage())
879 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000880
Vikram S. Advefee76262002-10-13 00:32:18 +0000881 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000882 printConstant(GV->getInitializer(), getID(GV));
883 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000884 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
885 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000886 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000887 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000888 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000889 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000890 }
891}
892
893
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000894void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000895 // First, get the constants there were marked by the code generator for
896 // inclusion in the assembly code data area and fold them all into a
897 // single constant pool since there may be lots of duplicates. Also,
898 // lets force these constants into the slot table so that we can get
899 // unique names for unnamed constants also.
900 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000901 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000902 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000903
Chris Lattner637ed862002-08-07 21:39:48 +0000904 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000905 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000906 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000907 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000908 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000909
910 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000911 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
912 if (! GI->isExternal()) {
913 assert(GI->hasInitializer());
914 if (GI->isConstant())
915 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
916 else if (GI->getInitializer()->isNullValue())
917 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
918 else
919 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
920
921 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000922 }
Chris Lattner637ed862002-08-07 21:39:48 +0000923
Chris Lattner697954c2002-01-20 22:54:45 +0000924 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000925}
926
Chris Lattnere88f78c2001-09-19 13:47:27 +0000927} // End anonymous namespace
928
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000929Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000930 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000931}