Skip to content
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

compiler: Refactor control flow graph data structures #582

Merged
merged 10 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions compiler/build_cfg.jou → compiler/build_cf_graph.jou
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "stdlib/str.jou"
import "stdlib/mem.jou"

import "./cf_graph.jou"
import "./structs.jou"
import "./evaluate.jou"
import "./types.jou"
Expand Down Expand Up @@ -113,8 +114,7 @@ def add_unary_op(
target: LocalVariable*,
) -> None:
ins = CfInstruction{location = location, kind = op, destvar = target}
operands = [arg, NULL]
ins.set_operands(operands)
ins.add_operand(arg)
add_instruction(st, ins)


Expand All @@ -128,8 +128,8 @@ def add_binary_op(
target: LocalVariable*,
) -> None:
ins = CfInstruction{location = location, kind = op, destvar = target}
operands = [lhs, rhs, NULL]
ins.set_operands(operands)
ins.add_operand(lhs)
ins.add_operand(rhs)
add_instruction(st, ins)


Expand Down Expand Up @@ -326,13 +326,11 @@ def build_class_field_pointer(
kind = CfInstructionKind::PtrClassField,
destvar = result,
}
ins.add_operand(instance)

assert sizeof(ins.fieldname) == sizeof(f->name)
strcpy(ins.fieldname, f->name)

operands = [instance, NULL]
ins.set_operands(operands)

add_instruction(st, ins)
return result

Expand Down Expand Up @@ -554,25 +552,25 @@ def build_function_or_method_call(

assert sig != NULL

args: LocalVariable** = calloc(call->nargs + 2, sizeof(args[0]))
k = 0
args: LocalVariable** = malloc(sizeof(args[0]) * (call->nargs + 2)) # +1 for self, another because why not
Akuli marked this conversation as resolved.
Show resolved Hide resolved
nargs = 0

if call->method_call_self != NULL:
if is_pointer_type(sig->argtypes[0]) and not call->uses_arrow_operator:
args[k++] = build_address_of_expression(st, call->method_call_self)
args[nargs++] = build_address_of_expression(st, call->method_call_self)
elif (not is_pointer_type(sig->argtypes[0])) and call->uses_arrow_operator:
self_ptr = build_expression(st, call->method_call_self)
assert self_ptr->type->kind == TypeKind::Pointer

# dereference the pointer
val = add_local_var(st, self_ptr->type->value_type)
add_unary_op(st, call->method_call_self->location, CfInstructionKind::PtrLoad, self_ptr, val)
args[k++] = val
args[nargs++] = val
else:
args[k++] = build_expression(st, call->method_call_self)
args[nargs++] = build_expression(st, call->method_call_self)

for i = 0; i < call->nargs; i++:
args[k++] = build_expression(st, &call->args[i])
args[nargs++] = build_expression(st, &call->args[i])

if sig->returntype != NULL:
return_value = add_local_var(st, sig->returntype)
Expand All @@ -584,15 +582,15 @@ def build_function_or_method_call(
kind = CfInstructionKind::Call,
signature = copy_signature(sig),
destvar = return_value,
operands = args,
noperands = nargs,
}
ins.set_operands(args)
add_instruction(st, ins)

if sig->is_noreturn:
# Place the remaining code into an unreachable block, so you will get a warning if there is any
add_jump(st, NULL, NULL, NULL, NULL)

free(args)
return return_value


Expand Down Expand Up @@ -912,12 +910,11 @@ def build_assert(st: State*, assert_location: Location, assertion: AstAssertion*
argtypes[1] = get_pointer_type(byteType)
argtypes[2] = intType

args = [
add_local_var(st, argtypes[0]),
add_local_var(st, argtypes[1]),
add_local_var(st, argtypes[2]),
NULL,
]
args: LocalVariable** = malloc(sizeof(args[0]) * 3)
Akuli marked this conversation as resolved.
Show resolved Hide resolved
assert args != NULL
args[0] = add_local_var(st, argtypes[0])
args[1] = add_local_var(st, argtypes[1])
args[2] = add_local_var(st, argtypes[2])

add_constant(st, assert_location, Constant{kind = ConstantKind::String, str = assertion->condition_str}, args[0])
tmp = strdup(assertion->condition.location.path)
Expand All @@ -937,8 +934,9 @@ def build_assert(st: State*, assert_location: Location, assertion: AstAssertion*
is_noreturn = True,
returntype_location = assert_location,
},
operands = args,
noperands = 3,
}
ins.set_operands(args)
add_instruction(st, ins)

st->current_block = trueblock
Expand Down
Loading
Loading