blob: c3849217257c8fd33cd1f589012cca2f2a5439fd [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"
Vikram S. Adveb2debdc2002-07-08 23:30:59 +000016#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattnerc019a172002-02-03 07:48:06 +000017#include "llvm/CodeGen/MachineCodeForMethod.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000018#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000019#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000020#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000021#include "llvm/BasicBlock.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022#include "llvm/Function.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000023#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000024#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000025#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000026#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000027#include "Support/StringExtras.h"
Vikram S. Adve9ee9d712002-03-03 20:46:32 +000028#include <iostream>
Chris Lattner697954c2002-01-20 22:54:45 +000029using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000030
31namespace {
32
Vikram S. Adved198c472002-03-18 03:07:26 +000033class GlobalIdTable: public Annotation {
34 static AnnotationID AnnotId;
35 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000036
Chris Lattner09ff1122002-07-24 21:21:32 +000037 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000038 typedef ValIdMap::const_iterator ValIdMapConstIterator;
39 typedef ValIdMap:: iterator ValIdMapIterator;
40public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000041 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000042 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000043
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000044 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000045};
46
47AnnotationID GlobalIdTable::AnnotId =
48 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
49
50//===---------------------------------------------------------------------===//
51// Code Shared By the two printer passes, as a mixin
52//===---------------------------------------------------------------------===//
53
54class AsmPrinter {
55 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000056public:
Chris Lattner697954c2002-01-20 22:54:45 +000057 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000058 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000059
Chris Lattnere88f78c2001-09-19 13:47:27 +000060 enum Sections {
61 Unknown,
62 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000063 ReadOnlyData,
64 InitRWData,
65 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000066 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000067
Chris Lattner59ba1092002-02-04 15:53:23 +000068 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000069 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
70
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000071 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000072 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000073 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000074 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000075 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000076 idTable = new GlobalIdTable(&M);
77 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000078 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000081 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000082 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000083 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000084 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000085 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000086 }
87 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000088 }
89
Chris Lattner8b1b4e22002-07-16 18:35:16 +000090 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000091 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000092 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
93 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000094 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000095
Chris Lattnere88f78c2001-09-19 13:47:27 +000096 // enterSection - Use this method to enter a different section of the output
97 // executable. This is used to only output neccesary section transitions.
98 //
99 void enterSection(enum Sections S) {
100 if (S == CurSection) return; // Only switch section if neccesary
101 CurSection = S;
102
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000103 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000104 switch (S)
105 {
106 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000107 case Text: toAsm << "\".text\""; break;
108 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
109 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
Vikram S. Adve537a8772002-09-05 18:28:10 +0000110 case UninitRWData: toAsm << "\".bss\",#alloc,#write"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000111 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000112 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000113 }
114
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000115 static std::string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000116 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000117
118 // Symbol names in Sparc assembly language have these rules:
119 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
120 // (b) A name beginning in "." is treated as a local name.
121 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
122 //
123 if (S[0] == '_' || isdigit(S[0]))
124 Result += "ll";
125
126 for (unsigned i = 0; i < S.size(); ++i)
127 {
128 char C = S[i];
129 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
130 Result += C;
131 else
132 {
133 Result += '_';
134 Result += char('0' + ((unsigned char)C >> 4));
135 Result += char('0' + (C & 0xF));
136 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000137 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000138 return Result;
139 }
140
Chris Lattnere88f78c2001-09-19 13:47:27 +0000141 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000142 // the name of the identifier if possible (qualified by the type), and
143 // use a numbered value based on prefix otherwise.
144 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000145 //
146 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000147 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
148
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000149 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adved198c472002-03-18 03:07:26 +0000150
151 // Qualify all internal names with a unique id.
152 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000153 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000154 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000155 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
156 if (I == idTable->valToIdMap.end())
157 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000158 else
159 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000160 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000161 Result = Result + "_" + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000163
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000164 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000166
Chris Lattnere88f78c2001-09-19 13:47:27 +0000167 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000168 string getID(const Function *F) {
169 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000170 }
171 string getID(const BasicBlock *BB) {
172 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
173 }
174 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000175 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000177 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000178 return getID(CV, "LLVMConst_", ".C_");
179 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000180 string getID(const GlobalValue *GV) {
181 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
182 return getID(V);
183 else if (const Function *F = dyn_cast<Function>(GV))
184 return getID(F);
185 assert(0 && "Unexpected type of GlobalValue!");
186 return "";
187 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000188
Vikram S. Adve537a8772002-09-05 18:28:10 +0000189 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
190 // and return this as a string.
Vikram S. Advee99941a2002-08-22 02:58:36 +0000191 std::string ConstantExprToString(const ConstantExpr* CE,
192 const TargetMachine& target) {
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000193 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000194
195 switch(CE->getOpcode()) {
196 case Instruction::GetElementPtr:
Vikram S. Adve537a8772002-09-05 18:28:10 +0000197 { // generate a symbolic expression for the byte address
Vikram S. Advee99941a2002-08-22 02:58:36 +0000198 const Value* ptrVal = CE->getOperand(0);
Chris Lattnerabfb0b52002-08-22 15:57:58 +0000199 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Vikram S. Adve537a8772002-09-05 18:28:10 +0000200 S += "(" + valToExprString(ptrVal, target) + ") + ("
201 + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
202 + ")";
Vikram S. Advee99941a2002-08-22 02:58:36 +0000203 break;
204 }
205
Vikram S. Adve537a8772002-09-05 18:28:10 +0000206 case Instruction::Cast:
207 // Support only non-converting casts for now, i.e., a no-op.
208 // This assertion is not a complete check.
209 assert(target.DataLayout.getTypeSize(CE->getType()) ==
210 target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
211 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
212 break;
213
214 case Instruction::Add:
215 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
216 + valToExprString(CE->getOperand(1), target) + ")";
217 break;
218
Vikram S. Advee99941a2002-08-22 02:58:36 +0000219 default:
220 assert(0 && "Unsupported operator in ConstantExprToString()");
221 break;
222 }
223
224 return S;
225 }
226
227 // valToExprString - Helper function for ConstantExprToString().
228 // Appends result to argument string S.
229 //
Vikram S. Adve537a8772002-09-05 18:28:10 +0000230 std::string valToExprString(const Value* V, const TargetMachine& target) {
231 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000232 bool failed = false;
233 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
234
235 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
236 S += std::string(CB == ConstantBool::True ? "1" : "0");
237 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
238 S += itostr(CI->getValue());
239 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
240 S += utostr(CI->getValue());
241 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
242 S += ftostr(CFP->getValue());
243 else if (isa<ConstantPointerNull>(CV))
244 S += "0";
245 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000246 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000247 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
248 S += ConstantExprToString(CE, target);
249 else
250 failed = true;
251
252 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
253 S += getID(GV);
254 }
255 else
256 failed = true;
257
258 if (failed) {
259 assert(0 && "Cannot convert value to string");
260 S += "<illegal-value>";
261 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000262 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000263 }
264
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000265};
266
267
268
269//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000270// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000271//===----------------------------------------------------------------------===//
272
Chris Lattnerf57b8452002-04-27 06:56:12 +0000273struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000274 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000275 : AsmPrinter(os, t) {}
276
Chris Lattner96c466b2002-04-29 14:57:45 +0000277 const char *getPassName() const {
278 return "Output Sparc Assembly for Functions";
279 }
280
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000281 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000282 startModule(M);
283 return false;
284 }
285
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000286 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000287 startFunction(F);
288 emitFunction(F);
289 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000290 return false;
291 }
292
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000293 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000294 endModule();
295 return false;
296 }
297
Chris Lattner97e52e42002-04-28 21:27:06 +0000298 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
299 AU.setPreservesAll();
300 }
301
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000302 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000303private :
304 void emitBasicBlock(const BasicBlock *BB);
305 void emitMachineInst(const MachineInstr *MI);
306
307 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
308 void printOneOperand(const MachineOperand &Op);
309
310 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
311 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000312
Chris Lattnere88f78c2001-09-19 13:47:27 +0000313 unsigned getOperandMask(unsigned Opcode) {
314 switch (Opcode) {
315 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000316 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000317 default: return 0; // By default, don't hack operands...
318 }
319 }
320};
321
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000322inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000323SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
324 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000325 switch (MI->getOpCode()) {
326 case JMPLCALL:
327 case JMPLRET: return (opNum == 0);
328 default: return false;
329 }
330}
331
332
333inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
335 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000336 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
337 return (opNum == 0);
338 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
339 return (opNum == 1);
340 else
341 return false;
342}
343
344
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000345#define PrintOp1PlusOp2(mop1, mop2) \
346 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000347 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000348 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000349
350unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000351SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000352 unsigned int opNum)
353{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000354 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000355
356 if (OpIsBranchTargetLabel(MI, opNum))
357 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000358 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000359 return 2;
360 }
361 else if (OpIsMemoryAddressBase(MI, opNum))
362 {
363 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000364 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000365 toAsm << "]";
366 return 2;
367 }
368 else
369 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000370 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000371 return 1;
372 }
373}
374
375
376void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000377SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000378{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000379 bool needBitsFlag = true;
380
381 if (mop.opHiBits32())
382 toAsm << "%lm(";
383 else if (mop.opLoBits32())
384 toAsm << "%lo(";
385 else if (mop.opHiBits64())
386 toAsm << "%hh(";
387 else if (mop.opLoBits64())
388 toAsm << "%hm(";
389 else
390 needBitsFlag = false;
391
392 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000393 {
394 case MachineOperand::MO_VirtualRegister:
395 case MachineOperand::MO_CCRegister:
396 case MachineOperand::MO_MachineRegister:
397 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000398 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000399
Vikram S. Advefbd21612002-03-31 19:03:58 +0000400 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000401 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
402 toAsm << "<NULL VALUE>";
403 } else {
404 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
405 }
406 break;
407 }
408
409 case MachineOperand::MO_PCRelativeDisp:
410 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000411 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000412 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
413
414 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000416 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000417 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000418 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000419 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000420 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000421 toAsm << getID(CV);
422 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000423 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000424 break;
425 }
426
427 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000428 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000429 break;
430
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000431 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000432 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000433 break;
434
435 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000436 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000437 break;
438 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000439
440 if (needBitsFlag)
441 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000442}
443
444
445void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000446SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000447{
448 unsigned Opcode = MI->getOpCode();
449
450 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
451 return; // IGNORE PHI NODES
452
453 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
454
455 unsigned Mask = getOperandMask(Opcode);
456
457 bool NeedComma = false;
458 unsigned N = 1;
459 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
460 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
461 if (NeedComma) toAsm << ", "; // Handle comma outputing
462 NeedComma = true;
463 N = printOperands(MI, OpNum);
464 }
465 else
466 N = 1;
467
468 toAsm << "\n";
469}
470
471void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000472SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000473{
474 // Emit a label for the basic block
475 toAsm << getID(BB) << ":\n";
476
477 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000478 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000479 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
480
481 // Loop over all of the instructions in the basic block...
482 for (; MII != MIE; ++MII)
483 emitMachineInst(*MII);
484 toAsm << "\n"; // Seperate BB's with newlines
485}
486
487void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000488SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000489{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000490 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000491 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000492 enterSection(AsmPrinter::Text);
493 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
494 //toAsm << "\t.type\t" << methName << ",#function\n";
495 toAsm << "\t.type\t" << methName << ", 2\n";
496 toAsm << methName << ":\n";
497
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000498 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000499 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
500 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000501
502 // Output a .size directive so the debugger knows the extents of the function
503 toAsm << ".EndOf_" << methName << ":\n\t.size "
504 << methName << ", .EndOf_"
505 << methName << "-" << methName << "\n";
506
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000507 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000508 toAsm << "\n\n";
509}
510
511} // End anonymous namespace
512
Chris Lattnerf57b8452002-04-27 06:56:12 +0000513Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000514 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000515}
516
517
518
519
520
521//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000522// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000523//===----------------------------------------------------------------------===//
524
525namespace {
526
527class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
528public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000529 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
530 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000531
Chris Lattner96c466b2002-04-29 14:57:45 +0000532 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
533
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000534 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000535 startModule(M);
536 emitGlobalsAndConstants(M);
537 endModule();
538 return false;
539 }
540
Chris Lattner97e52e42002-04-28 21:27:06 +0000541 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
542 AU.setPreservesAll();
543 }
544
545private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000546 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000547
548 void printGlobalVariable(const GlobalVariable *GV);
549 void printSingleConstant( const Constant* CV);
550 void printConstantValueOnly(const Constant* CV);
551 void printConstant( const Constant* CV, std::string valID = "");
552
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000553 static void FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000554 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000555};
556
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000557
558// Can we treat the specified array as a string? Only if it is an array of
559// ubytes or non-negative sbytes.
560//
Chris Lattner122787b2002-06-05 18:08:26 +0000561static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000562 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
563 if (ETy == Type::UByteTy) return true;
564 if (ETy != Type::SByteTy) return false;
565
566 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000567 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000568 return false;
569
570 return true;
571}
572
573// toOctal - Convert the low order bits of X into an octal letter
574static inline char toOctal(int X) {
575 return (X&7)+'0';
576}
577
578// getAsCString - Return the specified array as a C compatible string, only if
579// the predicate isStringCompatible is true.
580//
Chris Lattner122787b2002-06-05 18:08:26 +0000581static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000582 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000583
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000584 string Result;
585 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
586 Result = "\"";
587 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
588 unsigned char C = (ETy == Type::SByteTy) ?
589 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
590 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
591
Vikram S. Adve242a8082002-05-19 15:25:51 +0000592 if (C == '"') {
593 Result += "\\\"";
594 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000595 Result += C;
596 } else {
597 switch(C) {
598 case '\a': Result += "\\a"; break;
599 case '\b': Result += "\\b"; break;
600 case '\f': Result += "\\f"; break;
601 case '\n': Result += "\\n"; break;
602 case '\r': Result += "\\r"; break;
603 case '\t': Result += "\\t"; break;
604 case '\v': Result += "\\v"; break;
605 default:
606 Result += '\\';
607 Result += toOctal(C >> 6);
608 Result += toOctal(C >> 3);
609 Result += toOctal(C >> 0);
610 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000611 }
612 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000613 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000614 Result += "\"";
615
616 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000617}
618
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000619inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000620ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000621{
622 return (arrayType->getElementType() == Type::UByteTy ||
623 arrayType->getElementType() == Type::SByteTy);
624}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000625
Vikram S. Advee99941a2002-08-22 02:58:36 +0000626
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000627inline const string
628TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000629{
630 switch(type->getPrimitiveID())
631 {
632 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
633 return ".byte";
634 case Type::UShortTyID: case Type::ShortTyID:
635 return ".half";
636 case Type::UIntTyID: case Type::IntTyID:
637 return ".word";
638 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
639 return ".xword";
640 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000641 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000642 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000643 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 case Type::ArrayTyID:
645 if (ArrayTypeIsString((ArrayType*) type))
646 return ".ascii";
647 else
648 return "<InvaliDataTypeForPrinting>";
649 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000650 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000651 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000652}
653
Vikram S. Adve21447222001-11-10 02:03:06 +0000654// Get the size of the constant for the given target.
655// If this is an unsized array, return 0.
656//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000657inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000658ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000659{
Chris Lattner122787b2002-06-05 18:08:26 +0000660 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000661 {
Chris Lattner122787b2002-06-05 18:08:26 +0000662 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000663 if (ArrayTypeIsString(aty))
664 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000665 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000666
667 return target.findOptimalStorageSize(CV->getType());
668}
669
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000670
671
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000672// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000673// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
674//
675inline unsigned int
676SizeToAlignment(unsigned int size, const TargetMachine& target)
677{
678 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
679 if (size > (unsigned) cacheLineSize / 2)
680 return cacheLineSize;
681 else
682 for (unsigned sz=1; /*no condition*/; sz *= 2)
683 if (sz >= size)
684 return sz;
685}
686
687// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000688//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000689inline unsigned int
690TypeToAlignment(const Type* type, const TargetMachine& target)
691{
Vikram S. Adve21447222001-11-10 02:03:06 +0000692 return SizeToAlignment(target.findOptimalStorageSize(type), target);
693}
694
695// Get the size of the constant and then use SizeToAlignment.
696// Handles strings as a special case;
697inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000698ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000699{
Chris Lattner122787b2002-06-05 18:08:26 +0000700 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000701 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
702 return SizeToAlignment(1 + CPA->getNumOperands(), target);
703
704 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000705}
706
707
Vikram S. Adve21447222001-11-10 02:03:06 +0000708// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000709void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000710SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000711{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000712 assert(CV->getType() != Type::VoidTy &&
713 CV->getType() != Type::TypeTy &&
714 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000715 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000716
Chris Lattnerf678dc62002-04-11 21:44:02 +0000717 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
718 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000719
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000720 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000721
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000722 if (CV->getType()->isPrimitiveType())
723 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000724 if (CV->getType()->isFloatingPoint()) {
725 // FP Constants are printed as integer constants to avoid losing
726 // precision...
727 double Val = cast<ConstantFP>(CV)->getValue();
728 if (CV->getType() == Type::FloatTy) {
729 float FVal = (float)Val;
730 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
731 toAsm << *(unsigned int*)ProxyPtr;
732 } else if (CV->getType() == Type::DoubleTy) {
733 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
734 toAsm << *(uint64_t*)ProxyPtr;
735 } else {
736 assert(0 && "Unknown floating point type!");
737 }
738
739 toAsm << "\t! " << CV->getType()->getDescription()
740 << " value: " << Val << "\n";
741 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000742 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000743 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000744 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000745 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
746 { // This is a constant address for a global variable or method.
747 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000748 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000749 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000750 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000751 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000752 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000753 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000754 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
755 { // Constant expression built from operators, constants, and symbolic addrs
756 toAsm << ConstantExprToString(CE, Target) << "\n";
757 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000758 else
759 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000760 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000761 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000762}
763
Vikram S. Adve21447222001-11-10 02:03:06 +0000764// Print a constant value or values (it may be an aggregate).
765// Uses printSingleConstant() to print each individual value.
766void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000767SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000768{
Chris Lattner122787b2002-06-05 18:08:26 +0000769 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000770
771 if (CPA && isStringCompatible(CPA))
772 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000773 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000774 }
775 else if (CPA)
776 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000777 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000778 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000779 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000780 }
Chris Lattner122787b2002-06-05 18:08:26 +0000781 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000782 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000783 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000784 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000785 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000786 }
787 else
Chris Lattner122787b2002-06-05 18:08:26 +0000788 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000789}
790
791// Print a constant (which may be an aggregate) prefixed by all the
792// appropriate directives. Uses printConstantValueOnly() to print the
793// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000794void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000795SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000796{
797 if (valID.length() == 0)
798 valID = getID(CV);
799
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000800 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000801
802 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000803 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000804 if (CPA && isStringCompatible(CPA))
805 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000806 toAsm << valID << ":\n";
807 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000808 return;
809 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000810
Chris Lattner697954c2002-01-20 22:54:45 +0000811 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000812
813 unsigned int constSize = ConstantToSize(CV, Target);
814 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000815 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000816
Chris Lattner697954c2002-01-20 22:54:45 +0000817 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000818
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000819 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000820}
821
822
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000823void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000824 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000825 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
826 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000827 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000828 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000829 MC.insert(pool.begin(), pool.end());
830 }
831}
832
833void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000834{
Chris Lattner697954c2002-01-20 22:54:45 +0000835 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000836
837 if (GV->hasInitializer())
838 printConstant(GV->getInitializer(), getID(GV));
839 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000840 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
841 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000842 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000843 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000844 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000845 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000846 }
847}
848
849
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000850void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000851 // First, get the constants there were marked by the code generator for
852 // inclusion in the assembly code data area and fold them all into a
853 // single constant pool since there may be lots of duplicates. Also,
854 // lets force these constants into the slot table so that we can get
855 // unique names for unnamed constants also.
856 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000857 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000858 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000859
Chris Lattner637ed862002-08-07 21:39:48 +0000860 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000861 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000862 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000863 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000864 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000865
866 // Output global variables...
867 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) {
868 if (GI->hasInitializer() && GI->isConstant()) {
869 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
870 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
Chris Lattner637ed862002-08-07 21:39:48 +0000871 enterSection(AsmPrinter::InitRWData);
872 } else {
873 assert (!GI->hasInitializer() && "Unexpected global variable type found");
874 enterSection(AsmPrinter::UninitRWData); // Uninitialized data
875 }
876 printGlobalVariable(GI);
877 }
878
Chris Lattner697954c2002-01-20 22:54:45 +0000879 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000880}
881
Chris Lattnere88f78c2001-09-19 13:47:27 +0000882} // End anonymous namespace
883
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000884Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
885 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000886}