Skip to content

Commit

Permalink
Dump a.out
Browse files Browse the repository at this point in the history
  • Loading branch information
lupyuen committed Jan 27, 2024
1 parent 5c3b9ca commit a6602a6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
Binary file modified docs/tcc-wasm.wasm
Binary file not shown.
Binary file modified tcc-wasm.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion zig/hexdump.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ fn log(
}

/// Log Buffer
var buf: [256]u8 = [_]u8{0} ** 256;
var buf = std.mem.zeroes([256]u8);
var buflen: usize = 0;
21 changes: 20 additions & 1 deletion zig/tcc-wasm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ pub export fn compile_program() u32 {
// Call the TCC Compiler
_ = main(argc, &args_ptrs);

return 123;
// Dump the generated `a.out`
debug("a.out: {} bytes", .{write_buflen});
hexdump.hexdump(@ptrCast(&write_buf), write_buflen);

// Return size of `a.out` to JavaScript
return write_buflen;
}

/// Main Function from tcc.c
Expand Down Expand Up @@ -84,12 +89,21 @@ export fn read(fd0: c_int, buf: [*:0]u8, nbyte: size_t) isize {

export fn fputc(c: c_int, stream: *FILE) c_int {
debug("fputc: c=0x{X:0>2}, stream={*}", .{ @as(u8, @intCast(c)), stream });

// Copy to the Write Buffer. TODO: Support more than one `stream`
@memset(write_buf[write_buflen .. write_buflen + 1], @intCast(c));
write_buflen += 1;
return c;
}

export fn fwrite(ptr: [*:0]const u8, size: usize, nmemb: usize, stream: *FILE) usize {
debug("fwrite: size={}, nmemb={}, stream={*}", .{ size, nmemb, stream });
hexdump.hexdump(ptr, size * nmemb);

// Copy to the Write Buffer. TODO: Support more than one `stream`
const len = size * nmemb;
@memcpy(write_buf[write_buflen .. write_buflen + len], ptr[0..]);
write_buflen += len;
return nmemb;
}

Expand All @@ -108,6 +122,11 @@ export fn unlink(path: [*:0]const u8) c_int {
return 0;
}

/// Write Buffer for fputc and fwrite
var write_buf = std.mem.zeroes([8192]u8);
var write_buflen: usize = 0;

/// Next File Descriptor
var fd: c_int = 3;
var first_read: bool = true;

Expand Down

0 comments on commit a6602a6

Please sign in to comment.