Added:
[repair.git] / Repair / RepairCompiler / MCC / IR / StructureGenerator.java
1 package MCC.IR;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.State;
6
7 public class StructureGenerator {
8     State state;
9     CodeWriter cr;
10     CodeWriter crhead;
11     TypeDescriptor[] tdarray;
12     RepairGenerator rg;
13     StructureGenerator(State state, RepairGenerator rg) {
14         this.state=state;
15         this.rg=rg;
16         try {
17             cr=new StandardCodeWriter(new java.io.PrintWriter(new FileOutputStream("size.cc"),true));
18             crhead=new StandardCodeWriter(new java.io.PrintWriter(new FileOutputStream("size.h"),true));
19         } catch (Exception e) {
20             e.printStackTrace();
21         }
22     }
23
24     void buildall() {
25         int max=TypeDescriptor.counter;
26         tdarray=new TypeDescriptor[max];
27         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
28             TypeDescriptor ttd=(TypeDescriptor)it.next();
29             tdarray[ttd.getId()]=ttd;
30         }
31         cr.outputline("#include \"size.h\"");
32         generategetfield();
33         generategetnumfields();
34         generateisArray();
35         generateisPtr();
36         generateissubtype();
37         generatecalls();
38         generatecomputesize();
39         generateheader();
40     }
41
42     private void generatecalls() {
43         int max=TypeDescriptor.counter;
44         cr.outputline("int arsize["+max+"];");
45
46         for(int i=0;i<max;i++) {
47             TypeDescriptor ttd=tdarray[i];
48             if (ttd instanceof StructureTypeDescriptor) {
49                 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
50                 String str="int arnumelement"+std.getId()+"["+std.fieldlist.size()+"]={";
51                 for(int j=0;j<std.fieldlist.size();j++) {
52                     str+=0;
53                     if (((j+1)!=std.fieldlist.size()))
54                         str+=",";
55                 }
56                 str+="};";
57                 cr.outputline(str);
58             } else
59                 cr.outputline("int arnumelement"+ttd.getId()+"[0];");
60         }
61         String str="int* arnumelements["+String.valueOf(max)+"]={";
62         for(int i=0;i<max;i++) {
63             str+="arnumelement"+i;
64             if (((i+1)!=max))
65                 str+=",";
66         }
67         str+="};";
68         cr.outputline(str);
69         
70
71         cr.outputline("int typeobject::size(int type) {");
72         cr.outputline("return arsize[type];");
73         cr.outputline("}");
74
75         cr.outputline("int typeobject::numElements(int type, int fieldindex) {");
76         cr.outputline("return arnumelements[type][fieldindex];");
77         cr.outputline("}");
78         cr.outputline("typeobject::typeobject() {}");
79     }
80
81     private void generatecomputesize() {
82         int max=TypeDescriptor.counter;
83         cr.outputline("void typeobject::computesizes("+rg.name+"_state * obj) {");
84         cr.outputline("obj->computesizes(arsize,arnumelements);");
85         cr.outputline("}");
86     }
87
88     private void generateheader() {
89         crhead.outputline("#include \""+rg.headername + "\"");
90         crhead.outputline("class typeobject {");
91         crhead.outputline("public:");
92         crhead.outputline("typeobject();");
93         crhead.outputline("int getfield(int type, int fieldindex);");
94         crhead.outputline("int isArray(int type, int fieldindex);");
95         crhead.outputline("int isPtr(int type, int fieldindex);");
96         crhead.outputline("int numElements(int type, int fieldindex);");
97         crhead.outputline("int size(int type);");
98         crhead.outputline("int getnumfields(int type);");
99         crhead.outputline("bool issubtype(int subtype, int type);");
100         crhead.outputline("void computesizes("+rg.name+"_state *);");
101         crhead.outputline("};");
102     }
103
104     
105     private void generategetfield() {
106         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
107             TypeDescriptor ttd=(TypeDescriptor)it.next();
108             String str="";
109
110             if (ttd instanceof StructureTypeDescriptor) {
111                 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
112                 str="int argetfield"+std.getId()+"["+std.fieldlist.size()+"]={";
113                 for(int i=0;i<std.fieldlist.size();i++) {
114                     FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
115                     TypeDescriptor td = fd.getType();
116                     str+=String.valueOf(td.getId());
117                     if ((i+1)!=std.fieldlist.size())
118                         str+=",";
119                 }
120                 str+="};";
121                 cr.outputline(str);
122             } else
123                 cr.outputline("int argetfield"+ttd.getId()+"[0];");
124         }
125         int max=TypeDescriptor.counter;
126         String str="int* argetfield["+String.valueOf(max)+"]={";
127         for(int i=0;i<max;i++) {
128             str+="argetfield"+i;
129             if (((i+1)!=max))
130                 str+=",";
131         }
132         str+="};";
133         cr.outputline(str);
134         
135         cr.outputline("int typeobject::getfield(int type, int fieldindex) {");
136         cr.outputline("return argetfield[type][fieldindex];");
137         cr.outputline("}");
138     }
139
140    private void generategetnumfields() {
141         int max=TypeDescriptor.counter;
142         String str="int argetnumfield["+String.valueOf(max)+"]={";
143         for(int i=0;i<max;i++) {
144             TypeDescriptor ttd=tdarray[i];
145             if (ttd instanceof StructureTypeDescriptor) {
146                 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
147                 str+=String.valueOf(std.fieldlist.size());
148             } else
149                 str+="0";
150             if (((i+1)!=max))
151                 str+=",";
152         }
153         str+="};";
154         cr.outputline(str);
155         
156         cr.outputline("int typeobject::getnumfields(int type) {");
157         cr.outputline("return argetnumfield[type];");
158         cr.outputline("}");
159     }
160     private void generateisArray() {
161         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
162             TypeDescriptor ttd=(TypeDescriptor)it.next();
163             String str="";
164
165             if (ttd instanceof StructureTypeDescriptor) {
166                 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
167                 str="int arisArray"+std.getId()+"["+std.fieldlist.size()+"]={";
168                 for(int i=0;i<std.fieldlist.size();i++) {
169                     FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
170                     TypeDescriptor td = fd.getType();
171                     if (fd instanceof ArrayDescriptor)
172                         str+="1";
173                     else
174                         str+="0";
175                     if ((i+1)!=std.fieldlist.size())
176                         str+=",";
177                 }
178                 str+="};";
179                 cr.outputline(str);
180             } else
181                 cr.outputline("int arisArray"+ttd.getId()+"[0];");
182         }
183         int max=TypeDescriptor.counter;
184         String str="int* arisArray["+String.valueOf(max)+"]={";
185         for(int i=0;i<max;i++) {
186             str+="arisArray"+i;
187             if (((i+1)!=max))
188                 str+=",";
189         }
190         str+="};";
191         cr.outputline(str);
192         
193         cr.outputline("int typeobject::isArray(int type, int fieldindex) {");
194         cr.outputline("return arisArray[type][fieldindex];");
195         cr.outputline("}");
196     }
197     private void generateisPtr() {
198         for(Iterator it=state.stTypes.descriptors();it.hasNext();) {
199             TypeDescriptor ttd=(TypeDescriptor)it.next();
200             String str="";
201
202             if (ttd instanceof StructureTypeDescriptor) {
203                 StructureTypeDescriptor std=(StructureTypeDescriptor) ttd;
204                 str="int arisPtr"+std.getId()+"["+std.fieldlist.size()+"]={";
205                 for(int i=0;i<std.fieldlist.size();i++) {
206                     FieldDescriptor fd = (FieldDescriptor)std.fieldlist.elementAt(i);
207                     if (fd.getPtr())
208                         str+="1";
209                     else
210                         str+="0";
211                     if ((i+1)!=std.fieldlist.size())
212                         str+=",";
213                 }
214                 str+="};";
215                 cr.outputline(str);
216             } else
217                 cr.outputline("int arisPtr"+ttd.getId()+"[0];");
218         }
219         int max=TypeDescriptor.counter;
220         String str="int* arisPtr["+String.valueOf(max)+"]={";
221         for(int i=0;i<max;i++) {
222             str+="arisPtr"+i;
223             if (((i+1)!=max))
224                 str+=",";
225         }
226         str+="};";
227         cr.outputline(str);
228         
229         cr.outputline("int typeobject::isPtr(int type, int fieldindex) {");
230         cr.outputline("return arisPtr[type][fieldindex];");
231         cr.outputline("}");
232     }
233
234     void generateissubtype() {
235         int max=TypeDescriptor.counter;
236         String str="bool arissubtype["+max+"]["+max+"]={";
237         for(int i=0;i<max;i++) {
238             str+="{";
239             for(int j=0;j<max;j++) {
240                 TypeDescriptor tdi=tdarray[i];
241                 TypeDescriptor tdj=tdarray[j];
242                 if (tdi.isSubtypeOf(tdj))
243                     str+="1";
244                 else
245                     str+="0";
246                 if ((j+1)!=max)
247                     str+=",";
248             }
249             str+="}";
250             if ((i+1)!=max)
251                 str+=",";
252         }
253         str+="};";
254         cr.outputline(str);
255         cr.outputline("bool typeobject::issubtype(int subtype, int type) {");
256         cr.outputline("return arissubtype[subtype][type];");
257         cr.outputline("}");
258     }
259 }