Skip to content

Commit 0863a4f

Browse files
committed
Overhaul spill parameter filling
1 parent d57eda0 commit 0863a4f

4 files changed

Lines changed: 125 additions & 131 deletions

File tree

ext/lang/test.scm

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -519,57 +519,44 @@
519519
'(#x08 #x07 #x06 #x05 #x04 #x03 #x02 #x01)
520520
(u8vector->list b))))
521521

522-
;; --- asm-template :postamble ---
522+
;; --- link-template :postamble ---
523523

524-
;; No postamble: bytes size equals assembled code size.
525-
(let ()
526-
(define tmpl (asm-template '((ret)) :postamble 0))
527-
(receive (b _) (link-template tmpl '())
528-
(test* "asm-template :postamble 0"
524+
;; :postamble 0: no change to output size.
525+
(let ([tmpl (asm-template '((ret)))])
526+
(receive (b _) (link-template tmpl '() :postamble 0)
527+
(test* "link-template :postamble 0"
529528
'(#xc3)
530529
(u8vector->list b))))
531530

532-
;; :postamble 8: eight zero bytes appended after the code.
533-
(let ()
534-
(define tmpl (asm-template '((ret)) :postamble 8))
535-
(receive (b _) (link-template tmpl '())
536-
(test* "asm-template :postamble 8 size"
531+
;; :postamble 8: eight zero bytes appended after the linked bytes.
532+
(let ([tmpl (asm-template '((ret)))])
533+
(receive (b _) (link-template tmpl '() :postamble 8)
534+
(test* "link-template :postamble 8 size"
537535
9
538536
(uvector-size b))
539-
(test* "asm-template :postamble 8 content"
537+
(test* "link-template :postamble 8 content"
540538
'(#xc3 0 0 0 0 0 0 0 0)
541539
(u8vector->list b))))
542540

543-
;; :postamble preserves labels (labels point into code, not postamble).
544-
(let ()
545-
(define tmpl (asm-template '(entry: (ret)) :postamble 4))
546-
(receive (_ labels) (link-template tmpl '())
547-
(test* "asm-template :postamble labels preserved"
548-
'((entry: . 0))
549-
labels)))
550-
551-
;; .align at end of insn list pads code; postamble follows the aligned size.
552-
;; ret (1 byte) + .align 8 (7 padding bytes) = 8 bytes of code.
553-
(let ()
554-
(define tmpl (asm-template '((ret) (.align 8)) :postamble 8))
555-
(receive (b _) (link-template tmpl '())
556-
(test* "asm-template ends with .align + :postamble"
557-
(append '(#xc3 0 0 0 0 0 0 0) ; ret + 7 align bytes
558-
'(0 0 0 0 0 0 0 0)) ; 8-byte postamble
559-
(u8vector->list b))))
560-
561-
;; offset-form can fill into the postamble region using a patch as anchor.
562-
;; Template: (.dataq :anchor) [8 bytes] + postamble [8 bytes] = 16 bytes.
563-
;; :anchor patch is at offset 0; extra-offset 8 reaches into the postamble.
564-
(let ()
565-
(define tmpl (asm-template '((.dataq :anchor)) :postamble 8))
541+
;; :postamble region can be filled via offset form.
542+
;; Template: (.dataq :anchor) at offset 0 (8 bytes); postamble adds 8 more.
543+
;; Offset 8 from :anchor reaches into the postamble.
544+
(let ([tmpl (asm-template '((.dataq :anchor)))])
566545
(receive (b _) (link-template tmpl `((:anchor ,<uint64> #xfeedface00000000 0)
567-
(:anchor ,<uint32> #xdeadbeef 8)))
568-
(test* "asm-template :postamble filled via offset form"
569-
(append '(#x00 #x00 #x00 #x00 #xce #xfa #xed #xfe) ; .dataq
570-
'(#xef #xbe #xad #xde 0 0 0 0)) ; postamble
546+
(:anchor ,<uint32> #xdeadbeef 8))
547+
:postamble 8)
548+
(test* "link-template :postamble filled via offset form"
549+
(append '(#x00 #x00 #x00 #x00 #xce #xfa #xed #xfe)
550+
'(#xef #xbe #xad #xde 0 0 0 0))
571551
(u8vector->list b))))
572552

553+
;; labels are unchanged regardless of :postamble.
554+
(let ([tmpl (asm-template '(entry: (ret)))])
555+
(receive (_ labels) (link-template tmpl '() :postamble 4)
556+
(test* "link-template :postamble labels unchanged"
557+
'((entry: . 0))
558+
labels)))
559+
573560
;; --- offset parameter form (keyword native-type value extra-offset) ---
574561

575562
;; Template: 16 zero bytes, one 8-byte patch at offset 0 for :base.

lib/lang/asm/object.scm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,23 @@
112112
(hash-table-set! *patch-handlers* key handler))
113113

114114
;; link-template :: <obj-template>, [(keyword native-type value [extra-offset])]
115+
;; [#:postamble <int>]
115116
;; -> u8vector, AList
116117
;; Applies patches from PARAMS and returns the finalized byte vector and
117118
;; label alist. The template is never mutated; a fresh u8vector is returned.
119+
;; If POSTAMBLE > 0, the returned vector is extended by that many zero bytes
120+
;; beyond the template's own bytes (useful for per-call data regions).
118121
;; Each param entry is either:
119122
;; (keyword native-type value) -- fill at the patch's own offset
120123
;; (keyword native-type value extra-offset) -- fill at patch-offset + extra-offset
121124
;; (keyword c-array-type values) -- fill array starting at patch's offset
122125
;; (keyword c-array-type values extra-offset) -- fill array starting at patch-offset + extra-offset
123126
;; The offset form lets callers fill locations derived from a named anchor.
124-
(define (link-template tmpl params)
125-
(let* ([bytes (u8vector-copy (~ tmpl'bytes))]
126-
[len (uvector-size bytes)])
127+
(define (link-template tmpl params :key (postamble 0))
128+
(let* ([base (~ tmpl'bytes)]
129+
[len (+ (uvector-size base) postamble)]
130+
[bytes (rlet1 v (make-u8vector len 0)
131+
(u8vector-copy! v 0 base))])
127132

128133
;; Bounds-check ACTUAL then fill VALUE of NTYPE at that position.
129134
(define (checked-fill! kw actual ntype val)

libsrc/lang/asm/x86_64.scm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,17 @@
8888
;; Entry and x86 ISA definitions (subset)
8989
;;
9090

91-
;; asm-template :: [Insn] :postamble Int -> <obj-template>
91+
;; asm-template :: [Insn] -> <obj-template>
9292
;; First stage of assembly. Builds the full byte sequence with zeros at
9393
;; placeholder holes, and records where each hole is.
9494
;; :postamble N appends N zero bytes after the assembled code, making the
9595
;; returned bytes larger without adding any patches or labels there.
96-
(define (asm-template insns :key (postamble 0))
96+
(define (asm-template insns)
9797
(let* ([a-map (run-pass1 insns)]
9898
[acc (list '())] ; mutable cell: (list <patch-list>)
9999
[bss (parameterize ([patch-collector acc])
100100
(run-pass2 a-map))]
101-
[padding (make-list postamble 0)]
102-
[code (list->u8vector (concatenate (append bss (list padding))))]
101+
[code (list->u8vector (concatenate bss))]
103102
[labels (filter-map (^p (and (symbol? (car p)) p)) a-map)]
104103
[patches (car acc)])
105104
(make-obj-template code labels patches)))

0 commit comments

Comments
 (0)