-
Notifications
You must be signed in to change notification settings - Fork 109
Initial version #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8e438d
Initial version
gramalingam 13753ce
Patch for msdomain
gramalingam b5c7ee8
switch to uniform casing convention
gramalingam f165402
cleanup
gramalingam d96818d
Python formatting
gramalingam d2b8020
Python formatting
gramalingam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import ast | ||
|
|
||
| from numpy import isin | ||
|
|
||
| def used_vars(expr): | ||
| ''' Return set of all variables used with an expression.''' | ||
| if (isinstance(expr, ast.Name)): | ||
| return set([expr.id]) | ||
| result = set() | ||
| for c in ast.iter_child_nodes(expr): | ||
| result = result | used_vars(c) | ||
| return result | ||
|
|
||
| def local_defs(lhs): | ||
| '''Utility function to return set of assigned/defined variables in the lhs of an assignment statement.''' | ||
| def get_id(e): | ||
| assert isinstance(e, ast.Name), "Only simple assignments supported." | ||
| return e.id | ||
| if (isinstance(lhs, ast.Tuple)): | ||
| return set([get_id(x) for x in lhs.elts]) | ||
| else: | ||
| return set([get_id(lhs)]) | ||
|
|
||
| def defs(stmt): | ||
| ''' | ||
| Return the set of all variables that may be defined (assigned to) in an | ||
| execution of input stmt. | ||
| ''' | ||
| def block_defs(block): | ||
| result = set() | ||
| for s in block: | ||
| result = result | defs(s) | ||
| return result | ||
| if (isinstance(stmt, ast.Assign)): | ||
| return local_defs(stmt.targets[0]) | ||
| elif (isinstance(stmt, ast.Return)): | ||
| return set() | ||
| elif (isinstance(stmt, ast.If)): | ||
| return block_defs(stmt.body) | block_defs(stmt.orelse) | ||
| elif isinstance(stmt, list): | ||
| return block_defs(stmt) | ||
| else: | ||
| raise ValueError("Unsupported statement type: " + type(stmt).__name__) | ||
|
|
||
|
|
||
| def do_liveness_analysis(fun): | ||
| ''' | ||
| Perform liveness analysis of the given function-ast. The results of the | ||
| analysis are stored directly with each statement-ast `s` as attributes `s.live_in` | ||
| and `s.live_out`. | ||
| ''' | ||
| def visit (stmt, live_out): | ||
| def visitBlock(block, live_out): | ||
| for s in reversed(block): | ||
| live_out = visit(s, live_out) | ||
| return live_out | ||
| if (isinstance(stmt, ast.Assign)): | ||
| return (live_out.difference(local_defs(stmt.targets[0]))) | used_vars(stmt.value) | ||
| elif (isinstance(stmt, ast.Return)): | ||
| return used_vars(stmt.value) | ||
| elif (isinstance(stmt, ast.If)): | ||
| live1 = visitBlock(stmt.body, live_out) | ||
| live2 = visitBlock(stmt.orelse, live_out) | ||
| return (live1 | live2) | used_vars(stmt.test) | ||
| elif isinstance(stmt, ast.For): | ||
| return live_out # TODO | ||
| else: | ||
| raise ValueError("Unsupported statement type: " + type(stmt).__name__) | ||
| assert type(fun) == ast.FunctionDef | ||
| live = set() | ||
| for s in reversed(fun.body): | ||
| s.live_out = live | ||
| live = visit(s, live) | ||
| s.live_in = live | ||
| # print(ast.dump(s)) | ||
| # print("Live-In = ", live) | ||
|
|
||
| def exposed_uses(stmts): | ||
| ''' | ||
| Return the set of variables that are used before being defined by given block. | ||
| ''' | ||
| def visitBlock(block, live_out): | ||
| for stmt in reversed(block): | ||
| live_out = visit(stmt, live_out) | ||
| return live_out | ||
|
|
||
| def visit (stmt, live_out): | ||
| if (isinstance(stmt, ast.Assign)): | ||
| return (live_out.difference(local_defs(stmt.targets[0]))) | used_vars(stmt.value) | ||
| elif (isinstance(stmt, ast.Return)): | ||
| return used_vars(stmt.value) | ||
| elif (isinstance(stmt, ast.If)): | ||
| live1 = visitBlock(stmt.body, live_out) | ||
| live2 = visitBlock(stmt.orelse, live_out) | ||
| return (live1 | live2) | used_vars(stmt.test) | ||
| else: | ||
| raise ValueError("Unsupported statement type: " + type(stmt).__name__) | ||
|
|
||
| return visitBlock(stmts, set()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import sys | ||
| from converter import convert | ||
|
|
||
| if __name__ == '__main__': | ||
| for i in range(1, len(sys.argv)): | ||
| convert(sys.argv[i]) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if we should put these file in a subfolder. functions would go in another.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I think in terms of code cleanup, there is a bunch of stuff to be done. In this PR, I am just focusing on the high-level directory structure, to get the initial version in.