1 package org.simantics.scl.compiler.internal.codegen.ssa.exits;
\r
3 import java.util.ArrayList;
\r
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
\r
6 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
\r
7 import org.simantics.scl.compiler.internal.codegen.continuations.ContRef;
\r
8 import org.simantics.scl.compiler.internal.codegen.references.Val;
\r
9 import org.simantics.scl.compiler.internal.codegen.references.ValRef;
\r
10 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
\r
11 import org.simantics.scl.compiler.internal.codegen.ssa.SSAExit;
\r
12 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
\r
13 import org.simantics.scl.compiler.internal.codegen.ssa.binders.ValRefBinder;
\r
14 import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;
\r
15 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
\r
16 import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
\r
17 import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
\r
18 import org.simantics.scl.compiler.types.TVar;
\r
19 import org.simantics.scl.compiler.types.Type;
\r
21 public class Jump extends SSAExit implements ValRefBinder {
\r
22 private ContRef target;
\r
23 private ValRef[] parameters;
\r
25 public Jump(ContRef target, ValRef ... parameters) {
\r
27 setParameters(parameters);
\r
30 public ContRef getTarget() {
\r
34 public void setTarget(ContRef target) {
\r
35 this.target = target;
\r
36 target.setParent(this);
\r
39 public ValRef[] getParameters() {
\r
43 public void setParameters(ValRef[] parameters) {
\r
44 this.parameters = parameters;
\r
45 for(ValRef parameter : parameters)
\r
46 parameter.setParent(this);
\r
50 public void generateCode(MethodBuilder mb) {
\r
51 mb.jump(target, ValRef.getBindings(parameters));
\r
55 public void toString(PrintingContext context) {
\r
56 context.append(target);
\r
57 for(ValRef parameter : parameters) {
\r
58 context.append(' ');
\r
59 context.append(parameter);
\r
61 context.append('\n');
\r
62 for(SSABlock block : getSuccessors())
\r
63 context.addBlock(block);
\r
68 public void validate(SSAValidationContext context) {
\r
69 context.validate(target);
\r
70 if(target.getParent() != this)
\r
71 throw new InternalCompilerError();
\r
72 for(ValRef parameter : parameters) {
\r
73 context.validate(parameter);
\r
74 if(parameter.getParent() != this)
\r
75 throw new InternalCompilerError();
\r
78 Cont cont = target.getBinding();
\r
79 context.assertEquals(cont.getArity(), parameters.length);
\r
80 //for(int i=0;i<parameters.length;++i)
\r
81 // context.assertSubsumes(parameters[i].getType(), cont.getParameterType(i));
\r
85 public void destroy() {
\r
87 for(ValRef parameter : parameters)
\r
92 public SSAExit copy(CopyContext context) {
\r
93 return new Jump(context.copy(target), context.copy(parameters));
\r
97 public void replace(TVar[] vars, Type[] replacements) {
\r
98 for(ValRef parameter : parameters)
\r
99 parameter.replace(vars, replacements);
\r
103 public void collectFreeVariables(SSAFunction function,
\r
104 ArrayList<ValRef> vars) {
\r
105 for(ValRef parameter : parameters)
\r
106 parameter.collectFreeVariables(function, vars);
\r
110 public Cont addParametersInFrontOf(ContRef contRef, Val[] newParameters, Val[] oldParameters, Cont proxy) {
\r
111 ValRef[] occurences = ValRef.createOccurrences(newParameters);
\r
112 for(ValRef ref : occurences)
\r
113 ref.setParent(this);
\r
114 this.parameters = ValRef.concat(occurences, this.parameters);
\r
118 public void setParameter(int position, ValRef parameter) {
\r
119 parameters[position] = parameter;
\r
120 parameter.setParent(this);
\r
123 public ValRef getParameter(int position) {
\r
124 return parameters[position];
\r
128 public boolean isJump(Cont cont, Val parameter) {
\r
129 return target.getBinding() == cont &&
\r
130 parameters.length == 1 &&
\r
131 parameters[0].getBinding() == parameter;
\r
135 public SSABlock[] getSuccessors() {
\r
136 Cont cont = target.getBinding();
\r
137 if(cont instanceof SSABlock)
\r
138 return new SSABlock[] {(SSABlock)cont};
\r
140 return SSABlock.EMPTY_ARRAY;
\r