blob: 618fb6dc30f6411b3b3d08ab2f9558444f997d1a [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. Adve21447222001-11-10 02:03:06 +000061 void printGlobalVariable( const GlobalVariable* GV);
62 void printSingleConstant( const ConstPoolVal* CV);
63 void printConstantValueOnly(const ConstPoolVal* CV);
64 void printConstant( const ConstPoolVal* CV, string valID=string(""));
Vikram S. Adve953c83e2001-10-28 21:38:52 +000065
66 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
67 void printOneOperand(const MachineOperand &Op);
Chris Lattnere88f78c2001-09-19 13:47:27 +000068
Vikram S. Adve953c83e2001-10-28 21:38:52 +000069 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
70 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
71
Chris Lattnere88f78c2001-09-19 13:47:27 +000072 // enterSection - Use this method to enter a different section of the output
73 // executable. This is used to only output neccesary section transitions.
74 //
75 void enterSection(enum Sections S) {
76 if (S == CurSection) return; // Only switch section if neccesary
77 CurSection = S;
78
Vikram S. Adve29ff8732001-11-08 05:12:37 +000079 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000080 switch (S)
81 {
82 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000083 case Text: toAsm << "\".text\""; break;
84 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
85 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
86 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000087 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000088 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000089 }
90
Vikram S. Adve29ff8732001-11-08 05:12:37 +000091 string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000092 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000093
94 // Symbol names in Sparc assembly language have these rules:
95 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
96 // (b) A name beginning in "." is treated as a local name.
97 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
98 //
99 if (S[0] == '_' || isdigit(S[0]))
100 Result += "ll";
101
102 for (unsigned i = 0; i < S.size(); ++i)
103 {
104 char C = S[i];
105 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
106 Result += C;
107 else
108 {
109 Result += '_';
110 Result += char('0' + ((unsigned char)C >> 4));
111 Result += char('0' + (C & 0xF));
112 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000113 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000114 return Result;
115 }
116
Chris Lattnere88f78c2001-09-19 13:47:27 +0000117 // getID - Return a valid identifier for the specified value. Base it on
118 // the name of the identifier if possible, use a numbered value based on
119 // prefix otherwise. FPrefix is always prepended to the output identifier.
120 //
121 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000122 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000123 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
124 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000125 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000126 } else {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000127 int valId = Table.getValSlot(V);
128 if (valId == -1) {
129 ValIdMapConstIterator I = valToIdMap.find(V);
130 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
131 : (*I).second;
132 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000133 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000134 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000135 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000136 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000137
Chris Lattnere88f78c2001-09-19 13:47:27 +0000138 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000139 string getID(const Module *M) {
140 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000141 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000142 string getID(const Method *M) {
143 return getID(M, "LLVMMethod_");
144 }
145 string getID(const BasicBlock *BB) {
146 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
147 }
148 string getID(const GlobalVariable *GV) {
149 return getID(GV, "LLVMGlobal_", ".G_");
150 }
151 string getID(const ConstPoolVal *CV) {
152 return getID(CV, "LLVMConst_", ".C_");
153 }
154
Chris Lattnere88f78c2001-09-19 13:47:27 +0000155 unsigned getOperandMask(unsigned Opcode) {
156 switch (Opcode) {
157 case SUBcc: return 1 << 3; // Remove CC argument
Vikram S. Adve998cf0d2001-11-11 23:11:36 +0000158 case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000159 default: return 0; // By default, don't hack operands...
160 }
161 }
162};
163
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000164inline bool
165SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
166 unsigned int opNum) {
167 switch (MI->getOpCode()) {
168 case JMPLCALL:
169 case JMPLRET: return (opNum == 0);
170 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000171 }
172}
173
174
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000175inline bool
176SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
177 unsigned int opNum) {
178 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
179 return (opNum == 0);
180 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
181 return (opNum == 1);
182 else
183 return false;
184}
185
186
187#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000188 printOneOperand(Op1); \
189 toAsm << "+"; \
190 printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000191
192unsigned int
193SparcAsmPrinter::printOperands(const MachineInstr *MI,
194 unsigned int opNum)
195{
196 const MachineOperand& Op = MI->getOperand(opNum);
197
198 if (OpIsBranchTargetLabel(MI, opNum))
199 {
200 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
201 return 2;
202 }
203 else if (OpIsMemoryAddressBase(MI, opNum))
204 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000205 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000206 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000207 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000208 return 2;
209 }
210 else
211 {
212 printOneOperand(Op);
213 return 1;
214 }
215}
216
217
218void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000219SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000220{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000221 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000222 {
223 case MachineOperand::MO_VirtualRegister:
224 case MachineOperand::MO_CCRegister:
225 case MachineOperand::MO_MachineRegister:
226 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000227 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000228
229 // ****this code is temporary till NULL Values are fixed
Ruchira Sasanka5f98aca2001-11-13 23:08:19 +0000230 if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000231 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000232 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000233 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000234 }
235 break;
236 }
237
238 case MachineOperand::MO_PCRelativeDisp:
239 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000240 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000241 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000242 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000243 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000244 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000245 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000246 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000247 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000248 toAsm << getID(GV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000249 else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000250 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000251 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000252 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000253 break;
254 }
255
256 case MachineOperand::MO_SignExtendedImmed:
257 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000258 toAsm << op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000259 break;
260
261 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000262 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000263 break;
264 }
265}
266
267
268void
269SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
270{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000271 unsigned Opcode = MI->getOpCode();
272
273 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
274 return; // IGNORE PHI NODES
275
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000276 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000277
278 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000279
Chris Lattnere88f78c2001-09-19 13:47:27 +0000280 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000281 unsigned N = 1;
282 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
283 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000284 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000285 NeedComma = true;
286 N = printOperands(MI, OpNum);
287 }
288 else
289 N = 1;
290
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000291 toAsm << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000292}
293
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000294void
295SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
296{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000297 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000298 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000299
300 // Get the vector of machine instructions corresponding to this bb.
301 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
302 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
303
304 // Loop over all of the instructions in the basic block...
305 for (; MII != MIE; ++MII)
306 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000307 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000308}
309
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000310void
311SparcAsmPrinter::emitMethod(const Method *M)
312{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000313 if (M->isExternal()) return;
314
315 // Make sure the slot table has information about this method...
316 Table.incorporateMethod(M);
317
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000318 string methName = getID(M);
319 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000320 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000321 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
322 //toAsm << "\t.type\t" << methName << ",#function\n";
323 toAsm << "\t.type\t" << methName << ", 2\n";
324 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000325
326 // Output code for all of the basic blocks in the method...
327 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
328 emitBasicBlock(*I);
329
330 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000331 toAsm << ".EndOf_" << methName << ":\n\t.size "
332 << methName << ", .EndOf_"
333 << methName << "-" << methName << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000334
335 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000336 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000337
338 // Forget all about M.
339 Table.purgeMethod();
340}
341
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000342inline bool
343ArrayTypeIsString(ArrayType* arrayType)
344{
345 return (arrayType->getElementType() == Type::UByteTy ||
346 arrayType->getElementType() == Type::SByteTy);
347}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000348
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000349inline const string
350TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000351{
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
Vikram S. Adve21447222001-11-10 02:03:06 +0000376// Get the size of the constant for the given target.
377// If this is an unsized array, return 0.
378//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000379inline unsigned int
380ConstantToSize(const ConstPoolVal* CV, const TargetMachine& target)
381{
Vikram S. Adve21447222001-11-10 02:03:06 +0000382 if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV))
383 {
384 ArrayType *aty = cast<ArrayType>(CPA->getType());
385 if (ArrayTypeIsString(aty))
386 return 1 + CPA->getNumOperands();
387 else if (! aty->isSized())
388 return 0;
389 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000390
391 return target.findOptimalStorageSize(CV->getType());
392}
393
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000394inline
395unsigned int TypeToSize(const Type* type, const TargetMachine& target)
396{
397 return target.findOptimalStorageSize(type);
398}
399
400
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000401// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000402// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
403//
404inline unsigned int
405SizeToAlignment(unsigned int size, const TargetMachine& target)
406{
407 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
408 if (size > (unsigned) cacheLineSize / 2)
409 return cacheLineSize;
410 else
411 for (unsigned sz=1; /*no condition*/; sz *= 2)
412 if (sz >= size)
413 return sz;
414}
415
416// Get the size of the type and then use SizeToAlignment.
417// If this is an unsized array, just return the L1 cache line size
418// (viz., the default behavior for large global objects).
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000419//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000420inline unsigned int
421TypeToAlignment(const Type* type, const TargetMachine& target)
422{
Vikram S. Adve21447222001-11-10 02:03:06 +0000423 if (ArrayType* aty = dyn_cast<ArrayType>(type))
424 if (! aty->isSized())
425 return target.getCacheInfo().getCacheLineSize(1);
426
427 return SizeToAlignment(target.findOptimalStorageSize(type), target);
428}
429
430// Get the size of the constant and then use SizeToAlignment.
431// Handles strings as a special case;
432inline unsigned int
433ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target)
434{
435 unsigned int constantSize;
436 if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV))
437 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
438 return SizeToAlignment(1 + CPA->getNumOperands(), target);
439
440 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000441}
442
443
Vikram S. Adve21447222001-11-10 02:03:06 +0000444// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000445void
Vikram S. Adve21447222001-11-10 02:03:06 +0000446SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000447{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000448 assert(CV->getType() != Type::VoidTy &&
449 CV->getType() != Type::TypeTy &&
450 CV->getType() != Type::LabelTy &&
451 "Unexpected type for ConstPoolVal");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000452
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000453 assert((! isa<ConstPoolArray>( CV) && ! isa<ConstPoolStruct>(CV))
454 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000455
456 toAsm << "\t"
457 << TypeToDataDirective(CV->getType()) << "\t";
458
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000459 if (CV->getType()->isPrimitiveType())
460 {
461 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
462 toAsm << "0r"; // FP constants must have this prefix
463 toAsm << CV->getStrValue() << endl;
464 }
465 else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
466 {
467 if (! CPP->isNullValue())
468 assert(0 && "Cannot yet print non-null pointer constants to assembly");
469 else
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000470 toAsm << (void*) NULL << endl;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000471 }
472 else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
473 {
474 assert(0 && "Cannot yet initialize pointer refs in assembly");
475 }
476 else
477 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000478 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000479 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000480}
481
Vikram S. Adve21447222001-11-10 02:03:06 +0000482// Print a constant value or values (it may be an aggregate).
483// Uses printSingleConstant() to print each individual value.
484void
485SparcAsmPrinter::printConstantValueOnly(const ConstPoolVal* CV)
486{
487 ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
488
489 if (CPA && isStringCompatible(CPA))
490 { // print the string alone and return
491 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
492 }
493 else if (CPA)
494 { // Not a string. Print the values in successive locations
495 const vector<Use>& constValues = CPA->getValues();
496 for (unsigned i=1; i < constValues.size(); i++)
497 this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get()));
498 }
499 else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CV))
500 { // Print the fields in successive locations
501 const vector<Use>& constValues = CPS->getValues();
502 for (unsigned i=1; i < constValues.size(); i++)
503 this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get()));
504 }
505 else
506 this->printSingleConstant(CV);
507}
508
509// Print a constant (which may be an aggregate) prefixed by all the
510// appropriate directives. Uses printConstantValueOnly() to print the
511// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000512void
513SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
514{
515 if (valID.length() == 0)
516 valID = getID(CV);
517
Vikram S. Adve21447222001-11-10 02:03:06 +0000518 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000519 << endl;
520
521 // Print .size and .type only if it is not a string.
522 ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000523 if (CPA && isStringCompatible(CPA))
524 { // print it as a string and return
525 toAsm << valID << ":" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000526 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000527 return;
528 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000529
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000530 toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000531
532 unsigned int constSize = ConstantToSize(CV, Target);
533 if (constSize)
534 toAsm << "\t.size" << "\t" << valID << ","
535 << constSize << endl;
536
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000537 toAsm << valID << ":" << endl;
538
Vikram S. Adve21447222001-11-10 02:03:06 +0000539 this->printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000540}
541
542
543void
544SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
545{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000546 toAsm << "\t.global\t" << getID(GV) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000547
548 if (GV->hasInitializer())
549 printConstant(GV->getInitializer(), getID(GV));
550 else {
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000551 toAsm << "\t.align\t"
552 << TypeToAlignment(GV->getType()->getValueType(), Target) << endl;
553 toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
554 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000555 << TypeToSize(GV->getType()->getValueType(), Target)
556 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000557 }
558}
559
560
561static void
562FoldConstPools(const Module *M,
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000563 hash_set<const ConstPoolVal*>& moduleConstPool)
564{
565 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
566 if (! (*I)->isExternal())
567 {
568 const hash_set<const ConstPoolVal*>& pool =
569 MachineCodeForMethod::get(*I).getConstantPoolValues();
570 moduleConstPool.insert(pool.begin(), pool.end());
571 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000572}
573
574
575void
576SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
577{
578 // First, get the constants there were marked by the code generator for
579 // inclusion in the assembly code data area and fold them all into a
580 // single constant pool since there may be lots of duplicates. Also,
581 // lets force these constants into the slot table so that we can get
582 // unique names for unnamed constants also.
583 //
584 hash_set<const ConstPoolVal*> moduleConstPool;
585 FoldConstPools(M, moduleConstPool);
586
587 // Now, emit the three data sections separately; the cost of I/O should
588 // make up for the cost of extra passes over the globals list!
589 //
590 // Read-only data section (implies initialized)
591 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
592 {
593 const GlobalVariable* GV = *GI;
594 if (GV->hasInitializer() && GV->isConstant())
595 {
596 if (GI == M->gbegin())
597 enterSection(ReadOnlyData);
598 printGlobalVariable(GV);
599 }
600 }
601
602 for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
603 E = moduleConstPool.end(); I != E; ++I)
604 printConstant(*I);
605
606 // Initialized read-write data section
607 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
608 {
609 const GlobalVariable* GV = *GI;
610 if (GV->hasInitializer() && ! GV->isConstant())
611 {
612 if (GI == M->gbegin())
613 enterSection(InitRWData);
614 printGlobalVariable(GV);
615 }
616 }
617
618 // Uninitialized read-write data section
619 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
620 {
621 const GlobalVariable* GV = *GI;
622 if (! GV->hasInitializer())
623 {
624 if (GI == M->gbegin())
625 enterSection(UninitRWData);
626 printGlobalVariable(GV);
627 }
628 }
629
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000630 toAsm << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000631}
632
633
634void
635SparcAsmPrinter::emitModule(const Module *M)
636{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000637 // TODO: Look for a filename annotation on M to emit a .file directive
638 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
639 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000640
641 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000642}
643
644} // End anonymous namespace
645
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000646
Chris Lattnere88f78c2001-09-19 13:47:27 +0000647//
648// emitAssembly - Output assembly language code (a .s file) for the specified
649// method. The specified method must have been compiled before this may be
650// used.
651//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000652void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000653UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000654{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000655 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000656}