blob: bbcb12e5284c1bc0ef060de93c3a1d9e231b84b2 [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"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000024#include "Support/Statistic.h"
Misha Brukmanf4de7832003-08-05 16:01:50 +000025#include "SparcInternals.h"
26#include <string>
Chris Lattnere88f78c2001-09-19 13:47:27 +000027
28namespace {
29
Brian Gaeke2c9b9132003-10-06 15:41:21 +000030Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
31
Vikram S. Adved198c472002-03-18 03:07:26 +000032class GlobalIdTable: public Annotation {
33 static AnnotationID AnnotId;
34 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000035
Chris Lattner09ff1122002-07-24 21:21:32 +000036 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000037 typedef ValIdMap::const_iterator ValIdMapConstIterator;
38 typedef ValIdMap:: iterator ValIdMapIterator;
39public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000040 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000041 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000042
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000043 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000044};
45
46AnnotationID GlobalIdTable::AnnotId =
47 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
48
49//===---------------------------------------------------------------------===//
50// Code Shared By the two printer passes, as a mixin
51//===---------------------------------------------------------------------===//
52
53class AsmPrinter {
54 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000055public:
Chris Lattner697954c2002-01-20 22:54:45 +000056 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000057 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000058
Chris Lattnere88f78c2001-09-19 13:47:27 +000059 enum Sections {
60 Unknown,
61 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000062 ReadOnlyData,
63 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000064 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000065 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000066
Chris Lattner59ba1092002-02-04 15:53:23 +000067 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000068 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
69
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000070 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000071 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000072 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000073 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000074 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000075 idTable = new GlobalIdTable(&M);
76 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000077 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000078 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000079 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000080 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000081 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000082 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000083 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000084 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000085 }
86 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000087 }
88
Chris Lattner8b1b4e22002-07-16 18:35:16 +000089 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000090 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000091 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
92 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000093 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000094
Chris Lattnere88f78c2001-09-19 13:47:27 +000095 // enterSection - Use this method to enter a different section of the output
Misha Brukman5560c9d2003-08-18 14:43:39 +000096 // executable. This is used to only output necessary section transitions.
Chris Lattnere88f78c2001-09-19 13:47:27 +000097 //
98 void enterSection(enum Sections S) {
Misha Brukman5560c9d2003-08-18 14:43:39 +000099 if (S == CurSection) return; // Only switch section if necessary
Chris Lattnere88f78c2001-09-19 13:47:27 +0000100 CurSection = S;
101
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000102 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000103 switch (S)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000104 {
105 default: assert(0 && "Bad section name!");
106 case Text: toAsm << "\".text\""; break;
107 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
108 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
109 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
110 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000111 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000112 }
113
Misha Brukmanf4de7832003-08-05 16:01:50 +0000114 static std::string getValidSymbolName(const std::string &S) {
115 std::string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000116
117 // Symbol names in Sparc assembly language have these rules:
118 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
119 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000121 if (isdigit(S[0]))
122 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000123
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000124 for (unsigned i = 0; i < S.size(); ++i)
125 {
126 char C = S[i];
127 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
128 Result += C;
129 else
130 {
131 Result += '_';
132 Result += char('0' + ((unsigned char)C >> 4));
133 Result += char('0' + (C & 0xF));
134 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000135 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000136 return Result;
137 }
138
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000140 // the name of the identifier if possible (qualified by the type), and
141 // use a numbered value based on prefix otherwise.
142 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000143 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000144 std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
145 std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000146
Misha Brukmanf4de7832003-08-05 16:01:50 +0000147 Result += V->hasName() ? V->getName() : std::string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000148
Vikram S. Adved198c472002-03-18 03:07:26 +0000149 // Qualify all internal names with a unique id.
150 if (!isExternal(V)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000151 int valId = idTable->Table.getSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000152 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000153 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
154 if (I == idTable->valToIdMap.end())
155 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000156 else
157 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000158 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000159 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000160
161 // Replace or prefix problem characters in the name
162 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000164
165 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000166 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000167
Chris Lattnere88f78c2001-09-19 13:47:27 +0000168 // getID Wrappers - Ensure consistent usage...
Misha Brukmanf4de7832003-08-05 16:01:50 +0000169 std::string getID(const Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000170 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000171 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000172 std::string getID(const BasicBlock *BB) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000173 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
174 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000175 std::string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000176 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000177 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000178 std::string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000179 return getID(CV, "LLVMConst_", ".C_");
180 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000181 std::string getID(const GlobalValue *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000182 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
183 return getID(V);
184 else if (const Function *F = dyn_cast<Function>(GV))
185 return getID(F);
186 assert(0 && "Unexpected type of GlobalValue!");
187 return "";
188 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000189
Misha Brukmanf4de7832003-08-05 16:01:50 +0000190 // Combines expressions
191 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
192 const TargetMachine &TM,
193 const std::string &op) {
194 return "(" + valToExprString(CE->getOperand(0), TM) + op
195 + valToExprString(CE->getOperand(1), TM) + ")";
196 }
197
Vikram S. Adve537a8772002-09-05 18:28:10 +0000198 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
199 // and return this as a string.
Misha Brukmanf4de7832003-08-05 16:01:50 +0000200 std::string ConstantExprToString(const ConstantExpr* CE,
201 const TargetMachine& target) {
202 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000203 switch(CE->getOpcode()) {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000204 case Instruction::GetElementPtr:
205 { // generate a symbolic expression for the byte address
206 const Value* ptrVal = CE->getOperand(0);
207 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Misha Brukmanf4de7832003-08-05 16:01:50 +0000208 const TargetData &TD = target.getTargetData();
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000209 S += "(" + valToExprString(ptrVal, target) + ") + ("
210 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
211 break;
212 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000213
Vikram S. Adve537a8772002-09-05 18:28:10 +0000214 case Instruction::Cast:
215 // Support only non-converting casts for now, i.e., a no-op.
216 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000217 assert(target.getTargetData().getTypeSize(CE->getType()) ==
218 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000219 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
220 break;
221
222 case Instruction::Add:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000223 S += ConstantArithExprToString(CE, target, ") + (");
Vikram S. Adve537a8772002-09-05 18:28:10 +0000224 break;
225
Vikram S. Adve72666e62003-08-01 15:55:53 +0000226 case Instruction::Sub:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000227 S += ConstantArithExprToString(CE, target, ") - (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000228 break;
229
230 case Instruction::Mul:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000231 S += ConstantArithExprToString(CE, target, ") * (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000232 break;
233
234 case Instruction::Div:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000235 S += ConstantArithExprToString(CE, target, ") / (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000236 break;
237
238 case Instruction::Rem:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000239 S += ConstantArithExprToString(CE, target, ") % (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000240 break;
241
242 case Instruction::And:
243 // Logical && for booleans; bitwise & otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000244 S += ConstantArithExprToString(CE, target,
245 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000246 break;
247
248 case Instruction::Or:
249 // Logical || for booleans; bitwise | otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000250 S += ConstantArithExprToString(CE, target,
251 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000252 break;
253
254 case Instruction::Xor:
255 // Bitwise ^ for all types
Misha Brukmanf4de7832003-08-05 16:01:50 +0000256 S += ConstantArithExprToString(CE, target, ") ^ (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000257 break;
258
Vikram S. Advee99941a2002-08-22 02:58:36 +0000259 default:
260 assert(0 && "Unsupported operator in ConstantExprToString()");
261 break;
262 }
263
264 return S;
265 }
266
267 // valToExprString - Helper function for ConstantExprToString().
268 // Appends result to argument string S.
269 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000270 std::string valToExprString(const Value* V, const TargetMachine& target) {
271 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000272 bool failed = false;
273 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
274
275 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Misha Brukmanf4de7832003-08-05 16:01:50 +0000276 S += std::string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000277 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
278 S += itostr(CI->getValue());
279 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
280 S += utostr(CI->getValue());
281 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
282 S += ftostr(CFP->getValue());
283 else if (isa<ConstantPointerNull>(CV))
284 S += "0";
285 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000286 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000287 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
288 S += ConstantExprToString(CE, target);
289 else
290 failed = true;
291
292 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
293 S += getID(GV);
294 }
295 else
296 failed = true;
297
298 if (failed) {
299 assert(0 && "Cannot convert value to string");
300 S += "<illegal-value>";
301 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000302 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000303 }
304
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000305};
306
307
308
309//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000310// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000311//===----------------------------------------------------------------------===//
312
Chris Lattnerf57b8452002-04-27 06:56:12 +0000313struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000314 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000315 : AsmPrinter(os, t) {}
316
Chris Lattner96c466b2002-04-29 14:57:45 +0000317 const char *getPassName() const {
318 return "Output Sparc Assembly for Functions";
319 }
320
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000321 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 startModule(M);
323 return false;
324 }
325
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000326 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000327 startFunction(F);
328 emitFunction(F);
329 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000330 return false;
331 }
332
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000333 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000334 endModule();
335 return false;
336 }
337
Chris Lattner97e52e42002-04-28 21:27:06 +0000338 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
339 AU.setPreservesAll();
340 }
341
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000342 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000343private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000344 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345 void emitMachineInst(const MachineInstr *MI);
346
347 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000348 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000349
350 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
351 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000352
Chris Lattnere88f78c2001-09-19 13:47:27 +0000353 unsigned getOperandMask(unsigned Opcode) {
354 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000355 case V9::SUBccr:
356 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000357 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000358 default: return 0; // By default, don't hack operands...
359 }
360 }
361};
362
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000363inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000364SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
365 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000366 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000367 case V9::JMPLCALLr:
368 case V9::JMPLCALLi:
369 case V9::JMPLRETr:
370 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000371 return (opNum == 0);
372 default:
373 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000374 }
375}
376
377
378inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000379SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
380 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000381 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
382 return (opNum == 0);
383 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
384 return (opNum == 1);
385 else
386 return false;
387}
388
389
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000390#define PrintOp1PlusOp2(mop1, mop2, opCode) \
391 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000393 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000394
395unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000396SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000397 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000398{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000399 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000400
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000401 if (OpIsBranchTargetLabel(MI, opNum))
402 {
403 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
404 return 2;
405 }
406 else if (OpIsMemoryAddressBase(MI, opNum))
407 {
408 toAsm << "[";
409 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
410 toAsm << "]";
411 return 2;
412 }
413 else
414 {
415 printOneOperand(mop, MI->getOpCode());
416 return 1;
417 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000418}
419
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000420void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000421SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
422 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000423{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000424 bool needBitsFlag = true;
425
426 if (mop.opHiBits32())
427 toAsm << "%lm(";
428 else if (mop.opLoBits32())
429 toAsm << "%lo(";
430 else if (mop.opHiBits64())
431 toAsm << "%hh(";
432 else if (mop.opLoBits64())
433 toAsm << "%hm(";
434 else
435 needBitsFlag = false;
436
Chris Lattner133f0792002-10-28 04:45:29 +0000437 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000438 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000439 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000440 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000441 case MachineOperand::MO_MachineRegister:
442 {
443 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000444
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000445 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
446 // better to print code with NULL registers than to die
447 toAsm << "<NULL VALUE>";
448 } else {
449 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
450 }
451 break;
452 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000453
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000454 case MachineOperand::MO_PCRelativeDisp:
455 {
456 const Value *Val = mop.getVRegValue();
457 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000458
Chris Lattner949a3622003-07-23 15:30:06 +0000459 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000460 toAsm << getID(BB);
461 else if (const Function *M = dyn_cast<Function>(Val))
462 toAsm << getID(M);
463 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
464 toAsm << getID(GV);
465 else if (const Constant *CV = dyn_cast<Constant>(Val))
466 toAsm << getID(CV);
467 else
468 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
469 break;
470 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000471
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000472 case MachineOperand::MO_SignExtendedImmed:
473 toAsm << mop.getImmedValue();
474 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000475
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000476 case MachineOperand::MO_UnextendedImmed:
477 toAsm << (uint64_t) mop.getImmedValue();
478 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000479
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000480 default:
481 toAsm << mop; // use dump field
482 break;
483 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000484
485 if (needBitsFlag)
486 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000487}
488
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000489void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000490SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000491{
492 unsigned Opcode = MI->getOpCode();
493
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000494 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000495 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000496
Chris Lattnerf44f9052002-10-29 17:35:41 +0000497 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000498
499 unsigned Mask = getOperandMask(Opcode);
500
501 bool NeedComma = false;
502 unsigned N = 1;
503 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
504 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Misha Brukman8b2fe192003-09-23 17:27:28 +0000505 if (NeedComma) toAsm << ", "; // Handle comma outputting
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000506 NeedComma = true;
507 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000508 } else
509 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000510
511 toAsm << "\n";
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000512 ++EmittedInsts;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000513}
514
515void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000516SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000517{
518 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000519 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000520
521 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000522 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000523 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000524 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000525 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000526}
527
528void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000529SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000530{
Misha Brukmanf4de7832003-08-05 16:01:50 +0000531 std::string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000532 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000533 enterSection(AsmPrinter::Text);
534 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
535 //toAsm << "\t.type\t" << methName << ",#function\n";
536 toAsm << "\t.type\t" << methName << ", 2\n";
537 toAsm << methName << ":\n";
538
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000539 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000540 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000541 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000542 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000543
544 // Output a .size directive so the debugger knows the extents of the function
545 toAsm << ".EndOf_" << methName << ":\n\t.size "
546 << methName << ", .EndOf_"
547 << methName << "-" << methName << "\n";
548
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000549 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000550 toAsm << "\n\n";
551}
552
553} // End anonymous namespace
554
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000555Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000556 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000557}
558
559
560
561
562
563//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000564// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000565//===----------------------------------------------------------------------===//
566
567namespace {
568
569class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
570public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000571 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
572 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000573
Chris Lattner96c466b2002-04-29 14:57:45 +0000574 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
575
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000576 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000577 startModule(M);
578 emitGlobalsAndConstants(M);
579 endModule();
580 return false;
581 }
582
Chris Lattner97e52e42002-04-28 21:27:06 +0000583 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
584 AU.setPreservesAll();
585 }
586
587private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000588 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000589
Vikram S. Advefee76262002-10-13 00:32:18 +0000590 void printGlobalVariable (const GlobalVariable *GV);
591 void PrintZeroBytesToPad (int numBytes);
592 void printSingleConstantValue (const Constant* CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000593 void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
Misha Brukmanf4de7832003-08-05 16:01:50 +0000594 void printConstant (const Constant* CV, std::string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000595
Vikram S. Advefee76262002-10-13 00:32:18 +0000596 static void FoldConstants (const Module &M,
597 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000598};
599
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000600
601// Can we treat the specified array as a string? Only if it is an array of
602// ubytes or non-negative sbytes.
603//
Vikram S. Advefee76262002-10-13 00:32:18 +0000604static bool isStringCompatible(const ConstantArray *CVA) {
605 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000606 if (ETy == Type::UByteTy) return true;
607 if (ETy != Type::SByteTy) return false;
608
Vikram S. Advefee76262002-10-13 00:32:18 +0000609 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
610 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000611 return false;
612
613 return true;
614}
615
616// toOctal - Convert the low order bits of X into an octal letter
617static inline char toOctal(int X) {
618 return (X&7)+'0';
619}
620
621// getAsCString - Return the specified array as a C compatible string, only if
622// the predicate isStringCompatible is true.
623//
Misha Brukmanf4de7832003-08-05 16:01:50 +0000624static std::string getAsCString(const ConstantArray *CVA) {
Vikram S. Advefee76262002-10-13 00:32:18 +0000625 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000626
Misha Brukmanf4de7832003-08-05 16:01:50 +0000627 std::string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000628 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000629 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000630 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000631 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000632
Vikram S. Adve242a8082002-05-19 15:25:51 +0000633 if (C == '"') {
634 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000635 } else if (C == '\\') {
636 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000637 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000638 Result += C;
639 } else {
Vikram S. Adve00477cf2003-07-29 19:57:34 +0000640 Result += '\\'; // print all other chars as octal value
641 Result += toOctal(C >> 6);
642 Result += toOctal(C >> 3);
643 Result += toOctal(C >> 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000644 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000645 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000646 Result += "\"";
647
648 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000649}
650
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000651inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000652ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000653{
654 return (arrayType->getElementType() == Type::UByteTy ||
655 arrayType->getElementType() == Type::SByteTy);
656}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000657
Vikram S. Advee99941a2002-08-22 02:58:36 +0000658
Misha Brukmanf4de7832003-08-05 16:01:50 +0000659inline const std::string
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000660TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000661{
662 switch(type->getPrimitiveID())
663 {
664 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
665 return ".byte";
666 case Type::UShortTyID: case Type::ShortTyID:
667 return ".half";
668 case Type::UIntTyID: case Type::IntTyID:
669 return ".word";
670 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
671 return ".xword";
672 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000673 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000674 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000675 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000676 case Type::ArrayTyID:
677 if (ArrayTypeIsString((ArrayType*) type))
678 return ".ascii";
679 else
680 return "<InvaliDataTypeForPrinting>";
681 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000682 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000683 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000684}
685
Vikram S. Advefee76262002-10-13 00:32:18 +0000686// Get the size of the type
687//
688inline unsigned int
689TypeToSize(const Type* type, const TargetMachine& target)
690{
691 return target.findOptimalStorageSize(type);
692}
693
Vikram S. Adve21447222001-11-10 02:03:06 +0000694// Get the size of the constant for the given target.
695// If this is an unsized array, return 0.
696//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000697inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000698ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000699{
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000700 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
701 {
702 const ArrayType *aty = cast<ArrayType>(CVA->getType());
703 if (ArrayTypeIsString(aty))
704 return 1 + CVA->getNumOperands();
705 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000706
Vikram S. Advefee76262002-10-13 00:32:18 +0000707 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000708}
709
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000710// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000711// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
712//
713inline unsigned int
714SizeToAlignment(unsigned int size, const TargetMachine& target)
715{
716 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
717 if (size > (unsigned) cacheLineSize / 2)
718 return cacheLineSize;
719 else
720 for (unsigned sz=1; /*no condition*/; sz *= 2)
721 if (sz >= size)
722 return sz;
723}
724
725// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000726//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000727inline unsigned int
728TypeToAlignment(const Type* type, const TargetMachine& target)
729{
Vikram S. Advefee76262002-10-13 00:32:18 +0000730 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000731}
732
733// Get the size of the constant and then use SizeToAlignment.
734// Handles strings as a special case;
735inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000736ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000737{
Vikram S. Advefee76262002-10-13 00:32:18 +0000738 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
739 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
740 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000741
742 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000743}
744
745
Vikram S. Adve21447222001-11-10 02:03:06 +0000746// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000747void
Vikram S. Advefee76262002-10-13 00:32:18 +0000748SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000749{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000750 assert(CV->getType() != Type::VoidTy &&
751 CV->getType() != Type::TypeTy &&
752 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000753 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000754
Chris Lattnerf678dc62002-04-11 21:44:02 +0000755 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
756 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000757
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000758 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000759
Vikram S. Advedb685772003-07-30 12:54:47 +0000760 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
761 { // This is a constant address for a global variable or method.
762 // Use the name of the variable or method as the address value.
763 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
764 toAsm << getID(CPR->getValue()) << "\n";
765 }
766 else if (isa<ConstantPointerNull>(CV))
767 { // Null pointer value
768 toAsm << "0\n";
769 }
770 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
771 { // Constant expression built from operators, constants, and symbolic addrs
772 toAsm << ConstantExprToString(CE, Target) << "\n";
773 }
774 else if (CV->getType()->isPrimitiveType()) // Check primitive types last
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000775 {
776 if (CV->getType()->isFloatingPoint()) {
777 // FP Constants are printed as integer constants to avoid losing
778 // precision...
779 double Val = cast<ConstantFP>(CV)->getValue();
780 if (CV->getType() == Type::FloatTy) {
781 float FVal = (float)Val;
782 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
783 toAsm << *(unsigned int*)ProxyPtr;
784 } else if (CV->getType() == Type::DoubleTy) {
785 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
786 toAsm << *(uint64_t*)ProxyPtr;
787 } else {
788 assert(0 && "Unknown floating point type!");
789 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000790
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000791 toAsm << "\t! " << CV->getType()->getDescription()
792 << " value: " << Val << "\n";
793 } else {
794 WriteAsOperand(toAsm, CV, false, false) << "\n";
795 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000796 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000797 else
798 {
799 assert(0 && "Unknown elementary type for constant");
800 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000801}
802
Vikram S. Adve21447222001-11-10 02:03:06 +0000803void
Vikram S. Advefee76262002-10-13 00:32:18 +0000804SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000805{
Vikram S. Advefee76262002-10-13 00:32:18 +0000806 for ( ; numBytes >= 8; numBytes -= 8)
807 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
808
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000809 if (numBytes >= 4)
810 {
811 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
812 numBytes -= 4;
813 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000814
815 while (numBytes--)
816 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
817}
818
819// Print a constant value or values (it may be an aggregate).
820// Uses printSingleConstantValue() to print each individual value.
821void
822SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
Vikram S. Adve9e498242003-05-25 21:59:09 +0000823 int numPadBytesAfter /* = 0*/)
Vikram S. Advefee76262002-10-13 00:32:18 +0000824{
825 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
826
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000827 if (CVA && isStringCompatible(CVA))
828 { // print the string alone and return
829 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000830 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000831 else if (CVA)
832 { // Not a string. Print the values in successive locations
833 const std::vector<Use> &constValues = CVA->getValues();
834 for (unsigned i=0; i < constValues.size(); i++)
835 printConstantValueOnly(cast<Constant>(constValues[i].get()));
836 }
837 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
838 { // Print the fields in successive locations. Pad to align if needed!
839 const StructLayout *cvsLayout =
840 Target.getTargetData().getStructLayout(CVS->getType());
841 const std::vector<Use>& constValues = CVS->getValues();
842 unsigned sizeSoFar = 0;
843 for (unsigned i=0, N = constValues.size(); i < N; i++)
844 {
845 const Constant* field = cast<Constant>(constValues[i].get());
846
847 // Check if padding is needed and insert one or more 0s.
848 unsigned fieldSize =
849 Target.getTargetData().getTypeSize(field->getType());
850 int padSize = ((i == N-1? cvsLayout->StructSize
851 : cvsLayout->MemberOffsets[i+1])
852 - cvsLayout->MemberOffsets[i]) - fieldSize;
853 sizeSoFar += (fieldSize + padSize);
854
855 // Now print the actual field value
856 printConstantValueOnly(field, padSize);
857 }
858 assert(sizeSoFar == cvsLayout->StructSize &&
859 "Layout of constant struct may be incorrect!");
860 }
861 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000862 printSingleConstantValue(CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000863
864 if (numPadBytesAfter)
865 PrintZeroBytesToPad(numPadBytesAfter);
Vikram S. Adve21447222001-11-10 02:03:06 +0000866}
867
868// Print a constant (which may be an aggregate) prefixed by all the
869// appropriate directives. Uses printConstantValueOnly() to print the
870// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000871void
Misha Brukmanf4de7832003-08-05 16:01:50 +0000872SparcModuleAsmPrinter::printConstant(const Constant* CV, std::string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000873{
874 if (valID.length() == 0)
875 valID = getID(CV);
876
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000877 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000878
879 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000880 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000881 if (CVA && isStringCompatible(CVA))
882 { // print it as a string and return
883 toAsm << valID << ":\n";
884 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
885 return;
886 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000887
Chris Lattner697954c2002-01-20 22:54:45 +0000888 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000889
890 unsigned int constSize = ConstantToSize(CV, Target);
891 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000892 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000893
Chris Lattner697954c2002-01-20 22:54:45 +0000894 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000895
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000896 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000897}
898
899
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000900void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000901 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000902 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
903 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000904 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000905 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000906 MC.insert(pool.begin(), pool.end());
907 }
908}
909
910void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000911{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000912 if (GV->hasExternalLinkage())
913 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000914
Vikram S. Advefee76262002-10-13 00:32:18 +0000915 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000916 printConstant(GV->getInitializer(), getID(GV));
917 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000918 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
919 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000920 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000921 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000922 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000923 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000924 }
925}
926
927
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000928void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000929 // First, get the constants there were marked by the code generator for
930 // inclusion in the assembly code data area and fold them all into a
931 // single constant pool since there may be lots of duplicates. Also,
932 // lets force these constants into the slot table so that we can get
933 // unique names for unnamed constants also.
934 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000935 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000936 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000937
Chris Lattner637ed862002-08-07 21:39:48 +0000938 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000939 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000940 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000941 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000942 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000943
944 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000945 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
946 if (! GI->isExternal()) {
947 assert(GI->hasInitializer());
948 if (GI->isConstant())
949 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
950 else if (GI->getInitializer()->isNullValue())
951 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
952 else
953 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
954
955 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000956 }
Chris Lattner637ed862002-08-07 21:39:48 +0000957
Chris Lattner697954c2002-01-20 22:54:45 +0000958 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000959}
960
Chris Lattnere88f78c2001-09-19 13:47:27 +0000961} // End anonymous namespace
962
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000963Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000964 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000965}