Skip to content

Commit 865eaa7

Browse files
committed
Add fwrite
1 parent 6e211f6 commit 865eaa7

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

docs/tcc-wasm.wasm

52.1 KB
Binary file not shown.

tcc-wasm.wasm

52.1 KB
Binary file not shown.

zig/hexdump.zig

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Hexdump based on
2+
// https://gist.github.com/KaneRoot/caa34cba0a317fb6f96d3a4f93b1e228
3+
4+
/// Import the Zig Standard Library
5+
const std = @import("std");
6+
7+
/// Import the WebAssembly Logger
8+
const wasmlog = @import("wasmlog.zig");
9+
10+
/// Dump the buffer in hex
11+
pub fn hexdump(buffer: [*:0]const u8, len: usize) void {
12+
var hexb: u32 = 0;
13+
var ascii: [16]u8 = undefined;
14+
// First line, first left side (simple number).
15+
log(" {d:0>4}: ", .{hexb});
16+
17+
// Loop on all values in the buffer (i from 0 to len).
18+
var i: u32 = 0;
19+
while (i < len) : (i += 1) {
20+
// Print actual hexadecimal value.
21+
log("{X:0>2} ", .{buffer[i]});
22+
23+
// What to print (simple ascii text, right side).
24+
if (buffer[i] >= ' ' and buffer[i] <= '~') {
25+
ascii[(i % 16)] = buffer[i];
26+
} else {
27+
ascii[(i % 16)] = '.';
28+
}
29+
30+
// Next input is a multiple of 8 = extra space.
31+
if ((i + 1) % 8 == 0) {
32+
log(" ", .{});
33+
}
34+
35+
// No next input: print the right amount of spaces.
36+
if ((i + 1) == len) {
37+
// Each line is 16 bytes to print, each byte takes 3 characters.
38+
var missing_spaces = 3 * (15 - (i % 16));
39+
// Missing an extra space if the current index % 16 is less than 7.
40+
if ((i % 16) < 7) {
41+
missing_spaces += 1;
42+
}
43+
while (missing_spaces > 0) : (missing_spaces -= 1) {
44+
log(" ", .{});
45+
}
46+
}
47+
48+
// Every 16 bytes: print ascii text and line return.
49+
50+
// Case 1: it's been 16 bytes AND it's the last byte to print.
51+
if ((i + 1) % 16 == 0 and (i + 1) == len) {
52+
log("{s}\n", .{ascii[0..ascii.len]});
53+
}
54+
// Case 2: it's been 16 bytes but it's not the end of the buffer.
55+
else if ((i + 1) % 16 == 0 and (i + 1) != len) {
56+
log("{s}\n", .{ascii[0..ascii.len]});
57+
hexb += 16;
58+
log(" {d:0>4}: ", .{hexb});
59+
}
60+
// Case 3: not the end of the 16 bytes row but it's the end of the buffer.
61+
else if ((i + 1) % 16 != 0 and (i + 1) == len) {
62+
log(" {s}\n", .{ascii[0..((i + 1) % 16)]});
63+
}
64+
// Case 4: not the end of the 16 bytes row and not the end of the buffer.
65+
// Do nothing.
66+
}
67+
68+
log("\n", .{});
69+
}
70+
71+
/// Print to the WebAssembly Logger
72+
fn log(
73+
comptime format: []const u8,
74+
args: anytype,
75+
) void {
76+
// Format the message
77+
const slice = std.fmt.bufPrint(buf[buflen..], format, args) catch {
78+
wasmlog.Console.log("*** log error: buf too small", .{});
79+
return;
80+
};
81+
buflen += slice.len;
82+
83+
// Print the formatted message
84+
if (std.mem.containsAtLeast(u8, slice, 1, "\n")) {
85+
wasmlog.Console.log("{s}", .{buf[0 .. buflen - 1]});
86+
buflen = 0;
87+
}
88+
}
89+
90+
/// Log Buffer
91+
var buf: [256]u8 = [_]u8{0} ** 256;
92+
var buflen: usize = 0;

zig/tcc-wasm.zig

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const std = @import("std");
66
/// Import the WebAssembly Logger
77
const wasmlog = @import("wasmlog.zig");
88

9+
/// Import the Hexdump Logger
10+
const hexdump = @import("hexdump.zig");
11+
912
/// Compile a C program to 64-bit RISC-V
1013
pub export fn compile_program() u32 {
1114
debug("compile_program", .{});
@@ -79,8 +82,9 @@ export fn read(fd0: c_int, buf: [*:0]u8, nbyte: size_t) isize {
7982
return @intCast(strlen(s));
8083
}
8184

82-
export fn fwrite(ptr: [*:0]u8, size: usize, nmemb: usize, stream: *FILE) usize {
85+
export fn fwrite(ptr: [*:0]const u8, size: usize, nmemb: usize, stream: *FILE) usize {
8386
debug("fwrite: ptr={s}, size={}, nmemb={}, stream={*}", .{ ptr, size, nmemb, stream });
87+
hexdump.hexdump(ptr, size * nmemb);
8488
return nmemb;
8589
}
8690

0 commit comments

Comments
 (0)