-
Notifications
You must be signed in to change notification settings - Fork 26
Preprocessor for ULP/RTC macros #43
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
Changes from 16 commits
79db90f
84d734d
ec81ecc
c184924
25d34b0
76a81ac
56f4530
9907b10
27ab850
54b117e
feb42dc
87507c9
99352a3
d76fd26
4dded94
219f939
5c3eeb8
3e8c0d5
ac1de99
8d88fd1
46f1442
2f6ee78
69ae946
4f90f76
d44384f
254adf9
c3bd101
2a0a39a
47d5e8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
from uctypes import struct, addressof, LITTLE_ENDIAN, UINT32, BFUINT32, BF_POS, BF_LEN | ||
|
||
from .soc import * | ||
from .util import split_tokens, validate_expression | ||
|
||
# XXX dirty hack: use a global for the symbol table | ||
symbols = None | ||
|
@@ -112,7 +113,7 @@ def make_ins(layout): | |
unused : 8 # Unused | ||
low : 5 # Low bit | ||
high : 5 # High bit | ||
opcode : 4 # Opcode (OPCODE_WR_REG) | ||
opcode : 4 # Opcode (OPCODE_RD_REG) | ||
""") | ||
|
||
|
||
|
@@ -267,6 +268,20 @@ def make_ins(layout): | |
ARG = namedtuple('ARG', ('type', 'value', 'raw')) | ||
|
||
|
||
def eval_arg(arg): | ||
parts = [] | ||
for token in split_tokens(arg): | ||
if symbols.has_sym(token): | ||
_, _, sym_value = symbols.get_sym(token) | ||
parts.append(str(sym_value)) | ||
else: | ||
parts.append(token) | ||
parts = "".join(parts) | ||
if not validate_expression(parts): | ||
raise ValueError('Unsupported expression: %s' % parts) | ||
return eval(parts) | ||
|
||
|
||
def arg_qualify(arg): | ||
""" | ||
look at arg and qualify its type: | ||
|
@@ -289,8 +304,12 @@ def arg_qualify(arg): | |
return ARG(IMM, int(arg), arg) | ||
except ValueError: | ||
pass | ||
entry = symbols.get_sym(arg) | ||
return ARG(SYM, entry, arg) | ||
try: | ||
entry = symbols.get_sym(arg) | ||
return ARG(SYM, entry, arg) | ||
except KeyError: | ||
pass | ||
return ARG(IMM, int(eval_arg(arg)), arg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seen my previous comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed now. |
||
|
||
|
||
def get_reg(arg): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from . import nocomment | ||
ThomasWaldmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from .util import split_tokens | ||
|
||
|
||
class Preprocessor: | ||
def __init__(self): | ||
self._defines = {} | ||
|
||
def parse_defines(self, content): | ||
result = {} | ||
for line in content.splitlines(): | ||
wnienhaus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
line = line.strip() | ||
if not line.startswith("#define"): | ||
# skip lines not containing #define | ||
continue | ||
line = line[8:].strip() # remove #define | ||
parts = line.split(None, 1) | ||
if len(parts) != 2: | ||
# skip defines without value | ||
continue | ||
identifier, value = parts | ||
tmp = identifier.split('(', 1) | ||
if len(tmp) == 2: | ||
# skip parameterised defines (macros) | ||
continue | ||
value = "".join(nocomment.remove_comments(value)).strip() | ||
result[identifier] = value | ||
self._defines = result | ||
return result | ||
|
||
def expand_defines(self, line): | ||
found = True | ||
while found: # do as many passed as needed, until nothing was replaced anymore | ||
found = False | ||
tokens = split_tokens(line) | ||
line = "" | ||
for t in tokens: | ||
lu = self._defines.get(t, t) | ||
if lu != t: | ||
found = True | ||
line += lu | ||
|
||
return line | ||
|
||
def preprocess(self, content): | ||
self.parse_defines(content) | ||
lines = nocomment.remove_comments(content) | ||
result = [] | ||
for line in lines: | ||
line = self.expand_defines(line) | ||
result.append(line) | ||
result = "\n".join(result) | ||
return result | ||
|
||
|
||
def preprocess(content): | ||
return Preprocessor().preprocess(content) |
Uh oh!
There was an error while loading. Please reload this page.