blob: 680e7d20b52b1e34d6936819356ab13878347722 [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;
110 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; 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
189 // ConstantExprToString() - Convert a ConstantExpr to a C expression, and
190 // return this as a string.
191 //
192 std::string ConstantExprToString(const ConstantExpr* CE,
193 const TargetMachine& target) {
194 std::string S("");
195
196 switch(CE->getOpcode()) {
197 case Instruction::GetElementPtr:
198 {
199 const Value* ptrVal = CE->getOperand(0);
200 valToExprString(ptrVal, target, S);
201 std::vector<Value*> idxVec = CE->copyOperands();
202 idxVec.erase(idxVec.begin());
203 uint64_t byteOffset = target.DataLayout.getIndexedOffset(ptrVal->getType(),
204 idxVec);
205 uint64_t eltSize = target.DataLayout.getTypeSize(
206 cast<PointerType>(ptrVal->getType())->getElementType());
207 S += " + " + utostr(byteOffset / eltSize);
208 break;
209 }
210
211 default:
212 assert(0 && "Unsupported operator in ConstantExprToString()");
213 break;
214 }
215
216 return S;
217 }
218
219 // valToExprString - Helper function for ConstantExprToString().
220 // Appends result to argument string S.
221 //
222 void valToExprString(const Value* V, const TargetMachine& target,
223 std::string& S) {
224 bool failed = false;
225 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
226
227 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
228 S += std::string(CB == ConstantBool::True ? "1" : "0");
229 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
230 S += itostr(CI->getValue());
231 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
232 S += utostr(CI->getValue());
233 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
234 S += ftostr(CFP->getValue());
235 else if (isa<ConstantPointerNull>(CV))
236 S += "0";
237 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
238 valToExprString(CPR->getValue(), target, S);
239 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
240 S += ConstantExprToString(CE, target);
241 else
242 failed = true;
243
244 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
245 S += getID(GV);
246 }
247 else
248 failed = true;
249
250 if (failed) {
251 assert(0 && "Cannot convert value to string");
252 S += "<illegal-value>";
253 }
254 }
255
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000256};
257
258
259
260//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000261// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000262//===----------------------------------------------------------------------===//
263
Chris Lattnerf57b8452002-04-27 06:56:12 +0000264struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000265 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000266 : AsmPrinter(os, t) {}
267
Chris Lattner96c466b2002-04-29 14:57:45 +0000268 const char *getPassName() const {
269 return "Output Sparc Assembly for Functions";
270 }
271
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000272 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000273 startModule(M);
274 return false;
275 }
276
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000277 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000278 startFunction(F);
279 emitFunction(F);
280 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000281 return false;
282 }
283
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000284 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000285 endModule();
286 return false;
287 }
288
Chris Lattner97e52e42002-04-28 21:27:06 +0000289 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
290 AU.setPreservesAll();
291 }
292
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000293 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000294private :
295 void emitBasicBlock(const BasicBlock *BB);
296 void emitMachineInst(const MachineInstr *MI);
297
298 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
299 void printOneOperand(const MachineOperand &Op);
300
301 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
302 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000303
Chris Lattnere88f78c2001-09-19 13:47:27 +0000304 unsigned getOperandMask(unsigned Opcode) {
305 switch (Opcode) {
306 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000307 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000308 default: return 0; // By default, don't hack operands...
309 }
310 }
311};
312
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000313inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000314SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
315 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000316 switch (MI->getOpCode()) {
317 case JMPLCALL:
318 case JMPLRET: return (opNum == 0);
319 default: return false;
320 }
321}
322
323
324inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000325SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
326 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000327 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
328 return (opNum == 0);
329 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
330 return (opNum == 1);
331 else
332 return false;
333}
334
335
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000336#define PrintOp1PlusOp2(mop1, mop2) \
337 printOneOperand(mop1); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000338 toAsm << "+"; \
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000339 printOneOperand(mop2);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000340
341unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000342SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000343 unsigned int opNum)
344{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000345 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000346
347 if (OpIsBranchTargetLabel(MI, opNum))
348 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000349 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000350 return 2;
351 }
352 else if (OpIsMemoryAddressBase(MI, opNum))
353 {
354 toAsm << "[";
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000355 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000356 toAsm << "]";
357 return 2;
358 }
359 else
360 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000361 printOneOperand(mop);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000362 return 1;
363 }
364}
365
366
367void
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000368SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000369{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000370 bool needBitsFlag = true;
371
372 if (mop.opHiBits32())
373 toAsm << "%lm(";
374 else if (mop.opLoBits32())
375 toAsm << "%lo(";
376 else if (mop.opHiBits64())
377 toAsm << "%hh(";
378 else if (mop.opLoBits64())
379 toAsm << "%hm(";
380 else
381 needBitsFlag = false;
382
383 switch (mop.getOperandType())
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000384 {
385 case MachineOperand::MO_VirtualRegister:
386 case MachineOperand::MO_CCRegister:
387 case MachineOperand::MO_MachineRegister:
388 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000389 int RegNum = (int)mop.getAllocatedRegNum();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000390
Vikram S. Advefbd21612002-03-31 19:03:58 +0000391 // better to print code with NULL registers than to die
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000392 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
393 toAsm << "<NULL VALUE>";
394 } else {
395 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
396 }
397 break;
398 }
399
400 case MachineOperand::MO_PCRelativeDisp:
401 {
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000402 const Value *Val = mop.getVRegValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000403 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
404
405 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000406 toAsm << getID(BB);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000407 else if (const Function *M = dyn_cast<Function>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000408 toAsm << getID(M);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000409 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000410 toAsm << getID(GV);
Chris Lattner31bcdb82002-04-28 19:55:58 +0000411 else if (const Constant *CV = dyn_cast<Constant>(Val))
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000412 toAsm << getID(CV);
413 else
Vikram S. Adve242a8082002-05-19 15:25:51 +0000414 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000415 break;
416 }
417
418 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000419 toAsm << mop.getImmedValue();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000420 break;
421
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000422 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000423 toAsm << (uint64_t) mop.getImmedValue();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000424 break;
425
426 default:
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000427 toAsm << mop; // use dump field
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000428 break;
429 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000430
431 if (needBitsFlag)
432 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000433}
434
435
436void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000437SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000438{
439 unsigned Opcode = MI->getOpCode();
440
441 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
442 return; // IGNORE PHI NODES
443
444 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
445
446 unsigned Mask = getOperandMask(Opcode);
447
448 bool NeedComma = false;
449 unsigned N = 1;
450 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
451 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
452 if (NeedComma) toAsm << ", "; // Handle comma outputing
453 NeedComma = true;
454 N = printOperands(MI, OpNum);
455 }
456 else
457 N = 1;
458
459 toAsm << "\n";
460}
461
462void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000463SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000464{
465 // Emit a label for the basic block
466 toAsm << getID(BB) << ":\n";
467
468 // Get the vector of machine instructions corresponding to this bb.
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000469 const MachineCodeForBasicBlock &MIs = MachineCodeForBasicBlock::get(BB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000470 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
471
472 // Loop over all of the instructions in the basic block...
473 for (; MII != MIE; ++MII)
474 emitMachineInst(*MII);
475 toAsm << "\n"; // Seperate BB's with newlines
476}
477
478void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000479SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000480{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000481 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000482 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000483 enterSection(AsmPrinter::Text);
484 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
485 //toAsm << "\t.type\t" << methName << ",#function\n";
486 toAsm << "\t.type\t" << methName << ", 2\n";
487 toAsm << methName << ":\n";
488
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000489 // Output code for all of the basic blocks in the function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000490 for (Function::const_iterator I = F.begin(), E = F.end(); I != E; ++I)
491 emitBasicBlock(I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000492
493 // Output a .size directive so the debugger knows the extents of the function
494 toAsm << ".EndOf_" << methName << ":\n\t.size "
495 << methName << ", .EndOf_"
496 << methName << "-" << methName << "\n";
497
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000498 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000499 toAsm << "\n\n";
500}
501
502} // End anonymous namespace
503
Chris Lattnerf57b8452002-04-27 06:56:12 +0000504Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000505 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000506}
507
508
509
510
511
512//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000513// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000514//===----------------------------------------------------------------------===//
515
516namespace {
517
518class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
519public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000520 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
521 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000522
Chris Lattner96c466b2002-04-29 14:57:45 +0000523 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
524
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000525 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000526 startModule(M);
527 emitGlobalsAndConstants(M);
528 endModule();
529 return false;
530 }
531
Chris Lattner97e52e42002-04-28 21:27:06 +0000532 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
533 AU.setPreservesAll();
534 }
535
536private:
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000537 void emitGlobalsAndConstants(const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000538
539 void printGlobalVariable(const GlobalVariable *GV);
540 void printSingleConstant( const Constant* CV);
541 void printConstantValueOnly(const Constant* CV);
542 void printConstant( const Constant* CV, std::string valID = "");
543
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000544 static void FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000545 hash_set<const Constant*> &moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000546};
547
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000548
549// Can we treat the specified array as a string? Only if it is an array of
550// ubytes or non-negative sbytes.
551//
Chris Lattner122787b2002-06-05 18:08:26 +0000552static bool isStringCompatible(const ConstantArray *CPA) {
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000553 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
554 if (ETy == Type::UByteTy) return true;
555 if (ETy != Type::SByteTy) return false;
556
557 for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000558 if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000559 return false;
560
561 return true;
562}
563
564// toOctal - Convert the low order bits of X into an octal letter
565static inline char toOctal(int X) {
566 return (X&7)+'0';
567}
568
569// getAsCString - Return the specified array as a C compatible string, only if
570// the predicate isStringCompatible is true.
571//
Chris Lattner122787b2002-06-05 18:08:26 +0000572static string getAsCString(const ConstantArray *CPA) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000573 assert(isStringCompatible(CPA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000574
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000575 string Result;
576 const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
577 Result = "\"";
578 for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
579 unsigned char C = (ETy == Type::SByteTy) ?
580 (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
581 (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
582
Vikram S. Adve242a8082002-05-19 15:25:51 +0000583 if (C == '"') {
584 Result += "\\\"";
585 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000586 Result += C;
587 } else {
588 switch(C) {
589 case '\a': Result += "\\a"; break;
590 case '\b': Result += "\\b"; break;
591 case '\f': Result += "\\f"; break;
592 case '\n': Result += "\\n"; break;
593 case '\r': Result += "\\r"; break;
594 case '\t': Result += "\\t"; break;
595 case '\v': Result += "\\v"; break;
596 default:
597 Result += '\\';
598 Result += toOctal(C >> 6);
599 Result += toOctal(C >> 3);
600 Result += toOctal(C >> 0);
601 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000602 }
603 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000604 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000605 Result += "\"";
606
607 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000608}
609
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000610inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000611ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000612{
613 return (arrayType->getElementType() == Type::UByteTy ||
614 arrayType->getElementType() == Type::SByteTy);
615}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000616
Vikram S. Advee99941a2002-08-22 02:58:36 +0000617
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000618inline const string
619TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000620{
621 switch(type->getPrimitiveID())
622 {
623 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
624 return ".byte";
625 case Type::UShortTyID: case Type::ShortTyID:
626 return ".half";
627 case Type::UIntTyID: case Type::IntTyID:
628 return ".word";
629 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
630 return ".xword";
631 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000632 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000633 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000634 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000635 case Type::ArrayTyID:
636 if (ArrayTypeIsString((ArrayType*) type))
637 return ".ascii";
638 else
639 return "<InvaliDataTypeForPrinting>";
640 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000641 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000642 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000643}
644
Vikram S. Adve21447222001-11-10 02:03:06 +0000645// Get the size of the constant for the given target.
646// If this is an unsized array, return 0.
647//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000648inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000649ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000650{
Chris Lattner122787b2002-06-05 18:08:26 +0000651 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000652 {
Chris Lattner122787b2002-06-05 18:08:26 +0000653 const ArrayType *aty = cast<ArrayType>(CPA->getType());
Vikram S. Adve21447222001-11-10 02:03:06 +0000654 if (ArrayTypeIsString(aty))
655 return 1 + CPA->getNumOperands();
Vikram S. Adve21447222001-11-10 02:03:06 +0000656 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000657
658 return target.findOptimalStorageSize(CV->getType());
659}
660
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000661
662
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000663// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000664// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
665//
666inline unsigned int
667SizeToAlignment(unsigned int size, const TargetMachine& target)
668{
669 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
670 if (size > (unsigned) cacheLineSize / 2)
671 return cacheLineSize;
672 else
673 for (unsigned sz=1; /*no condition*/; sz *= 2)
674 if (sz >= size)
675 return sz;
676}
677
678// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000679//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000680inline unsigned int
681TypeToAlignment(const Type* type, const TargetMachine& target)
682{
Vikram S. Adve21447222001-11-10 02:03:06 +0000683 return SizeToAlignment(target.findOptimalStorageSize(type), target);
684}
685
686// Get the size of the constant and then use SizeToAlignment.
687// Handles strings as a special case;
688inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000689ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000690{
Chris Lattner122787b2002-06-05 18:08:26 +0000691 if (const ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000692 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
693 return SizeToAlignment(1 + CPA->getNumOperands(), target);
694
695 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000696}
697
698
Vikram S. Adve21447222001-11-10 02:03:06 +0000699// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000700void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000701SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000702{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000703 assert(CV->getType() != Type::VoidTy &&
704 CV->getType() != Type::TypeTy &&
705 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000706 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000707
Chris Lattnerf678dc62002-04-11 21:44:02 +0000708 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
709 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000710
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000711 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000712
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000713 if (CV->getType()->isPrimitiveType())
714 {
Chris Lattnerf678dc62002-04-11 21:44:02 +0000715 if (CV->getType()->isFloatingPoint()) {
716 // FP Constants are printed as integer constants to avoid losing
717 // precision...
718 double Val = cast<ConstantFP>(CV)->getValue();
719 if (CV->getType() == Type::FloatTy) {
720 float FVal = (float)Val;
721 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
722 toAsm << *(unsigned int*)ProxyPtr;
723 } else if (CV->getType() == Type::DoubleTy) {
724 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
725 toAsm << *(uint64_t*)ProxyPtr;
726 } else {
727 assert(0 && "Unknown floating point type!");
728 }
729
730 toAsm << "\t! " << CV->getType()->getDescription()
731 << " value: " << Val << "\n";
732 } else {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000733 WriteAsOperand(toAsm, CV, false, false) << "\n";
Chris Lattnerf678dc62002-04-11 21:44:02 +0000734 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000735 }
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000736 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
737 { // This is a constant address for a global variable or method.
738 // Use the name of the variable or method as the address value.
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000739 toAsm << getID(CPR->getValue()) << "\n";
Vikram S. Adveec0de5c2002-07-09 12:30:29 +0000740 }
Chris Lattner5a905e22002-08-20 17:02:48 +0000741 else if (isa<ConstantPointerNull>(CV))
Vikram S. Advee99941a2002-08-22 02:58:36 +0000742 { // Null pointer value
Chris Lattner697954c2002-01-20 22:54:45 +0000743 toAsm << "0\n";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000744 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000745 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
746 { // Constant expression built from operators, constants, and symbolic addrs
747 toAsm << ConstantExprToString(CE, Target) << "\n";
748 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000749 else
750 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000751 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000752 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000753}
754
Vikram S. Adve21447222001-11-10 02:03:06 +0000755// Print a constant value or values (it may be an aggregate).
756// Uses printSingleConstant() to print each individual value.
757void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000758SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
Vikram S. Adve21447222001-11-10 02:03:06 +0000759{
Chris Lattner122787b2002-06-05 18:08:26 +0000760 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000761
762 if (CPA && isStringCompatible(CPA))
763 { // print the string alone and return
Chris Lattner697954c2002-01-20 22:54:45 +0000764 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000765 }
766 else if (CPA)
767 { // Not a string. Print the values in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000768 const std::vector<Use> &constValues = CPA->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000769 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000770 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000771 }
Chris Lattner122787b2002-06-05 18:08:26 +0000772 else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
Vikram S. Adve21447222001-11-10 02:03:06 +0000773 { // Print the fields in successive locations
Chris Lattner697954c2002-01-20 22:54:45 +0000774 const std::vector<Use>& constValues = CPS->getValues();
Vikram S. Adve242a8082002-05-19 15:25:51 +0000775 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattner122787b2002-06-05 18:08:26 +0000776 printConstantValueOnly(cast<Constant>(constValues[i].get()));
Vikram S. Adve21447222001-11-10 02:03:06 +0000777 }
778 else
Chris Lattner122787b2002-06-05 18:08:26 +0000779 printSingleConstant(CV);
Vikram S. Adve21447222001-11-10 02:03:06 +0000780}
781
782// Print a constant (which may be an aggregate) prefixed by all the
783// appropriate directives. Uses printConstantValueOnly() to print the
784// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000785void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000786SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000787{
788 if (valID.length() == 0)
789 valID = getID(CV);
790
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000791 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000792
793 // Print .size and .type only if it is not a string.
Chris Lattner122787b2002-06-05 18:08:26 +0000794 const ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000795 if (CPA && isStringCompatible(CPA))
796 { // print it as a string and return
Chris Lattner697954c2002-01-20 22:54:45 +0000797 toAsm << valID << ":\n";
798 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000799 return;
800 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000801
Chris Lattner697954c2002-01-20 22:54:45 +0000802 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000803
804 unsigned int constSize = ConstantToSize(CV, Target);
805 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000806 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000807
Chris Lattner697954c2002-01-20 22:54:45 +0000808 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000809
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000810 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000811}
812
813
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000814void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000815 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000816 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
817 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000818 const hash_set<const Constant*> &pool =
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000819 MachineCodeForMethod::get(I).getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000820 MC.insert(pool.begin(), pool.end());
821 }
822}
823
824void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000825{
Chris Lattner697954c2002-01-20 22:54:45 +0000826 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000827
828 if (GV->hasInitializer())
829 printConstant(GV->getInitializer(), getID(GV));
830 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000831 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
832 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000833 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000834 toAsm << "\t.reserve\t" << getID(GV) << ","
Chris Lattnerc019a172002-02-03 07:48:06 +0000835 << Target.findOptimalStorageSize(GV->getType()->getElementType())
Chris Lattner697954c2002-01-20 22:54:45 +0000836 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000837 }
838}
839
840
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000841void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000842 // First, get the constants there were marked by the code generator for
843 // inclusion in the assembly code data area and fold them all into a
844 // single constant pool since there may be lots of duplicates. Also,
845 // lets force these constants into the slot table so that we can get
846 // unique names for unnamed constants also.
847 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000848 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000849 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000850
Chris Lattner637ed862002-08-07 21:39:48 +0000851 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000852 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000853 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000854 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000855 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000856
857 // Output global variables...
858 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) {
859 if (GI->hasInitializer() && GI->isConstant()) {
860 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
861 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
862 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
863 } else if (GI->hasInitializer() && !GI->isConstant()) { // read-write data
864 enterSection(AsmPrinter::InitRWData);
865 } else {
866 assert (!GI->hasInitializer() && "Unexpected global variable type found");
867 enterSection(AsmPrinter::UninitRWData); // Uninitialized data
868 }
869 printGlobalVariable(GI);
870 }
871
Chris Lattner697954c2002-01-20 22:54:45 +0000872 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000873}
874
Chris Lattnere88f78c2001-09-19 13:47:27 +0000875} // End anonymous namespace
876
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000877Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
878 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000879}