blob: 6c7ebaa99c4183cff16695a2c19a85b9c1646fe8 [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//
7// The entry point of this file is the UltraSparc::emitAssembly method.
8//
9//===----------------------------------------------------------------------===//
10
11#include "SparcInternals.h"
12#include "llvm/Analysis/SlotCalculator.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000013#include "llvm/Transforms/Linker.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000014#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000015#include "llvm/GlobalVariable.h"
16#include "llvm/GlobalValue.h"
17#include "llvm/ConstPoolVals.h"
18#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000019#include "llvm/BasicBlock.h"
20#include "llvm/Method.h"
21#include "llvm/Module.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000022#include "llvm/Support/HashExtras.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000023#include "llvm/Support/StringExtras.h"
Vikram S. Adve29ff8732001-11-08 05:12:37 +000024#include <locale.h>
Chris Lattnere88f78c2001-09-19 13:47:27 +000025
26namespace {
27
Chris Lattnere88f78c2001-09-19 13:47:27 +000028
Vikram S. Adve953c83e2001-10-28 21:38:52 +000029class SparcAsmPrinter {
30 typedef hash_map<const Value*, int> ValIdMap;
31 typedef ValIdMap:: iterator ValIdMapIterator;
32 typedef ValIdMap::const_iterator ValIdMapConstIterator;
33
Vikram S. Adve29ff8732001-11-08 05:12:37 +000034 ostream &toAsm;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000035 SlotCalculator Table; // map anonymous values to unique integer IDs
36 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
37 const UltraSparc &Target;
38
Chris Lattnere88f78c2001-09-19 13:47:27 +000039 enum Sections {
40 Unknown,
41 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +000042 ReadOnlyData,
43 InitRWData,
44 UninitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +000045 } CurSection;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000046
Chris Lattnere88f78c2001-09-19 13:47:27 +000047public:
Chris Lattnerec0a95f2001-10-15 15:54:43 +000048 inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
Vikram S. Adve29ff8732001-11-08 05:12:37 +000049 : toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
Chris Lattnere88f78c2001-09-19 13:47:27 +000050 emitModule(M);
51 }
52
53private :
54 void emitModule(const Module *M);
Chris Lattnere88f78c2001-09-19 13:47:27 +000055 void emitMethod(const Method *M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +000056 void emitGlobalsAndConstants(const Module* module);
Chris Lattnere88f78c2001-09-19 13:47:27 +000057 //void processMethodArgument(const MethodArgument *MA);
58 void emitBasicBlock(const BasicBlock *BB);
59 void emitMachineInst(const MachineInstr *MI);
Chris Lattnere88f78c2001-09-19 13:47:27 +000060
Vikram S. Adve953c83e2001-10-28 21:38:52 +000061 void printGlobalVariable(const GlobalVariable* GV);
62 void printConstant(const ConstPoolVal* CV, string valID = string(""));
63
64 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
65 void printOneOperand(const MachineOperand &Op);
Chris Lattnere88f78c2001-09-19 13:47:27 +000066
Vikram S. Adve953c83e2001-10-28 21:38:52 +000067 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
68 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
69
Chris Lattnere88f78c2001-09-19 13:47:27 +000070 // enterSection - Use this method to enter a different section of the output
71 // executable. This is used to only output neccesary section transitions.
72 //
73 void enterSection(enum Sections S) {
74 if (S == CurSection) return; // Only switch section if neccesary
75 CurSection = S;
76
Vikram S. Adve29ff8732001-11-08 05:12:37 +000077 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000078 switch (S)
79 {
80 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000081 case Text: toAsm << "\".text\""; break;
82 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
83 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
84 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000085 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000086 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000087 }
88
Vikram S. Adve29ff8732001-11-08 05:12:37 +000089 string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000090 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000091
92 // Symbol names in Sparc assembly language have these rules:
93 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
94 // (b) A name beginning in "." is treated as a local name.
95 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
96 //
97 if (S[0] == '_' || isdigit(S[0]))
98 Result += "ll";
99
100 for (unsigned i = 0; i < S.size(); ++i)
101 {
102 char C = S[i];
103 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
104 Result += C;
105 else
106 {
107 Result += '_';
108 Result += char('0' + ((unsigned char)C >> 4));
109 Result += char('0' + (C & 0xF));
110 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000111 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000112 return Result;
113 }
114
Chris Lattnere88f78c2001-09-19 13:47:27 +0000115 // getID - Return a valid identifier for the specified value. Base it on
116 // the name of the identifier if possible, use a numbered value based on
117 // prefix otherwise. FPrefix is always prepended to the output identifier.
118 //
119 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000120 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000121 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
122 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000123 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000124 } else {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000125 int valId = Table.getValSlot(V);
126 if (valId == -1) {
127 ValIdMapConstIterator I = valToIdMap.find(V);
128 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
129 : (*I).second;
130 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000131 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000132 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000133 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000134 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000135
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000137 string getID(const Module *M) {
138 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000139 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000140 string getID(const Method *M) {
141 return getID(M, "LLVMMethod_");
142 }
143 string getID(const BasicBlock *BB) {
144 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
145 }
146 string getID(const GlobalVariable *GV) {
147 return getID(GV, "LLVMGlobal_", ".G_");
148 }
149 string getID(const ConstPoolVal *CV) {
150 return getID(CV, "LLVMConst_", ".C_");
151 }
152
Chris Lattnere88f78c2001-09-19 13:47:27 +0000153 unsigned getOperandMask(unsigned Opcode) {
154 switch (Opcode) {
155 case SUBcc: return 1 << 3; // Remove CC argument
Chris Lattnerc56d7792001-09-28 15:07:24 +0000156 case BA: case BRZ: // Remove Arg #0, which is always null or xcc
157 case BRLEZ: case BRLZ:
158 case BRNZ: case BRGZ:
159 case BRGEZ: return 1 << 0;
Chris Lattner39f501c2001-10-01 02:32:34 +0000160
Chris Lattnere88f78c2001-09-19 13:47:27 +0000161 default: return 0; // By default, don't hack operands...
162 }
163 }
164};
165
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000166inline bool
167SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
168 unsigned int opNum) {
169 switch (MI->getOpCode()) {
170 case JMPLCALL:
171 case JMPLRET: return (opNum == 0);
172 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000173 }
174}
175
176
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000177inline bool
178SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
179 unsigned int opNum) {
180 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
181 return (opNum == 0);
182 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
183 return (opNum == 1);
184 else
185 return false;
186}
187
188
189#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000190 printOneOperand(Op1); toAsm << "+"; printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000191
192
193unsigned int
194SparcAsmPrinter::printOperands(const MachineInstr *MI,
195 unsigned int opNum)
196{
197 const MachineOperand& Op = MI->getOperand(opNum);
198
199 if (OpIsBranchTargetLabel(MI, opNum))
200 {
201 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
202 return 2;
203 }
204 else if (OpIsMemoryAddressBase(MI, opNum))
205 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000206 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000207 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000208 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000209 return 2;
210 }
211 else
212 {
213 printOneOperand(Op);
214 return 1;
215 }
216}
217
218
219void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000220SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000221{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000222 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000223 {
224 case MachineOperand::MO_VirtualRegister:
225 case MachineOperand::MO_CCRegister:
226 case MachineOperand::MO_MachineRegister:
227 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000228 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000229
230 // ****this code is temporary till NULL Values are fixed
231 if (RegNum == 10000) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000232 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000233 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000234 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000235 }
236 break;
237 }
238
239 case MachineOperand::MO_PCRelativeDisp:
240 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000241 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000242 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000243 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000244 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000245 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000246 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000247 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000248 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000249 toAsm << getID(GV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000250 else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000251 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000252 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000253 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000254 break;
255 }
256
257 case MachineOperand::MO_SignExtendedImmed:
258 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000259 toAsm << op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000260 break;
261
262 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000263 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000264 break;
265 }
266}
267
268
269void
270SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
271{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000272 unsigned Opcode = MI->getOpCode();
273
274 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
275 return; // IGNORE PHI NODES
276
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000277 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000278
279 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000280
Chris Lattnere88f78c2001-09-19 13:47:27 +0000281 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000282 unsigned N = 1;
283 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
284 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000285 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000286 NeedComma = true;
287 N = printOperands(MI, OpNum);
288 }
289 else
290 N = 1;
291
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000292 toAsm << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000293}
294
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000295void
296SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
297{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000298 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000299 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000300
301 // Get the vector of machine instructions corresponding to this bb.
302 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
303 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
304
305 // Loop over all of the instructions in the basic block...
306 for (; MII != MIE; ++MII)
307 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000308 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000309}
310
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000311void
312SparcAsmPrinter::emitMethod(const Method *M)
313{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000314 if (M->isExternal()) return;
315
316 // Make sure the slot table has information about this method...
317 Table.incorporateMethod(M);
318
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000319 string methName = getID(M);
320 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000321 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000322 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
323 //toAsm << "\t.type\t" << methName << ",#function\n";
324 toAsm << "\t.type\t" << methName << ", 2\n";
325 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000326
327 // Output code for all of the basic blocks in the method...
328 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
329 emitBasicBlock(*I);
330
331 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000332 toAsm << ".EndOf_" << methName << ":\n\t.size "
333 << methName << ", .EndOf_"
334 << methName << "-" << methName << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000335
336 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000337 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000338
339 // Forget all about M.
340 Table.purgeMethod();
341}
342
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000343inline bool
344ArrayTypeIsString(ArrayType* arrayType)
345{
346 return (arrayType->getElementType() == Type::UByteTy ||
347 arrayType->getElementType() == Type::SByteTy);
348}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000349
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000350inline const string TypeToDataDirective(const Type* type)
351{
352 switch(type->getPrimitiveID())
353 {
354 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
355 return ".byte";
356 case Type::UShortTyID: case Type::ShortTyID:
357 return ".half";
358 case Type::UIntTyID: case Type::IntTyID:
359 return ".word";
360 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
361 return ".xword";
362 case Type::FloatTyID:
363 return ".single";
364 case Type::DoubleTyID:
365 return ".double";
366 case Type::ArrayTyID:
367 if (ArrayTypeIsString((ArrayType*) type))
368 return ".ascii";
369 else
370 return "<InvaliDataTypeForPrinting>";
371 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000372 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000373 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000374}
375
376inline unsigned int ConstantToSize(const ConstPoolVal* CV,
377 const TargetMachine& target) {
378 if (ConstPoolArray* AV = dyn_cast<ConstPoolArray>(CV))
379 if (ArrayTypeIsString((ArrayType*) CV->getType()))
380 return 1 + AV->getNumOperands();
381
382 return target.findOptimalStorageSize(CV->getType());
383}
384
385
386inline
387unsigned int TypeToSize(const Type* type, const TargetMachine& target)
388{
389 return target.findOptimalStorageSize(type);
390}
391
392
393inline unsigned int
394TypeToAlignment(const Type* type, const TargetMachine& target)
395{
396 if (type->getPrimitiveID() == Type::ArrayTyID &&
397 ArrayTypeIsString((ArrayType*) type))
398 return target.findOptimalStorageSize(Type::LongTy);
399
400 return target.findOptimalStorageSize(type);
401}
402
403
404void
405SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
406{
407 if (valID.length() == 0)
408 valID = getID(CV);
409
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000410 assert(CV->getType() != Type::VoidTy &&
411 CV->getType() != Type::TypeTy &&
412 CV->getType() != Type::LabelTy &&
413 "Unexpected type for ConstPoolVal");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000414
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000415 toAsm << "\t.align\t" << TypeToAlignment(CV->getType(), Target)
416 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000417
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000418 toAsm << valID << ":" << endl;
419
420 toAsm << "\t"
421 << TypeToDataDirective(CV->getType()) << "\t";
422
423 if (ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV))
Chris Lattner24d3a8b2001-10-29 13:39:38 +0000424 if (isStringCompatible(CPA))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000425 {
426 toAsm << getAsCString(CPA) << endl;
427 return;
428 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000429
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000430 if (CV->getType()->isPrimitiveType())
431 {
432 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
433 toAsm << "0r"; // FP constants must have this prefix
434 toAsm << CV->getStrValue() << endl;
435 }
436 else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
437 {
438 if (! CPP->isNullValue())
439 assert(0 && "Cannot yet print non-null pointer constants to assembly");
440 else
441 toAsm << (void*) NULL;
442 }
443 else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
444 {
445 assert(0 && "Cannot yet initialize pointer refs in assembly");
446 }
447 else
448 {
449 assert(0 && "Cannot yet print non-primitive constants to assembly");
450 // toAsm << CV->getStrValue() << endl;
451 }
452
453 toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
454 toAsm << "\t.size" << "\t" << valID << ","
455 << ConstantToSize(CV, Target) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000456}
457
458
459void
460SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
461{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000462 toAsm << "\t.global\t" << getID(GV) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000463
464 if (GV->hasInitializer())
465 printConstant(GV->getInitializer(), getID(GV));
466 else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000467 toAsm << "\t.align" << TypeToAlignment(GV->getType()->getValueType(), Target)
468 << getID(GV) << ":" << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000469
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000470 toAsm << "\t.type" << "\t" << getID(GV) << ",#object" << endl;
471 toAsm << "\t.size" << "\t" << getID(GV) << ","
472 << TypeToSize(GV->getType()->getValueType(), Target)
473 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000474 }
475}
476
477
478static void
479FoldConstPools(const Module *M,
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000480 hash_set<const ConstPoolVal*>& moduleConstPool)
481{
482 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
483 if (! (*I)->isExternal())
484 {
485 const hash_set<const ConstPoolVal*>& pool =
486 MachineCodeForMethod::get(*I).getConstantPoolValues();
487 moduleConstPool.insert(pool.begin(), pool.end());
488 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000489}
490
491
492void
493SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
494{
495 // First, get the constants there were marked by the code generator for
496 // inclusion in the assembly code data area and fold them all into a
497 // single constant pool since there may be lots of duplicates. Also,
498 // lets force these constants into the slot table so that we can get
499 // unique names for unnamed constants also.
500 //
501 hash_set<const ConstPoolVal*> moduleConstPool;
502 FoldConstPools(M, moduleConstPool);
503
504 // Now, emit the three data sections separately; the cost of I/O should
505 // make up for the cost of extra passes over the globals list!
506 //
507 // Read-only data section (implies initialized)
508 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
509 {
510 const GlobalVariable* GV = *GI;
511 if (GV->hasInitializer() && GV->isConstant())
512 {
513 if (GI == M->gbegin())
514 enterSection(ReadOnlyData);
515 printGlobalVariable(GV);
516 }
517 }
518
519 for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
520 E = moduleConstPool.end(); I != E; ++I)
521 printConstant(*I);
522
523 // Initialized read-write data section
524 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
525 {
526 const GlobalVariable* GV = *GI;
527 if (GV->hasInitializer() && ! GV->isConstant())
528 {
529 if (GI == M->gbegin())
530 enterSection(InitRWData);
531 printGlobalVariable(GV);
532 }
533 }
534
535 // Uninitialized read-write data section
536 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
537 {
538 const GlobalVariable* GV = *GI;
539 if (! GV->hasInitializer())
540 {
541 if (GI == M->gbegin())
542 enterSection(UninitRWData);
543 printGlobalVariable(GV);
544 }
545 }
546
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000547 toAsm << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000548}
549
550
551void
552SparcAsmPrinter::emitModule(const Module *M)
553{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000554 // TODO: Look for a filename annotation on M to emit a .file directive
555 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
556 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000557
558 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000559}
560
561} // End anonymous namespace
562
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000563
Chris Lattnere88f78c2001-09-19 13:47:27 +0000564//
565// emitAssembly - Output assembly language code (a .s file) for the specified
566// method. The specified method must have been compiled before this may be
567// used.
568//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000569void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000570UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000571{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000572 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000573}