add dbg impl for use with roc test

This commit is contained in:
Brendan Hansknecht 2023-12-02 16:39:20 -08:00
parent 546e0778c4
commit 6c60da2832
No known key found for this signature in database
GPG key ID: 0EA784685083E75B
4 changed files with 47 additions and 2 deletions

View file

@ -0,0 +1,11 @@
const std = @import("std");
const builtin = @import("builtin");
const RocStr = @import("str.zig").RocStr;
// An optional debug impl to be called during `roc test`
pub fn dbg_impl(loc: *const RocStr, src: *const RocStr, msg: *const RocStr) callconv(.C) void {
if (builtin.target.cpu.arch != .wasm32) {
const stderr = std.io.getStdErr().writer();
stderr.print("[{s}] {s} = {s}\n", .{ loc.asSlice(), src.asSlice(), msg.asSlice() }) catch unreachable;
}
}

View file

@ -4,6 +4,7 @@ const math = std.math;
const utils = @import("utils.zig");
const expect = @import("expect.zig");
const panic_utils = @import("panic.zig");
const dbg_utils = @import("dbg.zig");
comptime {
_ = @import("compiler_rt.zig");
@ -245,6 +246,7 @@ comptime {
exportUtilsFn(utils.dictPseudoSeed, "dict_pseudo_seed");
@export(panic_utils.panic, .{ .name = "roc_builtins.utils." ++ "panic", .linkage = .Weak });
@export(dbg_utils.dbg_impl, .{ .name = "roc_builtins.utils." ++ "dbg_impl", .linkage = .Weak });
if (builtin.target.cpu.arch != .wasm32) {
exportUtilsFn(expect.expectFailedStartSharedBuffer, "expect_failed_start_shared_buffer");

View file

@ -422,6 +422,7 @@ pub const DEC_TAN: &str = "roc_builtins.dec.tan";
pub const DEC_TO_I128: &str = "roc_builtins.dec.to_i128";
pub const DEC_TO_STR: &str = "roc_builtins.dec.to_str";
pub const UTILS_DBG_IMPL: &str = "roc_builtins.utils.dbg_impl";
pub const UTILS_TEST_PANIC: &str = "roc_builtins.utils.test_panic";
pub const UTILS_ALLOCATE_WITH_REFCOUNT: &str = "roc_builtins.utils.allocate_with_refcount";
pub const UTILS_INCREF_RC_PTR: &str = "roc_builtins.utils.incref_rc_ptr";

View file

@ -160,8 +160,39 @@ pub fn add_default_roc_externs(env: &Env<'_, '_, '_>) {
}
}
// TODO: generate a valid impl of dbg here.
unreachable_function(env, "roc_dbg");
// roc_dbg
{
// The type of this function (but not the implementation) should have
// already been defined by the builtins, which rely on it.
let fn_val = module.get_function("roc_dbg").unwrap();
let mut params = fn_val.get_param_iter();
let loc_arg = params.next().unwrap();
let src_arg = params.next().unwrap();
let msg_arg = params.next().unwrap();
debug_assert!(params.next().is_none());
// Add a basic block for the entry point
let entry = ctx.append_basic_block(fn_val, "entry");
builder.position_at_end(entry);
// Call utils.dbg_impl()
let dbg_impl = module.get_function(bitcode::UTILS_DBG_IMPL).unwrap();
let call = builder.new_build_call(
dbg_impl,
&[loc_arg.into(), src_arg.into(), msg_arg.into()],
"call_utils_dbg_impl",
);
call.set_call_convention(C_CALL_CONV);
builder.new_build_return(None);
if cfg!(debug_assertions) {
crate::llvm::build::verify_fn(fn_val);
}
}
match env.target_info.operating_system {
roc_target::OperatingSystem::Windows => {