1 package org.simantics.scl.compiler.internal.codegen.ssa.exits;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;
6 import org.simantics.scl.compiler.internal.codegen.continuations.Cont;
7 import org.simantics.scl.compiler.internal.codegen.continuations.ContRef;
8 import org.simantics.scl.compiler.internal.codegen.references.Val;
9 import org.simantics.scl.compiler.internal.codegen.references.ValRef;
10 import org.simantics.scl.compiler.internal.codegen.ssa.SSABlock;
11 import org.simantics.scl.compiler.internal.codegen.ssa.SSAExit;
12 import org.simantics.scl.compiler.internal.codegen.ssa.SSAFunction;
13 import org.simantics.scl.compiler.internal.codegen.ssa.binders.ValRefBinder;
14 import org.simantics.scl.compiler.internal.codegen.utils.CopyContext;
15 import org.simantics.scl.compiler.internal.codegen.utils.MethodBuilder;
16 import org.simantics.scl.compiler.internal.codegen.utils.PrintingContext;
17 import org.simantics.scl.compiler.internal.codegen.utils.SSAValidationContext;
18 import org.simantics.scl.compiler.internal.codegen.utils.ValRefVisitor;
19 import org.simantics.scl.compiler.types.TVar;
20 import org.simantics.scl.compiler.types.Type;
22 public class Jump extends SSAExit implements ValRefBinder {
23 private ContRef target;
24 private ValRef[] parameters;
26 public Jump(ContRef target, ValRef ... parameters) {
28 setParameters(parameters);
31 public ContRef getTarget() {
35 public void setTarget(ContRef target) {
37 target.setParent(this);
40 public ValRef[] getParameters() {
44 public void setParameters(ValRef[] parameters) {
45 this.parameters = parameters;
46 for(ValRef parameter : parameters)
47 parameter.setParent(this);
51 public void generateCode(MethodBuilder mb) {
52 mb.jump(target, ValRef.getBindings(parameters));
56 public void toString(PrintingContext context) {
57 context.append(target);
58 for(ValRef parameter : parameters) {
60 context.append(parameter);
63 for(SSABlock block : getSuccessors())
64 context.addBlock(block);
69 public void validate(SSAValidationContext context) {
70 context.validate(target);
71 if(target.getParent() != this)
72 throw new InternalCompilerError();
73 for(ValRef parameter : parameters) {
74 context.validate(parameter);
75 if(parameter.getParent() != this)
76 throw new InternalCompilerError();
79 Cont cont = target.getBinding();
80 context.assertEquals(cont.getArity(), parameters.length);
81 //for(int i=0;i<parameters.length;++i)
82 // context.assertSubsumes(parameters[i].getType(), cont.getParameterType(i));
86 public void destroy() {
88 for(ValRef parameter : parameters)
93 public SSAExit copy(CopyContext context) {
94 return new Jump(context.copy(target), context.copy(parameters));
98 public void replace(TVar[] vars, Type[] replacements) {
99 for(ValRef parameter : parameters)
100 parameter.replace(vars, replacements);
104 public void collectFreeVariables(SSAFunction function,
105 ArrayList<ValRef> vars) {
106 for(ValRef parameter : parameters)
107 parameter.collectFreeVariables(function, vars);
111 public Cont addParametersInFrontOf(ContRef contRef, Val[] newParameters, Val[] oldParameters, Cont proxy) {
112 ValRef[] occurences = ValRef.createOccurrences(newParameters);
113 for(ValRef ref : occurences)
115 this.parameters = ValRef.concat(occurences, this.parameters);
119 public void setParameter(int position, ValRef parameter) {
120 parameters[position] = parameter;
121 parameter.setParent(this);
124 public ValRef getParameter(int position) {
125 return parameters[position];
129 public boolean isJump(Cont cont, Val parameter) {
130 return target.getBinding() == cont &&
131 parameters.length == 1 &&
132 parameters[0].getBinding() == parameter;
136 public SSABlock[] getSuccessors() {
137 Cont cont = target.getBinding();
138 if(cont instanceof SSABlock)
139 return new SSABlock[] {(SSABlock)cont};
141 return SSABlock.EMPTY_ARRAY;
145 public void forValRefs(ValRefVisitor visitor) {
146 for(ValRef parameter : parameters)
147 visitor.visit(parameter);