Small changes to allow:
[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.addDeclaration(td.getGenerateType().getSafeSymbol(), vd.getSafeSymbol());
48         cr.outputline(vd.getSafeSymbol()+"=("+td.getGenerateType().getSafeSymbol()+") calloc(1,"+size.getSafeSymbol()+");");
49         cr.outputline("alloc((void *) "+vd.getSafeSymbol()+","+size.getSafeSymbol()+");");
50         if (Compiler.ALLOCATECPLUSPLUS) {
51             if (td instanceof StructureTypeDescriptor) {
52                 if (((StructureTypeDescriptor)td).size()>0) {
53                     FieldDescriptor fd=((StructureTypeDescriptor)td).get(0);
54                     if (fd.getSymbol().startsWith("_vptr_")) {
55                         String vtable="_ZTV";
56                         vtable+=td.getSymbol().length();
57                         vtable+=td.getSymbol();
58                         cr.outputline("((int**) "+vd.getSafeSymbol()+")[0] = (int *) & "+vtable+"+2;");
59                     }
60                 }
61             }
62         }
63     }
64
65     public boolean relsetSource(RelationDescriptor rd, boolean domain) {
66         if (domain)
67             return setSource(rd.getDomain());
68         else return setSource(rd.getRange());
69     }
70     public boolean relallocSource(RelationDescriptor rd, boolean domain) {
71         if (domain)
72             return allocSource(rd.getDomain());
73         else return allocSource(rd.getRange());
74     }
75     
76     public SetDescriptor relgetSourceSet(RelationDescriptor rd, boolean domain) {
77         if (domain)
78             return getSourceSet(rd.getDomain());
79         else return getSourceSet(rd.getRange());
80     }
81     public void relgenerateSourceAlloc(CodeWriter cr,VarDescriptor vd, RelationDescriptor rd, boolean domain) {
82         SetDescriptor sd=null;
83         if (domain)
84             sd=rd.getDomain();
85         else
86             sd=rd.getRange();
87         
88         generateSourceAlloc(cr, vd, sd);
89     }
90 }