blob: 25034177da499c89c8e59597dd75f16a6542c291 [file] [log] [blame]
Chris Lattnere88f78c2001-09-19 13:47:27 +00001//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere88f78c2001-09-19 13:47:27 +00009//
Misha Brukman5560c9d2003-08-18 14:43:39 +000010// This file implements all of the stuff necessary to output a .s file from
Chris Lattnere88f78c2001-09-19 13:47:27 +000011// LLVM. The code in this file assumes that the specified module has already
12// been compiled into the internal data structures of the Module.
13//
Chris Lattnerf57b8452002-04-27 06:56:12 +000014// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
15// The FunctionPass is pipelined together with all of the rest of the code
16// generation stages, and the Pass runs at the end to emit code for global
17// variables and such.
Chris Lattnere88f78c2001-09-19 13:47:27 +000018//
19//===----------------------------------------------------------------------===//
20
Chris Lattnere88f78c2001-09-19 13:47:27 +000021#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmanf905ed52003-11-07 17:45:28 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Misha Brukmanfce11432002-10-28 00:28:31 +000023#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerd0fe5f52002-12-28 20:15:01 +000024#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000025#include "llvm/Constants.h"
Vikram S. Adve953c83e2001-10-28 21:38:52 +000026#include "llvm/DerivedTypes.h"
Chris Lattnere88f78c2001-09-19 13:47:27 +000027#include "llvm/Module.h"
Chris Lattnerb5794002002-04-07 22:49:37 +000028#include "llvm/SlotCalculator.h"
Chris Lattnerd50b6712002-04-28 20:40:59 +000029#include "llvm/Pass.h"
Chris Lattner4b1de8e2002-04-18 18:15:38 +000030#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000031#include "Support/StringExtras.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000032#include "Support/Statistic.h"
Misha Brukmanf4de7832003-08-05 16:01:50 +000033#include "SparcInternals.h"
34#include <string>
Chris Lattnere88f78c2001-09-19 13:47:27 +000035
36namespace {
37
Brian Gaeke2c9b9132003-10-06 15:41:21 +000038Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
Vikram S. Adved198c472002-03-18 03:07:26 +000040class GlobalIdTable: public Annotation {
41 static AnnotationID AnnotId;
42 friend class AsmPrinter; // give access to AnnotId
Vikram S. Adve953c83e2001-10-28 21:38:52 +000043
Chris Lattner09ff1122002-07-24 21:21:32 +000044 typedef hash_map<const Value*, int> ValIdMap;
Vikram S. Adved198c472002-03-18 03:07:26 +000045 typedef ValIdMap::const_iterator ValIdMapConstIterator;
46 typedef ValIdMap:: iterator ValIdMapIterator;
47public:
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000048 SlotCalculator Table; // map anonymous values to unique integer IDs
Chris Lattnerc19b8b12002-02-03 23:41:08 +000049 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
Vikram S. Adved198c472002-03-18 03:07:26 +000050
Chris Lattnerfd63f25f2002-04-28 04:50:54 +000051 GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
Vikram S. Adved198c472002-03-18 03:07:26 +000052};
53
54AnnotationID GlobalIdTable::AnnotId =
55 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
Misha Brukmanf905ed52003-11-07 17:45:28 +000056
57// Can we treat the specified array as a string? Only if it is an array of
58// ubytes or non-negative sbytes.
59//
60static bool isStringCompatible(const ConstantArray *CVA) {
61 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
62 if (ETy == Type::UByteTy) return true;
63 if (ETy != Type::SByteTy) return false;
64
65 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
66 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
67 return false;
68
69 return true;
70}
71
72// toOctal - Convert the low order bits of X into an octal letter
73static inline char toOctal(int X) {
74 return (X&7)+'0';
75}
76
77// getAsCString - Return the specified array as a C compatible string, only if
78// the predicate isStringCompatible is true.
79//
80static std::string getAsCString(const ConstantArray *CVA) {
81 assert(isStringCompatible(CVA) && "Array is not string compatible!");
82
83 std::string Result;
84 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
85 Result = "\"";
86 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
87 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
88
89 if (C == '"') {
90 Result += "\\\"";
91 } else if (C == '\\') {
92 Result += "\\\\";
93 } else if (isprint(C)) {
94 Result += C;
95 } else {
96 Result += '\\'; // print all other chars as octal value
97 Result += toOctal(C >> 6);
98 Result += toOctal(C >> 3);
99 Result += toOctal(C >> 0);
100 }
101 }
102 Result += "\"";
103
104 return Result;
105}
106
107inline bool
108ArrayTypeIsString(const ArrayType* arrayType)
109{
110 return (arrayType->getElementType() == Type::UByteTy ||
111 arrayType->getElementType() == Type::SByteTy);
112}
113
114
115inline const std::string
116TypeToDataDirective(const Type* type)
117{
118 switch(type->getPrimitiveID())
119 {
120 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
121 return ".byte";
122 case Type::UShortTyID: case Type::ShortTyID:
123 return ".half";
124 case Type::UIntTyID: case Type::IntTyID:
125 return ".word";
126 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
127 return ".xword";
128 case Type::FloatTyID:
129 return ".word";
130 case Type::DoubleTyID:
131 return ".xword";
132 case Type::ArrayTyID:
133 if (ArrayTypeIsString((ArrayType*) type))
134 return ".ascii";
135 else
136 return "<InvaliDataTypeForPrinting>";
137 default:
138 return "<InvaliDataTypeForPrinting>";
139 }
140}
141
142// Get the size of the type
143//
144inline unsigned int
145TypeToSize(const Type* type, const TargetMachine& target)
146{
147 return target.findOptimalStorageSize(type);
148}
149
150// Get the size of the constant for the given target.
151// If this is an unsized array, return 0.
152//
153inline unsigned int
154ConstantToSize(const Constant* CV, const TargetMachine& target)
155{
156 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
157 {
158 const ArrayType *aty = cast<ArrayType>(CVA->getType());
159 if (ArrayTypeIsString(aty))
160 return 1 + CVA->getNumOperands();
161 }
162
163 return TypeToSize(CV->getType(), target);
164}
165
166// Align data larger than one L1 cache line on L1 cache line boundaries.
167// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
168//
169inline unsigned int
170SizeToAlignment(unsigned int size, const TargetMachine& target)
171{
172 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
173 if (size > (unsigned) cacheLineSize / 2)
174 return cacheLineSize;
175 else
176 for (unsigned sz=1; /*no condition*/; sz *= 2)
177 if (sz >= size)
178 return sz;
179}
180
181// Get the size of the type and then use SizeToAlignment.
182//
183inline unsigned int
184TypeToAlignment(const Type* type, const TargetMachine& target)
185{
186 return SizeToAlignment(TypeToSize(type, target), target);
187}
188
189// Get the size of the constant and then use SizeToAlignment.
190// Handles strings as a special case;
191inline unsigned int
192ConstantToAlignment(const Constant* CV, const TargetMachine& target)
193{
194 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
195 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
196 return SizeToAlignment(1 + CVA->getNumOperands(), target);
197
198 return TypeToAlignment(CV->getType(), target);
199}
Vikram S. Adved198c472002-03-18 03:07:26 +0000200
201//===---------------------------------------------------------------------===//
202// Code Shared By the two printer passes, as a mixin
203//===---------------------------------------------------------------------===//
204
205class AsmPrinter {
206 GlobalIdTable* idTable;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000207public:
Chris Lattner697954c2002-01-20 22:54:45 +0000208 std::ostream &toAsm;
Chris Lattner59ba1092002-02-04 15:53:23 +0000209 const TargetMachine &Target;
Vikram S. Adved198c472002-03-18 03:07:26 +0000210
Chris Lattnere88f78c2001-09-19 13:47:27 +0000211 enum Sections {
212 Unknown,
213 Text,
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000214 ReadOnlyData,
215 InitRWData,
Vikram S. Advefee76262002-10-13 00:32:18 +0000216 ZeroInitRWData,
Chris Lattnere88f78c2001-09-19 13:47:27 +0000217 } CurSection;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000218
Chris Lattner59ba1092002-02-04 15:53:23 +0000219 AsmPrinter(std::ostream &os, const TargetMachine &T)
Vikram S. Adved198c472002-03-18 03:07:26 +0000220 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
221
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000222 // (start|end)(Module|Function) - Callback methods to be invoked by subclasses
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000223 void startModule(Module &M) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000224 // Create the global id table if it does not already exist
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000225 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
Vikram S. Adved198c472002-03-18 03:07:26 +0000226 if (idTable == NULL) {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000227 idTable = new GlobalIdTable(&M);
228 M.addAnnotation(idTable);
Vikram S. Adved198c472002-03-18 03:07:26 +0000229 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000230 }
Misha Brukmanf905ed52003-11-07 17:45:28 +0000231
232 void
233 PrintZeroBytesToPad(int numBytes)
234 {
235 for ( ; numBytes >= 8; numBytes -= 8)
236 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
237
238 if (numBytes >= 4)
239 {
240 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
241 numBytes -= 4;
242 }
243
244 while (numBytes--)
245 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
246 }
247
248 // Print a single constant value.
249 void printSingleConstantValue(const Constant* CV)
250 {
251 assert(CV->getType() != Type::VoidTy &&
252 CV->getType() != Type::TypeTy &&
253 CV->getType() != Type::LabelTy &&
254 "Unexpected type for Constant");
255
256 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
257 && "Aggregate types should be handled outside this function");
258
259 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
260
261 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
262 { // This is a constant address for a global variable or method.
263 // Use the name of the variable or method as the address value.
264 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
265 toAsm << getID(CPR->getValue()) << "\n";
266 }
267 else if (isa<ConstantPointerNull>(CV))
268 { // Null pointer value
269 toAsm << "0\n";
270 }
271 else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
272 { // Constant expression built from operators, constants, and symbolic addrs
273 toAsm << ConstantExprToString(CE, Target) << "\n";
274 }
275 else if (CV->getType()->isPrimitiveType()) // Check primitive types last
276 {
277 if (CV->getType()->isFloatingPoint()) {
278 // FP Constants are printed as integer constants to avoid losing
279 // precision...
280 double Val = cast<ConstantFP>(CV)->getValue();
281 if (CV->getType() == Type::FloatTy) {
282 float FVal = (float)Val;
283 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
284 toAsm << *(unsigned int*)ProxyPtr;
285 } else if (CV->getType() == Type::DoubleTy) {
286 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
287 toAsm << *(uint64_t*)ProxyPtr;
288 } else {
289 assert(0 && "Unknown floating point type!");
290 }
291
292 toAsm << "\t! " << CV->getType()->getDescription()
293 << " value: " << Val << "\n";
294 } else {
295 WriteAsOperand(toAsm, CV, false, false) << "\n";
296 }
297 }
298 else
299 {
300 assert(0 && "Unknown elementary type for constant");
301 }
302 }
303
304 // Print a constant value or values (it may be an aggregate).
305 // Uses printSingleConstantValue() to print each individual value.
306 void
307 printConstantValueOnly(const Constant* CV,
308 int numPadBytesAfter = 0)
309 {
310 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
311
312 if (CVA && isStringCompatible(CVA))
313 { // print the string alone and return
314 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
315 }
316 else if (CVA)
317 { // Not a string. Print the values in successive locations
318 const std::vector<Use> &constValues = CVA->getValues();
319 for (unsigned i=0; i < constValues.size(); i++)
320 printConstantValueOnly(cast<Constant>(constValues[i].get()));
321 }
322 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
323 { // Print the fields in successive locations. Pad to align if needed!
324 const StructLayout *cvsLayout =
325 Target.getTargetData().getStructLayout(CVS->getType());
326 const std::vector<Use>& constValues = CVS->getValues();
327 unsigned sizeSoFar = 0;
328 for (unsigned i=0, N = constValues.size(); i < N; i++)
329 {
330 const Constant* field = cast<Constant>(constValues[i].get());
331
332 // Check if padding is needed and insert one or more 0s.
333 unsigned fieldSize =
334 Target.getTargetData().getTypeSize(field->getType());
335 int padSize = ((i == N-1? cvsLayout->StructSize
336 : cvsLayout->MemberOffsets[i+1])
337 - cvsLayout->MemberOffsets[i]) - fieldSize;
338 sizeSoFar += (fieldSize + padSize);
339
340 // Now print the actual field value
341 printConstantValueOnly(field, padSize);
342 }
343 assert(sizeSoFar == cvsLayout->StructSize &&
344 "Layout of constant struct may be incorrect!");
345 }
346 else
347 printSingleConstantValue(CV);
348
349 if (numPadBytesAfter)
350 PrintZeroBytesToPad(numPadBytesAfter);
351 }
352
353 // Print a constant (which may be an aggregate) prefixed by all the
354 // appropriate directives. Uses printConstantValueOnly() to print the
355 // value or values.
356 void printConstant(const Constant* CV, std::string valID = "")
357 {
358 if (valID.length() == 0)
359 valID = getID(CV);
360
361 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
362
363 // Print .size and .type only if it is not a string.
364 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
365 if (CVA && isStringCompatible(CVA))
366 { // print it as a string and return
367 toAsm << valID << ":\n";
368 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
369 return;
370 }
371
372 toAsm << "\t.type" << "\t" << valID << ",#object\n";
373
374 unsigned int constSize = ConstantToSize(CV, Target);
375 if (constSize)
376 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
377
378 toAsm << valID << ":\n";
379
380 printConstantValueOnly(CV);
381 }
382
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000383 void startFunction(Function &F) {
Chris Lattnerb5794002002-04-07 22:49:37 +0000384 // Make sure the slot table has information about this function...
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000385 idTable->Table.incorporateFunction(&F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000386 }
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000387 void endFunction(Function &) {
Chris Lattnerfd63f25f2002-04-28 04:50:54 +0000388 idTable->Table.purgeFunction(); // Forget all about F
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000389 }
390 void endModule() {
Chris Lattnere88f78c2001-09-19 13:47:27 +0000391 }
392
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000393 // Check if a value is external or accessible from external code.
Vikram S. Adved198c472002-03-18 03:07:26 +0000394 bool isExternal(const Value* V) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000395 const GlobalValue *GV = dyn_cast<GlobalValue>(V);
396 return GV && GV->hasExternalLinkage();
Vikram S. Adved198c472002-03-18 03:07:26 +0000397 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000398
Chris Lattnere88f78c2001-09-19 13:47:27 +0000399 // enterSection - Use this method to enter a different section of the output
Misha Brukman5560c9d2003-08-18 14:43:39 +0000400 // executable. This is used to only output necessary section transitions.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000401 //
402 void enterSection(enum Sections S) {
Misha Brukman5560c9d2003-08-18 14:43:39 +0000403 if (S == CurSection) return; // Only switch section if necessary
Chris Lattnere88f78c2001-09-19 13:47:27 +0000404 CurSection = S;
405
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000406 toAsm << "\n\t.section ";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000407 switch (S)
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000408 {
409 default: assert(0 && "Bad section name!");
410 case Text: toAsm << "\".text\""; break;
411 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
412 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
413 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
414 }
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000415 toAsm << "\n";
Chris Lattnere88f78c2001-09-19 13:47:27 +0000416 }
417
Misha Brukmanf4de7832003-08-05 16:01:50 +0000418 static std::string getValidSymbolName(const std::string &S) {
419 std::string Result;
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000420
421 // Symbol names in Sparc assembly language have these rules:
422 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
423 // (b) A name beginning in "." is treated as a local name.
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000424 //
Chris Lattnerbb6b1212002-10-14 06:14:18 +0000425 if (isdigit(S[0]))
426 Result = "ll";
Vikram S. Adve29ff8732001-11-08 05:12:37 +0000427
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000428 for (unsigned i = 0; i < S.size(); ++i)
429 {
430 char C = S[i];
431 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
432 Result += C;
433 else
434 {
435 Result += '_';
436 Result += char('0' + ((unsigned char)C >> 4));
437 Result += char('0' + (C & 0xF));
438 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000439 }
Chris Lattnerc56d7792001-09-28 15:07:24 +0000440 return Result;
441 }
442
Chris Lattnere88f78c2001-09-19 13:47:27 +0000443 // getID - Return a valid identifier for the specified value. Base it on
Vikram S. Adved198c472002-03-18 03:07:26 +0000444 // the name of the identifier if possible (qualified by the type), and
445 // use a numbered value based on prefix otherwise.
446 // FPrefix is always prepended to the output identifier.
Chris Lattnere88f78c2001-09-19 13:47:27 +0000447 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000448 std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
449 std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
Vikram S. Adve96918072002-10-30 20:16:38 +0000450
Misha Brukmanf4de7832003-08-05 16:01:50 +0000451 Result += V->hasName() ? V->getName() : std::string(Prefix);
Vikram S. Adve96918072002-10-30 20:16:38 +0000452
Vikram S. Adved198c472002-03-18 03:07:26 +0000453 // Qualify all internal names with a unique id.
454 if (!isExternal(V)) {
Alkis Evlogimenos60596382003-10-17 02:02:40 +0000455 int valId = idTable->Table.getSlot(V);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000456 if (valId == -1) {
Vikram S. Adved198c472002-03-18 03:07:26 +0000457 GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
458 if (I == idTable->valToIdMap.end())
459 valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000460 else
461 valId = I->second;
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000462 }
Vikram S. Adved198c472002-03-18 03:07:26 +0000463 Result = Result + "_" + itostr(valId);
Vikram S. Adve96918072002-10-30 20:16:38 +0000464
465 // Replace or prefix problem characters in the name
466 Result = getValidSymbolName(Result);
Chris Lattnere88f78c2001-09-19 13:47:27 +0000467 }
Vikram S. Adve96918072002-10-30 20:16:38 +0000468
469 return Result;
Chris Lattnere88f78c2001-09-19 13:47:27 +0000470 }
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000471
Chris Lattnere88f78c2001-09-19 13:47:27 +0000472 // getID Wrappers - Ensure consistent usage...
Misha Brukmanf4de7832003-08-05 16:01:50 +0000473 std::string getID(const Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000474 return getID(F, "LLVMFunction_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000475 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000476 std::string getID(const BasicBlock *BB) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000477 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
478 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000479 std::string getID(const GlobalVariable *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000480 return getID(GV, "LLVMGlobal_");
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000481 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000482 std::string getID(const Constant *CV) {
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000483 return getID(CV, "LLVMConst_", ".C_");
484 }
Misha Brukmanf4de7832003-08-05 16:01:50 +0000485 std::string getID(const GlobalValue *GV) {
Chris Lattner8b1b4e22002-07-16 18:35:16 +0000486 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
487 return getID(V);
488 else if (const Function *F = dyn_cast<Function>(GV))
489 return getID(F);
490 assert(0 && "Unexpected type of GlobalValue!");
491 return "";
492 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000493
Misha Brukmanf4de7832003-08-05 16:01:50 +0000494 // Combines expressions
495 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
496 const TargetMachine &TM,
497 const std::string &op) {
498 return "(" + valToExprString(CE->getOperand(0), TM) + op
499 + valToExprString(CE->getOperand(1), TM) + ")";
500 }
501
Vikram S. Adve537a8772002-09-05 18:28:10 +0000502 // ConstantExprToString() - Convert a ConstantExpr to an asm expression
503 // and return this as a string.
Misha Brukmanf4de7832003-08-05 16:01:50 +0000504 std::string ConstantExprToString(const ConstantExpr* CE,
505 const TargetMachine& target) {
506 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000507 switch(CE->getOpcode()) {
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000508 case Instruction::GetElementPtr:
509 { // generate a symbolic expression for the byte address
510 const Value* ptrVal = CE->getOperand(0);
511 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Misha Brukmanf4de7832003-08-05 16:01:50 +0000512 const TargetData &TD = target.getTargetData();
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000513 S += "(" + valToExprString(ptrVal, target) + ") + ("
514 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
515 break;
516 }
Vikram S. Advee99941a2002-08-22 02:58:36 +0000517
Vikram S. Adve537a8772002-09-05 18:28:10 +0000518 case Instruction::Cast:
519 // Support only non-converting casts for now, i.e., a no-op.
520 // This assertion is not a complete check.
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000521 assert(target.getTargetData().getTypeSize(CE->getType()) ==
522 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
Vikram S. Adve537a8772002-09-05 18:28:10 +0000523 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
524 break;
525
526 case Instruction::Add:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000527 S += ConstantArithExprToString(CE, target, ") + (");
Vikram S. Adve537a8772002-09-05 18:28:10 +0000528 break;
529
Vikram S. Adve72666e62003-08-01 15:55:53 +0000530 case Instruction::Sub:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000531 S += ConstantArithExprToString(CE, target, ") - (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000532 break;
533
534 case Instruction::Mul:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000535 S += ConstantArithExprToString(CE, target, ") * (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000536 break;
537
538 case Instruction::Div:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000539 S += ConstantArithExprToString(CE, target, ") / (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000540 break;
541
542 case Instruction::Rem:
Misha Brukmanf4de7832003-08-05 16:01:50 +0000543 S += ConstantArithExprToString(CE, target, ") % (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000544 break;
545
546 case Instruction::And:
547 // Logical && for booleans; bitwise & otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000548 S += ConstantArithExprToString(CE, target,
549 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000550 break;
551
552 case Instruction::Or:
553 // Logical || for booleans; bitwise | otherwise
Misha Brukmanf4de7832003-08-05 16:01:50 +0000554 S += ConstantArithExprToString(CE, target,
555 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
Vikram S. Adve72666e62003-08-01 15:55:53 +0000556 break;
557
558 case Instruction::Xor:
559 // Bitwise ^ for all types
Misha Brukmanf4de7832003-08-05 16:01:50 +0000560 S += ConstantArithExprToString(CE, target, ") ^ (");
Vikram S. Adve72666e62003-08-01 15:55:53 +0000561 break;
562
Vikram S. Advee99941a2002-08-22 02:58:36 +0000563 default:
564 assert(0 && "Unsupported operator in ConstantExprToString()");
565 break;
566 }
567
568 return S;
569 }
570
571 // valToExprString - Helper function for ConstantExprToString().
572 // Appends result to argument string S.
573 //
Misha Brukmanf4de7832003-08-05 16:01:50 +0000574 std::string valToExprString(const Value* V, const TargetMachine& target) {
575 std::string S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000576 bool failed = false;
577 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
578
579 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
Misha Brukmanf4de7832003-08-05 16:01:50 +0000580 S += std::string(CB == ConstantBool::True ? "1" : "0");
Vikram S. Advee99941a2002-08-22 02:58:36 +0000581 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
582 S += itostr(CI->getValue());
583 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
584 S += utostr(CI->getValue());
585 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
586 S += ftostr(CFP->getValue());
587 else if (isa<ConstantPointerNull>(CV))
588 S += "0";
589 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
Vikram S. Adve537a8772002-09-05 18:28:10 +0000590 S += valToExprString(CPR->getValue(), target);
Vikram S. Advee99941a2002-08-22 02:58:36 +0000591 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
592 S += ConstantExprToString(CE, target);
593 else
594 failed = true;
595
596 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
597 S += getID(GV);
598 }
599 else
600 failed = true;
601
602 if (failed) {
603 assert(0 && "Cannot convert value to string");
604 S += "<illegal-value>";
605 }
Vikram S. Adve537a8772002-09-05 18:28:10 +0000606 return S;
Vikram S. Advee99941a2002-08-22 02:58:36 +0000607 }
608
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000609};
610
611
612
613//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000614// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000615//===----------------------------------------------------------------------===//
616
Chris Lattnerf57b8452002-04-27 06:56:12 +0000617struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000618 inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000619 : AsmPrinter(os, t) {}
620
Misha Brukmanf905ed52003-11-07 17:45:28 +0000621 const Function *currFunction;
622
Chris Lattner96c466b2002-04-29 14:57:45 +0000623 const char *getPassName() const {
624 return "Output Sparc Assembly for Functions";
625 }
626
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000627 virtual bool doInitialization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000628 startModule(M);
629 return false;
630 }
631
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000632 virtual bool runOnFunction(Function &F) {
Misha Brukmanf905ed52003-11-07 17:45:28 +0000633 currFunction = &F;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000634 startFunction(F);
635 emitFunction(F);
636 endFunction(F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000637 return false;
638 }
639
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000640 virtual bool doFinalization(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000641 endModule();
642 return false;
643 }
644
Chris Lattner97e52e42002-04-28 21:27:06 +0000645 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
646 AU.setPreservesAll();
647 }
648
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000649 void emitFunction(const Function &F);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000650private :
Misha Brukmane585a7d2002-10-28 20:01:13 +0000651 void emitBasicBlock(const MachineBasicBlock &MBB);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000652 void emitMachineInst(const MachineInstr *MI);
653
654 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000655 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000656
657 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
658 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000659
Chris Lattnere88f78c2001-09-19 13:47:27 +0000660 unsigned getOperandMask(unsigned Opcode) {
661 switch (Opcode) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000662 case V9::SUBccr:
663 case V9::SUBcci: return 1 << 3; // Remove CC argument
Vikram S. Adveb2debdc2002-07-08 23:30:59 +0000664 //case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
Chris Lattnere88f78c2001-09-19 13:47:27 +0000665 default: return 0; // By default, don't hack operands...
666 }
667 }
668};
669
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000670inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000671SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
672 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000673 switch (MI->getOpCode()) {
Misha Brukman71ed1c92003-05-27 22:35:43 +0000674 case V9::JMPLCALLr:
675 case V9::JMPLCALLi:
676 case V9::JMPLRETr:
677 case V9::JMPLRETi:
Misha Brukmana98cd452003-05-20 20:32:24 +0000678 return (opNum == 0);
679 default:
680 return false;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000681 }
682}
683
684
685inline bool
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000686SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
687 unsigned int opNum) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000688 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
689 return (opNum == 0);
690 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
691 return (opNum == 1);
692 else
693 return false;
694}
695
696
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000697#define PrintOp1PlusOp2(mop1, mop2, opCode) \
698 printOneOperand(mop1, opCode); \
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000699 toAsm << "+"; \
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000700 printOneOperand(mop2, opCode);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000701
702unsigned int
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000703SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000704 unsigned int opNum)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000705{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000706 const MachineOperand& mop = MI->getOperand(opNum);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000707
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000708 if (OpIsBranchTargetLabel(MI, opNum))
709 {
710 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
711 return 2;
712 }
713 else if (OpIsMemoryAddressBase(MI, opNum))
714 {
715 toAsm << "[";
716 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
717 toAsm << "]";
718 return 2;
719 }
720 else
721 {
722 printOneOperand(mop, MI->getOpCode());
723 return 1;
724 }
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000725}
726
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000727void
Vikram S. Adve78a4f232003-05-27 00:02:22 +0000728SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
729 MachineOpCode opCode)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000730{
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000731 bool needBitsFlag = true;
732
733 if (mop.opHiBits32())
734 toAsm << "%lm(";
735 else if (mop.opLoBits32())
736 toAsm << "%lo(";
737 else if (mop.opHiBits64())
738 toAsm << "%hh(";
739 else if (mop.opLoBits64())
740 toAsm << "%hm(";
741 else
742 needBitsFlag = false;
743
Chris Lattner133f0792002-10-28 04:45:29 +0000744 switch (mop.getType())
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000745 {
Vikram S. Adve786833a2003-07-06 20:13:59 +0000746 case MachineOperand::MO_VirtualRegister:
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000747 case MachineOperand::MO_CCRegister:
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000748 case MachineOperand::MO_MachineRegister:
749 {
750 int regNum = (int)mop.getAllocatedRegNum();
Vikram S. Adveb15f8d42003-07-10 19:42:11 +0000751
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000752 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
753 // better to print code with NULL registers than to die
754 toAsm << "<NULL VALUE>";
755 } else {
756 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
757 }
758 break;
759 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000760
Misha Brukmanf905ed52003-11-07 17:45:28 +0000761 case MachineOperand::MO_ConstantPoolIndex:
762 {
763 toAsm << ".CPI_" << currFunction->getName()
764 << "_" << mop.getConstantPoolIndex();
765 break;
766 }
767
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000768 case MachineOperand::MO_PCRelativeDisp:
769 {
770 const Value *Val = mop.getVRegValue();
771 assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000772
Chris Lattner949a3622003-07-23 15:30:06 +0000773 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000774 toAsm << getID(BB);
775 else if (const Function *M = dyn_cast<Function>(Val))
776 toAsm << getID(M);
777 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
778 toAsm << getID(GV);
779 else if (const Constant *CV = dyn_cast<Constant>(Val))
780 toAsm << getID(CV);
781 else
782 assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
783 break;
784 }
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000785
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000786 case MachineOperand::MO_SignExtendedImmed:
787 toAsm << mop.getImmedValue();
788 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000789
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000790 case MachineOperand::MO_UnextendedImmed:
791 toAsm << (uint64_t) mop.getImmedValue();
792 break;
Misha Brukmanb3fabe02003-05-31 06:22:37 +0000793
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000794 default:
795 toAsm << mop; // use dump field
796 break;
797 }
Vikram S. Adve195a5d52002-07-10 21:41:21 +0000798
799 if (needBitsFlag)
800 toAsm << ")";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000801}
802
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000803void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000804SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000805{
806 unsigned Opcode = MI->getOpCode();
807
Vikram S. Advec227a9a2002-11-06 00:34:26 +0000808 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
Vikram S. Adveaf9fd512003-05-31 07:27:17 +0000809 return; // IGNORE PHI NODES
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000810
Chris Lattnerf44f9052002-10-29 17:35:41 +0000811 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000812
813 unsigned Mask = getOperandMask(Opcode);
814
815 bool NeedComma = false;
816 unsigned N = 1;
817 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
818 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
Misha Brukman8b2fe192003-09-23 17:27:28 +0000819 if (NeedComma) toAsm << ", "; // Handle comma outputting
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000820 NeedComma = true;
821 N = printOperands(MI, OpNum);
Chris Lattnerebdc7f32002-11-17 22:57:23 +0000822 } else
823 N = 1;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000824
825 toAsm << "\n";
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000826 ++EmittedInsts;
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000827}
828
829void
Misha Brukmane585a7d2002-10-28 20:01:13 +0000830SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000831{
832 // Emit a label for the basic block
Misha Brukmane585a7d2002-10-28 20:01:13 +0000833 toAsm << getID(MBB.getBasicBlock()) << ":\n";
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000834
835 // Loop over all of the instructions in the basic block...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000836 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
Chris Lattner55291ea2002-10-28 01:41:47 +0000837 MII != MIE; ++MII)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000838 emitMachineInst(*MII);
Misha Brukmanbc0e9982003-07-14 17:20:40 +0000839 toAsm << "\n"; // Separate BB's with newlines
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000840}
841
842void
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000843SparcFunctionAsmPrinter::emitFunction(const Function &F)
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000844{
Misha Brukmanf4de7832003-08-05 16:01:50 +0000845 std::string methName = getID(&F);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000846 toAsm << "!****** Outputing Function: " << methName << " ******\n";
Misha Brukmanf905ed52003-11-07 17:45:28 +0000847
848 // Emit constant pool for this function
849 const MachineConstantPool *MCP = MachineFunction::get(&F).getConstantPool();
850 const std::vector<Constant*> &CP = MCP->getConstants();
851
852 enterSection(AsmPrinter::ReadOnlyData);
853 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
854 std::string cpiName = ".CPI_" + F.getName() + "_" + utostr(i);
855 printConstant(CP[i], cpiName);
856 }
857
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000858 enterSection(AsmPrinter::Text);
859 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
860 //toAsm << "\t.type\t" << methName << ",#function\n";
861 toAsm << "\t.type\t" << methName << ", 2\n";
862 toAsm << methName << ":\n";
863
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000864 // Output code for all of the basic blocks in the function...
Misha Brukmane585a7d2002-10-28 20:01:13 +0000865 MachineFunction &MF = MachineFunction::get(&F);
Chris Lattnerd0fe5f52002-12-28 20:15:01 +0000866 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
Misha Brukmane585a7d2002-10-28 20:01:13 +0000867 emitBasicBlock(*I);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000868
869 // Output a .size directive so the debugger knows the extents of the function
870 toAsm << ".EndOf_" << methName << ":\n\t.size "
871 << methName << ", .EndOf_"
872 << methName << "-" << methName << "\n";
873
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000874 // Put some spaces between the functions
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000875 toAsm << "\n\n";
876}
877
878} // End anonymous namespace
879
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000880Pass *UltraSparc::getFunctionAsmPrinterPass(std::ostream &Out) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000881 return new SparcFunctionAsmPrinter(Out, *this);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000882}
883
884
885
886
887
888//===----------------------------------------------------------------------===//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000889// SparcFunctionAsmPrinter Code
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000890//===----------------------------------------------------------------------===//
891
892namespace {
893
894class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
895public:
Chris Lattner49b8a9c2002-02-24 23:02:40 +0000896 SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
897 : AsmPrinter(os, t) {}
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000898
Chris Lattner96c466b2002-04-29 14:57:45 +0000899 const char *getPassName() const { return "Output Sparc Assembly for Module"; }
900
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000901 virtual bool run(Module &M) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000902 startModule(M);
Misha Brukmanf905ed52003-11-07 17:45:28 +0000903 emitGlobals(M);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000904 endModule();
905 return false;
906 }
907
Chris Lattner97e52e42002-04-28 21:27:06 +0000908 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
909 AU.setPreservesAll();
910 }
911
912private:
Misha Brukmanf905ed52003-11-07 17:45:28 +0000913 void emitGlobals(const Module &M);
914 void printGlobalVariable(const GlobalVariable *GV);
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000915};
916
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000917void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000918{
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000919 if (GV->hasExternalLinkage())
920 toAsm << "\t.global\t" << getID(GV) << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000921
Vikram S. Advefee76262002-10-13 00:32:18 +0000922 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000923 printConstant(GV->getInitializer(), getID(GV));
924 else {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000925 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
926 Target) << "\n";
Chris Lattner697954c2002-01-20 22:54:45 +0000927 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
Vikram S. Adveffbba0f2001-11-08 14:29:57 +0000928 toAsm << "\t.reserve\t" << getID(GV) << ","
Vikram S. Advefee76262002-10-13 00:32:18 +0000929 << TypeToSize(GV->getType()->getElementType(), Target)
Chris Lattner697954c2002-01-20 22:54:45 +0000930 << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000931 }
932}
933
Misha Brukmanf905ed52003-11-07 17:45:28 +0000934void SparcModuleAsmPrinter::emitGlobals(const Module &M) {
Chris Lattner637ed862002-08-07 21:39:48 +0000935 // Output global variables...
Vikram S. Advefee76262002-10-13 00:32:18 +0000936 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
937 if (! GI->isExternal()) {
938 assert(GI->hasInitializer());
939 if (GI->isConstant())
940 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
941 else if (GI->getInitializer()->isNullValue())
942 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
943 else
944 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
945
946 printGlobalVariable(GI);
Chris Lattner637ed862002-08-07 21:39:48 +0000947 }
Chris Lattner637ed862002-08-07 21:39:48 +0000948
Chris Lattner697954c2002-01-20 22:54:45 +0000949 toAsm << "\n";
Vikram S. Adve953c83e2001-10-28 21:38:52 +0000950}
951
Chris Lattnere88f78c2001-09-19 13:47:27 +0000952} // End anonymous namespace
953
Vikram S. Adve13f1d712002-09-16 15:54:02 +0000954Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
Chris Lattnerc19b8b12002-02-03 23:41:08 +0000955 return new SparcModuleAsmPrinter(Out, *this);
Chris Lattnerc019a172002-02-03 07:48:06 +0000956}