Skip to content

Commit

Permalink
forinof callback fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Haiyang-Sun committed Sep 26, 2019
1 parent 1ba2b97 commit eaf02ef
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 10 deletions.
15 changes: 15 additions & 0 deletions src/ch.usi.inf.nodeprof/js/analysis/forinof/analysis.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@
this.forObject = function (iid, isForIn) {
console.log('forObject@', J$.iidToLocation(iid), isForIn, lastExprResult);
}
this.forObjectPost = function (iid, isForIn) {
console.log('forObjectPost@', J$.iidToLocation(iid), isForIn);
}
this.cfBlockEnter = function(iid) {
console.log('iterationEnter@', J$.iidToLocation(iid));
}
this.cfBlockExit = function(iid) {
console.log('iterationExit@', J$.iidToLocation(iid));
}
this.read = function(iid, name, value) {
console.log('read@', J$.iidToLocation(iid), name);
}
this.write = function(iid, name, value) {
console.log('write@', J$.iidToLocation(iid), name);
}
this.endExpression = function (iid, type, result) {
lastExprResult = result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:1:66:4:2) obj
read@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:17:6:20) obj
forObject@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2) true { x: 'valueX', y: 'valueY' }
iterationEnter@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2)
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2) key
iterationExit@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2)
iterationEnter@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2)
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2) key
iterationExit@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2)
forObjectPost@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:6:1:7:2) true
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:9:5:9:21) arr
read@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:17:11:20) arr
forObject@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2) false [ 41, 42, 43 ]
iterationEnter@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2) val
iterationExit@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
iterationEnter@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2) val
iterationExit@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
iterationEnter@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
write@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2) val
iterationExit@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2)
forObjectPost@ (src/ch.usi.inf.nodeprof.test/js/minitests/forinof.js:11:1:12:2) false
11 changes: 11 additions & 0 deletions src/ch.usi.inf.nodeprof/js/analysis/trivial/emptyTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
**/
this.forObject = function (iid, isForIn) {
}
this.forObjectPost = function (iid, isForIn) {
}

/**
* This callback is called before a value is returned from a function using the <tt>return</tt> keyword.
*
* @param {number} iid - Static unique instruction identifier of this callback
* @param {*} val - Value to be returned
*/
this._return = function (iid, val) {
};

/**
* This callback is called before a value is returned from a function using the <tt>return</tt> keyword.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
public enum ProfiledTagEnum {
UNARY(JSTags.UnaryOperationTag.class, -1), // have multiple case
BINARY(JSTags.BinaryOperationTag.class, 2),
CF_BRANCH(JSTags.ControlFlowBranchTag.class, -1), // to be checked
CF_BLOCK(JSTags.ControlFlowBlockTag.class, -1), // to be checked
CF_ROOT(JSTags.ControlFlowRootTag.class, 0), // to be checked
CF_BRANCH(JSTags.ControlFlowBranchTag.class, -1), // have multiple case
CF_BLOCK(JSTags.ControlFlowBlockTag.class, 0),
CF_ROOT(JSTags.ControlFlowRootTag.class, 0),
EVAL(JSTags.EvalCallTag.class, 2),
DECLARE(JSTags.DeclareTag.class, 0),
VAR_READ(JSTags.ReadVariableTag.class, 0),
Expand All @@ -42,7 +42,7 @@ public enum ProfiledTagEnum {
BUILTIN(JSTags.BuiltinRootTag.class, 0),
LITERAL(JSTags.LiteralTag.class, 0),
STATEMENT(StandardTags.StatementTag.class, 0),
NEW(JSTags.ObjectAllocationTag.class, -1),
NEW(JSTags.ObjectAllocationTag.class, -1), // any number of inputs for arguments
EXPRESSION(StandardTags.ExpressionTag.class, 0);

// the corresponding JSTags class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright 2018 Dynamic Analysis Group, Università della Svizzera Italiana (USI)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ch.usi.inf.nodeprof.handlers;

import com.oracle.truffle.api.instrumentation.EventContext;

import ch.usi.inf.nodeprof.ProfiledTagEnum;

public abstract class CFBlockEventHandler extends BaseSingleTagEventHandler {
public CFBlockEventHandler(EventContext context) {
super(context, ProfiledTagEnum.CF_BLOCK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import ch.usi.inf.nodeprof.jalangi.factory.BinaryFactory;
import ch.usi.inf.nodeprof.jalangi.factory.ReturnFactory;
import ch.usi.inf.nodeprof.jalangi.factory.BuiltinFactory;
import ch.usi.inf.nodeprof.jalangi.factory.CFBlockFactory;
import ch.usi.inf.nodeprof.jalangi.factory.ConditionalFactory;
import ch.usi.inf.nodeprof.jalangi.factory.DeclareFactory;
import ch.usi.inf.nodeprof.jalangi.factory.EvalFactory;
Expand Down Expand Up @@ -121,11 +122,18 @@ public class JalangiAnalysis {
put("evalFunctionPost", EnumSet.of(BUILTIN));

put("forObject", EnumSet.of(CF_ROOT));
put("forObjectPost", EnumSet.of(CF_ROOT));

put("cfBlockEnter", EnumSet.of(CF_BLOCK));
put("cfBlockExit", EnumSet.of(CF_BLOCK));

put("_return", EnumSet.of(CF_BRANCH));

put("_return", EnumSet.of(CF_BRANCH));

put("startExpression", EnumSet.of(EXPRESSION));
put("endExpression", EnumSet.of(EXPRESSION));

}
});

Expand Down Expand Up @@ -271,15 +279,13 @@ public void onReady() {
new EvalFunctionFactory(this.jsAnalysis, callbacks.get("evalFunctionPre"), callbacks.get("evalFunctionPost")));
}

if (this.callbacks.containsKey("forObject")) {
if (this.callbacks.containsKey("forObject") || this.callbacks.containsKey("forObjectPost")) {
this.instrument.onCallback(
ProfiledTagEnum.CF_ROOT,
new ForObjectFactory(this.jsAnalysis, callbacks.get("forObject")));
new ForObjectFactory(this.jsAnalysis, callbacks.get("forObject"), callbacks.get("forObjectPost")));
}

/**
* TODO
*
* Loop not tested
*/
if (this.callbacks.containsKey("loopEnter") || this.callbacks.containsKey("loopExit")) {
Expand All @@ -288,6 +294,15 @@ public void onReady() {
new LoopFactory(this.jsAnalysis, callbacks.get("loopEnter"), callbacks.get("loopExit")));
}

/**
* cfBlockEnter, cfBlockExit
*/
if (this.callbacks.containsKey("cfBlockEnter") || this.callbacks.containsKey("cfBlockExit")) {
this.instrument.onCallback(
ProfiledTagEnum.CF_BLOCK,
new CFBlockFactory(this.jsAnalysis, callbacks.get("cfBlockEnter"), callbacks.get("cfBlockExit")));
}

/**
* _return callback
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright 2018 Dynamic Analysis Group, Università della Svizzera Italiana (USI)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package ch.usi.inf.nodeprof.jalangi.factory;

import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.instrumentation.EventContext;
import com.oracle.truffle.api.nodes.DirectCallNode;
import com.oracle.truffle.api.object.DynamicObject;

import ch.usi.inf.nodeprof.handlers.BaseEventHandlerNode;
import ch.usi.inf.nodeprof.handlers.CFBlockEventHandler;

public class CFBlockFactory extends AbstractFactory {
public CFBlockFactory(Object jalangiAnalysis, DynamicObject pre, DynamicObject post) {
super("cfblock", jalangiAnalysis, pre, post, 1, 1);
}

@Override
public BaseEventHandlerNode create(EventContext context) {
return new CFBlockEventHandler(context) {
@Child DirectCallNode preCall = createPreCallNode();
@Child DirectCallNode postCall = createPostCallNode();

@Override
public void executePre(VirtualFrame frame, Object[] inputs) {
if (pre != null) {
setPreArguments(0, getSourceIID());
// TODO
// add information about the type of the block (if, iteration, switch)
directCall(preCall, true, getSourceIID());
}
}

@Override
public void executePost(VirtualFrame frame, Object result,
Object[] inputs) {
if (post != null) {
setPostArguments(0, getSourceIID());
// TODO
// add information about the type of the block (if, iteration, switch)
directCall(postCall, false, getSourceIID());
}
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import ch.usi.inf.nodeprof.handlers.LoopEventHandler;

public class ForObjectFactory extends AbstractFactory {
public ForObjectFactory(Object jalangiAnalysis, DynamicObject pre) {
super("forObject", jalangiAnalysis, pre, null, 2, -1);
public ForObjectFactory(Object jalangiAnalysis, DynamicObject pre, DynamicObject post) {
super("forObject", jalangiAnalysis, pre, post, 2, 2);
}

@Override
public BaseEventHandlerNode create(EventContext context) {
return new LoopEventHandler(context) {
@Child DirectCallNode preCall = createPreCallNode();
@Child DirectCallNode postCall = createPostCallNode();

@Override
public void executePre(VirtualFrame frame,
Expand All @@ -42,6 +43,16 @@ public void executePre(VirtualFrame frame,
directCall(preCall, true, getSourceIID());
}
}

@Override
public void executePost(VirtualFrame frame, Object result, Object[] inputs) {
if (post != null && (isForIn() || isForOf())) {
setPostArguments(0, getSourceIID());
setPostArguments(1, isForIn());
directCall(postCall, true, getSourceIID());
}

}
};
}

Expand Down

0 comments on commit eaf02ef

Please sign in to comment.