forked from xiemaisi/acg.js
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpessimistic.js
More file actions
67 lines (60 loc) · 2.68 KB
/
pessimistic.js
File metadata and controls
67 lines (60 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
* Copyright (c) 2013 Max Schaefer.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Max Schaefer - initial API and implementation
*******************************************************************************/
/* Pessimistic call graph builder. */
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(function (require, exports) {
var graph = require('./graph'),
natives = require('./natives'),
flowgraph = require('./flowgraph'),
callgraph = require('./callgraph');
function addOneShotEdges(ast, fg) {
// set up flow for one-shot calls
ast.attr.functions.forEach(function (fn) {
var parent = fn.attr.parent,
childProp = fn.attr.childProp;
if (childProp === 'callee' && parent &&
(parent.type === 'CallExpression' || parent.type === 'NewExpression')) {
// one-shot closure
parent.attr.oneshot = true;
for (var i = 0, nargs = parent.arguments.length; i < nargs; ++i) {
if (i >= fn.params.length)
break;
fg.addEdge(flowgraph.argVertex(parent, i + 1), flowgraph.parmVertex(fn, i + 1));
}
fg.addEdge(flowgraph.retVertex(fn), flowgraph.resVertex(parent));
} else {
// not a one-shot closure
for (var i = 0, nparms = fn.params.length; i <= nparms; ++i)
fg.addEdge(flowgraph.unknownVertex(), flowgraph.parmVertex(fn, i));
fg.addEdge(flowgraph.retVertex(fn), flowgraph.unknownVertex());
}
});
// set up flow for all other calls
ast.attr.calls.forEach(function (call) {
if (!call.attr.oneshot)
for (var i = 0, nargs = call.arguments.length; i <= nargs; ++i)
fg.addEdge(flowgraph.argVertex(call, i), flowgraph.unknownVertex());
fg.addEdge(flowgraph.unknownVertex(), flowgraph.resVertex(call));
});
}
function buildCallGraph(ast, noOneShot) {
var fg = new graph.Graph();
natives.addNativeFlowEdges(fg);
if (!noOneShot)
addOneShotEdges(ast, fg);
flowgraph.addIntraproceduralFlowGraphEdges(ast, fg);
return callgraph.extractCG(ast, fg);
}
exports.buildCallGraph = buildCallGraph;
return exports;
});