blob: dd653057034b7e87d765f560c066a574b2d518af [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);
Vikram S. Adve915b58d2001-11-09 02:19:29 +000062 void printSingleConstant(const ConstPoolVal* CV, string valID = string(""));
63 void printConstant( const ConstPoolVal* CV, string valID = string(""));
Vikram S. Adve953c83e2001-10-28 21:38:52 +000064
65 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
66 void printOneOperand(const MachineOperand &Op);
Chris Lattnere88f78c2001-09-19 13:47:27 +000067
Vikram S. Adve953c83e2001-10-28 21:38:52 +000068 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
69 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
70
Chris Lattnere88f78c2001-09-19 13:47:27 +000071 // enterSection - Use this method to enter a different section of the output
72 // executable. This is used to only output neccesary section transitions.
73 //
74 void enterSection(enum Sections S) {
75 if (S == CurSection) return; // Only switch section if neccesary
76 CurSection = S;
77
Vikram S. Adve29ff8732001-11-08 05:12:37 +000078 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +000079 switch (S)
80 {
81 default: assert(0 && "Bad section name!");
Vikram S. Adve29ff8732001-11-08 05:12:37 +000082 case Text: toAsm << "\".text\""; break;
83 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
84 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
85 case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
Vikram S. Adve953c83e2001-10-28 21:38:52 +000086 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +000087 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +000088 }
89
Vikram S. Adve29ff8732001-11-08 05:12:37 +000090 string getValidSymbolName(const string &S) {
Chris Lattnerc56d7792001-09-28 15:07:24 +000091 string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +000092
93 // Symbol names in Sparc assembly language have these rules:
94 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
95 // (b) A name beginning in "." is treated as a local name.
96 // (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
97 //
98 if (S[0] == '_' || isdigit(S[0]))
99 Result += "ll";
100
101 for (unsigned i = 0; i < S.size(); ++i)
102 {
103 char C = S[i];
104 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
105 Result += C;
106 else
107 {
108 Result += '_';
109 Result += char('0' + ((unsigned char)C >> 4));
110 Result += char('0' + (C & 0xF));
111 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000112 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000113 return Result;
114 }
115
Chris Lattnere88f78c2001-09-19 13:47:27 +0000116 // getID - Return a valid identifier for the specified value. Base it on
117 // the name of the identifier if possible, use a numbered value based on
118 // prefix otherwise. FPrefix is always prepended to the output identifier.
119 //
120 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000121 string Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000122 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
123 if (V->hasName()) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000124 Result = FP + V->getName();
Chris Lattnere88f78c2001-09-19 13:47:27 +0000125 } else {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000126 int valId = Table.getValSlot(V);
127 if (valId == -1) {
128 ValIdMapConstIterator I = valToIdMap.find(V);
129 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
130 : (*I).second;
131 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000132 Result = FP + string(Prefix) + itostr(valId);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000133 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000134 return getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000135 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000136
Chris Lattnere88f78c2001-09-19 13:47:27 +0000137 // getID Wrappers - Ensure consistent usage...
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000138 string getID(const Module *M) {
139 return getID(M, "LLVMModule_");
Chris Lattnere88f78c2001-09-19 13:47:27 +0000140 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000141 string getID(const Method *M) {
142 return getID(M, "LLVMMethod_");
143 }
144 string getID(const BasicBlock *BB) {
145 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
146 }
147 string getID(const GlobalVariable *GV) {
148 return getID(GV, "LLVMGlobal_", ".G_");
149 }
150 string getID(const ConstPoolVal *CV) {
151 return getID(CV, "LLVMConst_", ".C_");
152 }
153
Chris Lattnere88f78c2001-09-19 13:47:27 +0000154 unsigned getOperandMask(unsigned Opcode) {
155 switch (Opcode) {
156 case SUBcc: return 1 << 3; // Remove CC argument
Chris Lattnerc56d7792001-09-28 15:07:24 +0000157 case BA: case BRZ: // Remove Arg #0, which is always null or xcc
158 case BRLEZ: case BRLZ:
159 case BRNZ: case BRGZ:
160 case BRGEZ: return 1 << 0;
Chris Lattner39f501c2001-10-01 02:32:34 +0000161
Chris Lattnere88f78c2001-09-19 13:47:27 +0000162 default: return 0; // By default, don't hack operands...
163 }
164 }
165};
166
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000167inline bool
168SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
169 unsigned int opNum) {
170 switch (MI->getOpCode()) {
171 case JMPLCALL:
172 case JMPLRET: return (opNum == 0);
173 default: return false;
Chris Lattnerc28f6d62001-10-15 19:21:31 +0000174 }
175}
176
177
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000178inline bool
179SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
180 unsigned int opNum) {
181 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
182 return (opNum == 0);
183 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
184 return (opNum == 1);
185 else
186 return false;
187}
188
189
190#define PrintOp1PlusOp2(Op1, Op2) \
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000191 printOneOperand(Op1); \
192 toAsm << "+"; \
193 printOneOperand(Op2);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000194
195unsigned int
196SparcAsmPrinter::printOperands(const MachineInstr *MI,
197 unsigned int opNum)
198{
199 const MachineOperand& Op = MI->getOperand(opNum);
200
201 if (OpIsBranchTargetLabel(MI, opNum))
202 {
203 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
204 return 2;
205 }
206 else if (OpIsMemoryAddressBase(MI, opNum))
207 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000208 toAsm << "[";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000209 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000210 toAsm << "]";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000211 return 2;
212 }
213 else
214 {
215 printOneOperand(Op);
216 return 1;
217 }
218}
219
220
221void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000222SparcAsmPrinter::printOneOperand(const MachineOperand &op)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000223{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000224 switch (op.getOperandType())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000225 {
226 case MachineOperand::MO_VirtualRegister:
227 case MachineOperand::MO_CCRegister:
228 case MachineOperand::MO_MachineRegister:
229 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000230 int RegNum = (int)op.getAllocatedRegNum();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000231
232 // ****this code is temporary till NULL Values are fixed
233 if (RegNum == 10000) {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000234 toAsm << "<NULL VALUE>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000235 } else {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000236 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000237 }
238 break;
239 }
240
241 case MachineOperand::MO_PCRelativeDisp:
242 {
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000243 const Value *Val = op.getVRegValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000244 if (!Val)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000245 toAsm << "\t<*NULL Value*>";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000246 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000247 toAsm << getID(BB);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000248 else if (const Method *M = dyn_cast<const Method>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000249 toAsm << getID(M);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000250 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000251 toAsm << getID(GV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000252 else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000253 toAsm << getID(CV);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000254 else
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000255 toAsm << "<unknown value=" << Val << ">";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000256 break;
257 }
258
259 case MachineOperand::MO_SignExtendedImmed:
260 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000261 toAsm << op.getImmedValue();
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000262 break;
263
264 default:
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000265 toAsm << op; // use dump field
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000266 break;
267 }
268}
269
270
271void
272SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
273{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000274 unsigned Opcode = MI->getOpCode();
275
276 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
277 return; // IGNORE PHI NODES
278
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000279 toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000280
281 unsigned Mask = getOperandMask(Opcode);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000282
Chris Lattnere88f78c2001-09-19 13:47:27 +0000283 bool NeedComma = false;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000284 unsigned N = 1;
285 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
286 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000287 if (NeedComma) toAsm << ", "; // Handle comma outputing
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000288 NeedComma = true;
289 N = printOperands(MI, OpNum);
290 }
291 else
292 N = 1;
293
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000294 toAsm << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000295}
296
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000297void
298SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
299{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000300 // Emit a label for the basic block
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000301 toAsm << getID(BB) << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000302
303 // Get the vector of machine instructions corresponding to this bb.
304 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
305 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
306
307 // Loop over all of the instructions in the basic block...
308 for (; MII != MIE; ++MII)
309 emitMachineInst(*MII);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000310 toAsm << "\n"; // Seperate BB's with newlines
Chris Lattnere88f78c2001-09-19 13:47:27 +0000311}
312
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000313void
314SparcAsmPrinter::emitMethod(const Method *M)
315{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000316 if (M->isExternal()) return;
317
318 // Make sure the slot table has information about this method...
319 Table.incorporateMethod(M);
320
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000321 string methName = getID(M);
322 toAsm << "!****** Outputing Method: " << methName << " ******\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000323 enterSection(Text);
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000324 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
325 //toAsm << "\t.type\t" << methName << ",#function\n";
326 toAsm << "\t.type\t" << methName << ", 2\n";
327 toAsm << methName << ":\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000328
329 // Output code for all of the basic blocks in the method...
330 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
331 emitBasicBlock(*I);
332
333 // Output a .size directive so the debugger knows the extents of the function
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000334 toAsm << ".EndOf_" << methName << ":\n\t.size "
335 << methName << ", .EndOf_"
336 << methName << "-" << methName << endl;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000337
338 // Put some spaces between the methods
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000339 toAsm << "\n\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000340
341 // Forget all about M.
342 Table.purgeMethod();
343}
344
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000345inline bool
346ArrayTypeIsString(ArrayType* arrayType)
347{
348 return (arrayType->getElementType() == Type::UByteTy ||
349 arrayType->getElementType() == Type::SByteTy);
350}
Chris Lattnere88f78c2001-09-19 13:47:27 +0000351
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000352inline const string
353TypeToDataDirective(const Type* type)
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000354{
355 switch(type->getPrimitiveID())
356 {
357 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
358 return ".byte";
359 case Type::UShortTyID: case Type::ShortTyID:
360 return ".half";
361 case Type::UIntTyID: case Type::IntTyID:
362 return ".word";
363 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
364 return ".xword";
365 case Type::FloatTyID:
366 return ".single";
367 case Type::DoubleTyID:
368 return ".double";
369 case Type::ArrayTyID:
370 if (ArrayTypeIsString((ArrayType*) type))
371 return ".ascii";
372 else
373 return "<InvaliDataTypeForPrinting>";
374 default:
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000375 return "<InvaliDataTypeForPrinting>";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000376 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000377}
378
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000379inline unsigned int
380ConstantToSize(const ConstPoolVal* CV, const TargetMachine& target)
381{
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000382 if (ConstPoolArray* AV = dyn_cast<ConstPoolArray>(CV))
383 if (ArrayTypeIsString((ArrayType*) CV->getType()))
384 return 1 + AV->getNumOperands();
385
386 return target.findOptimalStorageSize(CV->getType());
387}
388
389
390inline
391unsigned int TypeToSize(const Type* type, const TargetMachine& target)
392{
393 return target.findOptimalStorageSize(type);
394}
395
396
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000397// Align data larger than one L1 cache line on L1 cache line boundaries.
398// Align all smaller types on the next higher 2^x boundary (4, 8, ...).
399//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000400inline unsigned int
401TypeToAlignment(const Type* type, const TargetMachine& target)
402{
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000403 unsigned int typeSize = target.findOptimalStorageSize(type);
404 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
405 if (typeSize > (int) cacheLineSize / 2)
406 return cacheLineSize;
407 else
408 for (unsigned sz=1; /*no condition*/; sz *= 2)
409 if (sz >= typeSize)
410 return sz;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000411}
412
413
414void
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000415SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV,string valID)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000416{
417 if (valID.length() == 0)
418 valID = getID(CV);
419
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000420 assert(CV->getType() != Type::VoidTy &&
421 CV->getType() != Type::TypeTy &&
422 CV->getType() != Type::LabelTy &&
423 "Unexpected type for ConstPoolVal");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000424
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000425 assert((! isa<ConstPoolArray>( CV) && ! isa<ConstPoolStruct>(CV))
426 && "Collective types should be handled outside this function");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000427
428 toAsm << "\t"
429 << TypeToDataDirective(CV->getType()) << "\t";
430
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000431 if (CV->getType()->isPrimitiveType())
432 {
433 if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
434 toAsm << "0r"; // FP constants must have this prefix
435 toAsm << CV->getStrValue() << endl;
436 }
437 else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
438 {
439 if (! CPP->isNullValue())
440 assert(0 && "Cannot yet print non-null pointer constants to assembly");
441 else
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000442 toAsm << (void*) NULL << endl;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000443 }
444 else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
445 {
446 assert(0 && "Cannot yet initialize pointer refs in assembly");
447 }
448 else
449 {
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000450 assert(0 && "Unknown elementary type for constant");
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000451 }
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000452}
453
454void
455SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
456{
457 if (valID.length() == 0)
458 valID = getID(CV);
459
460 assert(CV->getType() != Type::VoidTy &&
461 CV->getType() != Type::TypeTy &&
462 CV->getType() != Type::LabelTy &&
463 "Unexpected type for ConstPoolVal");
464
465 toAsm << "\t.align\t" << TypeToAlignment(CV->getType(), Target)
466 << endl;
467
468 // Print .size and .type only if it is not a string.
469 ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV);
470
471 if (CPA && isStringCompatible(CPA))
472 { // print it as a string and return
473 toAsm << valID << ":" << endl;
474 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t"
475 << getAsCString(CPA) << endl;
476 return;
477 }
478
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000479 toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
480 toAsm << "\t.size" << "\t" << valID << ","
481 << ConstantToSize(CV, Target) << endl;
Vikram S. Adve915b58d2001-11-09 02:19:29 +0000482 toAsm << valID << ":" << endl;
483
484 if (CPA)
485 { // Not a string. Print the values in successive locations
486 const vector<Use>& constValues = CPA->getValues();
487 for (unsigned i=1; i < constValues.size(); i++)
488 this->printSingleConstant(cast<ConstPoolVal>(constValues[i].get()));
489 }
490 else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CV))
491 { // Print the fields in successive locations
492 const vector<Use>& constValues = CPA->getValues();
493 for (unsigned i=1; i < constValues.size(); i++)
494 this->printSingleConstant(cast<ConstPoolVal>(constValues[i].get()));
495 }
496 else
497 this->printSingleConstant(CV, valID);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000498}
499
500
501void
502SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
503{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000504 toAsm << "\t.global\t" << getID(GV) << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000505
506 if (GV->hasInitializer())
507 printConstant(GV->getInitializer(), getID(GV));
508 else {
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000509 toAsm << "\t.align\t"
510 << TypeToAlignment(GV->getType()->getValueType(), Target) << endl;
511 toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
512 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000513 << TypeToSize(GV->getType()->getValueType(), Target)
514 << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000515 }
516}
517
518
519static void
520FoldConstPools(const Module *M,
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000521 hash_set<const ConstPoolVal*>& moduleConstPool)
522{
523 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
524 if (! (*I)->isExternal())
525 {
526 const hash_set<const ConstPoolVal*>& pool =
527 MachineCodeForMethod::get(*I).getConstantPoolValues();
528 moduleConstPool.insert(pool.begin(), pool.end());
529 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000530}
531
532
533void
534SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
535{
536 // First, get the constants there were marked by the code generator for
537 // inclusion in the assembly code data area and fold them all into a
538 // single constant pool since there may be lots of duplicates. Also,
539 // lets force these constants into the slot table so that we can get
540 // unique names for unnamed constants also.
541 //
542 hash_set<const ConstPoolVal*> moduleConstPool;
543 FoldConstPools(M, moduleConstPool);
544
545 // Now, emit the three data sections separately; the cost of I/O should
546 // make up for the cost of extra passes over the globals list!
547 //
548 // Read-only data section (implies initialized)
549 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
550 {
551 const GlobalVariable* GV = *GI;
552 if (GV->hasInitializer() && GV->isConstant())
553 {
554 if (GI == M->gbegin())
555 enterSection(ReadOnlyData);
556 printGlobalVariable(GV);
557 }
558 }
559
560 for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
561 E = moduleConstPool.end(); I != E; ++I)
562 printConstant(*I);
563
564 // Initialized read-write data section
565 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
566 {
567 const GlobalVariable* GV = *GI;
568 if (GV->hasInitializer() && ! GV->isConstant())
569 {
570 if (GI == M->gbegin())
571 enterSection(InitRWData);
572 printGlobalVariable(GV);
573 }
574 }
575
576 // Uninitialized read-write data section
577 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
578 {
579 const GlobalVariable* GV = *GI;
580 if (! GV->hasInitializer())
581 {
582 if (GI == M->gbegin())
583 enterSection(UninitRWData);
584 printGlobalVariable(GV);
585 }
586 }
587
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000588 toAsm << endl;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000589}
590
591
592void
593SparcAsmPrinter::emitModule(const Module *M)
594{
Chris Lattnere88f78c2001-09-19 13:47:27 +0000595 // TODO: Look for a filename annotation on M to emit a .file directive
596 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
597 emitMethod(*I);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000598
599 emitGlobalsAndConstants(M);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000600}
601
602} // End anonymous namespace
603
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000604
Chris Lattnere88f78c2001-09-19 13:47:27 +0000605//
606// emitAssembly - Output assembly language code (a .s file) for the specified
607// method. The specified method must have been compiled before this may be
608// used.
609//
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000610void
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000611UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000612{
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000613 SparcAsmPrinter Print(toAsm, M, *this);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000614}