1 package org.simantics.scl.compiler.internal.codegen.references;
3 import java.util.ArrayList;
6 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
7 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
8 import org.simantics.scl.compiler.internal.codegen.ssa.binders.ValRefBinder;
9 import org.simantics.scl.compiler.internal.codegen.ssa.statements.LetApply;
10 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
11 import org.simantics.scl.compiler.internal.codegen.utils.SSASimplificationContext;
12 import org.simantics.scl.compiler.internal.codegen.utils.TransientClassBuilder;
13 import org.simantics.scl.compiler.top.SCLCompilerConfiguration;
14 import org.simantics.scl.compiler.types.TVar;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.Types;
18 public final class ValRef implements IVal {
19 public static final ValRef[] EMPTY_ARRAY = new ValRef[0];
22 ValRef prev; // FreeVars with the same binding form a linked list
28 * Just for deserialization
30 public ValRef(Type[] parameters) {
31 this.parameters = parameters;
34 ValRef(Val binding, Type[] parameters) {
35 //System.out.println("+ " + bindin g);
36 this.parameters = parameters;
40 public void setBinding(Val binding) {
41 this.binding = binding;
43 ValRef head = binding.occurrence;
44 binding.occurrence = this;
51 int removeModiId = -1;
52 public void remove() {
53 //System.out.println("- " + binding);
56 binding.occurrence = next;
57 } catch(NullPointerException e) {
58 System.err.println("removeModiId = " + removeModiId);
59 System.err.println("current ModiId = " + SSASimplificationContext.modiId);
60 throw new InternalCompilerError("The ValRef has already been removed.");
66 if(SCLCompilerConfiguration.DEBUG) {
71 removeModiId = SSASimplificationContext.modiId;
74 public Val getBinding() {
78 public ValRef getNext() {
82 public static Val[] getBindings(ValRef[] refs) {
83 Val[] result = new Val[refs.length];
84 for(int i=0;i<refs.length;++i)
85 result[i] = refs[i].getBinding();
90 public ValRef createOccurrence() {
91 return binding.createOccurrence(parameters);
95 public ValRef createOccurrence(Type... otherParameters) {
96 return binding.createOccurrence(Types.concat(parameters, otherParameters));
100 public IVal createSpecialization(Type... otherParameters) {
101 return new ValSpecialization(binding, Types.concat(parameters, otherParameters));
104 public void setParent(ValRefBinder parent) {
105 this.parent = parent;
109 public Type getType() {
110 if(parameters.length == 0)
111 return binding.getType();
113 return Types.instantiate(binding.getType(), parameters);
116 public static ValRef[] createOccurrences(IVal[] vals) {
117 ValRef[] result = new ValRef[vals.length];
118 for(int i=0;i<vals.length;++i)
119 result[i] = vals[i].createOccurrence();
123 public static <T extends IVal> ValRef[] createOccurrences(List<T> vals) {
124 ValRef[] result = new ValRef[vals.size()];
125 for(int i=0;i<vals.size();++i)
126 result[i] = vals.get(i).createOccurrence();
130 public Type[] getTypeParameters() {
134 public Type getTypeParameter(int i) {
135 return parameters[i];
138 public static ValRef[] concat(ValRef[] refs1, ValRef[] refs2) {
139 ValRef[] result = new ValRef[refs1.length + refs2.length];
140 for(int i=0;i<refs1.length;++i)
141 result[i] = refs1[i];
142 for(int i=0;i<refs2.length;++i)
143 result[refs1.length + i] = refs2[i];
148 public void push(MethodBuilder mb) {
152 public ValRefBinder getParent() {
156 public SSAFunction getParentFunction() {
157 return parent.getParentFunction();
160 public void replace(TVar[] vars, Type[] replacements) {
161 for(int i=0;i<parameters.length;++i) {
162 Type oldType = parameters[i];
163 Type newType = parameters[i].replace(vars, replacements);
164 if(oldType != newType) {
165 Type[] newParameters = new Type[parameters.length];
167 newParameters[j] = parameters[j];
168 newParameters[i] = newType;
169 for(int j=i+1;j<parameters.length;++j)
170 newParameters[j] = parameters[j].replace(vars, replacements);
171 this.parameters = newParameters;
178 public void setTypeParameters(Type[] parameters) {
179 this.parameters = parameters;
182 public static ValRef[] copy(ValRef[] refs) {
183 ValRef[] result = new ValRef[refs.length];
184 for(int i=0;i<refs.length;++i)
185 result[i] = refs[i].copy();
189 public ValRef copy() {
190 return binding.createOccurrence(parameters);
193 public void collectFreeVariables(SSAFunction function,
194 ArrayList<ValRef> vars) {
195 if(binding instanceof BoundVar) {
196 BoundVar var = (BoundVar)binding;
197 if(var.getFunctionParent() != function)
202 public void replaceBy(Val binding) {
208 public Type apply(MethodBuilder mb, Type[] typeParameters,
210 return binding.apply(mb, Types.concat(this.parameters, typeParameters), parameters);
214 * Replaces this reference with an application
216 public void replaceByApply(Val function, Val ... parameters) {
217 getParent().replaceByApply(this, function, this.parameters, parameters);
221 public Object realizeValue(TransientClassBuilder classLoader) {
222 return binding.realizeValue(classLoader);
226 public void setLabel(String label) {
229 public void updateParentEffect() {
230 if(parent instanceof LetApply) {
231 LetApply apply = (LetApply)parent;
232 if(apply.getFunction() == this)
233 apply.updateEffect();