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