blob: ed8059d12e94018b268d18792544f93068ed5e1a [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
Chris Lattnerc56d7792001-09-28 15:07:24 +0000158 case BA: case BRZ: // Remove Arg #0, which is always null or xcc
159 case BRLEZ: case BRLZ:
160 case BRNZ: case BRGZ:
161 case BRGEZ: return 1 << 0;
Chris Lattner39f501c2001-10-01 02:32:34 +0000162
Chris Lattnere88f78c2001-09-19 13:47:27 +0000163 default: return 0; // By default, don't hack operands...
164 }
165 }
166};
167
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000168inline bool
169SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
170 unsigned int opNum) {
171 switch (MI->getOpCode()) {
172 case JMPLCALL:
173 case JMPLRET: return (opNum == 0);
174 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000175 }
176}
177
178
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000179inline bool
180SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
181 unsigned int opNum) {
182 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
183 return (opNum == 0);
184 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
185 return (opNum == 1);
186 else
187 return false;
188}
189
190
191#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000192 printOneOperand(Op1); \
193 toAsm << "+"; \
194 printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000195
196unsigned int
197SparcAsmPrinter::printOperands(const MachineInstr *MI,
198 unsigned int opNum)
199{
200 const MachineOperand& Op = MI->getOperand(opNum);
201
202 if (OpIsBranchTargetLabel(MI, opNum))
203 {
204 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
205 return 2;
206 }
207 else if (OpIsMemoryAddressBase(MI, opNum))
208 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000209 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000210 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000211 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000212 return 2;
213 }
214 else
215 {
216 printOneOperand(Op);
217 return 1;
218 }
219}
220
221
222void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000223SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000224{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000225 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000226 {
227 case MachineOperand::MO_VirtualRegister:
228 case MachineOperand::MO_CCRegister:
229 case MachineOperand::MO_MachineRegister:
230 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000231 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000232
233 // ****this code is temporary till NULL Values are fixed
234 if (RegNum == 10000) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000235 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000236 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000237 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000238 }
239 break;
240 }
241
242 case MachineOperand::MO_PCRelativeDisp:
243 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000244 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000245 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000246 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000247 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000248 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000249 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000250 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000251 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000252 toAsm << getID(GV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000253 else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000254 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000255 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000256 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000257 break;
258 }
259
260 case MachineOperand::MO_SignExtendedImmed:
261 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000262 toAsm << op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000263 break;
264
265 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000266 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000267 break;
268 }
269}
270
271
272void
273SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
274{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000275 unsigned Opcode = MI->getOpCode();
276
277 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
278 return; // IGNORE PHI NODES
279
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000280 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000281
282 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000283
Chris Lattnere88f78c2001-09-19 13:47:27 +0000284 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000285 unsigned N = 1;
286 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
287 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000288 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000289 NeedComma = true;
290 N = printOperands(MI, OpNum);
291 }
292 else
293 N = 1;
294
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000295 toAsm << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000296}
297
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000298void
299SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
300{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000301 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000302 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000303
304 // Get the vector of machine instructions corresponding to this bb.
305 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
306 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
307
308 // Loop over all of the instructions in the basic block...
309 for (; MII != MIE; ++MII)
310 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000311 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000312}
313
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000314void
315SparcAsmPrinter::emitMethod(const Method *M)
316{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000317 if (M->isExternal()) return;
318
319 // Make sure the slot table has information about this method...
320 Table.incorporateMethod(M);
321
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000322 string methName = getID(M);
323 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000324 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000325 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
326 //toAsm << "\t.type\t" << methName << ",#function\n";
327 toAsm << "\t.type\t" << methName << ", 2\n";
328 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000329
330 // Output code for all of the basic blocks in the method...
331 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
332 emitBasicBlock(*I);
333
334 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000335 toAsm << ".EndOf_" << methName << ":\n\t.size "
336 << methName << ", .EndOf_"
337 << methName << "-" << methName << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000338
339 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000340 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000341
342 // Forget all about M.
343 Table.purgeMethod();
344}
345
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000346inline bool
347ArrayTypeIsString(ArrayType* arrayType)
348{
349 return (arrayType->getElementType() == Type::UByteTy ||
350 arrayType->getElementType() == Type::SByteTy);
351}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000352
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000353inline const string
354TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000355{
356 switch(type->getPrimitiveID())
357 {
358 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
359 return ".byte";
360 case Type::UShortTyID: case Type::ShortTyID:
361 return ".half";
362 case Type::UIntTyID: case Type::IntTyID:
363 return ".word";
364 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
365 return ".xword";
366 case Type::FloatTyID:
367 return ".single";
368 case Type::DoubleTyID:
369 return ".double";
370 case Type::ArrayTyID:
371 if (ArrayTypeIsString((ArrayType*) type))
372 return ".ascii";
373 else
374 return "<InvaliDataTypeForPrinting>";
375 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000376 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000377 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000378}
379
Vikram S. Adve21447222001-11-10 02:03:06 +0000380// Get the size of the constant for the given target.
381// If this is an unsized array, return 0.
382//
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000383inline unsigned int
384ConstantToSize(const ConstPoolVal* CV, const TargetMachine& target)
385{
Vikram S. Adve21447222001-11-10 02:03:06 +0000386 if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV))
387 {
388 ArrayType *aty = cast<ArrayType>(CPA->getType());
389 if (ArrayTypeIsString(aty))
390 return 1 + CPA->getNumOperands();
391 else if (! aty->isSized())
392 return 0;
393 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000394
395 return target.findOptimalStorageSize(CV->getType());
396}
397
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000398inline
399unsigned int TypeToSize(const Type* type, const TargetMachine& target)
400{
401 return target.findOptimalStorageSize(type);
402}
403
404
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000405// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve21447222001-11-10 02:03:06 +0000406// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
407//
408inline unsigned int
409SizeToAlignment(unsigned int size, const TargetMachine& target)
410{
411 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
412 if (size > (unsigned) cacheLineSize / 2)
413 return cacheLineSize;
414 else
415 for (unsigned sz=1; /*no condition*/; sz *= 2)
416 if (sz >= size)
417 return sz;
418}
419
420// Get the size of the type and then use SizeToAlignment.
421// If this is an unsized array, just return the L1 cache line size
422// (viz., the default behavior for large global objects).
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000423//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000424inline unsigned int
425TypeToAlignment(const Type* type, const TargetMachine& target)
426{
Vikram S. Adve21447222001-11-10 02:03:06 +0000427 if (ArrayType* aty = dyn_cast<ArrayType>(type))
428 if (! aty->isSized())
429 return target.getCacheInfo().getCacheLineSize(1);
430
431 return SizeToAlignment(target.findOptimalStorageSize(type), target);
432}
433
434// Get the size of the constant and then use SizeToAlignment.
435// Handles strings as a special case;
436inline unsigned int
437ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target)
438{
439 unsigned int constantSize;
440 if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV))
441 if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
442 return SizeToAlignment(1 + CPA->getNumOperands(), target);
443
444 return TypeToAlignment(CV->getType(), target);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000445}
446
447
Vikram S. Adve21447222001-11-10 02:03:06 +0000448// Print a single constant value.
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000449void
Vikram S. Adve21447222001-11-10 02:03:06 +0000450SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000451{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000452 assert(CV->getType() != Type::VoidTy &&
453 CV->getType() != Type::TypeTy &&
454 CV->getType() != Type::LabelTy &&
455 "Unexpected type for ConstPoolVal");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000456
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000457 assert((! isa<ConstPoolArray>( CV) && ! isa<ConstPoolStruct>(CV))
458 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000459
460 toAsm << "\t"
461 << TypeToDataDirective(CV->getType()) << "\t";
462
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000463 if (CV->getType()->isPrimitiveType())
464 {
465 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
466 toAsm << "0r"; // FP constants must have this prefix
467 toAsm << CV->getStrValue() << endl;
468 }
469 else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
470 {
471 if (! CPP->isNullValue())
472 assert(0 && "Cannot yet print non-null pointer constants to assembly");
473 else
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000474 toAsm << (void*) NULL << endl;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000475 }
476 else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
477 {
478 assert(0 && "Cannot yet initialize pointer refs in assembly");
479 }
480 else
481 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000482 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000483 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000484}
485
Vikram S. Adve21447222001-11-10 02:03:06 +0000486// Print a constant value or values (it may be an aggregate).
487// Uses printSingleConstant() to print each individual value.
488void
489SparcAsmPrinter::printConstantValueOnly(const ConstPoolVal* CV)
490{
491 ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
492
493 if (CPA && isStringCompatible(CPA))
494 { // print the string alone and return
495 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
496 }
497 else if (CPA)
498 { // Not a string. Print the values in successive locations
499 const vector<Use>& constValues = CPA->getValues();
500 for (unsigned i=1; i < constValues.size(); i++)
501 this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get()));
502 }
503 else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CV))
504 { // Print the fields in successive locations
505 const vector<Use>& constValues = CPS->getValues();
506 for (unsigned i=1; i < constValues.size(); i++)
507 this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get()));
508 }
509 else
510 this->printSingleConstant(CV);
511}
512
513// Print a constant (which may be an aggregate) prefixed by all the
514// appropriate directives. Uses printConstantValueOnly() to print the
515// value or values.
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000516void
517SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
518{
519 if (valID.length() == 0)
520 valID = getID(CV);
521
Vikram S. Adve21447222001-11-10 02:03:06 +0000522 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target)
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000523 << endl;
524
525 // Print .size and .type only if it is not a string.
526 ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000527 if (CPA && isStringCompatible(CPA))
528 { // print it as a string and return
529 toAsm << valID << ":" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000530 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl;
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000531 return;
532 }
Vikram S. Adve21447222001-11-10 02:03:06 +0000533
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000534 toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
Vikram S. Adve21447222001-11-10 02:03:06 +0000535
536 unsigned int constSize = ConstantToSize(CV, Target);
537 if (constSize)
538 toAsm << "\t.size" << "\t" << valID << ","
539 << constSize << endl;
540
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000541 toAsm << valID << ":" << endl;
542
Vikram S. Adve21447222001-11-10 02:03:06 +0000543 this->printConstantValueOnly(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000544}
545
546
547void
548SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
549{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000550 toAsm << "\t.global\t" << getID(GV) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000551
552 if (GV->hasInitializer())
553 printConstant(GV->getInitializer(), getID(GV));
554 else {
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000555 toAsm << "\t.align\t"
556 << TypeToAlignment(GV->getType()->getValueType(), Target) << endl;
557 toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
558 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000559 << TypeToSize(GV->getType()->getValueType(), Target)
560 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000561 }
562}
563
564
565static void
566FoldConstPools(const Module *M,
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000567 hash_set<const ConstPoolVal*>& moduleConstPool)
568{
569 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
570 if (! (*I)->isExternal())
571 {
572 const hash_set<const ConstPoolVal*>& pool =
573 MachineCodeForMethod::get(*I).getConstantPoolValues();
574 moduleConstPool.insert(pool.begin(), pool.end());
575 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000576}
577
578
579void
580SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
581{
582 // First, get the constants there were marked by the code generator for
583 // inclusion in the assembly code data area and fold them all into a
584 // single constant pool since there may be lots of duplicates. Also,
585 // lets force these constants into the slot table so that we can get
586 // unique names for unnamed constants also.
587 //
588 hash_set<const ConstPoolVal*> moduleConstPool;
589 FoldConstPools(M, moduleConstPool);
590
591 // Now, emit the three data sections separately; the cost of I/O should
592 // make up for the cost of extra passes over the globals list!
593 //
594 // Read-only data section (implies initialized)
595 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
596 {
597 const GlobalVariable* GV = *GI;
598 if (GV->hasInitializer() && GV->isConstant())
599 {
600 if (GI == M->gbegin())
601 enterSection(ReadOnlyData);
602 printGlobalVariable(GV);
603 }
604 }
605
606 for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
607 E = moduleConstPool.end(); I != E; ++I)
608 printConstant(*I);
609
610 // Initialized read-write data section
611 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
612 {
613 const GlobalVariable* GV = *GI;
614 if (GV->hasInitializer() && ! GV->isConstant())
615 {
616 if (GI == M->gbegin())
617 enterSection(InitRWData);
618 printGlobalVariable(GV);
619 }
620 }
621
622 // Uninitialized read-write data section
623 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
624 {
625 const GlobalVariable* GV = *GI;
626 if (! GV->hasInitializer())
627 {
628 if (GI == M->gbegin())
629 enterSection(UninitRWData);
630 printGlobalVariable(GV);
631 }
632 }
633
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000634 toAsm << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000635}
636
637
638void
639SparcAsmPrinter::emitModule(const Module *M)
640{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000641 // TODO: Look for a filename annotation on M to emit a .file directive
642 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
643 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000644
645 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000646}
647
648} // End anonymous namespace
649
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000650
Chris Lattnere88f78c2001-09-19 13:47:27 +0000651//
652// emitAssembly - Output assembly language code (a .s file) for the specified
653// method. The specified method must have been compiled before this may be
654// used.
655//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000656void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000657UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000658{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000659 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000660}