Fix bug for MGC garbage collection
[IRC.git] / Robust / src / IR / Flat / FlatCall.java
1 package IR.Flat;
2 import IR.MethodDescriptor;
3
4 public class FlatCall extends FlatNode {
5   TempDescriptor args[];
6   TempDescriptor this_temp;
7   TempDescriptor dst;
8   MethodDescriptor method;
9
10   public FlatCall(MethodDescriptor md, TempDescriptor dst, TempDescriptor this_temp, TempDescriptor[] args) {
11     this.method=md;
12     this.dst=dst;
13     this.this_temp=this_temp;
14     this.args=args;
15   }
16   public void rewriteUse(TempMap t) {
17     for(int i=0;i<args.length;i++)
18       args[i]=t.tempMap(args[i]);
19     this_temp=t.tempMap(this_temp);
20   }
21   public void rewriteDef(TempMap t) {
22     dst=t.tempMap(dst);
23   }
24   public FlatNode clone(TempMap t) {
25     TempDescriptor ndst=t.tempMap(dst);
26     TempDescriptor nthis=t.tempMap(this_temp);
27     TempDescriptor[] nargs=new TempDescriptor[args.length];
28     for(int i=0;i<nargs.length;i++)
29       nargs[i]=t.tempMap(args[i]);
30     
31     return new FlatCall(method, ndst, nthis, nargs);
32   }
33
34   public MethodDescriptor getMethod() {
35     return method;
36   }
37
38   public TempDescriptor getThis() {
39     return this_temp;
40   }
41
42   public TempDescriptor getReturnTemp() {
43     return dst;
44   }
45
46   public int numArgs() {
47     return args.length;
48   }
49
50   public TempDescriptor getArg(int i) {
51     return args[i];
52   }
53
54   public TempDescriptor getArgMatchingParamIndex(FlatMethod fm, int i) {
55     // in non-static methods the "this" pointer
56     // affects the matching index
57     if( method.isStatic() ) {
58       assert numArgs()   == fm.numParameters();
59     } else {
60       assert numArgs()+1 == fm.numParameters();
61     }
62
63     if( method.isStatic() ) {
64       return args[i];
65     }
66
67     if( i == 0 ) {
68       return this_temp;
69     }
70     
71     return args[i-1];
72   }
73
74   // return the temp for the argument in caller that
75   // becomes the given parameter
76   public TempDescriptor getArgMatchingParam(FlatMethod fm, 
77                                             TempDescriptor tdParam) {
78     // in non-static methods the "this" pointer
79     // affects the matching index
80     if( method.isStatic() ) {
81       assert numArgs()   == fm.numParameters();
82     } else {
83       assert numArgs()+1 == fm.numParameters();
84     }
85
86     for( int i = 0; i < fm.numParameters(); ++i ) {
87       TempDescriptor tdParamI = fm.getParameter( i );
88       
89       if( tdParamI.equals( tdParam ) ) {
90
91         if( method.isStatic() ) {
92           return args[i];
93         }
94         
95         if( i == 0 ) {
96           return this_temp;
97         }
98         
99         return args[i-1];
100       }
101     }
102
103     return null;
104   }
105
106   public String toString() {
107     String st="FlatCall_";
108     if (dst==null) {
109       if (method==null)
110         st+="null(";
111       else
112         st+=method.getSymbol()+"(";
113     } else
114       st+=dst+"="+method.getSymbol()+"(";
115     if (this_temp!=null) {
116       st+=this_temp;
117       if (args.length!=0)
118         st+=", ";
119     }
120
121     for(int i=0; i<args.length; i++) {
122       st+=args[i].toString();
123       if ((i+1)<args.length)
124         st+=", ";
125     }
126     return st+")";
127   }
128
129   public int kind() {
130     return FKind.FlatCall;
131   }
132
133   public TempDescriptor [] readsTemps() {
134     int size=args.length;
135     if (this_temp!=null)
136       size++;
137     TempDescriptor [] t=new TempDescriptor[size];
138     int offset=0;
139     if (this_temp!=null)
140       t[offset++]=this_temp;
141     for(int i=0; i<args.length; i++)
142       t[offset++]=args[i];
143     return t;
144   }
145
146   public TempDescriptor [] writesTemps() {
147     if (dst!=null)
148       return new TempDescriptor[] {dst};
149     else
150       return new TempDescriptor[0];
151   }
152 }