|
|
@ -6,10 +6,10 @@ from dataclasses import dataclass |
|
|
from pycparser import c_ast, parse_file |
|
|
from pycparser import c_ast, parse_file |
|
|
from pycparser.c_ast import ( |
|
|
from pycparser.c_ast import ( |
|
|
Compound, Constant, DeclList, Enum, FileAST, FuncDecl, Struct, TypeDecl, Typename, BinaryOp as |
|
|
Compound, Constant, DeclList, Enum, FileAST, FuncDecl, Struct, TypeDecl, Typename, BinaryOp as |
|
|
ast_BinaryOp |
|
|
|
|
|
|
|
|
ast_BinaryOp, UnaryOp as ast_UnaryOp |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
from .consts import func_binary_ops, func_unary_ops, SPECIAL_VARS, binary_ops_python |
|
|
|
|
|
|
|
|
from .consts import func_binary_ops, func_unary_ops, SPECIAL_VARS, binary_ops_python, unary_ops_python |
|
|
from .instructions import ( |
|
|
from .instructions import ( |
|
|
Instruction, |
|
|
Instruction, |
|
|
Noop, |
|
|
Noop, |
|
|
@ -61,7 +61,6 @@ def numify(i): |
|
|
else: |
|
|
else: |
|
|
return int(i) |
|
|
return int(i) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def int_constant_fold(node): |
|
|
def int_constant_fold(node): |
|
|
if isinstance(node, ast_BinaryOp): |
|
|
if isinstance(node, ast_BinaryOp): |
|
|
left = int_constant_fold(node.left) |
|
|
left = int_constant_fold(node.left) |
|
|
@ -71,7 +70,8 @@ def int_constant_fold(node): |
|
|
) and left.type == 'int' and right.type == 'int': |
|
|
) and left.type == 'int' and right.type == 'int': |
|
|
try: |
|
|
try: |
|
|
new_constant = Constant( |
|
|
new_constant = Constant( |
|
|
'int', str(int(binary_ops_python[node.op](numify(left.value), numify(right.value)))) |
|
|
|
|
|
|
|
|
'int', |
|
|
|
|
|
str(int(binary_ops_python[node.op](numify(left.value), numify(right.value)))) |
|
|
) |
|
|
) |
|
|
return new_constant |
|
|
return new_constant |
|
|
except KeyError: |
|
|
except KeyError: |
|
|
@ -82,6 +82,17 @@ def int_constant_fold(node): |
|
|
node.left = left |
|
|
node.left = left |
|
|
node.right = right |
|
|
node.right = right |
|
|
return node |
|
|
return node |
|
|
|
|
|
elif isinstance(node, ast_UnaryOp): |
|
|
|
|
|
expr = int_constant_fold(node.expr) |
|
|
|
|
|
if isinstance(expr, Constant) and expr.type == 'int': |
|
|
|
|
|
try: |
|
|
|
|
|
new_constant = Constant( |
|
|
|
|
|
'int', str(int(unary_ops_python[node.op](numify(expr.value)))) |
|
|
|
|
|
) |
|
|
|
|
|
return new_constant |
|
|
|
|
|
except KeyError: |
|
|
|
|
|
node.expr = expr |
|
|
|
|
|
return node |
|
|
else: |
|
|
else: |
|
|
return node |
|
|
return node |
|
|
|
|
|
|
|
|
|