Update virtual pointers for C++ code...Various fixes to emit extern declarations.
[repair.git] / Repair / RepairCompiler / MCC / IR / Sources.java
1 package MCC.IR;
2
3 import MCC.State;
4 import MCC.Compiler;
5 import java.util.Iterator;
6
7 public class Sources {
8     State state;
9
10     public Sources(State s) {
11         this.state=s;
12     }
13
14     public boolean setSource(SetDescriptor sd) {
15         SetDescriptor usedblock=(SetDescriptor)state.stSets.get("UsedBlock");
16         SetDescriptor usedinode=(SetDescriptor)state.stSets.get("UsedInode");
17
18         if (usedblock!=null&&usedblock.isSubset(sd))
19             return true;
20         if (usedinode!=null&&usedinode.isSubset(sd))
21             return true;
22
23         return false;
24     }
25     public boolean allocSource(SetDescriptor sd) {
26         return !setSource(sd);
27     }
28     public SetDescriptor getSourceSet(SetDescriptor sd) {
29         SetDescriptor usedblock=(SetDescriptor)state.stSets.get("UsedBlock");
30         SetDescriptor usedinode=(SetDescriptor)state.stSets.get("UsedInode");
31
32         if (usedblock!=null&&usedblock.isSubset(sd))
33             return (SetDescriptor)state.stSets.get("FreeBlock");
34         if (usedinode!=null&&usedinode.isSubset(sd))
35             return (SetDescriptor)state.stSets.get("FreeInode");
36
37         return null;
38     }
39
40     public void generateSourceAlloc(CodeWriter cr,VarDescriptor vd, SetDescriptor sd) {
41         TypeDescriptor td=sd.getType();
42         Expr e=td.getSizeExpr();
43         VarDescriptor size=VarDescriptor.makeNew("size");
44         cr.pushSymbolTable(state.stGlobals);
45         e.generate(cr, size);
46         cr.popSymbolTable();
47         cr.outputline(td.getGenerateType().getSafeSymbol()+" "+vd.getSafeSymbol()+"=("+td.getGenerateType().getSafeSymbol()+") calloc(1,"+size.getSafeSymbol()+");");
48         
49         if (Compiler.ALLOCATECPLUSPLUS) {
50             if (td instanceof StructureTypeDescriptor) {
51                 if (((StructureTypeDescriptor)td).size()>0) {
52                     FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
53                     if (fd.getSymbol().startsWith("_vptr_")) {
54                         String vtable="_ZTV";
55                         vtable+=td.getSymbol().length();
56                         vtable+=td.getSymbol();
57                         cr.outputline("((int**) &"+vd.getSafeSymbol()+")[0] = (int *)"+vtable+"+2;");
58                     }
59                 }
60             }
61         }
62     }
63
64     public boolean relsetSource(RelationDescriptor rd, boolean domain) {
65         if (domain)
66             return setSource(rd.getDomain());
67         else return setSource(rd.getRange());
68     }
69     public boolean relallocSource(RelationDescriptor rd, boolean domain) {
70         if (domain)
71             return allocSource(rd.getDomain());
72         else return allocSource(rd.getRange());
73     }
74     
75     public SetDescriptor relgetSourceSet(RelationDescriptor rd, boolean domain) {
76         if (domain)
77             return getSourceSet(rd.getDomain());
78         else return getSourceSet(rd.getRange());
79     }
80     public void relgenerateSourceAlloc(CodeWriter cr,VarDescriptor vd, RelationDescriptor rd, boolean domain) {
81         SetDescriptor sd=null;
82         if (domain)
83             sd=rd.getDomain();
84         else
85             sd=rd.getRange();
86         
87         generateSourceAlloc(cr, vd, sd);
88     }
89 }