Be const correct
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9AsmPrinter.cpp
1 //===-- 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"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Method.h"
16 #include "llvm/Module.h"
17 #include "llvm/Support/StringExtras.h"
18
19 namespace {
20
21 class SparcAsmPrinter {
22   ostream &Out;
23   SlotCalculator Table;
24   const UltraSparc &Target;
25
26   enum Sections {
27     Unknown,
28     Text,
29     Data,
30     ReadOnly,
31   } CurSection;
32 public:
33   inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
34     : Out(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
35     emitModule(M);
36   }
37
38 private :
39   void emitModule(const Module *M);
40   /*
41   void processSymbolTable(const SymbolTable &ST);
42   void processConstant(const ConstPoolVal *CPV);
43   void processGlobal(const GlobalVariable *GV);
44   */
45   void emitMethod(const Method *M);
46   //void processMethodArgument(const MethodArgument *MA);
47   void emitBasicBlock(const BasicBlock *BB);
48   void emitMachineInst(const MachineInstr *MI);
49
50   
51   //void writeOperand(const Value *Op, bool PrintType, bool PrintName = true);
52
53
54   // enterSection - Use this method to enter a different section of the output
55   // executable.  This is used to only output neccesary section transitions.
56   //
57   void enterSection(enum Sections S) {
58     if (S == CurSection) return;        // Only switch section if neccesary
59     CurSection = S;
60
61     Out << ".section \".";
62     switch (S) {
63     default: assert(0 && "Bad section name!");
64     case Text:     Out << "text"; break;
65     case Data:     Out << "data"; break;
66     case ReadOnly: Out << "rodata"; break;
67     }
68     Out << "\"\n";
69   }
70
71   string getEscapedString(const string &S) {
72     string Result;
73
74     for (unsigned i = 0; i < S.size(); ++i) {
75       char C = S[i];
76       if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') ||
77           (C >= '0' && C <= '9')) {
78         Result += C;
79       } else {
80         Result += '$';
81         Result += char('0' + ((unsigned char)C >> 4));
82         Result += char('0' + (C & 0xF));
83       }
84     }
85     return Result;
86   }
87
88   // getID - Return a valid identifier for the specified value.  Base it on
89   // the name of the identifier if possible, use a numbered value based on
90   // prefix otherwise.  FPrefix is always prepended to the output identifier.
91   //
92   string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
93     string FP(FPrefix ? FPrefix : "");  // "Forced prefix"
94     if (V->hasName()) {
95       return FP + getEscapedString(V->getName());
96     } else {
97       assert(Table.getValSlot(V) != -1 && "Value not in value table!");
98       return FP + string(Prefix) + itostr(Table.getValSlot(V));
99     }
100   }
101
102   // getID Wrappers - Ensure consistent usage...
103   string getID(const Method *M) { return getID(M, "anon_method$"); }
104   string getID(const BasicBlock *BB) {
105     return getID(BB, "LL", (".L$"+getID(BB->getParent())+"$").c_str());
106   }
107
108   unsigned getOperandMask(unsigned Opcode) {
109     switch (Opcode) {
110     case SUBcc:   return 1 << 3;  // Remove CC argument
111     case BA:    case BRZ:         // Remove Arg #0, which is always null or xcc
112     case BRLEZ: case BRLZ:
113     case BRNZ:  case BRGZ:
114     case BRGEZ:   return 1 << 0;
115     case RETURN:  return 1 << 1;  // Remove Arg #2 which is zero
116
117     default:      return 0;       // By default, don't hack operands...
118     }
119   }
120 };
121
122
123 void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) {
124   unsigned Opcode = MI->getOpCode();
125
126   if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
127     return;  // IGNORE PHI NODES
128
129   Out << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
130
131   unsigned Mask = getOperandMask(Opcode);
132
133   bool NeedComma = false;
134   for(unsigned OpNum = 0; OpNum < MI->getNumOperands(); ++OpNum) {
135     if ((1 << OpNum) & Mask) continue;  // Ignore this operand?
136     
137     const MachineOperand &Op = MI->getOperand(OpNum);
138     if (NeedComma) Out << ", ";    // Handle comma outputing
139     NeedComma = true;
140
141     switch (Op.getOperandType()) {
142     case MachineOperand::MO_VirtualRegister:
143     case MachineOperand::MO_CCRegister:
144     case MachineOperand::MO_MachineRegister: {
145       int RegNum = (int)Op.getAllocatedRegNum();
146       
147       // ****this code is temporary till NULL Values are fixed
148       if (RegNum == 10000) {
149         Out << "<NULL VALUE>";
150         continue;
151       }
152       
153       Out << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
154       break;
155     }
156       
157     case MachineOperand::MO_PCRelativeDisp: {
158       const Value *Val = Op.getVRegValue();
159       if (!Val) {
160         Out << "\t<*NULL Value*>";
161       } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) {
162         Out << getID(BB);
163       } else {
164         Out << "<unknown value=" << Val << ">";
165       }
166       break;
167     }
168
169     default:
170       Out << Op;      // use dump field
171       break;
172     }
173   }
174   Out << endl;
175 }
176
177 void SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) {
178   // Emit a label for the basic block
179   Out << getID(BB) << ":\n";
180
181   // Get the vector of machine instructions corresponding to this bb.
182   const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
183   MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
184
185   // Loop over all of the instructions in the basic block...
186   for (; MII != MIE; ++MII)
187     emitMachineInst(*MII);
188   Out << "\n";  // Seperate BB's with newlines
189 }
190
191 void SparcAsmPrinter::emitMethod(const Method *M) {
192   if (M->isExternal()) return;
193
194   // Make sure the slot table has information about this method...
195   Table.incorporateMethod(M);
196
197   string MethName = getID(M);
198   Out << "!****** Outputing Method: " << MethName << " ******\n";
199   enterSection(Text);
200   Out << "\t.align 4\n\t.global\t" << MethName << "\n";
201   //Out << "\t.type\t" << MethName << ",#function\n";
202   Out << MethName << ":\n";
203
204   // Output code for all of the basic blocks in the method...
205   for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
206     emitBasicBlock(*I);
207
208   // Output a .size directive so the debugger knows the extents of the function
209   Out << ".EndOf$" << MethName << ":\n\t.size " << MethName << ", .EndOf$"
210       << MethName << "-" << MethName << endl;
211
212   // Put some spaces between the methods
213   Out << "\n\n";
214
215   // Forget all about M.
216   Table.purgeMethod();
217 }
218
219
220 void SparcAsmPrinter::emitModule(const Module *M) {
221   // TODO: Look for a filename annotation on M to emit a .file directive
222   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
223     emitMethod(*I);
224 }
225
226 }  // End anonymous namespace
227
228 //
229 // emitAssembly - Output assembly language code (a .s file) for the specified
230 // method. The specified method must have been compiled before this may be
231 // used.
232 //
233 void UltraSparc::emitAssembly(const Module *M, ostream &Out) const {
234   SparcAsmPrinter Print(Out, M, *this);
235 }