blob: 6080e99e73f5005975d414bc13e36a24cddfa2dd [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 Lattnerd0fe5f52002-12-28 20:15:01 +000017#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000018#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000019#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000020#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000021#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000022#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000023#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000024#include "Support/StringExtras.h"
Chris Lattner697954c2002-01-20 22:54:45 +000025using std::string;
Chris Lattnere88f78c2001-09-19 13:47:27 +000026
27namespace {
28
Vikram S. Adved198c472002-03-18 03:07:26 +000029class GlobalIdTable: public Annotation {
30 static AnnotationID AnnotId;
31 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000032
Chris Lattner09ff1122002-07-24 21:21:32 +000033 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000034 typedef ValIdMap::const_iterator ValIdMapConstIterator;
35 typedef ValIdMap:: iterator ValIdMapIterator;
36public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000037 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000038 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000039
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000040 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000041};
42
43AnnotationID GlobalIdTable::AnnotId =
44 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
45
46//===---------------------------------------------------------------------===//
47// Code Shared By the two printer passes, as a mixin
48//===---------------------------------------------------------------------===//
49
50class AsmPrinter {
51 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000052public:
Chris Lattner697954c2002-01-20 22:54:45 +000053 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +000054 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +000055
Chris Lattnere88f78c2001-09-19 13:47:27 +000056 enum Sections {
57 Unknown,
58 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000059 ReadOnlyData,
60 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +000061 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000062 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +000063
Chris Lattner59ba1092002-02-04 15:53:23 +000064 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +000065 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
66
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000067 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +000068 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +000069 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +000070 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +000071 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +000072 idTable = new GlobalIdTable(&M);
73 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +000074 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +000075 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000076 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +000077 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +000078 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +000079 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +000080 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000081 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +000082 }
83 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +000084 }
85
Chris Lattner8b1b4e22002-07-16 18:35:16 +000086 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +000087 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +000088 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
89 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +000090 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +000091
Chris Lattnere88f78c2001-09-19 13:47:27 +000092 // enterSection - Use this method to enter a different section of the output
93 // executable. This is used to only output neccesary section transitions.
94 //
95 void enterSection(enum Sections S) {
96 if (S == CurSection) return; // Only switch section if neccesary
97 CurSection = S;
98
Vikram S. Adve29ff8732001-11-08 05:12:37 +000099 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000100 switch (S)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000101 {
102 default: assert(0 && "Bad section name!");
103 case Text: toAsm << "\".text\""; break;
104 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
105 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
106 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
107 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000108 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000109 }
110
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000111 static string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +0000112 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000113
114 // Symbol names in Sparc assembly language have these rules:
115 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
116 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000117 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000118 if (isdigit(S[0]))
119 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000121 for (unsigned i = 0; i < S.size(); ++i)
122 {
123 char C = S[i];
124 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
125 Result += C;
126 else
127 {
128 Result += '_';
129 Result += char('0' + ((unsigned char)C >> 4));
130 Result += char('0' + (C & 0xF));
131 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000132 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000133 return Result;
134 }
135
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000137 // the name of the identifier if possible (qualified by the type), and
138 // use a numbered value based on prefix otherwise.
139 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000140 //
141 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000142 string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000143
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000144 Result += V->hasName() ? V->getName() : string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000145
Vikram S. Adved198c472002-03-18 03:07:26 +0000146 // Qualify all internal names with a unique id.
147 if (!isExternal(V)) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000148 int valId = idTable->Table.getValSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000149 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000150 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
151 if (I == idTable->valToIdMap.end())
152 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000153 else
154 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000155 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000156 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000157
158 // Replace or prefix problem characters in the name
159 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000160 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000161
162 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000164
Chris Lattnere88f78c2001-09-19 13:47:27 +0000165 // getID Wrappers - Ensure consistent usage...
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000166 string getID(const Function *F) {
167 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168 }
169 string getID(const BasicBlock *BB) {
170 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
171 }
172 string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000173 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000174 }
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000175 string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000176 return getID(CV, "LLVMConst_", ".C_");
177 }
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000178 string getID(const GlobalValue *GV) {
179 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
180 return getID(V);
181 else if (const Function *F = dyn_cast<Function>(GV))
182 return getID(F);
183 assert(0 && "Unexpected type of GlobalValue!");
184 return "";
185 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000186
Vikram S. Adve537a8772002-09-05 18:28:10 +0000187 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
188 // and return this as a string.
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000189 string ConstantExprToString(const ConstantExpr* CE,
190 const TargetMachine& target) {
191 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000192 switch(CE->getOpcode()) {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000193 case Instruction::GetElementPtr:
194 { // generate a symbolic expression for the byte address
195 const Value* ptrVal = CE->getOperand(0);
196 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
197 const TargetData &TD = target.getTargetData();
198 S += "(" + valToExprString(ptrVal, target) + ") + ("
199 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
200 break;
201 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000202
Vikram S. Adve537a8772002-09-05 18:28:10 +0000203 case Instruction::Cast:
204 // Support only non-converting casts for now, i.e., a no-op.
205 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000206 assert(target.getTargetData().getTypeSize(CE->getType()) ==
207 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000208 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
209 break;
210
211 case Instruction::Add:
212 S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
213 + valToExprString(CE->getOperand(1), target) + ")";
214 break;
215
Vikram S. Advee99941a2002-08-22 02:58:36 +0000216 default:
217 assert(0 && "Unsupported operator in ConstantExprToString()");
218 break;
219 }
220
221 return S;
222 }
223
224 // valToExprString - Helper function for ConstantExprToString().
225 // Appends result to argument string S.
226 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000227 string valToExprString(const Value* V, const TargetMachine& target) {
228 string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000229 bool failed = false;
230 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
231
232 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000233 S += string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000234 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
235 S += itostr(CI->getValue());
236 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
237 S += utostr(CI->getValue());
238 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
239 S += ftostr(CFP->getValue());
240 else if (isa<ConstantPointerNull>(CV))
241 S += "0";
242 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000243 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000244 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
245 S += ConstantExprToString(CE, target);
246 else
247 failed = true;
248
249 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
250 S += getID(GV);
251 }
252 else
253 failed = true;
254
255 if (failed) {
256 assert(0 && "Cannot convert value to string");
257 S += "<illegal-value>";
258 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000259 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000260 }
261
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000262};
263
264
265
266//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000267// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000268//===----------------------------------------------------------------------===//
269
Chris Lattnerf57b8452002-04-27 06:56:12 +0000270struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000271 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000272 : AsmPrinter(os, t) {}
273
Chris Lattner96c466b2002-04-29 14:57:45 +0000274 const char *getPassName() const {
275 return "Output Sparc Assembly for Functions";
276 }
277
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000278 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000279 startModule(M);
280 return false;
281 }
282
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000283 virtual bool runOnFunction(Function &F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000284 startFunction(F);
285 emitFunction(F);
286 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000287 return false;
288 }
289
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000290 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000291 endModule();
292 return false;
293 }
294
Chris Lattner97e52e42002-04-28 21:27:06 +0000295 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
296 AU.setPreservesAll();
297 }
298
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000299 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000300private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000301 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000302 void emitMachineInst(const MachineInstr *MI);
303
304 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000305 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000306
307 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
308 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000309
Chris Lattnere88f78c2001-09-19 13:47:27 +0000310 unsigned getOperandMask(unsigned Opcode) {
311 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000312 case V9::SUBccr:
313 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000314 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000315 default: return 0; // By default, don't hack operands...
316 }
317 }
318};
319
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000320inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000321SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
322 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000323 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000324 case V9::JMPLCALLr:
325 case V9::JMPLCALLi:
326 case V9::JMPLRETr:
327 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000328 return (opNum == 0);
329 default:
330 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000331 }
332}
333
334
335inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000336SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
337 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000338 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
339 return (opNum == 0);
340 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
341 return (opNum == 1);
342 else
343 return false;
344}
345
346
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000347#define PrintOp1PlusOp2(mop1, mop2, opCode) \
348 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000349 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000350 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000351
352unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000353SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000354 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000355{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000356 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000357
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000358 if (OpIsBranchTargetLabel(MI, opNum))
359 {
360 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
361 return 2;
362 }
363 else if (OpIsMemoryAddressBase(MI, opNum))
364 {
365 toAsm << "[";
366 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
367 toAsm << "]";
368 return 2;
369 }
370 else
371 {
372 printOneOperand(mop, MI->getOpCode());
373 return 1;
374 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000375}
376
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000377void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000378SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
379 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000380{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000381 bool needBitsFlag = true;
382
383 if (mop.opHiBits32())
384 toAsm << "%lm(";
385 else if (mop.opLoBits32())
386 toAsm << "%lo(";
387 else if (mop.opHiBits64())
388 toAsm << "%hh(";
389 else if (mop.opLoBits64())
390 toAsm << "%hm(";
391 else
392 needBitsFlag = false;
393
Chris Lattner133f0792002-10-28 04:45:29 +0000394 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000395 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000396 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000397 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000398 case MachineOperand::MO_MachineRegister:
399 {
400 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000401
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000402 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
403 // better to print code with NULL registers than to die
404 toAsm << "<NULL VALUE>";
405 } else {
406 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
407 }
408 break;
409 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000410
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000411 case MachineOperand::MO_PCRelativeDisp:
412 {
413 const Value *Val = mop.getVRegValue();
414 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000415
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000416 if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
417 toAsm << getID(BB);
418 else if (const Function *M = dyn_cast<Function>(Val))
419 toAsm << getID(M);
420 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
421 toAsm << getID(GV);
422 else if (const Constant *CV = dyn_cast<Constant>(Val))
423 toAsm << getID(CV);
424 else
425 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
426 break;
427 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000428
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000429 case MachineOperand::MO_SignExtendedImmed:
430 toAsm << mop.getImmedValue();
431 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000432
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000433 case MachineOperand::MO_UnextendedImmed:
434 toAsm << (uint64_t) mop.getImmedValue();
435 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000436
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000437 default:
438 toAsm << mop; // use dump field
439 break;
440 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000441
442 if (needBitsFlag)
443 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000444}
445
446
447void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000448SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000449{
450 unsigned Opcode = MI->getOpCode();
451
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000452 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000453 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000454
Chris Lattnerf44f9052002-10-29 17:35:41 +0000455 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000456
457 unsigned Mask = getOperandMask(Opcode);
458
459 bool NeedComma = false;
460 unsigned N = 1;
461 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
462 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000463 if (NeedComma) toAsm << ", "; // Handle comma outputing
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000464 NeedComma = true;
465 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000466 } else
467 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000468
469 toAsm << "\n";
470}
471
472void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000473SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000474{
475 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000476 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000477
478 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000479 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000480 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000481 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000482 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000483}
484
485void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000486SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000487{
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000488 string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000489 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000490 enterSection(AsmPrinter::Text);
491 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
492 //toAsm << "\t.type\t" << methName << ",#function\n";
493 toAsm << "\t.type\t" << methName << ", 2\n";
494 toAsm << methName << ":\n";
495
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000496 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000497 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000498 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000499 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000500
501 // Output a .size directive so the debugger knows the extents of the function
502 toAsm << ".EndOf_" << methName << ":\n\t.size "
503 << methName << ", .EndOf_"
504 << methName << "-" << methName << "\n";
505
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000506 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000507 toAsm << "\n\n";
508}
509
510} // End anonymous namespace
511
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000512Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000513 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000514}
515
516
517
518
519
520//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000521// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000522//===----------------------------------------------------------------------===//
523
524namespace {
525
526class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
527public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000528 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
529 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000530
Chris Lattner96c466b2002-04-29 14:57:45 +0000531 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
532
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000533 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000534 startModule(M);
535 emitGlobalsAndConstants(M);
536 endModule();
537 return false;
538 }
539
Chris Lattner97e52e42002-04-28 21:27:06 +0000540 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
541 AU.setPreservesAll();
542 }
543
544private:
Vikram S. Advefee76262002-10-13 00:32:18 +0000545 void emitGlobalsAndConstants (const Module &M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000546
Vikram S. Advefee76262002-10-13 00:32:18 +0000547 void printGlobalVariable (const GlobalVariable *GV);
548 void PrintZeroBytesToPad (int numBytes);
549 void printSingleConstantValue (const Constant* CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000550 void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000551 void printConstant (const Constant* CV, string valID = "");
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000552
Vikram S. Advefee76262002-10-13 00:32:18 +0000553 static void FoldConstants (const Module &M,
554 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//
Vikram S. Advefee76262002-10-13 00:32:18 +0000561static bool isStringCompatible(const ConstantArray *CVA) {
562 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000563 if (ETy == Type::UByteTy) return true;
564 if (ETy != Type::SByteTy) return false;
565
Vikram S. Advefee76262002-10-13 00:32:18 +0000566 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
567 if (cast<ConstantSInt>(CVA->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//
Vikram S. Advefee76262002-10-13 00:32:18 +0000581static string getAsCString(const ConstantArray *CVA) {
582 assert(isStringCompatible(CVA) && "Array is not string compatible!");
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000583
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000584 string Result;
Vikram S. Advefee76262002-10-13 00:32:18 +0000585 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000586 Result = "\"";
Vikram S. Advefee76262002-10-13 00:32:18 +0000587 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000588 unsigned char C = (ETy == Type::SByteTy) ?
Vikram S. Advefee76262002-10-13 00:32:18 +0000589 (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
590 (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000591
Vikram S. Adve242a8082002-05-19 15:25:51 +0000592 if (C == '"') {
593 Result += "\\\"";
Chris Lattnerd3442422002-10-15 19:56:24 +0000594 } else if (C == '\\') {
595 Result += "\\\\";
Vikram S. Adve242a8082002-05-19 15:25:51 +0000596 } else if (isprint(C)) {
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000597 Result += C;
598 } else {
599 switch(C) {
600 case '\a': Result += "\\a"; break;
601 case '\b': Result += "\\b"; break;
602 case '\f': Result += "\\f"; break;
603 case '\n': Result += "\\n"; break;
604 case '\r': Result += "\\r"; break;
605 case '\t': Result += "\\t"; break;
606 case '\v': Result += "\\v"; break;
607 default:
608 Result += '\\';
609 Result += toOctal(C >> 6);
610 Result += toOctal(C >> 3);
611 Result += toOctal(C >> 0);
612 break;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000613 }
614 }
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000615 }
Chris Lattner4b1de8e2002-04-18 18:15:38 +0000616 Result += "\"";
617
618 return Result;
Chris Lattnercee8f9a2001-11-27 00:03:19 +0000619}
620
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000621inline bool
Chris Lattner122787b2002-06-05 18:08:26 +0000622ArrayTypeIsString(const ArrayType* arrayType)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000623{
624 return (arrayType->getElementType() == Type::UByteTy ||
625 arrayType->getElementType() == Type::SByteTy);
626}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000627
Vikram S. Advee99941a2002-08-22 02:58:36 +0000628
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000629inline const string
630TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000631{
632 switch(type->getPrimitiveID())
633 {
634 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
635 return ".byte";
636 case Type::UShortTyID: case Type::ShortTyID:
637 return ".half";
638 case Type::UIntTyID: case Type::IntTyID:
639 return ".word";
640 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
641 return ".xword";
642 case Type::FloatTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000643 return ".word";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000644 case Type::DoubleTyID:
Chris Lattnerf678dc62002-04-11 21:44:02 +0000645 return ".xword";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000646 case Type::ArrayTyID:
647 if (ArrayTypeIsString((ArrayType*) type))
648 return ".ascii";
649 else
650 return "<InvaliDataTypeForPrinting>";
651 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000652 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000653 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000654}
655
Vikram S. Advefee76262002-10-13 00:32:18 +0000656// Get the size of the type
657//
658inline unsigned int
659TypeToSize(const Type* type, const TargetMachine& target)
660{
661 return target.findOptimalStorageSize(type);
662}
663
Vikram S. Adve21447222001-11-10 02:03:06 +0000664// Get the size of the constant for the given target.
665// If this is an unsized array, return 0.
666//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000667inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000668ConstantToSize(const Constant* CV, const TargetMachine& target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000669{
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000670 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
671 {
672 const ArrayType *aty = cast<ArrayType>(CVA->getType());
673 if (ArrayTypeIsString(aty))
674 return 1 + CVA->getNumOperands();
675 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000676
Vikram S. Advefee76262002-10-13 00:32:18 +0000677 return TypeToSize(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000678}
679
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000680// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000681// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
682//
683inline unsigned int
684SizeToAlignment(unsigned int size, const TargetMachine& target)
685{
686 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
687 if (size > (unsigned) cacheLineSize / 2)
688 return cacheLineSize;
689 else
690 for (unsigned sz=1; /*no condition*/; sz *= 2)
691 if (sz >= size)
692 return sz;
693}
694
695// Get the size of the type and then use SizeToAlignment.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000696//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000697inline unsigned int
698TypeToAlignment(const Type* type, const TargetMachine& target)
699{
Vikram S. Advefee76262002-10-13 00:32:18 +0000700 return SizeToAlignment(TypeToSize(type, target), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000701}
702
703// Get the size of the constant and then use SizeToAlignment.
704// Handles strings as a special case;
705inline unsigned int
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000706ConstantToAlignment(const Constant* CV, const TargetMachine& target)
Vikram S. Adve21447222001-11-10 02:03:06 +0000707{
Vikram S. Advefee76262002-10-13 00:32:18 +0000708 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
709 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
710 return SizeToAlignment(1 + CVA->getNumOperands(), target);
Vikram S. Adve21447222001-11-10 02:03:06 +0000711
712 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000713}
714
715
Vikram S. Adve21447222001-11-10 02:03:06 +0000716// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000717void
Vikram S. Advefee76262002-10-13 00:32:18 +0000718SparcModuleAsmPrinter::printSingleConstantValue(const Constant* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000719{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000720 assert(CV->getType() != Type::VoidTy &&
721 CV->getType() != Type::TypeTy &&
722 CV->getType() != Type::LabelTy &&
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000723 "Unexpected type for Constant");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000724
Chris Lattnerf678dc62002-04-11 21:44:02 +0000725 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
726 && "Aggregate types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000727
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000728 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000729
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000730 if (CV->getType()->isPrimitiveType())
731 {
732 if (CV->getType()->isFloatingPoint()) {
733 // FP Constants are printed as integer constants to avoid losing
734 // precision...
735 double Val = cast<ConstantFP>(CV)->getValue();
736 if (CV->getType() == Type::FloatTy) {
737 float FVal = (float)Val;
738 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
739 toAsm << *(unsigned int*)ProxyPtr;
740 } else if (CV->getType() == Type::DoubleTy) {
741 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
742 toAsm << *(uint64_t*)ProxyPtr;
743 } else {
744 assert(0 && "Unknown floating point type!");
745 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000746
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000747 toAsm << "\t! " << CV->getType()->getDescription()
748 << " value: " << Val << "\n";
749 } else {
750 WriteAsOperand(toAsm, CV, false, false) << "\n";
751 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000752 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000753 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
754 { // This is a constant address for a global variable or method.
755 // Use the name of the variable or method as the address value.
756 toAsm << getID(CPR->getValue()) << "\n";
757 }
758 else if (isa<ConstantPointerNull>(CV))
759 { // Null pointer value
760 toAsm << "0\n";
761 }
762 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
763 { // Constant expression built from operators, constants, and symbolic addrs
764 toAsm << ConstantExprToString(CE, Target) << "\n";
765 }
766 else
767 {
768 assert(0 && "Unknown elementary type for constant");
769 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000770}
771
Vikram S. Adve21447222001-11-10 02:03:06 +0000772void
Vikram S. Advefee76262002-10-13 00:32:18 +0000773SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
Vikram S. Adve21447222001-11-10 02:03:06 +0000774{
Vikram S. Advefee76262002-10-13 00:32:18 +0000775 for ( ; numBytes >= 8; numBytes -= 8)
776 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
777
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000778 if (numBytes >= 4)
779 {
780 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
781 numBytes -= 4;
782 }
Vikram S. Advefee76262002-10-13 00:32:18 +0000783
784 while (numBytes--)
785 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
786}
787
788// Print a constant value or values (it may be an aggregate).
789// Uses printSingleConstantValue() to print each individual value.
790void
791SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
Vikram S. Adve9e498242003-05-25 21:59:09 +0000792 int numPadBytesAfter /* = 0*/)
Vikram S. Advefee76262002-10-13 00:32:18 +0000793{
794 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
795
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000796 if (CVA && isStringCompatible(CVA))
797 { // print the string alone and return
798 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000799 }
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000800 else if (CVA)
801 { // Not a string. Print the values in successive locations
802 const std::vector<Use> &constValues = CVA->getValues();
803 for (unsigned i=0; i < constValues.size(); i++)
804 printConstantValueOnly(cast<Constant>(constValues[i].get()));
805 }
806 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
807 { // Print the fields in successive locations. Pad to align if needed!
808 const StructLayout *cvsLayout =
809 Target.getTargetData().getStructLayout(CVS->getType());
810 const std::vector<Use>& constValues = CVS->getValues();
811 unsigned sizeSoFar = 0;
812 for (unsigned i=0, N = constValues.size(); i < N; i++)
813 {
814 const Constant* field = cast<Constant>(constValues[i].get());
815
816 // Check if padding is needed and insert one or more 0s.
817 unsigned fieldSize =
818 Target.getTargetData().getTypeSize(field->getType());
819 int padSize = ((i == N-1? cvsLayout->StructSize
820 : cvsLayout->MemberOffsets[i+1])
821 - cvsLayout->MemberOffsets[i]) - fieldSize;
822 sizeSoFar += (fieldSize + padSize);
823
824 // Now print the actual field value
825 printConstantValueOnly(field, padSize);
826 }
827 assert(sizeSoFar == cvsLayout->StructSize &&
828 "Layout of constant struct may be incorrect!");
829 }
830 else
Vikram S. Advefee76262002-10-13 00:32:18 +0000831 printSingleConstantValue(CV);
Vikram S. Adve9e498242003-05-25 21:59:09 +0000832
833 if (numPadBytesAfter)
834 PrintZeroBytesToPad(numPadBytesAfter);
Vikram S. Adve21447222001-11-10 02:03:06 +0000835}
836
837// Print a constant (which may be an aggregate) prefixed by all the
838// appropriate directives. Uses printConstantValueOnly() to print the
839// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000840void
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000841SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000842{
843 if (valID.length() == 0)
844 valID = getID(CV);
845
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000846 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000847
848 // Print .size and .type only if it is not a string.
Vikram S. Advefee76262002-10-13 00:32:18 +0000849 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000850 if (CVA && isStringCompatible(CVA))
851 { // print it as a string and return
852 toAsm << valID << ":\n";
853 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
854 return;
855 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000856
Chris Lattner697954c2002-01-20 22:54:45 +0000857 toAsm << "\t.type" << "\t" << valID << ",#object\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000858
859 unsigned int constSize = ConstantToSize(CV, Target);
860 if (constSize)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000861 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
Vikram S. Adve21447222001-11-10 02:03:06 +0000862
Chris Lattner697954c2002-01-20 22:54:45 +0000863 toAsm << valID << ":\n";
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000864
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000865 printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000866}
867
868
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000869void SparcModuleAsmPrinter::FoldConstants(const Module &M,
Chris Lattner09ff1122002-07-24 21:21:32 +0000870 hash_set<const Constant*> &MC) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000871 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
872 if (!I->isExternal()) {
Chris Lattner09ff1122002-07-24 21:21:32 +0000873 const hash_set<const Constant*> &pool =
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000874 MachineFunction::get(I).getInfo()->getConstantPoolValues();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000875 MC.insert(pool.begin(), pool.end());
876 }
877}
878
879void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000880{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000881 if (GV->hasExternalLinkage())
882 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000883
Vikram S. Advefee76262002-10-13 00:32:18 +0000884 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000885 printConstant(GV->getInitializer(), getID(GV));
886 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000887 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
888 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000889 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000890 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000891 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000892 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000893 }
894}
895
896
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000897void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module &M) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000898 // First, get the constants there were marked by the code generator for
899 // inclusion in the assembly code data area and fold them all into a
900 // single constant pool since there may be lots of duplicates. Also,
901 // lets force these constants into the slot table so that we can get
902 // unique names for unnamed constants also.
903 //
Chris Lattner09ff1122002-07-24 21:21:32 +0000904 hash_set<const Constant*> moduleConstants;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000905 FoldConstants(M, moduleConstants);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000906
Chris Lattner637ed862002-08-07 21:39:48 +0000907 // Output constants spilled to memory
Vikram S. Advefbd21612002-03-31 19:03:58 +0000908 enterSection(AsmPrinter::ReadOnlyData);
Chris Lattner637ed862002-08-07 21:39:48 +0000909 for (hash_set<const Constant*>::const_iterator I = moduleConstants.begin(),
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000910 E = moduleConstants.end(); I != E; ++I)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000911 printConstant(*I);
Chris Lattner637ed862002-08-07 21:39:48 +0000912
913 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000914 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
915 if (! GI->isExternal()) {
916 assert(GI->hasInitializer());
917 if (GI->isConstant())
918 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
919 else if (GI->getInitializer()->isNullValue())
920 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
921 else
922 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
923
924 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000925 }
Chris Lattner637ed862002-08-07 21:39:48 +0000926
Chris Lattner697954c2002-01-20 22:54:45 +0000927 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000928}
929
Chris Lattnere88f78c2001-09-19 13:47:27 +0000930} // End anonymous namespace
931
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000932Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000933 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000934}