3 Commits

2 changed files with 37 additions and 54 deletions

View File

@ -8,68 +8,52 @@ logger: Logger = logging.getLogger(__name__)
def recursive_dereferencer(var, builder): def recursive_dereferencer(var, builder):
"""dereference until primitive type comes out""" """dereference until primitive type comes out"""
if var.type == ir.PointerType(ir.PointerType(ir.IntType(64))): # TODO: Not worrying about stack overflow for now
if isinstance(var.type, ir.PointerType):
a = builder.load(var) a = builder.load(var)
return recursive_dereferencer(a, builder) return recursive_dereferencer(a, builder)
elif var.type == ir.PointerType(ir.IntType(64)): elif isinstance(var.type, ir.IntType):
a = builder.load(var)
return recursive_dereferencer(a, builder)
elif var.type == ir.IntType(64):
return var return var
else: else:
raise TypeError(f"Unsupported type for dereferencing: {var.type}") raise TypeError(f"Unsupported type for dereferencing: {var.type}")
def get_operand_value(operand, builder, local_sym_tab):
"""Extract the value from an operand, handling variables and constants."""
if isinstance(operand, ast.Name):
if operand.id in local_sym_tab:
return recursive_dereferencer(local_sym_tab[operand.id].var, builder)
raise ValueError(f"Undefined variable: {operand.id}")
elif isinstance(operand, ast.Constant):
if isinstance(operand.value, int):
return ir.Constant(ir.IntType(64), operand.value)
raise TypeError(f"Unsupported constant type: {type(operand.value)}")
raise TypeError(f"Unsupported operand type: {type(operand)}")
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func): def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func):
logger.info(f"module {module}")
left = rval.left
right = rval.right
op = rval.op op = rval.op
left = get_operand_value(rval.left, builder, local_sym_tab)
# Handle left operand right = get_operand_value(rval.right, builder, local_sym_tab)
if isinstance(left, ast.Name):
if left.id in local_sym_tab:
left = recursive_dereferencer(local_sym_tab[left.id].var, builder)
else:
raise SyntaxError(f"Undefined variable: {left.id}")
elif isinstance(left, ast.Constant):
left = ir.Constant(ir.IntType(64), left.value)
else:
raise SyntaxError("Unsupported left operand type")
if isinstance(right, ast.Name):
if right.id in local_sym_tab:
right = recursive_dereferencer(local_sym_tab[right.id].var, builder)
else:
raise SyntaxError(f"Undefined variable: {right.id}")
elif isinstance(right, ast.Constant):
right = ir.Constant(ir.IntType(64), right.value)
else:
raise SyntaxError("Unsupported right operand type")
logger.info(f"left is {left}, right is {right}, op is {op}") logger.info(f"left is {left}, right is {right}, op is {op}")
if isinstance(op, ast.Add): # Map AST operation nodes to LLVM IR builder methods
builder.store(builder.add(left, right), local_sym_tab[var_name].var) op_map = {
elif isinstance(op, ast.Sub): ast.Add: builder.add,
builder.store(builder.sub(left, right), local_sym_tab[var_name].var) ast.Sub: builder.sub,
elif isinstance(op, ast.Mult): ast.Mult: builder.mul,
builder.store(builder.mul(left, right), local_sym_tab[var_name].var) ast.Div: builder.sdiv,
elif isinstance(op, ast.Div): ast.Mod: builder.srem,
builder.store(builder.sdiv(left, right), local_sym_tab[var_name].var) ast.LShift: builder.shl,
elif isinstance(op, ast.Mod): ast.RShift: builder.lshr,
builder.store(builder.srem(left, right), local_sym_tab[var_name].var) ast.BitOr: builder.or_,
elif isinstance(op, ast.LShift): ast.BitXor: builder.xor,
builder.store(builder.shl(left, right), local_sym_tab[var_name].var) ast.BitAnd: builder.and_,
elif isinstance(op, ast.RShift): ast.FloorDiv: builder.udiv,
builder.store(builder.lshr(left, right), local_sym_tab[var_name].var) }
elif isinstance(op, ast.BitOr):
builder.store(builder.or_(left, right), local_sym_tab[var_name].var) if type(op) in op_map:
elif isinstance(op, ast.BitXor): result = op_map[type(op)](left, right)
builder.store(builder.xor(left, right), local_sym_tab[var_name].var) builder.store(result, local_sym_tab[var_name].var)
elif isinstance(op, ast.BitAnd):
builder.store(builder.and_(left, right), local_sym_tab[var_name].var)
elif isinstance(op, ast.FloorDiv):
builder.store(builder.udiv(left, right), local_sym_tab[var_name].var)
else: else:
raise SyntaxError("Unsupported binary operation") raise SyntaxError("Unsupported binary operation")

View File

@ -41,8 +41,7 @@ def _handle_attribute_expr(
attr_name = expr.attr attr_name = expr.attr
if var_name in local_sym_tab: if var_name in local_sym_tab:
var_ptr, var_type, var_metadata = local_sym_tab[var_name] var_ptr, var_type, var_metadata = local_sym_tab[var_name]
logger.info(f"Loading attribute { logger.info(f"Loading attribute {attr_name} from variable {var_name}")
attr_name} from variable {var_name}")
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}") logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
metadata = structs_sym_tab[var_metadata] metadata = structs_sym_tab[var_metadata]