Add support for reading and writing pointersize/endianness to and from bytecode
[oota-llvm.git] / lib / Target / SparcV9 / MappingInfo.cpp
1 //===- MappingInfo.cpp - create LLVM info and output to .s file ---------===//
2 //
3 // Create Map from LLVM BB and Instructions and Machine Instructions
4 // and output the information as .byte directives to the .s file
5 // Currently Sparc specific but will be extended for others later
6 //
7 //===--------------------------------------------------------------------===//
8
9 #include "llvm/Reoptimizer/Mapping/MappingInfo.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "llvm/CodeGen/MachineInstr.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/MachineCodeForInstruction.h"
15 #include <map>
16 using std::vector;
17
18
19 // MappingInfo - This method collects mapping info 
20 // for the mapping from LLVM to machine code.
21 //
22 namespace {
23   class getMappingInfoForFunction : public FunctionPass { 
24     std::ostream &Out;
25   public:
26     getMappingInfoForFunction(std::ostream &out) : Out(out){}
27     const char* getPassName() const{return "Sparc MappingInformation";}
28     bool runOnFunction(Function &FI);
29   private:
30     std::map<const Function*, unsigned> Fkey; //key of F to num
31     std::map<const MachineInstr*, unsigned> BBkey; //key BB to num
32     std::map<const MachineInstr*, unsigned> MIkey; //key MI to num
33     
34     bool doInitialization(Module &M);
35     void create_BB_to_MInumber_Key(Function &FI);    
36     void create_MI_to_number_Key(Function &FI);
37     void writeBBToMImap(Function &FI);
38     void writeLLVMToMImap(Function &FI);
39     void getMappingInfoForFunction::writePrologue(const char * area,    
40                                                   const char *label,
41                                                   unsigned FunctionNo);
42     void getMappingInfoForFunction::writeEpilogue(const char *area, 
43                                                   const char *label,
44                                                   unsigned FunctionNo);
45     unsigned writeNumber(unsigned X);
46   };
47 }
48
49
50 //pass definition
51 Pass *MappingInfoForFunction(std::ostream &out){
52   return (new getMappingInfoForFunction(out));
53 }
54
55 //function definitions :
56 //create and output maps to the .s file
57 bool getMappingInfoForFunction::runOnFunction(Function &FI) {
58   
59   
60   //first create reference maps
61   //createFunctionKey(M);
62   create_BB_to_MInumber_Key(FI);
63   create_MI_to_number_Key(FI);
64   unsigned FunctionNo = Fkey[&(FI)];
65
66   //now print out the maps
67   writePrologue("BB TO MI MAP", "BBMIMap", FunctionNo);
68   writeBBToMImap(FI);
69   writeEpilogue("BB TO MI MAP", "BBMIMap", FunctionNo);  
70   
71   writePrologue("LLVM I TO MI MAP", "LMIMap", FunctionNo);
72   writeLLVMToMImap(FI);
73   writeEpilogue("LLVM I TO MI MAP", "LMIMap", FunctionNo); 
74   return false; 
75 }  
76
77 void getMappingInfoForFunction::writePrologue(const char *area,
78                                               const char *label, 
79                                               unsigned FunctionNo){
80   Out << "!" << area << "\n";   
81   Out << "\t.section \".rodata\"\n\t.align 8\n";  
82   Out << "\t.global " << label << FunctionNo << "\n";    
83   Out << "\t.type " << label << FunctionNo << ",#object\n"; 
84   Out << label << FunctionNo << ":\n"; 
85   Out << "\t.word .end_" << label << FunctionNo << "-"
86       << label << FunctionNo << "\n";
87 }
88
89 void getMappingInfoForFunction::writeEpilogue(const char *area,
90                                               const char *label,
91                                               unsigned FunctionNo){
92   Out << ".end_" << label << FunctionNo << ":\n";    
93   Out << "\t.size " << label << FunctionNo << ", .end_" 
94       << label << FunctionNo << "-" << label 
95       << FunctionNo << "\n\n\n\n";
96 }
97
98 //write out information as .byte directives
99 unsigned getMappingInfoForFunction::writeNumber(unsigned X) {
100   unsigned i=0;
101   do {
102     unsigned tmp = X & 127;
103     X >>= 7;
104     if (X) tmp |= 128;
105     Out << "\t.byte " << tmp << "\n";
106     ++i;
107   } while(X);
108   return i;
109 }
110
111 //Assign a number to each Function 
112 bool getMappingInfoForFunction::doInitialization(Module &M) {
113   unsigned i = 0;
114   for (Module::iterator FI = M.begin(), FE = M.end();
115        FI != FE; ++FI){
116     //dont count F with 0 BBs
117     if(FI->isExternal()) continue;
118     Fkey[FI] = i;
119     ++i;
120   }
121   return false;
122 }
123      
124 //Assign a Number to each BB
125 void getMappingInfoForFunction::create_BB_to_MInumber_Key(Function &FI) {
126   unsigned i = 0;
127   MachineFunction &MF = MachineFunction::get(&FI);
128   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
129        BI != BE; ++BI) {
130     MachineBasicBlock &miBB = *BI;
131     BBkey[miBB[0]] = i;
132     i = i+(miBB.size());
133   }
134 }
135
136 //Assign a number to each MI wrt beginning of the BB
137 void getMappingInfoForFunction::create_MI_to_number_Key(Function &FI) {
138   MachineFunction &MF = MachineFunction::get(&FI);
139   for (MachineFunction::iterator BI=MF.begin(), BE=MF.end(); BI != BE; ++BI) {
140     MachineBasicBlock &miBB = *BI;
141     unsigned j = 0;
142     for(MachineBasicBlock::iterator miI=miBB.begin(), miE=miBB.end();
143         miI!=miE; ++miI, ++j) {
144       MIkey[*miI]=j;
145     }
146   }
147 }
148
149 //BBtoMImap: contains F#, BB#, 
150 //              MI#[wrt beginning of F], #MI in BB
151 void getMappingInfoForFunction::writeBBToMImap(Function &FI){
152   unsigned bb = 0;
153   MachineFunction &MF = MachineFunction::get(&FI);  
154   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
155        BI != BE; ++BI, ++bb) {
156     MachineBasicBlock &miBB = *BI;
157     writeNumber(bb);
158     //Out << " BB: "<<(void *)BI<<"\n";
159     //for(int i=0; i<miBB.size(); ++i)
160     //Out<<*miBB[i]<<"\n";
161     writeNumber( BBkey[ miBB[0] ]);
162     writeNumber(miBB.size());
163   }
164 }
165
166 //LLVMtoMImap: contains F#, BB#, LLVM#, 
167 //                           MIs[wrt to beginning of BB] 
168 void getMappingInfoForFunction::writeLLVMToMImap(Function &FI) {
169
170   unsigned bb =0;
171   for (Function::iterator BI = FI.begin(),  BE = FI.end(); 
172        BI != BE; ++BI, ++bb) {
173     unsigned li = 0;
174     writeNumber(bb);
175     //std::cerr<<"BasicBlockNumber= "<<bb<<"\n";
176
177     //Out << "BB: "<<(void *)BI<<"\n";
178     writeNumber(BI->size());
179     //std::cerr<<"BasicBlockSize  = "<<BI->size()<<"\n";
180
181     for (BasicBlock::iterator II = BI->begin(), 
182            IE = BI->end(); II != IE; ++II, ++li) {
183     //Out << "I: "<<*II<<"\n";
184       MachineCodeForInstruction& miI = 
185         MachineCodeForInstruction::get(II);
186       
187       //do for each corr. MI
188       writeNumber(li);
189       //std::cerr<<"InstructionNumber= "<<li<<"\n";
190
191       writeNumber(miI.size());
192       //std::cerr<<"InstructionSize  = "<<miI.size()<<"\n";
193    
194       for (MachineCodeForInstruction::iterator miII = miI.begin(), 
195              miIE = miI.end(); miII != miIE; ++miII){
196         //Out << "MI: "<<**miII<<"\n";
197         writeNumber(MIkey[*miII]);
198         //std::cerr<<"MachineInstruction= "<<MIkey[*miII]<<"\n";
199       }
200     }
201   } 
202 }