Skip to content

Commit a6602a6

Browse files
committed
Dump a.out
1 parent 5c3b9ca commit a6602a6

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

docs/tcc-wasm.wasm

2.95 KB
Binary file not shown.

tcc-wasm.wasm

2.95 KB
Binary file not shown.

zig/hexdump.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,5 @@ fn log(
8888
}
8989

9090
/// Log Buffer
91-
var buf: [256]u8 = [_]u8{0} ** 256;
91+
var buf = std.mem.zeroes([256]u8);
9292
var buflen: usize = 0;

zig/tcc-wasm.zig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ pub export fn compile_program() u32 {
3838
// Call the TCC Compiler
3939
_ = main(argc, &args_ptrs);
4040

41-
return 123;
41+
// Dump the generated `a.out`
42+
debug("a.out: {} bytes", .{write_buflen});
43+
hexdump.hexdump(@ptrCast(&write_buf), write_buflen);
44+
45+
// Return size of `a.out` to JavaScript
46+
return write_buflen;
4247
}
4348

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

8590
export fn fputc(c: c_int, stream: *FILE) c_int {
8691
debug("fputc: c=0x{X:0>2}, stream={*}", .{ @as(u8, @intCast(c)), stream });
92+
93+
// Copy to the Write Buffer. TODO: Support more than one `stream`
94+
@memset(write_buf[write_buflen .. write_buflen + 1], @intCast(c));
95+
write_buflen += 1;
8796
return c;
8897
}
8998

9099
export fn fwrite(ptr: [*:0]const u8, size: usize, nmemb: usize, stream: *FILE) usize {
91100
debug("fwrite: size={}, nmemb={}, stream={*}", .{ size, nmemb, stream });
92101
hexdump.hexdump(ptr, size * nmemb);
102+
103+
// Copy to the Write Buffer. TODO: Support more than one `stream`
104+
const len = size * nmemb;
105+
@memcpy(write_buf[write_buflen .. write_buflen + len], ptr[0..]);
106+
write_buflen += len;
93107
return nmemb;
94108
}
95109

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

125+
/// Write Buffer for fputc and fwrite
126+
var write_buf = std.mem.zeroes([8192]u8);
127+
var write_buflen: usize = 0;
128+
129+
/// Next File Descriptor
111130
var fd: c_int = 3;
112131
var first_read: bool = true;
113132

0 commit comments

Comments
 (0)