|
8 | 8 |
|
9 | 9 | def recursive_dereferencer(var, builder): |
10 | 10 | """dereference until primitive type comes out""" |
| 11 | + # TODO: Not worrying about stack overflow for now |
11 | 12 | if isinstance(var.type, ir.PointerType): |
12 | 13 | a = builder.load(var) |
13 | 14 | return recursive_dereferencer(a, builder) |
14 | | - elif var.type == ir.IntType(64): |
| 15 | + elif isinstance(var.type, ir.IntType): |
15 | 16 | return var |
16 | 17 | else: |
17 | 18 | raise TypeError(f"Unsupported type for dereferencing: {var.type}") |
18 | 19 |
|
19 | 20 |
|
| 21 | +def get_operand_value(operand, builder, local_sym_tab): |
| 22 | + """Extract the value from an operand, handling variables and constants.""" |
| 23 | + if isinstance(operand, ast.Name): |
| 24 | + if operand.id in local_sym_tab: |
| 25 | + return recursive_dereferencer(local_sym_tab[operand.id].var, builder) |
| 26 | + raise ValueError(f"Undefined variable: {operand.id}") |
| 27 | + elif isinstance(operand, ast.Constant): |
| 28 | + if isinstance(operand.value, int): |
| 29 | + return ir.Constant(ir.IntType(64), operand.value) |
| 30 | + raise TypeError(f"Unsupported constant type: {type(operand.value)}") |
| 31 | + raise TypeError(f"Unsupported operand type: {type(operand)}") |
| 32 | + |
| 33 | + |
20 | 34 | def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func): |
21 | 35 | logger.info(f"module {module}") |
22 | | - left = rval.left |
23 | | - right = rval.right |
24 | 36 | op = rval.op |
25 | 37 |
|
26 | | - # Handle left operand |
27 | | - if isinstance(left, ast.Name): |
28 | | - if left.id in local_sym_tab: |
29 | | - left = recursive_dereferencer(local_sym_tab[left.id].var, builder) |
30 | | - else: |
31 | | - raise SyntaxError(f"Undefined variable: {left.id}") |
32 | | - elif isinstance(left, ast.Constant): |
33 | | - left = ir.Constant(ir.IntType(64), left.value) |
34 | | - else: |
35 | | - raise SyntaxError("Unsupported left operand type") |
36 | | - |
37 | | - if isinstance(right, ast.Name): |
38 | | - if right.id in local_sym_tab: |
39 | | - right = recursive_dereferencer(local_sym_tab[right.id].var, builder) |
40 | | - else: |
41 | | - raise SyntaxError(f"Undefined variable: {right.id}") |
42 | | - elif isinstance(right, ast.Constant): |
43 | | - right = ir.Constant(ir.IntType(64), right.value) |
44 | | - else: |
45 | | - raise SyntaxError("Unsupported right operand type") |
46 | | - |
| 38 | + left = get_operand_value(rval.left, builder, local_sym_tab) |
| 39 | + right = get_operand_value(rval.right, builder, local_sym_tab) |
47 | 40 | logger.info(f"left is {left}, right is {right}, op is {op}") |
48 | 41 |
|
49 | 42 | if isinstance(op, ast.Add): |
|
0 commit comments