commit 5344093cdf6b73c444dbd42242b03dea455076ea
parent a3869ad6ec05587cfa7835042f2f8ab305be2f06
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Mon, 27 Apr 2026 04:01:53 -0700
scheme1: add (heap-usage) primitive returning bytes consumed
Returns (heap_next - heap_buf_ptr) as a tagged fixnum. Used by cc to
instrument per-phase compile-time allocation cost.
Implementation note: M0 appends a NUL byte to every \"...\" string
literal, so adding a 10-byte name like "heap-usage" emits 11 bytes
and breaks 8-alignment of subsequent data slots (the prim_table $()
size fields, the bss pointer slots like :heap_buf_ptr) -- causing
bus errors at startup. Pad with 5 explicit '00' bytes so the total
addition is 16 bytes (multiple of 8). Verified with a small (cons
1 (cons 2 (cons 3 '()))) test: usage rose from 2488 to 3224 bytes.
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/scheme1/scheme1.P1pp b/scheme1/scheme1.P1pp
@@ -4801,6 +4801,16 @@
%ret
%endscope
+# (heap-usage) -> tagged fixnum: bytes consumed since heap_init
+# (heap_next - heap_buf_ptr). Used by cc to instrument per-phase
+# allocation cost. Args list ignored.
+:prim_heap_usage_entry
+ %ld_global(t0, &heap_next)
+ %ld_global(t1, &heap_buf_ptr)
+ %sub(a0, t0, t1)
+ %mkfix(a0, a0)
+ %ret
+
# Surface names. Length is hard-coded at the call site; no NUL needed
# because intern takes (ptr, len). Aligned padding via "\0" bytes is
# fine -- M0 emits ASCII verbatim.
@@ -4886,6 +4896,10 @@
:name_write "write"
:name_error "error"
:name_format "format"
+;; "heap-usage" + auto-NUL = 11 bytes; pad to 16 (multiple of 8) so
+;; subsequent 8-aligned data slots (prim_table $() rows, the bss
+;; pointer slots) stay aligned. M0 appends a NUL to every "..." string.
+:name_heap_usage "heap-usage" '00' '00' '00' '00' '00'
# Writer string constants. Lengths are hard-coded at the bv_putn call
# sites (write_to_bv branches). No NUL needed -- bv_putn takes (ptr, n).
@@ -4964,6 +4978,7 @@
&name_write %(0) $(5) &prim_write_entry %(0)
&name_error %(0) $(5) &prim_error_entry %(0)
&name_format %(0) $(6) &prim_format_entry %(0)
+&name_heap_usage %(0) $(10) &prim_heap_usage_entry %(0)
:prim_table_end
:msg_usage "scheme1: usage: scheme1 SOURCE.scm" '0a' '00'