blob: 50249e1e3565d74a187ee0367c5f05e26d9eeecc [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere88f78c2001-09-19 13:47:27 +00009//
Misha Brukman5560c9d2003-08-18 14:43:39 +000010// This file implements all of the stuff necessary to output a .s file from
Chris Lattnere88f78c2001-09-19 13:47:27 +000011// LLVM. The code in this file assumes that the specified module has already
12// been compiled into the internal data structures of the Module.
13//
Chris Lattnerf57b8452002-04-27 06:56:12 +000014// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
15// The FunctionPass is pipelined together with all of the rest of the code
16// generation stages, and the Pass runs at the end to emit code for global
17// variables and such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattnere88f78c2001-09-19 13:47:27 +000021#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000022#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd0fe5f52002-12-28 20:15:01 +000023#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000024#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000025#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000026#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000027#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000028#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000029#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000030#include "Support/StringExtras.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000031#include "Support/Statistic.h"
Misha Brukmanf4de7832003-08-05 16:01:50 +000032#include "SparcInternals.h"
33#include <string>
Chris Lattnere88f78c2001-09-19 13:47:27 +000034
35namespace {
36
Brian Gaeke2c9b9132003-10-06 15:41:21 +000037Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Vikram S. Adved198c472002-03-18 03:07:26 +000039class GlobalIdTable: public Annotation {
40 static AnnotationID AnnotId;
41 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000042
Chris Lattner09ff1122002-07-24 21:21:32 +000043 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000044 typedef ValIdMap::const_iterator ValIdMapConstIterator;
45 typedef ValIdMap:: iterator ValIdMapIterator;
46public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000047 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000048 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000049
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000050 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000051};
52
53AnnotationID GlobalIdTable::AnnotId =
54 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
55
56//===---------------------------------------------------------------------===//
57// Code Shared By the two printer passes, as a mixin
58//===---------------------------------------------------------------------===//
59
60class AsmPrinter {
61 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000062public:
Chris Lattner697954c2002-01-20 22:54:45 +000063 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000064 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000065
Chris Lattnere88f78c2001-09-19 13:47:27 +000066 enum Sections {
67 Unknown,
68 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000069 ReadOnlyData,
70 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000071 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000072 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000073
Chris Lattner59ba1092002-02-04 15:53:23 +000074 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000075 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
76
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000077 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000078 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000079 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000081 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000082 idTable = new GlobalIdTable(&M);
83 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000084 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000085 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000086 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000087 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000088 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000089 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000090 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000091 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000092 }
93 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000094 }
95
Chris Lattner8b1b4e22002-07-16 18:35:16 +000096 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000097 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000098 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
99 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +0000100 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000101
Chris Lattnere88f78c2001-09-19 13:47:27 +0000102 // enterSection - Use this method to enter a different section of the output
Misha Brukman5560c9d2003-08-18 14:43:39 +0000103 // executable. This is used to only output necessary section transitions.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000104 //
105 void enterSection(enum Sections S) {
Misha Brukman5560c9d2003-08-18 14:43:39 +0000106 if (S == CurSection) return; // Only switch section if necessary
Chris Lattnere88f78c2001-09-19 13:47:27 +0000107 CurSection = S;
108
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000109 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000110 switch (S)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000111 {
112 default: assert(0 && "Bad section name!");
113 case Text: toAsm << "\".text\""; break;
114 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
115 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
116 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
117 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000118 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000119 }
120
Misha Brukmanf4de7832003-08-05 16:01:50 +0000121 static std::string getValidSymbolName(const std::string &S) {
122 std::string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000123
124 // Symbol names in Sparc assembly language have these rules:
125 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
126 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000127 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000128 if (isdigit(S[0]))
129 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000130
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000131 for (unsigned i = 0; i < S.size(); ++i)
132 {
133 char C = S[i];
134 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
135 Result += C;
136 else
137 {
138 Result += '_';
139 Result += char('0' + ((unsigned char)C >> 4));
140 Result += char('0' + (C & 0xF));
141 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000142 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000143 return Result;
144 }
145
Chris Lattnere88f78c2001-09-19 13:47:27 +0000146 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000147 // the name of the identifier if possible (qualified by the type), and
148 // use a numbered value based on prefix otherwise.
149 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000150 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000151 std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
152 std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000153
Misha Brukmanf4de7832003-08-05 16:01:50 +0000154 Result += V->hasName() ? V->getName() : std::string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000155
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 // Qualify all internal names with a unique id.
157 if (!isExternal(V)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000158 int valId = idTable->Table.getSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000159 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000160 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
161 if (I == idTable->valToIdMap.end())
162 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000163 else
164 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000165 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000166 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000167
168 // Replace or prefix problem characters in the name
169 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000170 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000171
172 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000173 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174
Chris Lattnere88f78c2001-09-19 13:47:27 +0000175 // getID Wrappers - Ensure consistent usage...
Misha Brukmanf4de7832003-08-05 16:01:50 +0000176 std::string getID(const Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000177 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000178 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000179 std::string getID(const BasicBlock *BB) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000180 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
181 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000182 std::string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000183 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000184 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000185 std::string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000186 return getID(CV, "LLVMConst_", ".C_");
187 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000188 std::string getID(const GlobalValue *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000189 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
190 return getID(V);
191 else if (const Function *F = dyn_cast<Function>(GV))
192 return getID(F);
193 assert(0 && "Unexpected type of GlobalValue!");
194 return "";
195 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000196
Misha Brukmanf4de7832003-08-05 16:01:50 +0000197 // Combines expressions
198 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
199 const TargetMachine &TM,
200 const std::string &op) {
201 return "(" + valToExprString(CE->getOperand(0), TM) + op
202 + valToExprString(CE->getOperand(1), TM) + ")";
203 }
204
Vikram S. Adve537a8772002-09-05 18:28:10 +0000205 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
206 // and return this as a string.
Misha Brukmanf4de7832003-08-05 16:01:50 +0000207 std::string ConstantExprToString(const ConstantExpr* CE,
208 const TargetMachine& target) {
209 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000210 switch(CE->getOpcode()) {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000211 case Instruction::GetElementPtr:
212 { // generate a symbolic expression for the byte address
213 const Value* ptrVal = CE->getOperand(0);
214 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Misha Brukmanf4de7832003-08-05 16:01:50 +0000215 const TargetData &TD = target.getTargetData();
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000216 S += "(" + valToExprString(ptrVal, target) + ") + ("
217 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
218 break;
219 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000220
Vikram S. Adve537a8772002-09-05 18:28:10 +0000221 case Instruction::Cast:
222 // Support only non-converting casts for now, i.e., a no-op.
223 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000224 assert(target.getTargetData().getTypeSize(CE->getType()) ==
225 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000226 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
227 break;
228
229 case Instruction::Add:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000230 S += ConstantArithExprToString(CE, target, ") + (");
Vikram S. Adve537a8772002-09-05 18:28:10 +0000231 break;
232
Vikram S. Adve72666e62003-08-01 15:55:53 +0000233 case Instruction::Sub:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000234 S += ConstantArithExprToString(CE, target, ") - (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000235 break;
236
237 case Instruction::Mul:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000238 S += ConstantArithExprToString(CE, target, ") * (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000239 break;
240
241 case Instruction::Div:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000242 S += ConstantArithExprToString(CE, target, ") / (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000243 break;
244
245 case Instruction::Rem:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000246 S += ConstantArithExprToString(CE, target, ") % (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000247 break;
248
249 case Instruction::And:
250 // Logical && for booleans; bitwise & otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000251 S += ConstantArithExprToString(CE, target,
252 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000253 break;
254
255 case Instruction::Or:
256 // Logical || for booleans; bitwise | otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000257 S += ConstantArithExprToString(CE, target,
258 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000259 break;
260
261 case Instruction::Xor:
262 // Bitwise ^ for all types
Misha Brukmanf4de7832003-08-05 16:01:50 +0000263 S += ConstantArithExprToString(CE, target, ") ^ (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000264 break;
265
Vikram S. Advee99941a2002-08-22 02:58:36 +0000266 default:
267 assert(0 && "Unsupported operator in ConstantExprToString()");
268 break;
269 }
270
271 return S;
272 }
273
274 // valToExprString - Helper function for ConstantExprToString().
275 // Appends result to argument string S.
276 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000277 std::string valToExprString(const Value* V, const TargetMachine& target) {
278 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000279 bool failed = false;
280 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
281
282 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Misha Brukmanf4de7832003-08-05 16:01:50 +0000283 S += std::string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000284 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
285 S += itostr(CI->getValue());
286 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
287 S += utostr(CI->getValue());
288 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
289 S += ftostr(CFP->getValue());
290 else if (isa<ConstantPointerNull>(CV))
291 S += "0";
292 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000293 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000294 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
295 S += ConstantExprToString(CE, target);
296 else
297 failed = true;
298
299 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
300 S += getID(GV);
301 }
302 else
303 failed = true;
304
305 if (failed) {
306 assert(0 && "Cannot convert value to string");
307 S += "<illegal-value>";
308 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000309 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000310 }
311
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000312};
313
314
315
316//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000317// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318//===----------------------------------------------------------------------===//
319
Chris Lattnerf57b8452002-04-27 06:56:12 +0000320struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000321 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322 : AsmPrinter(os, t) {}
323
Chris Lattner96c466b2002-04-29 14:57:45 +0000324 const char *getPassName() const {
325 return "Output Sparc Assembly for Functions";
326 }
327
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000328 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000329 startModule(M);
330 return false;
331 }
332
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000333 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334 startFunction(F);
335 emitFunction(F);
336 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000337 return false;
338 }
339
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000340 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000341 endModule();
342 return false;
343 }
344
Chris Lattner97e52e42002-04-28 21:27:06 +0000345 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
346 AU.setPreservesAll();
347 }
348
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000349 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000350private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000351 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000352 void emitMachineInst(const MachineInstr *MI);
353
354 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000355 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000356
357 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
358 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000359
Chris Lattnere88f78c2001-09-19 13:47:27 +0000360 unsigned getOperandMask(unsigned Opcode) {
361 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000362 case V9::SUBccr:
363 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000364 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000365 default: return 0; // By default, don't hack operands...
366 }
367 }
368};
369
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000370inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000371SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
372 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000373 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000374 case V9::JMPLCALLr:
375 case V9::JMPLCALLi:
376 case V9::JMPLRETr:
377 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000378 return (opNum == 0);
379 default:
380 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000381 }
382}
383
384
385inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000386SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
387 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000388 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
389 return (opNum == 0);
390 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
391 return (opNum == 1);
392 else
393 return false;
394}
395
396
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000397#define PrintOp1PlusOp2(mop1, mop2, opCode) \
398 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000400 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000401
402unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000403SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000404 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000405{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000406 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000407
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000408 if (OpIsBranchTargetLabel(MI, opNum))
409 {
410 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
411 return 2;
412 }
413 else if (OpIsMemoryAddressBase(MI, opNum))
414 {
415 toAsm << "[";
416 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
417 toAsm << "]";
418 return 2;
419 }
420 else
421 {
422 printOneOperand(mop, MI->getOpCode());
423 return 1;
424 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000425}
426
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000427void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000428SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
429 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000430{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000431 bool needBitsFlag = true;
432
433 if (mop.opHiBits32())
434 toAsm << "%lm(";
435 else if (mop.opLoBits32())
436 toAsm << "%lo(";
437 else if (mop.opHiBits64())
438 toAsm << "%hh(";
439 else if (mop.opLoBits64())
440 toAsm << "%hm(";
441 else
442 needBitsFlag = false;
443
Chris Lattner133f0792002-10-28 04:45:29 +0000444 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000445 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000446 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000447 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000448 case MachineOperand::MO_MachineRegister:
449 {
450 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000451
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000452 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
453 // better to print code with NULL registers than to die
454 toAsm << "<NULL VALUE>";
455 } else {
456 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
457 }
458 break;
459 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000460
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000461 case MachineOperand::MO_PCRelativeDisp:
462 {
463 const Value *Val = mop.getVRegValue();
464 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000465
Chris Lattner949a3622003-07-23 15:30:06 +0000466 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000467 toAsm << getID(BB);
468 else if (const Function *M = dyn_cast<Function>(Val))
469 toAsm << getID(M);
470 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
471 toAsm << getID(GV);
472 else if (const Constant *CV = dyn_cast<Constant>(Val))
473 toAsm << getID(CV);
474 else
475 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
476 break;
477 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000478
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000479 case MachineOperand::MO_SignExtendedImmed:
480 toAsm << mop.getImmedValue();
481 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000482
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000483 case MachineOperand::MO_UnextendedImmed:
484 toAsm << (uint64_t) mop.getImmedValue();
485 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000486
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000487 default:
488 toAsm << mop; // use dump field
489 break;
490 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000491
492 if (needBitsFlag)
493 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000494}
495
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000496void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000497SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000498{
499 unsigned Opcode = MI->getOpCode();
500
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000501 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000502 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000503
Chris Lattnerf44f9052002-10-29 17:35:41 +0000504 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000505
506 unsigned Mask = getOperandMask(Opcode);
507
508 bool NeedComma = false;
509 unsigned N = 1;
510 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
511 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Misha Brukman8b2fe192003-09-23 17:27:28 +0000512 if (NeedComma) toAsm << ", "; // Handle comma outputting
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000513 NeedComma = true;
514 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000515 } else
516 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000517
518 toAsm << "\n";
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000519 ++EmittedInsts;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000520}
521
522void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000523SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000524{
525 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000526 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000527
528 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000529 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000530 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000531 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000532 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000533}
534
535void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000536SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000537{
Misha Brukmanf4de7832003-08-05 16:01:50 +0000538 std::string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000539 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000540 enterSection(AsmPrinter::Text);
541 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
542 //toAsm << "\t.type\t" << methName << ",#function\n";
543 toAsm << "\t.type\t" << methName << ", 2\n";
544 toAsm << methName << ":\n";
545
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000546 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000547 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000548 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000549 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000550
551 // Output a .size directive so the debugger knows the extents of the function
552 toAsm << ".EndOf_" << methName << ":\n\t.size "
553 << methName << ", .EndOf_"
554 << methName << "-" << methName << "\n";
555
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000556 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000557 toAsm << "\n\n";
558}
559
560} // End anonymous namespace
561
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000562Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000563 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000564}
565
566
567
568
569
570//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000571// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000572//===----------------------------------------------------------------------===//
573
574namespace {
575
576class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
577public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000578 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
579 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000580
Chris Lattner96c466b2002-04-29 14:57:45 +0000581 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
582
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000583 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000584 startModule(M);
585 emitGlobalsAndConstants(M);
586 endModule();
587 return false;
588 }
589
Chris Lattner97e52e42002-04-28 21:27:06 +0000590 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
591 AU.setPreservesAll();
592 }
593
594private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000595 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000596
Vikram S. Advefee76262002-10-13 00:32:18 +0000597 void printGlobalVariable (const GlobalVariable *GV);
598 void PrintZeroBytesToPad (int numBytes);
599 void printSingleConstantValue (const Constant* CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000600 void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
Misha Brukmanf4de7832003-08-05 16:01:50 +0000601 void printConstant (const Constant* CV, std::string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000602
Vikram S. Advefee76262002-10-13 00:32:18 +0000603 static void FoldConstants (const Module &M,
604 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000605};
606
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000607
608// Can we treat the specified array as a string? Only if it is an array of
609// ubytes or non-negative sbytes.
610//
Vikram S. Advefee76262002-10-13 00:32:18 +0000611static bool isStringCompatible(const ConstantArray *CVA) {
612 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000613 if (ETy == Type::UByteTy) return true;
614 if (ETy != Type::SByteTy) return false;
615
Vikram S. Advefee76262002-10-13 00:32:18 +0000616 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
617 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000618 return false;
619
620 return true;
621}
622
623// toOctal - Convert the low order bits of X into an octal letter
624static inline char toOctal(int X) {
625 return (X&7)+'0';
626}
627
628// getAsCString - Return the specified array as a C compatible string, only if
629// the predicate isStringCompatible is true.
630//
Misha Brukmanf4de7832003-08-05 16:01:50 +0000631static std::string getAsCString(const ConstantArray *CVA) {
Vikram S. Advefee76262002-10-13 00:32:18 +0000632 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000633
Misha Brukmanf4de7832003-08-05 16:01:50 +0000634 std::string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000635 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000636 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000637 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000638 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000639
Vikram S. Adve242a8082002-05-19 15:25:51 +0000640 if (C == '"') {
641 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000642 } else if (C == '\\') {
643 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000644 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000645 Result += C;
646 } else {
Vikram S. Adve00477cf2003-07-29 19:57:34 +0000647 Result += '\\'; // print all other chars as octal value
648 Result += toOctal(C >> 6);
649 Result += toOctal(C >> 3);
650 Result += toOctal(C >> 0);
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000651 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000652 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000653 Result += "\"";
654
655 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000656}
657
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000658inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000659ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000660{
661 return (arrayType->getElementType() == Type::UByteTy ||
662 arrayType->getElementType() == Type::SByteTy);
663}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000664
Vikram S. Advee99941a2002-08-22 02:58:36 +0000665
Misha Brukmanf4de7832003-08-05 16:01:50 +0000666inline const std::string
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000667TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000668{
669 switch(type->getPrimitiveID())
670 {
671 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
672 return ".byte";
673 case Type::UShortTyID: case Type::ShortTyID:
674 return ".half";
675 case Type::UIntTyID: case Type::IntTyID:
676 return ".word";
677 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
678 return ".xword";
679 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000680 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000681 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000682 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000683 case Type::ArrayTyID:
684 if (ArrayTypeIsString((ArrayType*) type))
685 return ".ascii";
686 else
687 return "<InvaliDataTypeForPrinting>";
688 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000689 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000690 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000691}
692
Vikram S. Advefee76262002-10-13 00:32:18 +0000693// Get the size of the type
694//
695inline unsigned int
696TypeToSize(const Type* type, const TargetMachine& target)
697{
698 return target.findOptimalStorageSize(type);
699}
700
Vikram S. Adve21447222001-11-10 02:03:06 +0000701// Get the size of the constant for the given target.
702// If this is an unsized array, return 0.
703//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000704inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000705ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000706{
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000707 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
708 {
709 const ArrayType *aty = cast<ArrayType>(CVA->getType());
710 if (ArrayTypeIsString(aty))
711 return 1 + CVA->getNumOperands();
712 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000713
Vikram S. Advefee76262002-10-13 00:32:18 +0000714 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000715}
716
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000717// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000718// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
719//
720inline unsigned int
721SizeToAlignment(unsigned int size, const TargetMachine& target)
722{
723 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
724 if (size > (unsigned) cacheLineSize / 2)
725 return cacheLineSize;
726 else
727 for (unsigned sz=1; /*no condition*/; sz *= 2)
728 if (sz >= size)
729 return sz;
730}
731
732// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000733//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000734inline unsigned int
735TypeToAlignment(const Type* type, const TargetMachine& target)
736{
Vikram S. Advefee76262002-10-13 00:32:18 +0000737 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000738}
739
740// Get the size of the constant and then use SizeToAlignment.
741// Handles strings as a special case;
742inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000743ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000744{
Vikram S. Advefee76262002-10-13 00:32:18 +0000745 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
746 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
747 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000748
749 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000750}
751
752
Vikram S. Adve21447222001-11-10 02:03:06 +0000753// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000754void
Vikram S. Advefee76262002-10-13 00:32:18 +0000755SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000756{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000757 assert(CV->getType() != Type::VoidTy &&
758 CV->getType() != Type::TypeTy &&
759 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000760 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000761
Chris Lattnerf678dc62002-04-11 21:44:02 +0000762 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
763 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000764
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000765 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000766
Vikram S. Advedb685772003-07-30 12:54:47 +0000767 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
768 { // This is a constant address for a global variable or method.
769 // Use the name of the variable or method as the address value.
770 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
771 toAsm << getID(CPR->getValue()) << "\n";
772 }
773 else if (isa<ConstantPointerNull>(CV))
774 { // Null pointer value
775 toAsm << "0\n";
776 }
777 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
778 { // Constant expression built from operators, constants, and symbolic addrs
779 toAsm << ConstantExprToString(CE, Target) << "\n";
780 }
781 else if (CV->getType()->isPrimitiveType()) // Check primitive types last
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000782 {
783 if (CV->getType()->isFloatingPoint()) {
784 // FP Constants are printed as integer constants to avoid losing
785 // precision...
786 double Val = cast<ConstantFP>(CV)->getValue();
787 if (CV->getType() == Type::FloatTy) {
788 float FVal = (float)Val;
789 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
790 toAsm << *(unsigned int*)ProxyPtr;
791 } else if (CV->getType() == Type::DoubleTy) {
792 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
793 toAsm << *(uint64_t*)ProxyPtr;
794 } else {
795 assert(0 && "Unknown floating point type!");
796 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000797
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000798 toAsm << "\t! " << CV->getType()->getDescription()
799 << " value: " << Val << "\n";
800 } else {
801 WriteAsOperand(toAsm, CV, false, false) << "\n";
802 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000803 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000804 else
805 {
806 assert(0 && "Unknown elementary type for constant");
807 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000808}
809
Vikram S. Adve21447222001-11-10 02:03:06 +0000810void
Vikram S. Advefee76262002-10-13 00:32:18 +0000811SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000812{
Vikram S. Advefee76262002-10-13 00:32:18 +0000813 for ( ; numBytes >= 8; numBytes -= 8)
814 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
815
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000816 if (numBytes >= 4)
817 {
818 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
819 numBytes -= 4;
820 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000821
822 while (numBytes--)
823 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
824}
825
826// Print a constant value or values (it may be an aggregate).
827// Uses printSingleConstantValue() to print each individual value.
828void
829SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
Vikram S. Adve9e498242003-05-25 21:59:09 +0000830 int numPadBytesAfter /* = 0*/)
Vikram S. Advefee76262002-10-13 00:32:18 +0000831{
832 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
833
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000834 if (CVA && isStringCompatible(CVA))
835 { // print the string alone and return
836 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000837 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000838 else if (CVA)
839 { // Not a string. Print the values in successive locations
840 const std::vector<Use> &constValues = CVA->getValues();
841 for (unsigned i=0; i < constValues.size(); i++)
842 printConstantValueOnly(cast<Constant>(constValues[i].get()));
843 }
844 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
845 { // Print the fields in successive locations. Pad to align if needed!
846 const StructLayout *cvsLayout =
847 Target.getTargetData().getStructLayout(CVS->getType());
848 const std::vector<Use>& constValues = CVS->getValues();
849 unsigned sizeSoFar = 0;
850 for (unsigned i=0, N = constValues.size(); i < N; i++)
851 {
852 const Constant* field = cast<Constant>(constValues[i].get());
853
854 // Check if padding is needed and insert one or more 0s.
855 unsigned fieldSize =
856 Target.getTargetData().getTypeSize(field->getType());
857 int padSize = ((i == N-1? cvsLayout->StructSize
858 : cvsLayout->MemberOffsets[i+1])
859 - cvsLayout->MemberOffsets[i]) - fieldSize;
860 sizeSoFar += (fieldSize + padSize);
861
862 // Now print the actual field value
863 printConstantValueOnly(field, padSize);
864 }
865 assert(sizeSoFar == cvsLayout->StructSize &&
866 "Layout of constant struct may be incorrect!");
867 }
868 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000869 printSingleConstantValue(CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000870
871 if (numPadBytesAfter)
872 PrintZeroBytesToPad(numPadBytesAfter);
Vikram S. Adve21447222001-11-10 02:03:06 +0000873}
874
875// Print a constant (which may be an aggregate) prefixed by all the
876// appropriate directives. Uses printConstantValueOnly() to print the
877// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000878void
Misha Brukmanf4de7832003-08-05 16:01:50 +0000879SparcModuleAsmPrinter::printConstant(const Constant* CV, std::string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000880{
881 if (valID.length() == 0)
882 valID = getID(CV);
883
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000884 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000885
886 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000887 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000888 if (CVA && isStringCompatible(CVA))
889 { // print it as a string and return
890 toAsm << valID << ":\n";
891 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
892 return;
893 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000894
Chris Lattner697954c2002-01-20 22:54:45 +0000895 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000896
897 unsigned int constSize = ConstantToSize(CV, Target);
898 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000899 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000900
Chris Lattner697954c2002-01-20 22:54:45 +0000901 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000902
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000903 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000904}
905
906
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000907void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000908 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000909 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
910 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000911 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000912 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000913 MC.insert(pool.begin(), pool.end());
914 }
915}
916
917void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000918{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000919 if (GV->hasExternalLinkage())
920 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000921
Vikram S. Advefee76262002-10-13 00:32:18 +0000922 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000923 printConstant(GV->getInitializer(), getID(GV));
924 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000925 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
926 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000927 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000928 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000929 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000930 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000931 }
932}
933
934
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000935void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000936 // First, get the constants there were marked by the code generator for
937 // inclusion in the assembly code data area and fold them all into a
938 // single constant pool since there may be lots of duplicates. Also,
939 // lets force these constants into the slot table so that we can get
940 // unique names for unnamed constants also.
941 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000942 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000943 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000944
Chris Lattner637ed862002-08-07 21:39:48 +0000945 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000946 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000947 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000948 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000949 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000950
951 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000952 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
953 if (! GI->isExternal()) {
954 assert(GI->hasInitializer());
955 if (GI->isConstant())
956 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
957 else if (GI->getInitializer()->isNullValue())
958 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
959 else
960 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
961
962 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000963 }
Chris Lattner637ed862002-08-07 21:39:48 +0000964
Chris Lattner697954c2002-01-20 22:54:45 +0000965 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000966}
967
Chris Lattnere88f78c2001-09-19 13:47:27 +0000968} // End anonymous namespace
969
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000970Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000971 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000972}