blob: 3a9893162b8450d31a771a57c820005ee542ecc3 [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2//
Misha Brukman5560c9d2003-08-18 14:43:39 +00003// This file implements all of the stuff necessary to output a .s file from
Chris Lattnere88f78c2001-09-19 13:47:27 +00004// 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
Chris Lattnere88f78c2001-09-19 13:47:27 +000014#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd0fe5f52002-12-28 20:15:01 +000016#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000017#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000019#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000020#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000021#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000022#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/StringExtras.h"
Misha Brukmanf4de7832003-08-05 16:01:50 +000024#include "SparcInternals.h"
25#include <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
Misha Brukman5560c9d2003-08-18 14:43:39 +000093 // executable. This is used to only output necessary section transitions.
Chris Lattnere88f78c2001-09-19 13:47:27 +000094 //
95 void enterSection(enum Sections S) {
Misha Brukman5560c9d2003-08-18 14:43:39 +000096 if (S == CurSection) return; // Only switch section if necessary
Chris Lattnere88f78c2001-09-19 13:47:27 +000097 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)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +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
Misha Brukmanf4de7832003-08-05 16:01:50 +0000111 static std::string getValidSymbolName(const std::string &S) {
112 std::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
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000121 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 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000141 std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
142 std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000143
Misha Brukmanf4de7832003-08-05 16:01:50 +0000144 Result += V->hasName() ? V->getName() : std::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...
Misha Brukmanf4de7832003-08-05 16:01:50 +0000166 std::string getID(const Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000167 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000169 std::string getID(const BasicBlock *BB) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000170 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
171 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000172 std::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 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000175 std::string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 return getID(CV, "LLVMConst_", ".C_");
177 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000178 std::string getID(const GlobalValue *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000179 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
Misha Brukmanf4de7832003-08-05 16:01:50 +0000187 // Combines expressions
188 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
189 const TargetMachine &TM,
190 const std::string &op) {
191 return "(" + valToExprString(CE->getOperand(0), TM) + op
192 + valToExprString(CE->getOperand(1), TM) + ")";
193 }
194
Vikram S. Adve537a8772002-09-05 18:28:10 +0000195 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
196 // and return this as a string.
Misha Brukmanf4de7832003-08-05 16:01:50 +0000197 std::string ConstantExprToString(const ConstantExpr* CE,
198 const TargetMachine& target) {
199 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000200 switch(CE->getOpcode()) {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000201 case Instruction::GetElementPtr:
202 { // generate a symbolic expression for the byte address
203 const Value* ptrVal = CE->getOperand(0);
204 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Misha Brukmanf4de7832003-08-05 16:01:50 +0000205 const TargetData &TD = target.getTargetData();
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000206 S += "(" + valToExprString(ptrVal, target) + ") + ("
207 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
208 break;
209 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000210
Vikram S. Adve537a8772002-09-05 18:28:10 +0000211 case Instruction::Cast:
212 // Support only non-converting casts for now, i.e., a no-op.
213 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000214 assert(target.getTargetData().getTypeSize(CE->getType()) ==
215 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000216 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
217 break;
218
219 case Instruction::Add:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000220 S += ConstantArithExprToString(CE, target, ") + (");
Vikram S. Adve537a8772002-09-05 18:28:10 +0000221 break;
222
Vikram S. Adve72666e62003-08-01 15:55:53 +0000223 case Instruction::Sub:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000224 S += ConstantArithExprToString(CE, target, ") - (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000225 break;
226
227 case Instruction::Mul:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000228 S += ConstantArithExprToString(CE, target, ") * (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000229 break;
230
231 case Instruction::Div:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000232 S += ConstantArithExprToString(CE, target, ") / (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000233 break;
234
235 case Instruction::Rem:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000236 S += ConstantArithExprToString(CE, target, ") % (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000237 break;
238
239 case Instruction::And:
240 // Logical && for booleans; bitwise & otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000241 S += ConstantArithExprToString(CE, target,
242 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000243 break;
244
245 case Instruction::Or:
246 // Logical || for booleans; bitwise | otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000247 S += ConstantArithExprToString(CE, target,
248 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000249 break;
250
251 case Instruction::Xor:
252 // Bitwise ^ for all types
Misha Brukmanf4de7832003-08-05 16:01:50 +0000253 S += ConstantArithExprToString(CE, target, ") ^ (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000254 break;
255
Vikram S. Advee99941a2002-08-22 02:58:36 +0000256 default:
257 assert(0 && "Unsupported operator in ConstantExprToString()");
258 break;
259 }
260
261 return S;
262 }
263
264 // valToExprString - Helper function for ConstantExprToString().
265 // Appends result to argument string S.
266 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000267 std::string valToExprString(const Value* V, const TargetMachine& target) {
268 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000269 bool failed = false;
270 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
271
272 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Misha Brukmanf4de7832003-08-05 16:01:50 +0000273 S += std::string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000274 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
275 S += itostr(CI->getValue());
276 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
277 S += utostr(CI->getValue());
278 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
279 S += ftostr(CFP->getValue());
280 else if (isa<ConstantPointerNull>(CV))
281 S += "0";
282 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000283 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000284 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
285 S += ConstantExprToString(CE, target);
286 else
287 failed = true;
288
289 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
290 S += getID(GV);
291 }
292 else
293 failed = true;
294
295 if (failed) {
296 assert(0 && "Cannot convert value to string");
297 S += "<illegal-value>";
298 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000299 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000300 }
301
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000302};
303
304
305
306//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000307// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000308//===----------------------------------------------------------------------===//
309
Chris Lattnerf57b8452002-04-27 06:56:12 +0000310struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000311 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000312 : AsmPrinter(os, t) {}
313
Chris Lattner96c466b2002-04-29 14:57:45 +0000314 const char *getPassName() const {
315 return "Output Sparc Assembly for Functions";
316 }
317
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000318 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000319 startModule(M);
320 return false;
321 }
322
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000323 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000324 startFunction(F);
325 emitFunction(F);
326 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000327 return false;
328 }
329
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000330 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000331 endModule();
332 return false;
333 }
334
Chris Lattner97e52e42002-04-28 21:27:06 +0000335 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
336 AU.setPreservesAll();
337 }
338
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000339 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000340private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000341 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000342 void emitMachineInst(const MachineInstr *MI);
343
344 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000345 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000346
347 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
348 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000349
Chris Lattnere88f78c2001-09-19 13:47:27 +0000350 unsigned getOperandMask(unsigned Opcode) {
351 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000352 case V9::SUBccr:
353 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000354 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000355 default: return 0; // By default, don't hack operands...
356 }
357 }
358};
359
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000360inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000361SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
362 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000363 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000364 case V9::JMPLCALLr:
365 case V9::JMPLCALLi:
366 case V9::JMPLRETr:
367 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000368 return (opNum == 0);
369 default:
370 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000371 }
372}
373
374
375inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000376SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
377 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000378 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
379 return (opNum == 0);
380 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
381 return (opNum == 1);
382 else
383 return false;
384}
385
386
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000387#define PrintOp1PlusOp2(mop1, mop2, opCode) \
388 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000390 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000391
392unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000393SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000394 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000395{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000396 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000397
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000398 if (OpIsBranchTargetLabel(MI, opNum))
399 {
400 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
401 return 2;
402 }
403 else if (OpIsMemoryAddressBase(MI, opNum))
404 {
405 toAsm << "[";
406 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
407 toAsm << "]";
408 return 2;
409 }
410 else
411 {
412 printOneOperand(mop, MI->getOpCode());
413 return 1;
414 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415}
416
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000418SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
419 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000420{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000421 bool needBitsFlag = true;
422
423 if (mop.opHiBits32())
424 toAsm << "%lm(";
425 else if (mop.opLoBits32())
426 toAsm << "%lo(";
427 else if (mop.opHiBits64())
428 toAsm << "%hh(";
429 else if (mop.opLoBits64())
430 toAsm << "%hm(";
431 else
432 needBitsFlag = false;
433
Chris Lattner133f0792002-10-28 04:45:29 +0000434 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000435 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000436 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000437 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000438 case MachineOperand::MO_MachineRegister:
439 {
440 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000441
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000442 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
443 // better to print code with NULL registers than to die
444 toAsm << "<NULL VALUE>";
445 } else {
446 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
447 }
448 break;
449 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000450
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000451 case MachineOperand::MO_PCRelativeDisp:
452 {
453 const Value *Val = mop.getVRegValue();
454 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000455
Chris Lattner949a3622003-07-23 15:30:06 +0000456 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000457 toAsm << getID(BB);
458 else if (const Function *M = dyn_cast<Function>(Val))
459 toAsm << getID(M);
460 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
461 toAsm << getID(GV);
462 else if (const Constant *CV = dyn_cast<Constant>(Val))
463 toAsm << getID(CV);
464 else
465 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
466 break;
467 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000468
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000469 case MachineOperand::MO_SignExtendedImmed:
470 toAsm << mop.getImmedValue();
471 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000472
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000473 case MachineOperand::MO_UnextendedImmed:
474 toAsm << (uint64_t) mop.getImmedValue();
475 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000476
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000477 default:
478 toAsm << mop; // use dump field
479 break;
480 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000481
482 if (needBitsFlag)
483 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000484}
485
486
487void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000488SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000489{
490 unsigned Opcode = MI->getOpCode();
491
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000492 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000493 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000494
Chris Lattnerf44f9052002-10-29 17:35:41 +0000495 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000496
497 unsigned Mask = getOperandMask(Opcode);
498
499 bool NeedComma = false;
500 unsigned N = 1;
501 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
502 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000503 if (NeedComma) toAsm << ", "; // Handle comma outputing
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000504 NeedComma = true;
505 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000506 } else
507 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000508
509 toAsm << "\n";
510}
511
512void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000513SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000514{
515 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000516 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000517
518 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000519 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000520 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000521 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000522 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000523}
524
525void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000526SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000527{
Misha Brukmanf4de7832003-08-05 16:01:50 +0000528 std::string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000529 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000530 enterSection(AsmPrinter::Text);
531 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
532 //toAsm << "\t.type\t" << methName << ",#function\n";
533 toAsm << "\t.type\t" << methName << ", 2\n";
534 toAsm << methName << ":\n";
535
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000536 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000537 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000538 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000539 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000540
541 // Output a .size directive so the debugger knows the extents of the function
542 toAsm << ".EndOf_" << methName << ":\n\t.size "
543 << methName << ", .EndOf_"
544 << methName << "-" << methName << "\n";
545
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000546 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000547 toAsm << "\n\n";
548}
549
550} // End anonymous namespace
551
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000552Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000553 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000554}
555
556
557
558
559
560//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000561// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000562//===----------------------------------------------------------------------===//
563
564namespace {
565
566class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
567public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000568 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
569 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000570
Chris Lattner96c466b2002-04-29 14:57:45 +0000571 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
572
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000573 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000574 startModule(M);
575 emitGlobalsAndConstants(M);
576 endModule();
577 return false;
578 }
579
Chris Lattner97e52e42002-04-28 21:27:06 +0000580 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
581 AU.setPreservesAll();
582 }
583
584private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000585 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000586
Vikram S. Advefee76262002-10-13 00:32:18 +0000587 void printGlobalVariable (const GlobalVariable *GV);
588 void PrintZeroBytesToPad (int numBytes);
589 void printSingleConstantValue (const Constant* CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000590 void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
Misha Brukmanf4de7832003-08-05 16:01:50 +0000591 void printConstant (const Constant* CV, std::string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000592
Vikram S. Advefee76262002-10-13 00:32:18 +0000593 static void FoldConstants (const Module &M,
594 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000595};
596
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000597
598// Can we treat the specified array as a string? Only if it is an array of
599// ubytes or non-negative sbytes.
600//
Vikram S. Advefee76262002-10-13 00:32:18 +0000601static bool isStringCompatible(const ConstantArray *CVA) {
602 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000603 if (ETy == Type::UByteTy) return true;
604 if (ETy != Type::SByteTy) return false;
605
Vikram S. Advefee76262002-10-13 00:32:18 +0000606 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
607 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000608 return false;
609
610 return true;
611}
612
613// toOctal - Convert the low order bits of X into an octal letter
614static inline char toOctal(int X) {
615 return (X&7)+'0';
616}
617
618// getAsCString - Return the specified array as a C compatible string, only if
619// the predicate isStringCompatible is true.
620//
Misha Brukmanf4de7832003-08-05 16:01:50 +0000621static std::string getAsCString(const ConstantArray *CVA) {
Vikram S. Advefee76262002-10-13 00:32:18 +0000622 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000623
Misha Brukmanf4de7832003-08-05 16:01:50 +0000624 std::string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000625 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000626 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000627 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000628 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000629
Vikram S. Adve242a8082002-05-19 15:25:51 +0000630 if (C == '"') {
631 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000632 } else if (C == '\\') {
633 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000634 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000635 Result += C;
636 } else {
Vikram S. Adve00477cf2003-07-29 19:57:34 +0000637 Result += '\\'; // print all other chars as octal value
638 Result += toOctal(C >> 6);
639 Result += toOctal(C >> 3);
640 Result += toOctal(C >> 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000641 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000642 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000643 Result += "\"";
644
645 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000646}
647
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000648inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000649ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000650{
651 return (arrayType->getElementType() == Type::UByteTy ||
652 arrayType->getElementType() == Type::SByteTy);
653}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000654
Vikram S. Advee99941a2002-08-22 02:58:36 +0000655
Misha Brukmanf4de7832003-08-05 16:01:50 +0000656inline const std::string
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000657TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000658{
659 switch(type->getPrimitiveID())
660 {
661 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
662 return ".byte";
663 case Type::UShortTyID: case Type::ShortTyID:
664 return ".half";
665 case Type::UIntTyID: case Type::IntTyID:
666 return ".word";
667 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
668 return ".xword";
669 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000670 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000671 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000672 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000673 case Type::ArrayTyID:
674 if (ArrayTypeIsString((ArrayType*) type))
675 return ".ascii";
676 else
677 return "<InvaliDataTypeForPrinting>";
678 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000679 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000680 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000681}
682
Vikram S. Advefee76262002-10-13 00:32:18 +0000683// Get the size of the type
684//
685inline unsigned int
686TypeToSize(const Type* type, const TargetMachine& target)
687{
688 return target.findOptimalStorageSize(type);
689}
690
Vikram S. Adve21447222001-11-10 02:03:06 +0000691// Get the size of the constant for the given target.
692// If this is an unsized array, return 0.
693//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000694inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000695ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000696{
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000697 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
698 {
699 const ArrayType *aty = cast<ArrayType>(CVA->getType());
700 if (ArrayTypeIsString(aty))
701 return 1 + CVA->getNumOperands();
702 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000703
Vikram S. Advefee76262002-10-13 00:32:18 +0000704 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000705}
706
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000707// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000708// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
709//
710inline unsigned int
711SizeToAlignment(unsigned int size, const TargetMachine& target)
712{
713 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
714 if (size > (unsigned) cacheLineSize / 2)
715 return cacheLineSize;
716 else
717 for (unsigned sz=1; /*no condition*/; sz *= 2)
718 if (sz >= size)
719 return sz;
720}
721
722// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000723//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000724inline unsigned int
725TypeToAlignment(const Type* type, const TargetMachine& target)
726{
Vikram S. Advefee76262002-10-13 00:32:18 +0000727 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000728}
729
730// Get the size of the constant and then use SizeToAlignment.
731// Handles strings as a special case;
732inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000733ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000734{
Vikram S. Advefee76262002-10-13 00:32:18 +0000735 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
736 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
737 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000738
739 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000740}
741
742
Vikram S. Adve21447222001-11-10 02:03:06 +0000743// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000744void
Vikram S. Advefee76262002-10-13 00:32:18 +0000745SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000746{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000747 assert(CV->getType() != Type::VoidTy &&
748 CV->getType() != Type::TypeTy &&
749 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000750 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000751
Chris Lattnerf678dc62002-04-11 21:44:02 +0000752 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
753 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000754
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000755 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000756
Vikram S. Advedb685772003-07-30 12:54:47 +0000757 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
758 { // This is a constant address for a global variable or method.
759 // Use the name of the variable or method as the address value.
760 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
761 toAsm << getID(CPR->getValue()) << "\n";
762 }
763 else if (isa<ConstantPointerNull>(CV))
764 { // Null pointer value
765 toAsm << "0\n";
766 }
767 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
768 { // Constant expression built from operators, constants, and symbolic addrs
769 toAsm << ConstantExprToString(CE, Target) << "\n";
770 }
771 else if (CV->getType()->isPrimitiveType()) // Check primitive types last
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000772 {
773 if (CV->getType()->isFloatingPoint()) {
774 // FP Constants are printed as integer constants to avoid losing
775 // precision...
776 double Val = cast<ConstantFP>(CV)->getValue();
777 if (CV->getType() == Type::FloatTy) {
778 float FVal = (float)Val;
779 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
780 toAsm << *(unsigned int*)ProxyPtr;
781 } else if (CV->getType() == Type::DoubleTy) {
782 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
783 toAsm << *(uint64_t*)ProxyPtr;
784 } else {
785 assert(0 && "Unknown floating point type!");
786 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000787
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000788 toAsm << "\t! " << CV->getType()->getDescription()
789 << " value: " << Val << "\n";
790 } else {
791 WriteAsOperand(toAsm, CV, false, false) << "\n";
792 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000793 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000794 else
795 {
796 assert(0 && "Unknown elementary type for constant");
797 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000798}
799
Vikram S. Adve21447222001-11-10 02:03:06 +0000800void
Vikram S. Advefee76262002-10-13 00:32:18 +0000801SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000802{
Vikram S. Advefee76262002-10-13 00:32:18 +0000803 for ( ; numBytes >= 8; numBytes -= 8)
804 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
805
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000806 if (numBytes >= 4)
807 {
808 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
809 numBytes -= 4;
810 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000811
812 while (numBytes--)
813 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
814}
815
816// Print a constant value or values (it may be an aggregate).
817// Uses printSingleConstantValue() to print each individual value.
818void
819SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
Vikram S. Adve9e498242003-05-25 21:59:09 +0000820 int numPadBytesAfter /* = 0*/)
Vikram S. Advefee76262002-10-13 00:32:18 +0000821{
822 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
823
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000824 if (CVA && isStringCompatible(CVA))
825 { // print the string alone and return
826 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000827 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000828 else if (CVA)
829 { // Not a string. Print the values in successive locations
830 const std::vector<Use> &constValues = CVA->getValues();
831 for (unsigned i=0; i < constValues.size(); i++)
832 printConstantValueOnly(cast<Constant>(constValues[i].get()));
833 }
834 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
835 { // Print the fields in successive locations. Pad to align if needed!
836 const StructLayout *cvsLayout =
837 Target.getTargetData().getStructLayout(CVS->getType());
838 const std::vector<Use>& constValues = CVS->getValues();
839 unsigned sizeSoFar = 0;
840 for (unsigned i=0, N = constValues.size(); i < N; i++)
841 {
842 const Constant* field = cast<Constant>(constValues[i].get());
843
844 // Check if padding is needed and insert one or more 0s.
845 unsigned fieldSize =
846 Target.getTargetData().getTypeSize(field->getType());
847 int padSize = ((i == N-1? cvsLayout->StructSize
848 : cvsLayout->MemberOffsets[i+1])
849 - cvsLayout->MemberOffsets[i]) - fieldSize;
850 sizeSoFar += (fieldSize + padSize);
851
852 // Now print the actual field value
853 printConstantValueOnly(field, padSize);
854 }
855 assert(sizeSoFar == cvsLayout->StructSize &&
856 "Layout of constant struct may be incorrect!");
857 }
858 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000859 printSingleConstantValue(CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000860
861 if (numPadBytesAfter)
862 PrintZeroBytesToPad(numPadBytesAfter);
Vikram S. Adve21447222001-11-10 02:03:06 +0000863}
864
865// Print a constant (which may be an aggregate) prefixed by all the
866// appropriate directives. Uses printConstantValueOnly() to print the
867// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000868void
Misha Brukmanf4de7832003-08-05 16:01:50 +0000869SparcModuleAsmPrinter::printConstant(const Constant* CV, std::string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000870{
871 if (valID.length() == 0)
872 valID = getID(CV);
873
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000874 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000875
876 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000877 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000878 if (CVA && isStringCompatible(CVA))
879 { // print it as a string and return
880 toAsm << valID << ":\n";
881 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
882 return;
883 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000884
Chris Lattner697954c2002-01-20 22:54:45 +0000885 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000886
887 unsigned int constSize = ConstantToSize(CV, Target);
888 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000889 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000890
Chris Lattner697954c2002-01-20 22:54:45 +0000891 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000892
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000893 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000894}
895
896
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000897void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000898 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000899 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
900 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000901 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000902 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000903 MC.insert(pool.begin(), pool.end());
904 }
905}
906
907void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000908{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000909 if (GV->hasExternalLinkage())
910 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000911
Vikram S. Advefee76262002-10-13 00:32:18 +0000912 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000913 printConstant(GV->getInitializer(), getID(GV));
914 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000915 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
916 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000917 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000918 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000919 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000920 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000921 }
922}
923
924
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000925void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000926 // First, get the constants there were marked by the code generator for
927 // inclusion in the assembly code data area and fold them all into a
928 // single constant pool since there may be lots of duplicates. Also,
929 // lets force these constants into the slot table so that we can get
930 // unique names for unnamed constants also.
931 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000932 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000933 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000934
Chris Lattner637ed862002-08-07 21:39:48 +0000935 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000936 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000937 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000938 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000939 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000940
941 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000942 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
943 if (! GI->isExternal()) {
944 assert(GI->hasInitializer());
945 if (GI->isConstant())
946 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
947 else if (GI->getInitializer()->isNullValue())
948 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
949 else
950 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
951
952 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000953 }
Chris Lattner637ed862002-08-07 21:39:48 +0000954
Chris Lattner697954c2002-01-20 22:54:45 +0000955 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000956}
957
Chris Lattnere88f78c2001-09-19 13:47:27 +0000958} // End anonymous namespace
959
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000960Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000961 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000962}