1 //===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
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.
7 // The entry point of this file is the UltraSparc::emitAssembly method.
9 //===----------------------------------------------------------------------===//
11 #include "SparcInternals.h"
12 #include "llvm/Analysis/SlotCalculator.h"
13 #include "llvm/Transforms/Linker.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/GlobalVariable.h"
16 #include "llvm/GlobalValue.h"
17 #include "llvm/ConstPoolVals.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/Method.h"
21 #include "llvm/Module.h"
22 #include "llvm/Support/HashExtras.h"
23 #include "llvm/Support/StringExtras.h"
28 class SparcAsmPrinter {
29 typedef hash_map<const Value*, int> ValIdMap;
30 typedef ValIdMap:: iterator ValIdMapIterator;
31 typedef ValIdMap::const_iterator ValIdMapConstIterator;
34 SlotCalculator Table; // map anonymous values to unique integer IDs
35 ValIdMap valToIdMap; // used for values not handled by SlotCalculator
36 const UltraSparc &Target;
47 inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
48 : Out(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
53 void emitModule(const Module *M);
54 void emitMethod(const Method *M);
55 void emitGlobalsAndConstants(const Module* module);
56 //void processMethodArgument(const MethodArgument *MA);
57 void emitBasicBlock(const BasicBlock *BB);
58 void emitMachineInst(const MachineInstr *MI);
60 void printGlobalVariable(const GlobalVariable* GV);
61 void printConstant(const ConstPoolVal* CV, string valID = string(""));
63 unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
64 void printOneOperand(const MachineOperand &Op);
66 bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
67 bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
69 // enterSection - Use this method to enter a different section of the output
70 // executable. This is used to only output neccesary section transitions.
72 void enterSection(enum Sections S) {
73 if (S == CurSection) return; // Only switch section if neccesary
76 Out << "\n\t.section ";
79 default: assert(0 && "Bad section name!");
80 case Text: Out << "\".text\""; break;
81 case ReadOnlyData: Out << "\".rodata\",#alloc"; break;
82 case InitRWData: Out << "\".data\",#alloc,#write"; break;
83 case UninitRWData: Out << "\".bss\",#alloc,#write\nBbss.bss:"; break;
88 string getEscapedString(const string &S) {
91 for (unsigned i = 0; i < S.size(); ++i) {
93 if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
94 (C >= '0' && C <= '9')) {
98 Result += char('0' + ((unsigned char)C >> 4));
99 Result += char('0' + (C & 0xF));
105 // getID - Return a valid identifier for the specified value. Base it on
106 // the name of the identifier if possible, use a numbered value based on
107 // prefix otherwise. FPrefix is always prepended to the output identifier.
109 string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
110 string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
112 return FP + getEscapedString(V->getName());
114 int valId = Table.getValSlot(V);
116 ValIdMapConstIterator I = valToIdMap.find(V);
117 valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
120 return FP + string(Prefix) + itostr(valId);
124 // getID Wrappers - Ensure consistent usage...
125 string getID(const Module *M) {
126 return getID(M, "LLVMModule_");
128 string getID(const Method *M) {
129 return getID(M, "LLVMMethod_");
131 string getID(const BasicBlock *BB) {
132 return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
134 string getID(const GlobalVariable *GV) {
135 return getID(GV, "LLVMGlobal_", ".G_");
137 string getID(const ConstPoolVal *CV) {
138 return getID(CV, "LLVMConst_", ".C_");
141 unsigned getOperandMask(unsigned Opcode) {
143 case SUBcc: return 1 << 3; // Remove CC argument
144 case BA: case BRZ: // Remove Arg #0, which is always null or xcc
145 case BRLEZ: case BRLZ:
146 case BRNZ: case BRGZ:
147 case BRGEZ: return 1 << 0;
149 default: return 0; // By default, don't hack operands...
155 SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
156 unsigned int opNum) {
157 switch (MI->getOpCode()) {
159 case JMPLRET: return (opNum == 0);
160 default: return false;
166 SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
167 unsigned int opNum) {
168 if (Target.getInstrInfo().isLoad(MI->getOpCode()))
170 else if (Target.getInstrInfo().isStore(MI->getOpCode()))
177 #define PrintOp1PlusOp2(Op1, Op2) \
178 printOneOperand(Op1); Out << "+"; printOneOperand(Op2);
182 SparcAsmPrinter::printOperands(const MachineInstr *MI,
185 const MachineOperand& Op = MI->getOperand(opNum);
187 if (OpIsBranchTargetLabel(MI, opNum))
189 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
192 else if (OpIsMemoryAddressBase(MI, opNum))
195 PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
208 SparcAsmPrinter::printOneOperand(const MachineOperand &Op)
210 switch (Op.getOperandType())
212 case MachineOperand::MO_VirtualRegister:
213 case MachineOperand::MO_CCRegister:
214 case MachineOperand::MO_MachineRegister:
216 int RegNum = (int)Op.getAllocatedRegNum();
218 // ****this code is temporary till NULL Values are fixed
219 if (RegNum == 10000) {
220 Out << "<NULL VALUE>";
222 Out << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
227 case MachineOperand::MO_PCRelativeDisp:
229 const Value *Val = Op.getVRegValue();
231 Out << "\t<*NULL Value*>";
232 else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
234 else if (const Method *M = dyn_cast<const Method>(Val))
236 else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
238 else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
241 Out << "<unknown value=" << Val << ">";
245 case MachineOperand::MO_SignExtendedImmed:
246 case MachineOperand::MO_UnextendedImmed:
247 Out << Op.getImmedValue();
251 Out << Op; // use dump field
258 SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
260 unsigned Opcode = MI->getOpCode();
262 if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
263 return; // IGNORE PHI NODES
265 Out << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
267 unsigned Mask = getOperandMask(Opcode);
269 bool NeedComma = false;
271 for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
272 if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
273 if (NeedComma) Out << ", "; // Handle comma outputing
275 N = printOperands(MI, OpNum);
284 SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
286 // Emit a label for the basic block
287 Out << getID(BB) << ":\n";
289 // Get the vector of machine instructions corresponding to this bb.
290 const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
291 MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
293 // Loop over all of the instructions in the basic block...
294 for (; MII != MIE; ++MII)
295 emitMachineInst(*MII);
296 Out << "\n"; // Seperate BB's with newlines
300 SparcAsmPrinter::emitMethod(const Method *M)
302 if (M->isExternal()) return;
304 // Make sure the slot table has information about this method...
305 Table.incorporateMethod(M);
307 string MethName = getID(M);
308 Out << "!****** Outputing Method: " << MethName << " ******\n";
310 Out << "\t.align\t4\n\t.global\t" << MethName << "\n";
311 //Out << "\t.type\t" << MethName << ",#function\n";
312 Out << "\t.type\t" << MethName << ", 2\n";
313 Out << MethName << ":\n";
315 // Output code for all of the basic blocks in the method...
316 for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
319 // Output a .size directive so the debugger knows the extents of the function
320 Out << ".EndOf_" << MethName << ":\n\t.size "
321 << MethName << ", .EndOf_"
322 << MethName << "-" << MethName << endl;
324 // Put some spaces between the methods
327 // Forget all about M.
332 ArrayTypeIsString(ArrayType* arrayType)
334 return (arrayType->getElementType() == Type::UByteTy ||
335 arrayType->getElementType() == Type::SByteTy);
338 inline const string TypeToDataDirective(const Type* type) {
339 switch(type->getPrimitiveID()) {
340 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
342 case Type::UShortTyID: case Type::ShortTyID:
344 case Type::UIntTyID: case Type::IntTyID:
346 case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
348 case Type::FloatTyID:
350 case Type::DoubleTyID:
352 case Type::ArrayTyID:
353 if (ArrayTypeIsString((ArrayType*) type))
356 return "<InvaliDataTypeForPrinting>";
358 return "<InvaliDataTypeForPrinting>";
362 inline unsigned int ConstantToSize(const ConstPoolVal* CV,
363 const TargetMachine& target) {
364 if (ConstPoolArray* AV = dyn_cast<ConstPoolArray>(CV))
365 if (ArrayTypeIsString((ArrayType*) CV->getType()))
366 return 1 + AV->getNumOperands();
368 return target.findOptimalStorageSize(CV->getType());
373 unsigned int TypeToSize(const Type* type, const TargetMachine& target)
375 return target.findOptimalStorageSize(type);
380 TypeToAlignment(const Type* type, const TargetMachine& target)
382 if (type->getPrimitiveID() == Type::ArrayTyID &&
383 ArrayTypeIsString((ArrayType*) type))
384 return target.findOptimalStorageSize(Type::LongTy);
386 return target.findOptimalStorageSize(type);
391 SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
393 if (valID.length() == 0)
396 Out << "\t.align\t" << TypeToAlignment(CV->getType(), Target)
399 Out << valID << ":" << endl;
402 << TypeToDataDirective(CV->getType()) << "\t"
403 << CV->getStrValue(false) << endl;
405 Out << "\t.type" << "\t" << valID << ",#object" << endl;
406 Out << "\t.size" << "\t" << valID << ","
407 << ConstantToSize(CV, Target) << endl;
412 SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
414 Out << "\t.global\t" << getID(GV) << endl;
416 if (GV->hasInitializer())
417 printConstant(GV->getInitializer(), getID(GV));
419 Out << "\t.align" << TypeToAlignment(GV->getType()->getValueType(), Target)
420 << getID(GV) << ":" << endl;
422 Out << "\t.type" << "\t" << getID(GV) << ",#object" << endl;
423 Out << "\t.size" << "\t" << getID(GV) << ","
424 << TypeToSize(GV->getType()->getValueType(), Target)
431 FoldConstPools(const Module *M,
432 hash_set<const ConstPoolVal*>& moduleConstPool) {
433 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
434 const hash_set<const ConstPoolVal*>& pool =
435 (*I)->getMachineCode().getConstantPoolValues();
436 moduleConstPool.insert(pool.begin(), pool.end());
442 SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
444 // First, get the constants there were marked by the code generator for
445 // inclusion in the assembly code data area and fold them all into a
446 // single constant pool since there may be lots of duplicates. Also,
447 // lets force these constants into the slot table so that we can get
448 // unique names for unnamed constants also.
450 hash_set<const ConstPoolVal*> moduleConstPool;
451 FoldConstPools(M, moduleConstPool);
453 // Now, emit the three data sections separately; the cost of I/O should
454 // make up for the cost of extra passes over the globals list!
456 // Read-only data section (implies initialized)
457 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
459 const GlobalVariable* GV = *GI;
460 if (GV->hasInitializer() && GV->isConstant())
462 if (GI == M->gbegin())
463 enterSection(ReadOnlyData);
464 printGlobalVariable(GV);
468 for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
469 E = moduleConstPool.end(); I != E; ++I)
472 // Initialized read-write data section
473 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
475 const GlobalVariable* GV = *GI;
476 if (GV->hasInitializer() && ! GV->isConstant())
478 if (GI == M->gbegin())
479 enterSection(InitRWData);
480 printGlobalVariable(GV);
484 // Uninitialized read-write data section
485 for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
487 const GlobalVariable* GV = *GI;
488 if (! GV->hasInitializer())
490 if (GI == M->gbegin())
491 enterSection(UninitRWData);
492 printGlobalVariable(GV);
501 SparcAsmPrinter::emitModule(const Module *M)
503 // TODO: Look for a filename annotation on M to emit a .file directive
504 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
507 emitGlobalsAndConstants(M);
510 } // End anonymous namespace
514 // emitAssembly - Output assembly language code (a .s file) for the specified
515 // method. The specified method must have been compiled before this may be
519 UltraSparc::emitAssembly(const Module *M, ostream &Out) const
521 SparcAsmPrinter Print(Out, M, *this);