blob: 80e0f642718d7b12577b9b98ab652a1d9e16d3cf [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
2//
3// This file implements all of the stuff neccesary to output a .s file from
4// LLVM. The code in this file assumes that the specified module has already
5// been compiled into the internal data structures of the Module.
6//
Chris Lattnerf57b8452002-04-27 06:56:12 +00007// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
8// The FunctionPass is pipelined together with all of the rest of the code
9// generation stages, and the Pass runs at the end to emit code for global
10// variables and such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "SparcInternals.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000015#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris 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"
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000025
26namespace {
27
Vikram S. Adved198c472002-03-18 03:07:26 +000028class GlobalIdTable: public Annotation {
29 static AnnotationID AnnotId;
30 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000031
Chris Lattner09ff1122002-07-24 21:21:32 +000032 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000033 typedef ValIdMap::const_iterator ValIdMapConstIterator;
34 typedef ValIdMap:: iterator ValIdMapIterator;
35public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000036 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000037 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000038
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000039 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000040};
41
42AnnotationID GlobalIdTable::AnnotId =
43 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
44
45//===---------------------------------------------------------------------===//
46// Code Shared By the two printer passes, as a mixin
47//===---------------------------------------------------------------------===//
48
49class AsmPrinter {
50 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000051public:
Chris Lattner697954c2002-01-20 22:54:45 +000052 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000053 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000054
Chris Lattnere88f78c2001-09-19 13:47:27 +000055 enum Sections {
56 Unknown,
57 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000058 ReadOnlyData,
59 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000060 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000061 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000062
Chris Lattner59ba1092002-02-04 15:53:23 +000063 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000064 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
65
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000066 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000067 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000068 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000069 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000070 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000071 idTable = new GlobalIdTable(&M);
72 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000073 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000074 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000075 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000076 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000077 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000078 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000079 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000080 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000081 }
82 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000083 }
84
Chris Lattner8b1b4e22002-07-16 18:35:16 +000085 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000086 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000087 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
88 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000089 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000090
Chris Lattnere88f78c2001-09-19 13:47:27 +000091 // enterSection - Use this method to enter a different section of the output
92 // executable. This is used to only output neccesary section transitions.
93 //
94 void enterSection(enum Sections S) {
95 if (S == CurSection) return; // Only switch section if neccesary
96 CurSection = S;
97
Vikram S. Adve29ff8732001-11-08 05:12:37 +000098 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000099 switch (S)
100 {
101 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000102 case Text: toAsm << "\".text\""; break;
103 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
104 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Advefee76262002-10-13 00:32:18 +0000105 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000106 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000107 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000108 }
109
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000110 static string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000111 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000112
113 // Symbol names in Sparc assembly language have these rules:
114 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
115 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000116 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000117 if (isdigit(S[0]))
118 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000119
120 for (unsigned i = 0; i < S.size(); ++i)
121 {
122 char C = S[i];
123 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
124 Result += C;
125 else
126 {
127 Result += '_';
128 Result += char('0' + ((unsigned char)C >> 4));
129 Result += char('0' + (C & 0xF));
130 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000131 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000132 return Result;
133 }
134
Chris Lattnere88f78c2001-09-19 13:47:27 +0000135 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000136 // the name of the identifier if possible (qualified by the type), and
137 // use a numbered value based on prefix otherwise.
138 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 //
140 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000141 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000142
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000143 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000144
Vikram S. Adved198c472002-03-18 03:07:26 +0000145 // Qualify all internal names with a unique id.
146 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000147 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000148 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000149 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
150 if (I == idTable->valToIdMap.end())
151 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000152 else
153 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000154 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000155 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000156
157 // Replace or prefix problem characters in the name
158 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000159 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000160
161 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000163
Chris Lattnere88f78c2001-09-19 13:47:27 +0000164 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000165 string getID(const Function *F) {
166 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000167 }
168 string getID(const BasicBlock *BB) {
169 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
170 }
171 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000172 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000173 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000174 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000175 return getID(CV, "LLVMConst_", ".C_");
176 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000177 string getID(const GlobalValue *GV) {
178 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
179 return getID(V);
180 else if (const Function *F = dyn_cast<Function>(GV))
181 return getID(F);
182 assert(0 && "Unexpected type of GlobalValue!");
183 return "";
184 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000185
Vikram S. Adve537a8772002-09-05 18:28:10 +0000186 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
187 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000188 string ConstantExprToString(const ConstantExpr* CE,
189 const TargetMachine& target) {
190 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000191 switch(CE->getOpcode()) {
192 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000193 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000194 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000195 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000196 S += "(" + valToExprString(ptrVal, target) + ") + ("
197 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
198 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000199 break;
200 }
201
Vikram S. Adve537a8772002-09-05 18:28:10 +0000202 case Instruction::Cast:
203 // Support only non-converting casts for now, i.e., a no-op.
204 // This assertion is not a complete check.
205 assert(target.DataLayout.getTypeSize(CE->getType()) ==
206 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
207 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
208 break;
209
210 case Instruction::Add:
211 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
212 + valToExprString(CE->getOperand(1), target) + ")";
213 break;
214
Vikram S. Advee99941a2002-08-22 02:58:36 +0000215 default:
216 assert(0 && "Unsupported operator in ConstantExprToString()");
217 break;
218 }
219
220 return S;
221 }
222
223 // valToExprString - Helper function for ConstantExprToString().
224 // Appends result to argument string S.
225 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000226 string valToExprString(const Value* V, const TargetMachine& target) {
227 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000228 bool failed = false;
229 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
230
231 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000232 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000233 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
234 S += itostr(CI->getValue());
235 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
236 S += utostr(CI->getValue());
237 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
238 S += ftostr(CFP->getValue());
239 else if (isa<ConstantPointerNull>(CV))
240 S += "0";
241 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000242 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000243 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
244 S += ConstantExprToString(CE, target);
245 else
246 failed = true;
247
248 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
249 S += getID(GV);
250 }
251 else
252 failed = true;
253
254 if (failed) {
255 assert(0 && "Cannot convert value to string");
256 S += "<illegal-value>";
257 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000258 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000259 }
260
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000261};
262
263
264
265//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000266// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000267//===----------------------------------------------------------------------===//
268
Chris Lattnerf57b8452002-04-27 06:56:12 +0000269struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000271 : AsmPrinter(os, t) {}
272
Chris Lattner96c466b2002-04-29 14:57:45 +0000273 const char *getPassName() const {
274 return "Output Sparc Assembly for Functions";
275 }
276
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000277 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000278 startModule(M);
279 return false;
280 }
281
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000282 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000283 startFunction(F);
284 emitFunction(F);
285 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000286 return false;
287 }
288
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000289 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000290 endModule();
291 return false;
292 }
293
Chris Lattner97e52e42002-04-28 21:27:06 +0000294 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
295 AU.setPreservesAll();
296 }
297
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000298 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000299private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000300 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000301 void emitMachineInst(const MachineInstr *MI);
302
303 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
304 void printOneOperand(const MachineOperand &Op);
305
306 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
307 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000308
Chris Lattnere88f78c2001-09-19 13:47:27 +0000309 unsigned getOperandMask(unsigned Opcode) {
310 switch (Opcode) {
311 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000312 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000313 default: return 0; // By default, don't hack operands...
314 }
315 }
316};
317
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000318inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000319SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
320 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000321 switch (MI->getOpCode()) {
322 case JMPLCALL:
323 case JMPLRET: return (opNum == 0);
324 default: return false;
325 }
326}
327
328
329inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000330SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
331 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000332 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
333 return (opNum == 0);
334 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
335 return (opNum == 1);
336 else
337 return false;
338}
339
340
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000341#define PrintOp1PlusOp2(mop1, mop2) \
342 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000343 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000344 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000345
346unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000347SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000348 unsigned int opNum)
349{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000350 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000351
352 if (OpIsBranchTargetLabel(MI, opNum))
353 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000354 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000355 return 2;
356 }
357 else if (OpIsMemoryAddressBase(MI, opNum))
358 {
359 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000360 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000361 toAsm << "]";
362 return 2;
363 }
364 else
365 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000366 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000367 return 1;
368 }
369}
370
371
372void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000373SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000374{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000375 bool needBitsFlag = true;
376
377 if (mop.opHiBits32())
378 toAsm << "%lm(";
379 else if (mop.opLoBits32())
380 toAsm << "%lo(";
381 else if (mop.opHiBits64())
382 toAsm << "%hh(";
383 else if (mop.opLoBits64())
384 toAsm << "%hm(";
385 else
386 needBitsFlag = false;
387
Chris Lattner133f0792002-10-28 04:45:29 +0000388 switch (mop.getType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389 {
390 case MachineOperand::MO_VirtualRegister:
391 case MachineOperand::MO_CCRegister:
392 case MachineOperand::MO_MachineRegister:
393 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000394 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000395
Vikram S. Advefbd21612002-03-31 19:03:58 +0000396 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000397 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
398 toAsm << "<NULL VALUE>";
399 } else {
400 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
401 }
402 break;
403 }
404
405 case MachineOperand::MO_PCRelativeDisp:
406 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000407 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000408 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
409
410 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000411 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000412 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000413 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000414 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000416 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 toAsm << getID(CV);
418 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000419 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000420 break;
421 }
422
423 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000424 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000425 break;
426
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000427 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000428 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000429 break;
430
431 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000432 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000433 break;
434 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000435
436 if (needBitsFlag)
437 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000438}
439
440
441void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000442SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000443{
444 unsigned Opcode = MI->getOpCode();
445
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000446 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000447 return; // IGNORE PHI NODES
448
Chris Lattnerf44f9052002-10-29 17:35:41 +0000449 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000450
451 unsigned Mask = getOperandMask(Opcode);
452
453 bool NeedComma = false;
454 unsigned N = 1;
455 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
456 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
457 if (NeedComma) toAsm << ", "; // Handle comma outputing
458 NeedComma = true;
459 N = printOperands(MI, OpNum);
460 }
461 else
462 N = 1;
463
464 toAsm << "\n";
465}
466
467void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000468SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000469{
470 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000471 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000472
473 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000474 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000475 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000476 emitMachineInst(*MII);
477 toAsm << "\n"; // Seperate BB's with newlines
478}
479
480void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000481SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000482{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000483 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000484 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000485 enterSection(AsmPrinter::Text);
486 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
487 //toAsm << "\t.type\t" << methName << ",#function\n";
488 toAsm << "\t.type\t" << methName << ", 2\n";
489 toAsm << methName << ":\n";
490
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000491 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000492 MachineFunction &MF = MachineFunction::get(&F);
493 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E; ++I)
494 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000495
496 // Output a .size directive so the debugger knows the extents of the function
497 toAsm << ".EndOf_" << methName << ":\n\t.size "
498 << methName << ", .EndOf_"
499 << methName << "-" << methName << "\n";
500
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000501 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000502 toAsm << "\n\n";
503}
504
505} // End anonymous namespace
506
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000507Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000508 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000509}
510
511
512
513
514
515//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000516// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000517//===----------------------------------------------------------------------===//
518
519namespace {
520
521class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
522public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000523 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
524 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000525
Chris Lattner96c466b2002-04-29 14:57:45 +0000526 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
527
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000528 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000529 startModule(M);
530 emitGlobalsAndConstants(M);
531 endModule();
532 return false;
533 }
534
Chris Lattner97e52e42002-04-28 21:27:06 +0000535 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
536 AU.setPreservesAll();
537 }
538
539private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000540 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000541
Vikram S. Advefee76262002-10-13 00:32:18 +0000542 void printGlobalVariable (const GlobalVariable *GV);
543 void PrintZeroBytesToPad (int numBytes);
544 void printSingleConstantValue (const Constant* CV);
545 void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000546 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000547
Vikram S. Advefee76262002-10-13 00:32:18 +0000548 static void FoldConstants (const Module &M,
549 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000550};
551
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000552
553// Can we treat the specified array as a string? Only if it is an array of
554// ubytes or non-negative sbytes.
555//
Vikram S. Advefee76262002-10-13 00:32:18 +0000556static bool isStringCompatible(const ConstantArray *CVA) {
557 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000558 if (ETy == Type::UByteTy) return true;
559 if (ETy != Type::SByteTy) return false;
560
Vikram S. Advefee76262002-10-13 00:32:18 +0000561 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
562 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000563 return false;
564
565 return true;
566}
567
568// toOctal - Convert the low order bits of X into an octal letter
569static inline char toOctal(int X) {
570 return (X&7)+'0';
571}
572
573// getAsCString - Return the specified array as a C compatible string, only if
574// the predicate isStringCompatible is true.
575//
Vikram S. Advefee76262002-10-13 00:32:18 +0000576static string getAsCString(const ConstantArray *CVA) {
577 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000578
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000579 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000580 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000581 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000582 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000583 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000584 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
585 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000586
Vikram S. Adve242a8082002-05-19 15:25:51 +0000587 if (C == '"') {
588 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000589 } else if (C == '\\') {
590 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000591 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000592 Result += C;
593 } else {
594 switch(C) {
595 case '\a': Result += "\\a"; break;
596 case '\b': Result += "\\b"; break;
597 case '\f': Result += "\\f"; break;
598 case '\n': Result += "\\n"; break;
599 case '\r': Result += "\\r"; break;
600 case '\t': Result += "\\t"; break;
601 case '\v': Result += "\\v"; break;
602 default:
603 Result += '\\';
604 Result += toOctal(C >> 6);
605 Result += toOctal(C >> 3);
606 Result += toOctal(C >> 0);
607 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000608 }
609 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000610 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000611 Result += "\"";
612
613 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000614}
615
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000616inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000617ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000618{
619 return (arrayType->getElementType() == Type::UByteTy ||
620 arrayType->getElementType() == Type::SByteTy);
621}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000622
Vikram S. Advee99941a2002-08-22 02:58:36 +0000623
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000624inline const string
625TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000626{
627 switch(type->getPrimitiveID())
628 {
629 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
630 return ".byte";
631 case Type::UShortTyID: case Type::ShortTyID:
632 return ".half";
633 case Type::UIntTyID: case Type::IntTyID:
634 return ".word";
635 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
636 return ".xword";
637 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000638 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000639 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000640 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000641 case Type::ArrayTyID:
642 if (ArrayTypeIsString((ArrayType*) type))
643 return ".ascii";
644 else
645 return "<InvaliDataTypeForPrinting>";
646 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000647 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000648 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000649}
650
Vikram S. Advefee76262002-10-13 00:32:18 +0000651// Get the size of the type
652//
653inline unsigned int
654TypeToSize(const Type* type, const TargetMachine& target)
655{
656 return target.findOptimalStorageSize(type);
657}
658
Vikram S. Adve21447222001-11-10 02:03:06 +0000659// Get the size of the constant for the given target.
660// If this is an unsized array, return 0.
661//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000662inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000663ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000664{
Vikram S. Advefee76262002-10-13 00:32:18 +0000665 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000666 {
Vikram S. Advefee76262002-10-13 00:32:18 +0000667 const ArrayType *aty = cast<ArrayType>(CVA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000668 if (ArrayTypeIsString(aty))
Vikram S. Advefee76262002-10-13 00:32:18 +0000669 return 1 + CVA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000670 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000671
Vikram S. Advefee76262002-10-13 00:32:18 +0000672 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000673}
674
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000675// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000676// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
677//
678inline unsigned int
679SizeToAlignment(unsigned int size, const TargetMachine& target)
680{
681 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
682 if (size > (unsigned) cacheLineSize / 2)
683 return cacheLineSize;
684 else
685 for (unsigned sz=1; /*no condition*/; sz *= 2)
686 if (sz >= size)
687 return sz;
688}
689
690// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000691//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000692inline unsigned int
693TypeToAlignment(const Type* type, const TargetMachine& target)
694{
Vikram S. Advefee76262002-10-13 00:32:18 +0000695 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000696}
697
698// Get the size of the constant and then use SizeToAlignment.
699// Handles strings as a special case;
700inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000701ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000702{
Vikram S. Advefee76262002-10-13 00:32:18 +0000703 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
704 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
705 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000706
707 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000708}
709
710
Vikram S. Adve21447222001-11-10 02:03:06 +0000711// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000712void
Vikram S. Advefee76262002-10-13 00:32:18 +0000713SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000714{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000715 assert(CV->getType() != Type::VoidTy &&
716 CV->getType() != Type::TypeTy &&
717 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000718 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000719
Chris Lattnerf678dc62002-04-11 21:44:02 +0000720 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
721 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000722
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000723 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000724
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000725 if (CV->getType()->isPrimitiveType())
726 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000727 if (CV->getType()->isFloatingPoint()) {
728 // FP Constants are printed as integer constants to avoid losing
729 // precision...
730 double Val = cast<ConstantFP>(CV)->getValue();
731 if (CV->getType() == Type::FloatTy) {
732 float FVal = (float)Val;
733 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
734 toAsm << *(unsigned int*)ProxyPtr;
735 } else if (CV->getType() == Type::DoubleTy) {
736 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
737 toAsm << *(uint64_t*)ProxyPtr;
738 } else {
739 assert(0 && "Unknown floating point type!");
740 }
741
742 toAsm << "\t! " << CV->getType()->getDescription()
743 << " value: " << Val << "\n";
744 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000745 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000746 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000747 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000748 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
749 { // This is a constant address for a global variable or method.
750 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000751 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000752 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000753 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000754 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000755 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000756 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000757 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
758 { // Constant expression built from operators, constants, and symbolic addrs
759 toAsm << ConstantExprToString(CE, Target) << "\n";
760 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000761 else
762 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000763 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000764 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000765}
766
Vikram S. Adve21447222001-11-10 02:03:06 +0000767void
Vikram S. Advefee76262002-10-13 00:32:18 +0000768SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000769{
Vikram S. Advefee76262002-10-13 00:32:18 +0000770 for ( ; numBytes >= 8; numBytes -= 8)
771 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
772
773 if (numBytes >= 4)
774 {
775 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
776 numBytes -= 4;
777 }
778
779 while (numBytes--)
780 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
781}
782
783// Print a constant value or values (it may be an aggregate).
784// Uses printSingleConstantValue() to print each individual value.
785void
786SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
787 int numPadBytes /* = 0*/)
788{
789 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
790
791 if (numPadBytes)
792 PrintZeroBytesToPad(numPadBytes);
793
794 if (CVA && isStringCompatible(CVA))
Vikram S. Adve21447222001-11-10 02:03:06 +0000795 { // print the string alone and return
Vikram S. Advefee76262002-10-13 00:32:18 +0000796 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000797 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000798 else if (CVA)
Vikram S. Adve21447222001-11-10 02:03:06 +0000799 { // Not a string. Print the values in successive locations
Vikram S. Advefee76262002-10-13 00:32:18 +0000800 const std::vector<Use> &constValues = CVA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000801 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000802 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000803 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000804 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
805 { // Print the fields in successive locations. Pad to align if needed!
806 const StructLayout *cvsLayout =
807 Target.DataLayout.getStructLayout(CVS->getType());
808 const std::vector<Use>& constValues = CVS->getValues();
809 unsigned sizeSoFar = 0;
810 for (unsigned i=0, N = constValues.size(); i < N; i++)
811 {
812 const Constant* field = cast<Constant>(constValues[i].get());
813
814 // Check if padding is needed and insert one or more 0s.
815 unsigned fieldSize = Target.DataLayout.getTypeSize(field->getType());
816 int padSize = ((i == N-1? cvsLayout->StructSize
817 : cvsLayout->MemberOffsets[i+1])
818 - cvsLayout->MemberOffsets[i]) - fieldSize;
819 sizeSoFar += (fieldSize + padSize);
820
821 // Now print the actual field value
822 printConstantValueOnly(field, padSize);
823 }
824 assert(sizeSoFar == cvsLayout->StructSize &&
825 "Layout of constant struct may be incorrect!");
Vikram S. Adve21447222001-11-10 02:03:06 +0000826 }
827 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000828 printSingleConstantValue(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000829}
830
831// Print a constant (which may be an aggregate) prefixed by all the
832// appropriate directives. Uses printConstantValueOnly() to print the
833// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000834void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000835SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000836{
837 if (valID.length() == 0)
838 valID = getID(CV);
839
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000840 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000841
842 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000843 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
844 if (CVA && isStringCompatible(CVA))
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000845 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000846 toAsm << valID << ":\n";
Vikram S. Advefee76262002-10-13 00:32:18 +0000847 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000848 return;
849 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000850
Chris Lattner697954c2002-01-20 22:54:45 +0000851 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000852
853 unsigned int constSize = ConstantToSize(CV, Target);
854 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000855 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000856
Chris Lattner697954c2002-01-20 22:54:45 +0000857 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000858
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000859 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000860}
861
862
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000863void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000864 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000865 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
866 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000867 const hash_set<const Constant*> &pool =
Misha Brukmanfce11432002-10-28 00:28:31 +0000868 MachineFunction::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000869 MC.insert(pool.begin(), pool.end());
870 }
871}
872
873void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000874{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000875 if (GV->hasExternalLinkage())
876 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000877
Vikram S. Advefee76262002-10-13 00:32:18 +0000878 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000879 printConstant(GV->getInitializer(), getID(GV));
880 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000881 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
882 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000883 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000884 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000885 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000886 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000887 }
888}
889
890
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000891void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000892 // First, get the constants there were marked by the code generator for
893 // inclusion in the assembly code data area and fold them all into a
894 // single constant pool since there may be lots of duplicates. Also,
895 // lets force these constants into the slot table so that we can get
896 // unique names for unnamed constants also.
897 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000898 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000899 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000900
Chris Lattner637ed862002-08-07 21:39:48 +0000901 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000902 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000903 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000904 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000905 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000906
907 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000908 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
909 if (! GI->isExternal()) {
910 assert(GI->hasInitializer());
911 if (GI->isConstant())
912 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
913 else if (GI->getInitializer()->isNullValue())
914 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
915 else
916 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
917
918 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000919 }
Chris Lattner637ed862002-08-07 21:39:48 +0000920
Chris Lattner697954c2002-01-20 22:54:45 +0000921 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000922}
923
Chris Lattnere88f78c2001-09-19 13:47:27 +0000924} // End anonymous namespace
925
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000926Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000927 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000928}