-
Notifications
You must be signed in to change notification settings - Fork 7
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
Mnemonic support & Improve Disassembler #21
Open
PhilippvK
wants to merge
9
commits into
coredsl_exceptions
Choose a base branch
from
mnemonic-support
base: coredsl_exceptions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
620e719
disass backend: use disass string
PhilippvK 82afa3b
coredsl2 frontend: remove old args_disass syntax from grammar
PhilippvK 35bf8e9
coredsl2 frontend: update grammar to support specifying mnemonic
PhilippvK 4ecba8c
rename disass to assembly
PhilippvK 45b0235
coredsl2 frontend: generate new parser
PhilippvK 093d480
metamodel, etiss backend, coredsl frontend: add mnemonic support
PhilippvK 40cd8d3
disass backend: use custom asm formatter
PhilippvK f2abf3c
disass backend: ommit repeated 0x0000
PhilippvK ad9034f
Merge remote-tracking branch 'upstream/coredsl_exceptions' into mnemo…
PhilippvK 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
This file contains 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,131 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# This file is part of the M2-ISA-R project: https://github.com/tum-ei-eda/M2-ISA-R | ||
# | ||
# Copyright (C) 2022 | ||
# Chair of Electrical Design Automation | ||
# Technical University of Munich | ||
|
||
"""Fortmatter implementation for CoreDSL assembly strings.""" | ||
|
||
# Inspired by: https://stackoverflow.com/a/9558001 | ||
|
||
from string import Formatter | ||
|
||
import re | ||
import ast | ||
import operator as op | ||
|
||
operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, | ||
ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor, | ||
ast.USub: op.neg} | ||
|
||
NAMES = { | ||
0: "zero", | ||
1: "ra", | ||
2: "sp", | ||
3: "gp", | ||
4: "tp", | ||
5: "t0", | ||
6: "t1", | ||
7: "t2", | ||
8: "s0", | ||
9: "s1", | ||
10: "a0", | ||
11: "a1", | ||
12: "a2", | ||
13: "a3", | ||
14: "a4", | ||
15: "a5", | ||
16: "a6", | ||
17: "a7", | ||
18: "s2", | ||
19: "s3", | ||
20: "s4", | ||
21: "s5", | ||
22: "s6", | ||
23: "s7", | ||
24: "s8", | ||
25: "s9", | ||
26: "s10", | ||
27: "s11", | ||
28: "t3", | ||
29: "t4", | ||
30: "t5", | ||
31: "t6", | ||
} | ||
|
||
FNAMES = { | ||
0: "f0", | ||
1: "f1", | ||
2: "f2", | ||
3: "f3", | ||
4: "f4", | ||
5: "f5", | ||
6: "f6", | ||
7: "f7", | ||
8: "fs0", | ||
9: "fs1", | ||
10: "fa0", | ||
11: "fa1", | ||
12: "fa2", | ||
13: "fa3", | ||
14: "fa4", | ||
15: "fa5", | ||
16: "fa6", | ||
17: "fa7", | ||
18: "fs2", | ||
19: "fs3", | ||
20: "fs4", | ||
21: "fs5", | ||
22: "fs6", | ||
23: "fs7", | ||
24: "fs8", | ||
25: "fs9", | ||
26: "fs10", | ||
27: "fs11", | ||
28: "ft8", | ||
29: "ft9", | ||
30: "ft10", | ||
31: "ft11", | ||
} | ||
|
||
class AsmFormatter(Formatter): | ||
|
||
def get_value(self, key, args, kwargs): | ||
if isinstance(key, int): | ||
return key | ||
assert isinstance(key, str) | ||
try: | ||
value = self.eval_expr(key, args, kwargs) | ||
return value | ||
except ValueError: | ||
pass | ||
|
||
return super().get_value(key, args, kwargs) | ||
|
||
def get_field(self, field_name, args, kwargs): | ||
return super().get_field(field_name, args, kwargs) | ||
|
||
def eval_expr(self, expr, args, kwargs): | ||
return self.eval_(ast.parse(expr, mode='eval').body, args, kwargs) | ||
|
||
def eval_(self, node, args, kwargs): | ||
if isinstance(node, ast.Num): # <number> | ||
return node.n | ||
elif isinstance(node, ast.Name): # <name> | ||
return super().get_value(node.id, args, kwargs) | ||
elif isinstance(node, ast.BinOp): # <left> <operator> <right> | ||
return operators[type(node.op)](self.eval_(node.left, args, kwargs), self.eval_(node.right, args, kwargs)) | ||
elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1 | ||
return operators[type(node.op)](self.eval_(node.operand, args, kwargs)) | ||
elif isinstance(node, ast.Call): | ||
if node.func.id == "name": | ||
assert len(node.args) == 1 | ||
return NAMES[self.eval_(node.args[0], args, kwargs)] | ||
if node.func.id == "fname": | ||
assert len(node.args) == 1 | ||
return FNAMES[self.eval_(node.args[0], args, kwargs)] | ||
assert False | ||
else: | ||
raise TypeError(node) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
these should be pulled from the actual model, not hardcoded here
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.
good idea