ed24815cf05f7e21153708baad436ee001229f2e
[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     return new FlatCall(method, ndst, nthis, nargs);
31   }
32
33   public MethodDescriptor getMethod() {
34     return method;
35   }
36
37   public TempDescriptor getThis() {
38     return this_temp;
39   }
40
41   public TempDescriptor getReturnTemp() {
42     return dst;
43   }
44
45   public int numArgs() {
46     return args.length;
47   }
48
49   public TempDescriptor getArg(int i) {
50     return args[i];
51   }
52
53   public TempDescriptor getArgMatchingParamIndex(FlatMethod fm, int i) {
54     // in non-static methods the "this" pointer
55     // affects the matching index
56     if( method.isStatic() ) {
57       assert numArgs()   == fm.numParameters();
58     } else {
59       assert numArgs()+1 == fm.numParameters();
60     }
61
62     if( method.isStatic() ) {
63       return args[i];
64     }
65
66     if( i == 0 ) {
67       return this_temp;
68     }
69     
70     return args[i-1];
71   }
72
73   // return the temp for the argument in caller that
74   // becomes the given parameter
75   public TempDescriptor getArgMatchingParam(FlatMethod fm, 
76                                             TempDescriptor tdParam) {
77     // in non-static methods the "this" pointer
78     // affects the matching index
79     if( method.isStatic() ) {
80       assert numArgs()   == fm.numParameters();
81     } else {
82       assert numArgs()+1 == fm.numParameters();
83     }
84
85     for( int i = 0; i < fm.numParameters(); ++i ) {
86       TempDescriptor tdParamI = fm.getParameter( i );
87       
88       if( tdParamI.equals( tdParam ) ) {
89
90         if( method.isStatic() ) {
91           return args[i];
92         }
93         
94         if( i == 0 ) {
95           return this_temp;
96         }
97         
98         return args[i-1];
99       }
100     }
101
102     return null;
103   }
104
105   public String toString() {
106     String st="FlatCall_";
107     if (dst==null) {
108       if (method==null)
109         st+="null(";
110       else
111         st+=method.getSymbol()+"(";
112     } else
113       st+=dst+"="+method.getSymbol()+"(";
114     if (this_temp!=null) {
115       st+=this_temp;
116       if (args.length!=0)
117         st+=", ";
118     }
119
120     for(int i=0; i<args.length; i++) {
121       st+=args[i].toString();
122       if ((i+1)<args.length)
123         st+=", ";
124     }
125     return st+")";
126   }
127
128   public int kind() {
129     return FKind.FlatCall;
130   }
131
132   public TempDescriptor [] readsTemps() {
133     int size=args.length;
134     if (this_temp!=null)
135       size++;
136     TempDescriptor [] t=new TempDescriptor[size];
137     int offset=0;
138     if (this_temp!=null)
139       t[offset++]=this_temp;
140     for(int i=0; i<args.length; i++)
141       t[offset++]=args[i];
142     return t;
143   }
144
145   public TempDescriptor [] writesTemps() {
146     if (dst!=null)
147       return new TempDescriptor[] {dst};
148     else
149       return new TempDescriptor[0];
150   }
151 }