Skip to content

Commit

Permalink
Merge pull request #6116 from roc-lang/dbg-with-inspect
Browse files Browse the repository at this point in the history
Dbg with inspect
  • Loading branch information
bhansconnect authored Nov 30, 2023
2 parents 996ff79 + 9f1f061 commit 02d97bc
Show file tree
Hide file tree
Showing 84 changed files with 856 additions and 484 deletions.
13 changes: 0 additions & 13 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,19 +1136,6 @@ fn roc_dev_native(
)
.unwrap();

memory.reset();
}
ChildProcessMsg::Dbg => {
roc_repl_expect::run::render_dbgs_in_memory(
&mut writer,
arena,
&mut expectations,
&interns,
&layout_interner,
&memory,
)
.unwrap();

memory.reset();
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ mod cli_run {
x : Num *
x = 42
[<ignored for tests> 19:9] 42
[<ignored for tests> 20:9] "Fjoer en ferdjer frieten oan dyn geve lea"
[<ignored for tests> 13:9] "abc"
[<ignored for tests> 13:9] 10
[<ignored for tests> 13:9] A (B C)
[#UserApp] 42
[#UserApp] "Fjoer en ferdjer frieten oan dyn geve lea"
[#UserApp] "abc"
[#UserApp] 10
[#UserApp] (A (B C))
Program finished!
"#
),
Expand Down
20 changes: 15 additions & 5 deletions crates/cli/tests/fixtures/multi-dep-str/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
return memset(dst, value, size);
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

extern fn kill(pid: c_int, sig: c_int) c_int;
Expand Down
20 changes: 15 additions & 5 deletions crates/cli/tests/fixtures/multi-dep-thunk/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
return memset(dst, value, size);
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

extern fn kill(pid: c_int, sig: c_int) c_int;
Expand Down
20 changes: 15 additions & 5 deletions crates/cli/tests/fixtures/packages/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,23 @@ export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
return memset(dst, value, size);
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

extern fn kill(pid: c_int, sig: c_int) c_int;
Expand Down
22 changes: 17 additions & 5 deletions crates/cli_testing_examples/algorithms/fibonacci-platform/host.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const std = @import("std");
const builtin = @import("builtin");
const str = @import("glue").str;
const RocStr = str.RocStr;
const testing = std.testing;
const expectEqual = testing.expectEqual;
const expect = testing.expect;
Expand Down Expand Up @@ -50,13 +52,23 @@ export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
free(@as([*]align(Align) u8, @alignCast(@ptrCast(c_ptr))));
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
Expand Down
20 changes: 15 additions & 5 deletions crates/cli_testing_examples/algorithms/quicksort-platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
free(@as([*]align(Align) u8, @alignCast(@ptrCast(c_ptr))));
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
Expand Down
20 changes: 15 additions & 5 deletions crates/cli_testing_examples/benchmarks/platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,23 @@ export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
free(@as([*]align(Align) u8, @alignCast(@ptrCast(c_ptr))));
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
_ = tag_id;
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
switch (tag_id) {
0 => {
stderr.print("Roc standard library crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
1 => {
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
},
else => unreachable,
}
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
Expand Down
13 changes: 9 additions & 4 deletions crates/cli_testing_examples/expects/zig-platform/host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ export fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void {
free(@as([*]align(Align) u8, @alignCast(@ptrCast(c_ptr))));
}

export fn roc_panic(c_ptr: *anyopaque, tag_id: u32) callconv(.C) void {
export fn roc_panic(msg: *RocStr, tag_id: u32) callconv(.C) void {
_ = tag_id;

const stderr = std.io.getStdErr().writer();
const msg = @as([*:0]const u8, @ptrCast(c_ptr));
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg}) catch unreachable;
std.process.exit(0);
stderr.print("Application crashed with message\n\n {s}\n\nShutting down\n", .{msg.asSlice()}) catch unreachable;
std.process.exit(1);
}

export fn roc_dbg(loc: *RocStr, msg: *RocStr) callconv(.C) void {
// This platform uses stdout for testing purposes instead of the normal stderr.
const stdout = std.io.getStdOut().writer();
stdout.print("[{s}] {s}\n", .{ loc.asSlice(), msg.asSlice() }) catch unreachable;
}

export fn roc_memset(dst: [*]u8, value: i32, size: usize) callconv(.C) void {
Expand Down
24 changes: 14 additions & 10 deletions crates/compiler/builtins/bitcode/benchmark/dec.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ fn roc_alloc(_: usize, _: u32) callconv(.C) ?*anyopaque {
fn roc_panic(_: *anyopaque, _: u32) callconv(.C) void {
@panic("Not needed for dec benchmark");
}
fn roc_dbg(_: *anyopaque, _: *anyopaque) callconv(.C) void {
@panic("Not needed for dec benchmark");
}

comptime {
@export(roc_alloc, .{ .name = "roc_alloc", .linkage = .Strong });
@export(roc_panic, .{ .name = "roc_panic", .linkage = .Strong });
@export(roc_dbg, .{ .name = "roc_dbg", .linkage = .Strong });
}

var timer: Timer = undefined;
Expand Down Expand Up @@ -100,16 +104,16 @@ pub fn main() !void {
const f64Atan = try avg_runs(f64, n, atanF64, f1);

try stdout.print("\n\nDec/F64:\n", .{});
try stdout.print("addition: {d:0.2}\n", .{@intToFloat(f64, decAdd) / @intToFloat(f64, f64Add)});
try stdout.print("subtraction: {d:0.2}\n", .{@intToFloat(f64, decSub) / @intToFloat(f64, f64Sub)});
try stdout.print("multiplication: {d:0.2}\n", .{@intToFloat(f64, decMul) / @intToFloat(f64, f64Mul)});
try stdout.print("division: {d:0.2}\n", .{@intToFloat(f64, decDiv) / @intToFloat(f64, f64Div)});
try stdout.print("sin: {d:0.2}\n", .{@intToFloat(f64, decSin) / @intToFloat(f64, f64Sin)});
try stdout.print("cos: {d:0.2}\n", .{@intToFloat(f64, decCos) / @intToFloat(f64, f64Cos)});
try stdout.print("tan: {d:0.2}\n", .{@intToFloat(f64, decTan) / @intToFloat(f64, f64Tan)});
try stdout.print("asin: {d:0.2}\n", .{@intToFloat(f64, decAsin) / @intToFloat(f64, f64Asin)});
try stdout.print("acos: {d:0.2}\n", .{@intToFloat(f64, decAcos) / @intToFloat(f64, f64Acos)});
try stdout.print("atan: {d:0.2}\n", .{@intToFloat(f64, decAtan) / @intToFloat(f64, f64Atan)});
try stdout.print("addition: {d:0.2}\n", .{@as(f64, @floatFromInt(decAdd)) / @as(f64, @floatFromInt(f64Add))});
try stdout.print("subtraction: {d:0.2}\n", .{@as(f64, @floatFromInt(decSub)) / @as(f64, @floatFromInt(f64Sub))});
try stdout.print("multiplication: {d:0.2}\n", .{@as(f64, @floatFromInt(decMul)) / @as(f64, @floatFromInt(f64Mul))});
try stdout.print("division: {d:0.2}\n", .{@as(f64, @floatFromInt(decDiv)) / @as(f64, @floatFromInt(f64Div))});
try stdout.print("sin: {d:0.2}\n", .{@as(f64, @floatFromInt(decSin)) / @as(f64, @floatFromInt(f64Sin))});
try stdout.print("cos: {d:0.2}\n", .{@as(f64, @floatFromInt(decCos)) / @as(f64, @floatFromInt(f64Cos))});
try stdout.print("tan: {d:0.2}\n", .{@as(f64, @floatFromInt(decTan)) / @as(f64, @floatFromInt(f64Tan))});
try stdout.print("asin: {d:0.2}\n", .{@as(f64, @floatFromInt(decAsin)) / @as(f64, @floatFromInt(f64Asin))});
try stdout.print("acos: {d:0.2}\n", .{@as(f64, @floatFromInt(decAcos)) / @as(f64, @floatFromInt(f64Acos))});
try stdout.print("atan: {d:0.2}\n", .{@as(f64, @floatFromInt(decAtan)) / @as(f64, @floatFromInt(f64Atan))});
}

fn avg_runs(comptime T: type, comptime n: usize, comptime op: fn (T, T) T, v: T) !u64 {
Expand Down
4 changes: 0 additions & 4 deletions crates/compiler/builtins/bitcode/src/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,3 @@ pub fn notifyParent(shared_buffer: [*]u8, tag: u32) callconv(.C) void {
pub fn notifyParentExpect(shared_buffer: [*]u8) callconv(.C) void {
notifyParent(shared_buffer, 1);
}

pub fn notifyParentDbg(shared_buffer: [*]u8) callconv(.C) void {
notifyParent(shared_buffer, 2);
}
2 changes: 1 addition & 1 deletion crates/compiler/builtins/bitcode/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ comptime {

// Utils
comptime {
exportUtilsFn(utils.test_dbg, "test_dbg");
exportUtilsFn(utils.test_panic, "test_panic");
exportUtilsFn(utils.increfRcPtrC, "incref_rc_ptr");
exportUtilsFn(utils.decrefRcPtrC, "decref_rc_ptr");
Expand All @@ -248,7 +249,6 @@ comptime {
exportUtilsFn(expect.expectFailedStartSharedBuffer, "expect_failed_start_shared_buffer");
exportUtilsFn(expect.expectFailedStartSharedFile, "expect_failed_start_shared_file");
exportUtilsFn(expect.notifyParentExpect, "notify_parent_expect");
exportUtilsFn(expect.notifyParentDbg, "notify_parent_dbg");

// sets the buffer used for expect failures
@export(expect.setSharedBuffer, .{ .name = "set_shared_buffer", .linkage = .Weak });
Expand Down
13 changes: 13 additions & 0 deletions crates/compiler/builtins/bitcode/src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ extern fn roc_realloc(c_ptr: *anyopaque, new_size: usize, old_size: usize, align
// This should never be passed a null pointer.
extern fn roc_dealloc(c_ptr: *anyopaque, alignment: u32) callconv(.C) void;

extern fn roc_dbg(file_path: *anyopaque, message: *anyopaque) callconv(.C) void;

// Since roc_dbg is never used by the builtins, we need at export a function that uses it to stop DCE.
pub fn test_dbg(file_path: *anyopaque, message: *anyopaque) callconv(.C) void {
roc_dbg(file_path, message);
}

extern fn kill(pid: c_int, sig: c_int) c_int;
extern fn shm_open(name: *const i8, oflag: c_int, mode: c_uint) c_int;
extern fn mmap(addr: ?*anyopaque, length: c_uint, prot: c_int, flags: c_int, fd: c_int, offset: c_uint) *anyopaque;
Expand All @@ -40,13 +47,19 @@ fn testing_roc_mmap(addr: ?*anyopaque, length: c_uint, prot: c_int, flags: c_int
return mmap(addr, length, prot, flags, fd, offset);
}

fn testing_roc_dbg(file_path: *anyopaque, message: *anyopaque) callconv(.C) void {
_ = message;
_ = file_path;
}

comptime {
// During tests, use the testing allocators to satisfy these functions.
if (builtin.is_test) {
@export(testing_roc_alloc, .{ .name = "roc_alloc", .linkage = .Strong });
@export(testing_roc_realloc, .{ .name = "roc_realloc", .linkage = .Strong });
@export(testing_roc_dealloc, .{ .name = "roc_dealloc", .linkage = .Strong });
@export(testing_roc_panic, .{ .name = "roc_panic", .linkage = .Strong });
@export(testing_roc_dbg, .{ .name = "roc_dbg", .linkage = .Strong });

if (builtin.os.tag == .macos or builtin.os.tag == .linux) {
@export(testing_roc_getppid, .{ .name = "roc_getppid", .linkage = .Strong });
Expand Down
1 change: 0 additions & 1 deletion crates/compiler/builtins/src/bitcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ pub const UTILS_EXPECT_FAILED_START_SHARED_FILE: &str =
"roc_builtins.utils.expect_failed_start_shared_file";
pub const UTILS_EXPECT_READ_ENV_SHARED_BUFFER: &str = "roc_builtins.utils.read_env_shared_buffer";
pub const NOTIFY_PARENT_EXPECT: &str = "roc_builtins.utils.notify_parent_expect";
pub const NOTIFY_PARENT_DBG: &str = "roc_builtins.utils.notify_parent_dbg";

pub const UTILS_LONGJMP: &str = "longjmp";
pub const UTILS_SETJMP: &str = "setjmp";
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/can/src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,12 +670,12 @@ fn deep_copy_expr_help<C: CopyEnv>(env: &mut C, copied: &mut Vec<Variable>, expr
},

Dbg {
loc_condition,
loc_message,
loc_continuation,
variable,
symbol,
} => Dbg {
loc_condition: Box::new(loc_condition.map(|e| go_help!(e))),
loc_message: Box::new(loc_message.map(|e| go_help!(e))),
loc_continuation: Box::new(loc_continuation.map(|e| go_help!(e))),
variable: sub!(*variable),
symbol: *symbol,
Expand Down
Loading

0 comments on commit 02d97bc

Please sign in to comment.