1 //===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements all of the stuff necessary to output a .s file from
11 // LLVM. The code in this file assumes that the specified module has already
12 // been compiled into the internal data structures of the Module.
14 // 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.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineFunctionInfo.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "Support/StringExtras.h"
31 #include "Support/Statistic.h"
32 #include "SparcInternals.h"
37 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39 class GlobalIdTable: public Annotation {
40 static AnnotationID AnnotId;
41 friend class AsmPrinter; // give access to AnnotId
43 // AnonymousObjectMap - map anonymous values to unique integer IDs
44 std::map<const Value*, unsigned> AnonymousObjectMap;
45 unsigned LastAnonIDUsed;
47 GlobalIdTable() : Annotation(AnnotId), LastAnonIDUsed(0) {}
50 AnnotationID GlobalIdTable::AnnotId =
51 AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
53 //===--------------------------------------------------------------------===//
56 /// getAsCString - Return the specified array as a C compatible string, only
57 /// if the predicate isString() is true.
59 std::string getAsCString(const ConstantArray *CVA) {
60 assert(CVA->isString() && "Array is not string compatible!");
62 std::string Result = "\"";
63 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
64 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
68 } else if (C == '\\') {
70 } else if (isprint(C)) {
73 Result += '\\'; // print all other chars as octal value
74 // Convert C to octal representation
75 Result += ((C >> 6) & 7) + '0';
76 Result += ((C >> 3) & 7) + '0';
77 Result += ((C >> 0) & 7) + '0';
85 inline bool ArrayTypeIsString(const ArrayType* arrayType) {
86 return (arrayType->getElementType() == Type::UByteTy ||
87 arrayType->getElementType() == Type::SByteTy);
90 inline const std::string
91 TypeToDataDirective(const Type* type) {
92 switch(type->getPrimitiveID())
94 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
96 case Type::UShortTyID: case Type::ShortTyID:
98 case Type::UIntTyID: case Type::IntTyID:
100 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
102 case Type::FloatTyID:
104 case Type::DoubleTyID:
106 case Type::ArrayTyID:
107 if (ArrayTypeIsString((ArrayType*) type))
110 return "<InvaliDataTypeForPrinting>";
112 return "<InvaliDataTypeForPrinting>";
116 /// Get the size of the constant for the given target.
117 /// If this is an unsized array, return 0.
120 ConstantToSize(const Constant* CV, const TargetMachine& target) {
121 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) {
122 const ArrayType *aty = cast<ArrayType>(CVA->getType());
123 if (ArrayTypeIsString(aty))
124 return 1 + CVA->getNumOperands();
127 return target.findOptimalStorageSize(CV->getType());
130 /// Align data larger than one L1 cache line on L1 cache line boundaries.
131 /// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
134 SizeToAlignment(unsigned int size, const TargetMachine& target) {
135 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
136 if (size > (unsigned) cacheLineSize / 2)
137 return cacheLineSize;
139 for (unsigned sz=1; /*no condition*/; sz *= 2)
144 /// Get the size of the type and then use SizeToAlignment.
147 TypeToAlignment(const Type* type, const TargetMachine& target) {
148 return SizeToAlignment(target.findOptimalStorageSize(type), target);
151 /// Get the size of the constant and then use SizeToAlignment.
152 /// Handles strings as a special case;
154 ConstantToAlignment(const Constant* CV, const TargetMachine& target) {
155 if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
156 if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
157 return SizeToAlignment(1 + CVA->getNumOperands(), target);
159 return TypeToAlignment(CV->getType(), target);
162 } // End anonymous namespace
166 //===---------------------------------------------------------------------===//
167 // Code abstracted away from the AsmPrinter
168 //===---------------------------------------------------------------------===//
172 GlobalIdTable* idTable;
175 const TargetMachine &Target;
185 AsmPrinter(std::ostream &os, const TargetMachine &T)
186 : idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
188 // (start|end)(Module|Function) - Callback methods invoked by subclasses
189 void startModule(Module &M) {
190 // Create the global id table if it does not already exist
191 idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
192 if (idTable == NULL) {
193 idTable = new GlobalIdTable();
194 M.addAnnotation(idTable);
198 void PrintZeroBytesToPad(int numBytes) {
199 for (/* no init */; numBytes >= 8; numBytes -= 8)
200 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
203 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
208 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
211 /// Print a single constant value.
213 void printSingleConstantValue(const Constant* CV);
215 /// Print a constant value or values (it may be an aggregate).
216 /// Uses printSingleConstantValue() to print each individual value.
218 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
220 // Print a constant (which may be an aggregate) prefixed by all the
221 // appropriate directives. Uses printConstantValueOnly() to print the
223 void printConstant(const Constant* CV, std::string valID = "") {
224 if (valID.length() == 0)
227 toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
229 // Print .size and .type only if it is not a string.
230 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
231 if (CVA->isString()) {
232 // print it as a string and return
233 toAsm << valID << ":\n";
234 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
238 toAsm << "\t.type" << "\t" << valID << ",#object\n";
240 unsigned int constSize = ConstantToSize(CV, Target);
242 toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
244 toAsm << valID << ":\n";
246 printConstantValueOnly(CV);
249 // enterSection - Use this method to enter a different section of the output
250 // executable. This is used to only output necessary section transitions.
252 void enterSection(enum Sections S) {
253 if (S == CurSection) return; // Only switch section if necessary
256 toAsm << "\n\t.section ";
259 default: assert(0 && "Bad section name!");
260 case Text: toAsm << "\".text\""; break;
261 case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
262 case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
263 case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
268 static std::string getValidSymbolName(const std::string &S) {
271 // Symbol names in Sparc assembly language have these rules:
272 // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
273 // (b) A name beginning in "." is treated as a local name.
278 for (unsigned i = 0; i < S.size(); ++i) {
280 if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
284 Result += char('0' + ((unsigned char)C >> 4));
285 Result += char('0' + (C & 0xF));
291 // getID - Return a valid identifier for the specified value. Base it on
292 // the name of the identifier if possible (qualified by the type), and
293 // use a numbered value based on prefix otherwise.
294 // FPrefix is always prepended to the output identifier.
296 std::string getID(const Value *V, const char *Prefix,
297 const char *FPrefix = "")
299 std::string Result = FPrefix; // "Forced prefix"
301 Result += V->hasName() ? V->getName() : std::string(Prefix);
303 // Qualify all internal names with a unique id.
304 if (!isa<GlobalValue>(V) || !cast<GlobalValue>(V)->hasExternalLinkage()) {
305 unsigned &ValID = idTable->AnonymousObjectMap[V];
307 ValID = ++idTable->LastAnonIDUsed;
309 Result += "_" + utostr(ValID);
311 // Replace or prefix problem characters in the name
312 Result = getValidSymbolName(Result);
318 // getID Wrappers - Ensure consistent usage...
319 std::string getID(const Function *F) {
320 return getID(F, "LLVMFunction_");
322 std::string getID(const BasicBlock *BB) {
323 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
325 std::string getID(const GlobalVariable *GV) {
326 return getID(GV, "LLVMGlobal_");
328 std::string getID(const Constant *CV) {
329 return getID(CV, "LLVMConst_", ".C_");
331 std::string getID(const GlobalValue *GV) {
332 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
334 else if (const Function *F = dyn_cast<Function>(GV))
336 assert(0 && "Unexpected type of GlobalValue!");
340 // Combines expressions
341 inline std::string ConstantArithExprToString(const ConstantExpr* CE,
342 const TargetMachine &TM,
343 const std::string &op) {
344 return "(" + valToExprString(CE->getOperand(0), TM) + op
345 + valToExprString(CE->getOperand(1), TM) + ")";
348 /// ConstantExprToString() - Convert a ConstantExpr to an asm expression
349 /// and return this as a string.
351 std::string ConstantExprToString(const ConstantExpr* CE,
352 const TargetMachine& target);
354 /// valToExprString - Helper function for ConstantExprToString().
355 /// Appends result to argument string S.
357 std::string valToExprString(const Value* V, const TargetMachine& target);
359 } // End anonymous namespace
362 /// Print a single constant value.
364 void AsmPrinter::printSingleConstantValue(const Constant* CV) {
365 assert(CV->getType() != Type::VoidTy &&
366 CV->getType() != Type::TypeTy &&
367 CV->getType() != Type::LabelTy &&
368 "Unexpected type for Constant");
370 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
371 && "Aggregate types should be handled outside this function");
373 toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
375 if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV)) {
376 // This is a constant address for a global variable or method.
377 // Use the name of the variable or method as the address value.
378 assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
379 toAsm << getID(CPR->getValue()) << "\n";
380 } else if (isa<ConstantPointerNull>(CV)) {
381 // Null pointer value
383 } else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
384 // Constant expression built from operators, constants, and symbolic addrs
385 toAsm << ConstantExprToString(CE, Target) << "\n";
386 } else if (CV->getType()->isPrimitiveType()) {
387 // Check primitive types last
388 if (CV->getType()->isFloatingPoint()) {
389 // FP Constants are printed as integer constants to avoid losing
391 double Val = cast<ConstantFP>(CV)->getValue();
392 if (CV->getType() == Type::FloatTy) {
393 float FVal = (float)Val;
394 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
395 toAsm << *(unsigned int*)ProxyPtr;
396 } else if (CV->getType() == Type::DoubleTy) {
397 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
398 toAsm << *(uint64_t*)ProxyPtr;
400 assert(0 && "Unknown floating point type!");
403 toAsm << "\t! " << CV->getType()->getDescription()
404 << " value: " << Val << "\n";
406 WriteAsOperand(toAsm, CV, false, false) << "\n";
409 assert(0 && "Unknown elementary type for constant");
413 /// Print a constant value or values (it may be an aggregate).
414 /// Uses printSingleConstantValue() to print each individual value.
416 void AsmPrinter::printConstantValueOnly(const Constant* CV,
417 int numPadBytesAfter) {
418 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
419 if (CVA->isString()) {
420 // print the string alone and return
421 toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
423 // Not a string. Print the values in successive locations
424 const std::vector<Use> &constValues = CVA->getValues();
425 for (unsigned i=0; i < constValues.size(); i++)
426 printConstantValueOnly(cast<Constant>(constValues[i].get()));
428 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
429 // Print the fields in successive locations. Pad to align if needed!
430 const StructLayout *cvsLayout =
431 Target.getTargetData().getStructLayout(CVS->getType());
432 const std::vector<Use>& constValues = CVS->getValues();
433 unsigned sizeSoFar = 0;
434 for (unsigned i=0, N = constValues.size(); i < N; i++) {
435 const Constant* field = cast<Constant>(constValues[i].get());
437 // Check if padding is needed and insert one or more 0s.
439 Target.getTargetData().getTypeSize(field->getType());
440 int padSize = ((i == N-1? cvsLayout->StructSize
441 : cvsLayout->MemberOffsets[i+1])
442 - cvsLayout->MemberOffsets[i]) - fieldSize;
443 sizeSoFar += (fieldSize + padSize);
445 // Now print the actual field value
446 printConstantValueOnly(field, padSize);
448 assert(sizeSoFar == cvsLayout->StructSize &&
449 "Layout of constant struct may be incorrect!");
452 printSingleConstantValue(CV);
454 if (numPadBytesAfter)
455 PrintZeroBytesToPad(numPadBytesAfter);
458 /// ConstantExprToString() - Convert a ConstantExpr to an asm expression
459 /// and return this as a string.
461 std::string AsmPrinter::ConstantExprToString(const ConstantExpr* CE,
462 const TargetMachine& target) {
464 switch(CE->getOpcode()) {
465 case Instruction::GetElementPtr:
466 { // generate a symbolic expression for the byte address
467 const Value* ptrVal = CE->getOperand(0);
468 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
469 const TargetData &TD = target.getTargetData();
470 S += "(" + valToExprString(ptrVal, target) + ") + ("
471 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
475 case Instruction::Cast:
476 // Support only non-converting casts for now, i.e., a no-op.
477 // This assertion is not a complete check.
478 assert(target.getTargetData().getTypeSize(CE->getType()) ==
479 target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
480 S += "(" + valToExprString(CE->getOperand(0), target) + ")";
483 case Instruction::Add:
484 S += ConstantArithExprToString(CE, target, ") + (");
487 case Instruction::Sub:
488 S += ConstantArithExprToString(CE, target, ") - (");
491 case Instruction::Mul:
492 S += ConstantArithExprToString(CE, target, ") * (");
495 case Instruction::Div:
496 S += ConstantArithExprToString(CE, target, ") / (");
499 case Instruction::Rem:
500 S += ConstantArithExprToString(CE, target, ") % (");
503 case Instruction::And:
504 // Logical && for booleans; bitwise & otherwise
505 S += ConstantArithExprToString(CE, target,
506 ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
509 case Instruction::Or:
510 // Logical || for booleans; bitwise | otherwise
511 S += ConstantArithExprToString(CE, target,
512 ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
515 case Instruction::Xor:
516 // Bitwise ^ for all types
517 S += ConstantArithExprToString(CE, target, ") ^ (");
521 assert(0 && "Unsupported operator in ConstantExprToString()");
528 /// valToExprString - Helper function for ConstantExprToString().
529 /// Appends result to argument string S.
531 std::string AsmPrinter::valToExprString(const Value* V,
532 const TargetMachine& target) {
535 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
536 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
537 S += std::string(CB == ConstantBool::True ? "1" : "0");
538 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
539 S += itostr(CI->getValue());
540 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
541 S += utostr(CI->getValue());
542 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
543 S += ftostr(CFP->getValue());
544 else if (isa<ConstantPointerNull>(CV))
546 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
547 S += valToExprString(CPR->getValue(), target);
548 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
549 S += ConstantExprToString(CE, target);
552 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
558 assert(0 && "Cannot convert value to string");
559 S += "<illegal-value>";
565 //===----------------------------------------------------------------------===//
566 // SparcAsmPrinter Code
567 //===----------------------------------------------------------------------===//
571 struct SparcAsmPrinter : public FunctionPass, public AsmPrinter {
572 inline SparcAsmPrinter(std::ostream &os, const TargetMachine &t)
573 : AsmPrinter(os, t) {}
575 const Function *currFunction;
577 const char *getPassName() const {
578 return "Output Sparc Assembly for Functions";
581 virtual bool doInitialization(Module &M) {
586 virtual bool runOnFunction(Function &F) {
592 virtual bool doFinalization(Module &M) {
597 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
598 AU.setPreservesAll();
601 void emitFunction(const Function &F);
603 void emitBasicBlock(const MachineBasicBlock &MBB);
604 void emitMachineInst(const MachineInstr *MI);
606 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
607 void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
609 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
610 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
612 unsigned getOperandMask(unsigned Opcode) {
615 case V9::SUBcci: return 1 << 3; // Remove CC argument
616 default: return 0; // By default, don't hack operands...
620 void emitGlobals(const Module &M);
621 void printGlobalVariable(const GlobalVariable *GV);
624 } // End anonymous namespace
627 SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
628 unsigned int opNum) {
629 switch (MI->getOpCode()) {
641 SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
642 unsigned int opNum) {
643 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
645 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
652 #define PrintOp1PlusOp2(mop1, mop2, opCode) \
653 printOneOperand(mop1, opCode); \
655 printOneOperand(mop2, opCode);
658 SparcAsmPrinter::printOperands(const MachineInstr *MI,
661 const MachineOperand& mop = MI->getOperand(opNum);
663 if (OpIsBranchTargetLabel(MI, opNum)) {
664 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
666 } else if (OpIsMemoryAddressBase(MI, opNum)) {
668 PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
672 printOneOperand(mop, MI->getOpCode());
678 SparcAsmPrinter::printOneOperand(const MachineOperand &mop,
679 MachineOpCode opCode)
681 bool needBitsFlag = true;
683 if (mop.isHiBits32())
685 else if (mop.isLoBits32())
687 else if (mop.isHiBits64())
689 else if (mop.isLoBits64())
692 needBitsFlag = false;
694 switch (mop.getType())
696 case MachineOperand::MO_VirtualRegister:
697 case MachineOperand::MO_CCRegister:
698 case MachineOperand::MO_MachineRegister:
700 int regNum = (int)mop.getAllocatedRegNum();
702 if (regNum == Target.getRegInfo().getInvalidRegNum()) {
703 // better to print code with NULL registers than to die
704 toAsm << "<NULL VALUE>";
706 toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
711 case MachineOperand::MO_ConstantPoolIndex:
713 toAsm << ".CPI_" << currFunction->getName()
714 << "_" << mop.getConstantPoolIndex();
718 case MachineOperand::MO_PCRelativeDisp:
720 const Value *Val = mop.getVRegValue();
721 assert(Val && "\tNULL Value in SparcAsmPrinter");
723 if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
725 else if (const Function *M = dyn_cast<Function>(Val))
727 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
729 else if (const Constant *CV = dyn_cast<Constant>(Val))
732 assert(0 && "Unrecognized value in SparcAsmPrinter");
736 case MachineOperand::MO_SignExtendedImmed:
737 toAsm << mop.getImmedValue();
740 case MachineOperand::MO_UnextendedImmed:
741 toAsm << (uint64_t) mop.getImmedValue();
745 toAsm << mop; // use dump field
753 void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
754 unsigned Opcode = MI->getOpCode();
756 if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
757 return; // IGNORE PHI NODES
759 toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
761 unsigned Mask = getOperandMask(Opcode);
763 bool NeedComma = false;
765 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
766 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
767 if (NeedComma) toAsm << ", "; // Handle comma outputting
769 N = printOperands(MI, OpNum);
777 void SparcAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
778 // Emit a label for the basic block
779 toAsm << getID(MBB.getBasicBlock()) << ":\n";
781 // Loop over all of the instructions in the basic block...
782 for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
784 emitMachineInst(*MII);
785 toAsm << "\n"; // Separate BB's with newlines
788 void SparcAsmPrinter::emitFunction(const Function &F) {
789 std::string methName = getID(&F);
790 toAsm << "!****** Outputing Function: " << methName << " ******\n";
792 // Emit constant pool for this function
793 const MachineConstantPool *MCP = MachineFunction::get(&F).getConstantPool();
794 const std::vector<Constant*> &CP = MCP->getConstants();
796 enterSection(AsmPrinter::ReadOnlyData);
797 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
798 std::string cpiName = ".CPI_" + F.getName() + "_" + utostr(i);
799 printConstant(CP[i], cpiName);
802 enterSection(AsmPrinter::Text);
803 toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
804 //toAsm << "\t.type\t" << methName << ",#function\n";
805 toAsm << "\t.type\t" << methName << ", 2\n";
806 toAsm << methName << ":\n";
808 // Output code for all of the basic blocks in the function...
809 MachineFunction &MF = MachineFunction::get(&F);
810 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
813 // Output a .size directive so the debugger knows the extents of the function
814 toAsm << ".EndOf_" << methName << ":\n\t.size "
815 << methName << ", .EndOf_"
816 << methName << "-" << methName << "\n";
818 // Put some spaces between the functions
822 void SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
823 if (GV->hasExternalLinkage())
824 toAsm << "\t.global\t" << getID(GV) << "\n";
826 if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue()) {
827 printConstant(GV->getInitializer(), getID(GV));
829 toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
831 toAsm << "\t.type\t" << getID(GV) << ",#object\n";
832 toAsm << "\t.reserve\t" << getID(GV) << ","
833 << Target.findOptimalStorageSize(GV->getType()->getElementType())
838 void SparcAsmPrinter::emitGlobals(const Module &M) {
839 // Output global variables...
840 for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
841 if (! GI->isExternal()) {
842 assert(GI->hasInitializer());
843 if (GI->isConstant())
844 enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
845 else if (GI->getInitializer()->isNullValue())
846 enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
848 enterSection(AsmPrinter::InitRWData); // read-write non-zero data
850 printGlobalVariable(GI);
856 FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out,
857 const TargetMachine &TM) {
858 return new SparcAsmPrinter(Out, TM);