Skip to content

Commit

Permalink
codegen assert to if statement
Browse files Browse the repository at this point in the history
Making it a function call made it pretty slow even in the happy case.
Better to codegen an if-statement, and only call the function to
pretty print the assertion if we fail.
  • Loading branch information
mustafaquraish committed May 11, 2024
1 parent 622d1c2 commit e6366b9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
10 changes: 6 additions & 4 deletions compiler/passes/code_generator.oc
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,9 @@ def CodeGenerator::gen_statement(&this, node: &AST) {
.gen_indent()

if node.u.child? {
.out += "return "
if not node.u.child.returns {
.out += "return "
}
.gen_expression(node.u.child, is_top_level: true)
.out += ";\n"
} else {
Expand Down Expand Up @@ -907,9 +909,9 @@ def CodeGenerator::gen_statement(&this, node: &AST) {
ASTType::Assert => {
let expr = node.u.assertion.expr
.gen_indent()
.out += "ae_assert("
.out += "if(!("
.gen_expression(expr, is_top_level: true)
.out += ", "
.out += ")) { ae_assert_fail("
{
.out += "\""
.out <<= expr.span.start.str()
Expand Down Expand Up @@ -937,7 +939,7 @@ def CodeGenerator::gen_statement(&this, node: &AST) {
if expr.type == BoolLiteral and expr.u.bool_literal == false {
.out += " exit(1);"
}
.out += "\n"
.out += " }\n"
}
else => {
.gen_indent()
Expand Down
10 changes: 7 additions & 3 deletions compiler/passes/typechecker.oc
Original file line number Diff line number Diff line change
Expand Up @@ -1727,14 +1727,18 @@ def TypeChecker::check_statement(&this, node: &AST) {
let expected = cur_func.return_type

let res: &Type = null
if node.u.child? then res = .check_expression(node.u.child, hint: expected)
let child = node.u.child
if child? then res = .check_expression(child, hint: expected)

if expected.base == BaseType::Void {
if child? and child.returns {
// No need to check anything

} else if expected.base == BaseType::Void {
// We allow using arrow returns in void functions, they just don't return anything.
if node.u.child? {
.error(Error::new(node.span, "Cannot return a value from a void function"))
}
} else if node.u.child? {
} else if child? {
if res? and not res.eq(expected) {
.error(Error::new(node.span, `Return type {res.str()} does not match function return type {expected.str()}`))
}
Expand Down
6 changes: 5 additions & 1 deletion std/mod.oc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import .variadic

[extern] [exits] def exit(code: i32 = 0)

[extern "oc_trap"] def builtin_trap()

[exits]
def panic(msg: str = "<panic>") {
println("%s", msg)
exit(1)
dump_backtrace()
builtin_trap()
std::exit(1)
}

[extern "atoi"] def str::to_i32(this): i32
Expand Down
33 changes: 17 additions & 16 deletions std/prelude.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,28 @@ void dump_backtrace() {
void *array[40];
size_t size = backtrace(array, 40);
char **strings = backtrace_symbols(array, size);
printf("\nBacktrace:\n");
for (size_t i = 1; i < size; i++) {
printf("%s\n", strings[i]);
}
free(strings);
#endif
}

void ae_assert(int cond, char *dbg_msg, char *msg) {
if (!cond) {
printf("--------------------------------------------------------------------------------\n");
printf("%s\n", dbg_msg);
if (msg) {
printf(" Message: %s\n", msg);
}
printf("--------------------------------------------------------------------------------\n");
fflush(stdout);
dump_backtrace();
#ifdef __APPLE__
__builtin_debugtrap();
#else
__builtin_trap();
#endif
#ifdef __APPLE__
#define oc_trap __builtin_debugtrap
#else
#define oc_trap __builtin_trap
#endif

void ae_assert_fail(char *dbg_msg, char *msg) {
printf("--------------------------------------------------------------------------------\n");
printf("%s\n", dbg_msg);
if (msg) {
printf(" Message: %s\n", msg);
}
}
printf("--------------------------------------------------------------------------------\n");
fflush(stdout);
dump_backtrace();
oc_trap();
}

0 comments on commit e6366b9

Please sign in to comment.