about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-09-07 22:08:39 -0700
committerIrene Knapp <ireneista@irenes.space>2026-09-07 22:08:39 -0700
commit08d58db689cac50ef582340e93047273f7759a37 (patch)
tree842477c73f0d4b4201143132d9158afdb91d1f19
parente42c36f0887de2bf2d5471e4eb59f6ad7cb79a21 (diff)
implement magic hex-transform comments
they work! that is, they show up in the output. to prove it, two of them are added to elf.e.

they don't yet deal with substring parameters, and there's some formatting niceties to consider.

Force-Push: yes
Change-Id: I0a0f72e21ca023bf99287880d52b5a8dddbf8e34
-rw-r--r--elf.e3
-rw-r--r--transform.e38
2 files changed, 36 insertions, 5 deletions
diff --git a/elf.e b/elf.e
index 7801bf6..a832c61 100644
--- a/elf.e
+++ b/elf.e
@@ -23,6 +23,7 @@
 ~   The only interesting thing here is the entry pointer.
 
 : elf-file-header
+  ~ : ELF file header
   ~ * denotes mandatory fields according to breadbox
   current-offset 3unroll
 
@@ -86,6 +87,7 @@
 
 ~ ~~
 : elf-program-header
+  ~ : ELF program header
   ~ * denotes mandatory fields according to breadbox
   current-offset L!' elf-program-header
   current-offset 3unroll
@@ -112,6 +114,7 @@
 ~ may be useful for other programs, such as hex.e. Its only difference from
 ~ the base version is that it adds the write permission.
 : elf-program-header-writable
+  ~ : ELF program header
   ~ * denotes mandatory fields according to breadbox
   current-offset L!' elf-program-header
   current-offset 3unroll
diff --git a/transform.e b/transform.e
index 1598d19..eb8e8bf 100644
--- a/transform.e
+++ b/transform.e
@@ -2719,7 +2719,8 @@ allocate-transformation-state s" transformation-state" variable
 ~ accessors for the transformation state, defined far, far above.
 : hex-output-metadata-label-loop-buffer-start ;
 : hex-output-metadata-label-loop-buffer-length 8 + ;
-: hex-output-metadata-first-entry 2 8 * + ;
+: hex-output-metadata-latest-output-point 2 8 * + ;
+: hex-output-metadata-first-entry 3 8 * + ;
 : hex-output-metadata-entry-data-start ;
 : hex-output-metadata-entry-data-length 8 + ;
 : hex-output-metadata-entry-type 2 8 * + ;
@@ -2740,6 +2741,7 @@ allocate-transformation-state s" transformation-state" variable
   transformation-state transformation-state-output-metadata @
   dup hex-output-metadata-label-loop-buffer-start 0 swap !
   dup hex-output-metadata-label-loop-buffer-length 0 swap !
+  dup hex-output-metadata-latest-output-point 0 swap !
   dup hex-output-metadata-first-entry 0 swap ! ;
 
 ~ (pointer -- boolean)
@@ -2790,6 +2792,10 @@ allocate-transformation-state s" transformation-state" variable
 ~   This overwrites whatever entries currently exist with the new ones, and
 ~ adds a zero delimiter at the end.
 ~
+~   In addition to setting up the entries, we also set latest-output-point to
+~ the start of the label-loop buffer here. This needs to happen at the start
+~ of each pass, so although it doesn't quite fit the name, we do it here.
+~
 ~   It requires the header to have already been initialized; in particular it
 ~ requires the label-loop buffer start pointer to be valid.
 : create-hex-output-metadata-top-of-file-entries
@@ -2798,6 +2804,18 @@ allocate-transformation-state s" transformation-state" variable
   swap hex-output-metadata-first-entry
   ~ (label buffer start, metadata output point)
 
+  ~   This likely won't affect anything, because every call to pack*
+  ~ overwrites it, but we initialize the latest output point to the start of
+  ~ the label loop buffer here. That way, in the event the very first thing in
+  ~ the output is something that's supposed to have a special comment on it,
+  ~ hex-tilde-replacement will know where to attach the metadata entry.
+  ~
+  ~   If that was word soup, go read about hex-tilde-replacement and
+  ~ hex-tilde-alternate and come back. :)
+  over
+  transformation-state transformation-state-output-metadata @
+  hex-output-metadata-latest-output-point !
+
   over 0 hex-output-metadata-entry-type-line-comment
   s"   This file is a commented hexadecimal representation of a compiled "
   pack-hex-output-metadata-entry
@@ -2975,9 +2993,14 @@ allocate-transformation-state s" transformation-state" variable
 
 ~ (data start, data length)
 : hex-pack-trace
-  hex-output-metadata-entry-type-line-comment
-  s" pack"
-  add-hex-output-metadata-entry ;
+  ~   We keep a running tally of the next address in the output, for the
+  ~ benefit of hex-tilde-replacement, which needs to know where to attach its
+  ~ metadata entries.
+  +
+  dup is-in-label-loop-buffer {
+    transformation-state transformation-state-output-metadata @
+    hex-output-metadata-latest-output-point !
+  } { drop } if-else ;
 
 ~   By overriding colon, we can special-case the definitions of particular
 ~ words. It's very metacircular.
@@ -3630,7 +3653,12 @@ allocate-transformation-state s" transformation-state" variable
 ~
 ~ (string pointer)
 : hex-tilde-replacement
-  ." hmmm " emitstring newline ;
+  transformation-state transformation-state-output-metadata @
+  hex-output-metadata-latest-output-point @
+  0
+  hex-output-metadata-entry-type-line-comment
+  4 roll
+  add-hex-output-metadata-entry ;
 
 
 ~   The tilde alternate is a very important word for the hex transform; it has