1 package org.simantics.scl.compiler.internal.elaboration.profiling;
3 import java.util.ArrayList;
5 import org.simantics.scl.compiler.elaboration.expressions.Case;
6 import org.simantics.scl.compiler.elaboration.expressions.EAmbiguous;
7 import org.simantics.scl.compiler.elaboration.expressions.EApply;
8 import org.simantics.scl.compiler.elaboration.expressions.EApplyType;
9 import org.simantics.scl.compiler.elaboration.expressions.EAsPattern;
10 import org.simantics.scl.compiler.elaboration.expressions.EBinary;
11 import org.simantics.scl.compiler.elaboration.expressions.EBind;
12 import org.simantics.scl.compiler.elaboration.expressions.EBlock;
13 import org.simantics.scl.compiler.elaboration.expressions.EConstant;
14 import org.simantics.scl.compiler.elaboration.expressions.ECoveringBranchPoint;
15 import org.simantics.scl.compiler.elaboration.expressions.EEnforce;
16 import org.simantics.scl.compiler.elaboration.expressions.EEntityTypeAnnotation;
17 import org.simantics.scl.compiler.elaboration.expressions.EError;
18 import org.simantics.scl.compiler.elaboration.expressions.EExternalConstant;
19 import org.simantics.scl.compiler.elaboration.expressions.EFieldAccess;
20 import org.simantics.scl.compiler.elaboration.expressions.EGetConstraint;
21 import org.simantics.scl.compiler.elaboration.expressions.EIf;
22 import org.simantics.scl.compiler.elaboration.expressions.EIntegerLiteral;
23 import org.simantics.scl.compiler.elaboration.expressions.ELambda;
24 import org.simantics.scl.compiler.elaboration.expressions.ELambdaType;
25 import org.simantics.scl.compiler.elaboration.expressions.ELet;
26 import org.simantics.scl.compiler.elaboration.expressions.EListComprehension;
27 import org.simantics.scl.compiler.elaboration.expressions.EListLiteral;
28 import org.simantics.scl.compiler.elaboration.expressions.ELiteral;
29 import org.simantics.scl.compiler.elaboration.expressions.EMatch;
30 import org.simantics.scl.compiler.elaboration.expressions.EPlaceholder;
31 import org.simantics.scl.compiler.elaboration.expressions.EPreLet;
32 import org.simantics.scl.compiler.elaboration.expressions.EPreRuleset;
33 import org.simantics.scl.compiler.elaboration.expressions.ERange;
34 import org.simantics.scl.compiler.elaboration.expressions.ERealLiteral;
35 import org.simantics.scl.compiler.elaboration.expressions.ERecord;
36 import org.simantics.scl.compiler.elaboration.expressions.ERuleset;
37 import org.simantics.scl.compiler.elaboration.expressions.ESelect;
38 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLambda;
39 import org.simantics.scl.compiler.elaboration.expressions.ESimpleLet;
40 import org.simantics.scl.compiler.elaboration.expressions.EStringLiteral;
41 import org.simantics.scl.compiler.elaboration.expressions.ETransformation;
42 import org.simantics.scl.compiler.elaboration.expressions.ETypeAnnotation;
43 import org.simantics.scl.compiler.elaboration.expressions.EVar;
44 import org.simantics.scl.compiler.elaboration.expressions.EVariable;
45 import org.simantics.scl.compiler.elaboration.expressions.EWhen;
46 import org.simantics.scl.compiler.elaboration.expressions.Expression;
47 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpression;
48 import org.simantics.scl.compiler.elaboration.expressions.GuardedExpressionGroup;
49 import org.simantics.scl.compiler.elaboration.expressions.StandardExpressionTransformer;
50 import org.simantics.scl.compiler.elaboration.expressions.block.BindStatement;
51 import org.simantics.scl.compiler.elaboration.expressions.block.GuardStatement;
52 import org.simantics.scl.compiler.elaboration.expressions.block.LetStatement;
53 import org.simantics.scl.compiler.elaboration.expressions.block.RuleStatement;
54 import org.simantics.scl.compiler.elaboration.expressions.list.ListAssignment;
55 import org.simantics.scl.compiler.elaboration.expressions.list.ListGenerator;
56 import org.simantics.scl.compiler.elaboration.expressions.list.ListGuard;
57 import org.simantics.scl.compiler.elaboration.expressions.list.ListQualifier;
58 import org.simantics.scl.compiler.elaboration.expressions.list.ListSeq;
59 import org.simantics.scl.compiler.elaboration.expressions.list.ListThen;
60 import org.simantics.scl.compiler.elaboration.query.QAlternative;
61 import org.simantics.scl.compiler.elaboration.query.QAtom;
62 import org.simantics.scl.compiler.elaboration.query.QConjunction;
63 import org.simantics.scl.compiler.elaboration.query.QDisjunction;
64 import org.simantics.scl.compiler.elaboration.query.QExists;
65 import org.simantics.scl.compiler.elaboration.query.QIf;
66 import org.simantics.scl.compiler.elaboration.query.QMapping;
67 import org.simantics.scl.compiler.elaboration.query.QNegation;
68 import org.simantics.scl.compiler.elaboration.query.Query;
69 import org.simantics.scl.compiler.elaboration.query.pre.QPreBinds;
70 import org.simantics.scl.compiler.elaboration.query.pre.QPreEquals;
71 import org.simantics.scl.compiler.elaboration.query.pre.QPreExists;
72 import org.simantics.scl.compiler.elaboration.query.pre.QPreGuard;
73 import org.simantics.scl.runtime.profiling.BranchPoint;
75 public class BranchPointInjector extends StandardExpressionTransformer {
76 public ArrayList<BranchPoint> currentBranchPoints = new ArrayList<BranchPoint>();
79 public Expression injectBranchPoint(Expression expression) {
80 ArrayList<BranchPoint> oldBranchPoints = currentBranchPoints;
81 currentBranchPoints = new ArrayList<BranchPoint>();
82 int beginCodeCounter = codeCounter;
83 expression = expression.accept(this);
84 BranchPoint branchPoint = new BranchPoint(expression.location,
85 codeCounter - beginCodeCounter,
86 currentBranchPoints.isEmpty() ? BranchPoint.EMPTY_ARRAY
87 : currentBranchPoints.toArray(new BranchPoint[currentBranchPoints.size()]));
88 oldBranchPoints.add(branchPoint);
89 currentBranchPoints = oldBranchPoints;
90 return new ECoveringBranchPoint(expression, branchPoint);
94 public Expression transform(ESimpleLambda expression) {
96 expression.value = injectBranchPoint(expression.value);
101 public Expression transform(ELambda expression) {
103 for(Case case_ : expression.cases)
104 case_.value = injectBranchPoint(case_.value);
109 public Expression transform(EMatch expression) {
111 for(int i=0;i<expression.scrutinee.length;++i)
112 expression.scrutinee[i] = expression.scrutinee[i].accept(this);
113 for(Case case_ : expression.cases)
114 case_.value = injectBranchPoint(case_.value);
119 public Expression transform(EIf expression) {
121 expression.condition = expression.condition.accept(this);
122 expression.then_ = injectBranchPoint(expression.then_);
123 expression.else_ = injectBranchPoint(expression.else_);
128 public Expression transform(EBind expression) {
130 expression.pattern = expression.pattern.accept(this);
131 expression.value = expression.value.accept(this);
132 expression.in = injectBranchPoint(expression.in);
137 public Expression transform(EListComprehension expression) {
139 expression.head = injectBranchPoint(expression.head);
140 expression.qualifier = expression.qualifier.accept(this);
145 public Expression transform(EWhen expression) {
147 expression.query = expression.query.accept(this);
148 expression.action = injectBranchPoint(expression.action);
153 public Expression transform(GuardedExpressionGroup expression) {
155 for(GuardedExpression ge : expression.expressions) {
156 for(int i=0;i<ge.guards.length;++i)
157 ge.guards[i] = ge.guards[i].accept(this);
158 ge.value = injectBranchPoint(ge.value);
164 public Expression transform(EAmbiguous expression) {
166 return super.transform(expression);
170 public Expression transform(EApply expression) {
172 return super.transform(expression);
176 public Expression transform(EApplyType expression) {
178 return super.transform(expression);
182 public Expression transform(EAsPattern expression) {
184 return super.transform(expression);
188 public Expression transform(EBinary expression) {
190 return super.transform(expression);
194 public Expression transform(EBlock expression) {
196 return super.transform(expression);
200 public Expression transform(EConstant expression) {
202 return super.transform(expression);
206 public Expression transform(ECoveringBranchPoint expression) {
208 return super.transform(expression);
212 public Expression transform(EEnforce expression) {
214 return super.transform(expression);
218 public Expression transform(EEntityTypeAnnotation expression) {
220 return super.transform(expression);
224 public Expression transform(EError expression) {
226 return super.transform(expression);
230 public Expression transform(EExternalConstant expression) {
232 return super.transform(expression);
236 public Expression transform(EFieldAccess expression) {
238 return super.transform(expression);
242 public Expression transform(EGetConstraint expression) {
244 return super.transform(expression);
248 public Expression transform(EIntegerLiteral expression) {
250 return super.transform(expression);
254 public Expression transform(ELambdaType expression) {
256 return super.transform(expression);
260 public Expression transform(ELet expression) {
262 return super.transform(expression);
266 public Expression transform(EListLiteral expression) {
268 return super.transform(expression);
272 public Expression transform(ELiteral expression) {
274 return super.transform(expression);
278 public Expression transform(EPlaceholder expression) {
280 return super.transform(expression);
284 public Expression transform(EPreLet expression) {
286 return super.transform(expression);
290 public Expression transform(EPreRuleset expression) {
292 return super.transform(expression);
296 public Expression transform(ERange expression) {
298 return super.transform(expression);
302 public Expression transform(ERealLiteral expression) {
304 return super.transform(expression);
308 public Expression transform(ERecord expression) {
310 return super.transform(expression);
314 public Expression transform(ERuleset expression) {
316 return super.transform(expression);
320 public Expression transform(ESelect expression) {
322 return super.transform(expression);
326 public Expression transform(ESimpleLet expression) {
328 return super.transform(expression);
332 public Expression transform(EStringLiteral expression) {
334 return super.transform(expression);
338 public Expression transform(ETransformation expression) {
340 return super.transform(expression);
344 public Expression transform(ETypeAnnotation expression) {
346 return super.transform(expression);
350 public Expression transform(EVar expression) {
352 return super.transform(expression);
356 public Expression transform(EVariable expression) {
358 return super.transform(expression);
362 public ListQualifier transform(ListAssignment qualifier) {
364 return super.transform(qualifier);
368 public ListQualifier transform(ListGenerator qualifier) {
370 return super.transform(qualifier);
374 public ListQualifier transform(ListGuard qualifier) {
376 return super.transform(qualifier);
380 public ListQualifier transform(ListSeq qualifier) {
382 return super.transform(qualifier);
386 public ListQualifier transform(ListThen qualifier) {
388 return super.transform(qualifier);
392 public Query transform(QAlternative query) {
394 return super.transform(query);
398 public Query transform(QAtom query) {
400 return super.transform(query);
404 public Query transform(QConjunction query) {
406 return super.transform(query);
410 public Query transform(QDisjunction query) {
412 return super.transform(query);
416 public Query transform(QExists query) {
418 return super.transform(query);
422 public Query transform(QIf query) {
424 return super.transform(query);
428 public Query transform(QMapping query) {
430 return super.transform(query);
434 public Query transform(QNegation query) {
436 return super.transform(query);
440 public Query transform(QPreBinds query) {
442 return super.transform(query);
446 public Query transform(QPreEquals query) {
448 return super.transform(query);
452 public Query transform(QPreExists query) {
454 return super.transform(query);
458 public Query transform(QPreGuard query) {
460 return super.transform(query);
463 public BranchPoint[] getAndClearBranchPoints() {
464 BranchPoint[] result = currentBranchPoints.toArray(new BranchPoint[currentBranchPoints.size()]);
465 currentBranchPoints.clear();
470 public void visit(BindStatement statement) {
472 super.visit(statement);
476 public void visit(GuardStatement statement) {
478 super.visit(statement);
482 public void visit(LetStatement statement) {
484 if(statement.pattern.isFunctionDefinitionLhs())
485 statement.value = injectBranchPoint(statement.value);
487 super.visit(statement);
491 public void visit(RuleStatement statement) {
493 super.visit(statement);