1 ;;; Guile RTL assembler
3 ;;; Copyright (C) 2001, 2009, 2010, 2012, 2013 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;;; This module implements an assembler that creates an ELF image from
22 ;;; RTL assembly and macro-assembly. The input can be given in
23 ;;; s-expression form, like ((OP ARG ...) ...). Internally there is a
24 ;;; procedural interface, the emit-OP procedures, but that is not
25 ;;; currently exported.
27 ;;; "Primitive instructions" correspond to RTL VM operations.
28 ;;; Assemblers for primitive instructions are generated programmatically
29 ;;; from (rtl-instruction-list), which itself is derived from the VM
30 ;;; sources. There are also "macro-instructions" like "label" or
31 ;;; "load-constant" that expand to 0 or more primitive instructions.
33 ;;; The assembler also handles some higher-level tasks, like creating
34 ;;; the symbol table, other metadata sections, creating a constant table
35 ;;; for the whole compilation unit, and writing the dynamic section of
36 ;;; the ELF file along with the appropriate initialization routines.
38 ;;; Most compilers will want to use the trio of make-assembler,
39 ;;; emit-text, and link-assembly. That will result in the creation of
40 ;;; an ELF image as a bytevector, which can then be loaded using
41 ;;; load-thunk-from-memory, or written to disk as a .go file.
45 (define-module (system vm assembler)
46 #:use-module (system base target)
47 #:use-module (system vm instruction)
48 #:use-module (system vm dwarf)
49 #:use-module (system vm elf)
50 #:use-module (system vm linker)
51 #:use-module (system vm objcode)
52 #:use-module (rnrs bytevectors)
53 #:use-module (ice-9 binary-ports)
54 #:use-module (ice-9 vlist)
55 #:use-module (ice-9 match)
56 #:use-module (srfi srfi-1)
57 #:use-module (srfi srfi-4)
58 #:use-module (srfi srfi-9)
59 #:use-module (srfi srfi-11)
60 #:export (make-assembler
68 ;;; RTL code consists of 32-bit units, often subdivided in some way.
69 ;;; These helpers create one 32-bit unit from multiple components.
71 (define-syntax-rule (pack-u8-u24 x y)
74 (define-syntax-rule (pack-u8-s24 x y)
80 (else (error "out of range" y)))
83 (define-syntax-rule (pack-u1-u7-u24 x y z)
84 (logior x (ash y 1) (ash z 8)))
86 (define-syntax-rule (pack-u8-u12-u12 x y z)
87 (logior x (ash y 8) (ash z 20)))
89 (define-syntax-rule (pack-u8-u8-u16 x y z)
90 (logior x (ash y 8) (ash z 16)))
92 (define-syntax-rule (pack-u8-u8-u8-u8 x y z w)
93 (logior x (ash y 8) (ash z 16) (ash w 24)))
95 (define-syntax pack-flags
97 ;; Add clauses as needed.
98 ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
99 (if f2 (ash 2 0) 0)))))
101 ;;; Helpers to read and write 32-bit units in a buffer.
103 (define-syntax-rule (u32-ref buf n)
104 (bytevector-u32-native-ref buf (* n 4)))
106 (define-syntax-rule (u32-set! buf n val)
107 (bytevector-u32-native-set! buf (* n 4) val))
109 (define-syntax-rule (s32-ref buf n)
110 (bytevector-s32-native-ref buf (* n 4)))
112 (define-syntax-rule (s32-set! buf n val)
113 (bytevector-s32-native-set! buf (* n 4) val))
118 ;;; A <meta> entry collects metadata for one procedure. Procedures are
119 ;;; written as contiguous ranges of RTL code.
121 (define-syntax-rule (assert-match arg pattern kind)
123 (unless (match x (pattern #t) (_ #f))
124 (error (string-append "expected " kind) x))))
126 (define-record-type <meta>
127 (%make-meta label properties low-pc high-pc arities)
130 (properties meta-properties set-meta-properties!)
132 (high-pc meta-high-pc set-meta-high-pc!)
133 (arities meta-arities set-meta-arities!))
135 (define (make-meta label properties low-pc)
136 (assert-match label (? symbol?) "symbol")
137 (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
138 (%make-meta label properties low-pc #f '()))
140 (define (meta-name meta)
141 (assq-ref (meta-properties meta) 'name))
143 ;; Metadata for one <lambda-case>.
144 (define-record-type <arity>
145 (make-arity req opt rest kw-indices allow-other-keys?
151 (kw-indices arity-kw-indices)
152 (allow-other-keys? arity-allow-other-keys?)
153 (low-pc arity-low-pc)
154 (high-pc arity-high-pc set-arity-high-pc!))
156 (define-syntax *block-size* (identifier-syntax 32))
158 ;;; An assembler collects all of the words emitted during assembly, and
159 ;;; also maintains ancillary information such as the constant table, a
160 ;;; relocation list, and so on.
162 ;;; RTL code consists of 32-bit units. We emit RTL code using native
163 ;;; endianness. If we're targeting a foreign endianness, we byte-swap
164 ;;; the bytevector as a whole instead of conditionalizing each access.
166 (define-record-type <asm>
167 (make-asm cur idx start prev written
171 shstrtab next-section-number
175 ;; We write RTL code into what is logically a growable vector,
176 ;; implemented as a list of blocks. asm-cur is the current block, and
177 ;; asm-idx is the current index into that block, in 32-bit units.
179 (cur asm-cur set-asm-cur!)
180 (idx asm-idx set-asm-idx!)
182 ;; asm-start is an absolute position, indicating the offset of the
183 ;; beginning of an instruction (in u32 units). It is updated after
184 ;; writing all the words for one primitive instruction. It models the
185 ;; position of the instruction pointer during execution, given that
186 ;; the RTL VM updates the IP only at the end of executing the
187 ;; instruction, and is thus useful for computing offsets between two
188 ;; points in a program.
190 (start asm-start set-asm-start!)
192 ;; The list of previously written blocks.
194 (prev asm-prev set-asm-prev!)
196 ;; The number of u32 words written in asm-prev, which is the same as
197 ;; the offset of the current block.
199 (written asm-written set-asm-written!)
201 ;; An alist of symbol -> position pairs, indicating the labels defined
202 ;; in this compilation unit.
204 (labels asm-labels set-asm-labels!)
206 ;; A list of relocations needed by the program text. We use an
207 ;; internal representation for relocations, and handle textualn
208 ;; relative relocations in the assembler. Other kinds of relocations
209 ;; are later reified as linker relocations and resolved by the linker.
211 (relocs asm-relocs set-asm-relocs!)
213 ;; Target information.
215 (word-size asm-word-size)
216 (endianness asm-endianness)
218 ;; The constant table, as a vhash of object -> label. All constants
219 ;; get de-duplicated and written into separate sections -- either the
220 ;; .rodata section, for read-only data, or .data, for constants that
221 ;; need initialization at load-time (like symbols). Constants can
222 ;; depend on other constants (e.g. a symbol depending on a stringbuf),
223 ;; so order in this table is important.
225 (constants asm-constants set-asm-constants!)
227 ;; A list of RTL instructions needed to initialize the constants.
228 ;; Will run in a thunk with 2 local variables.
230 (inits asm-inits set-asm-inits!)
232 ;; The shstrtab, for section names.
234 (shstrtab asm-shstrtab set-asm-shstrtab!)
236 ;; The section number for the next section to be written.
238 (next-section-number asm-next-section-number set-asm-next-section-number!)
240 ;; A list of <meta>, corresponding to procedure metadata.
242 (meta asm-meta set-asm-meta!)
244 ;; A list of (pos . source) pairs, indicating source information. POS
245 ;; is relative to the beginning of the text section, and SOURCE is in
246 ;; the same format that source-properties returns.
248 (sources asm-sources set-asm-sources!))
250 (define-inlinable (fresh-block)
251 (make-u32vector *block-size*))
253 (define* (make-assembler #:key (word-size (target-word-size))
254 (endianness (target-endianness)))
255 "Create an assembler for a given target @var{word-size} and
256 @var{endianness}, falling back to appropriate values for the configured
258 (make-asm (fresh-block) 0 0 '() 0
262 (make-string-table) 1
265 (define (intern-section-name! asm string)
266 "Add a string to the section name table (shstrtab)."
267 (string-table-intern! (asm-shstrtab asm) string))
269 (define-inlinable (asm-pos asm)
270 "The offset of the next word to be written into the code buffer, in
272 (+ (asm-idx asm) (asm-written asm)))
274 (define (allocate-new-block asm)
275 "Close off the current block, and arrange for the next word to be
276 written to a fresh block."
277 (let ((new (fresh-block)))
278 (set-asm-prev! asm (cons (asm-cur asm) (asm-prev asm)))
279 (set-asm-written! asm (asm-pos asm))
280 (set-asm-cur! asm new)
281 (set-asm-idx! asm 0)))
283 (define-inlinable (emit asm u32)
284 "Emit one 32-bit word into the instruction stream. Assumes that there
285 is space for the word, and ensures that there is space for the next
287 (u32-set! (asm-cur asm) (asm-idx asm) u32)
288 (set-asm-idx! asm (1+ (asm-idx asm)))
289 (if (= (asm-idx asm) *block-size*)
290 (allocate-new-block asm)))
292 (define-inlinable (make-reloc type label base word)
293 "Make an internal relocation of type @var{type} referencing symbol
294 @var{label}, @var{word} words after position @var{start}. @var{type}
295 may be x8-s24, indicating a 24-bit relative label reference that can be
296 fixed up by the assembler, or s32, indicating a 32-bit relative
297 reference that needs to be fixed up by the linker."
298 (list type label base word))
300 (define-inlinable (reset-asm-start! asm)
301 "Reset the asm-start after writing the words for one instruction."
302 (set-asm-start! asm (asm-pos asm)))
304 (define (emit-exported-label asm label)
305 "Define a linker symbol associating @var{label} with the current
307 (set-asm-labels! asm (acons label (asm-start asm) (asm-labels asm))))
309 (define (record-label-reference asm label)
310 "Record an x8-s24 local label reference. This value will get patched
311 up later by the assembler."
312 (let* ((start (asm-start asm))
314 (reloc (make-reloc 'x8-s24 label start (- pos start))))
315 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
317 (define* (record-far-label-reference asm label #:optional (offset 0))
318 "Record an s32 far label reference. This value will get patched up
319 later by the linker."
320 (let* ((start (- (asm-start asm) offset))
322 (reloc (make-reloc 's32 label start (- pos start))))
323 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
329 ;;; Primitive assemblers are defined by expanding `assembler' for each
330 ;;; opcode in `(rtl-instruction-list)'.
333 (eval-when (expand compile load eval)
334 (define (id-append ctx a b)
335 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b)))))
337 (define-syntax assembler
339 (define-syntax op-case
342 ((_ asm name ((type arg ...) code ...) clause ...)
343 #`(if (eq? name 'type)
344 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
347 (op-case asm name clause ...)))
349 #'(error "unmatched name" name)))))
351 (define (pack-first-word asm opcode type)
352 (with-syntax ((opcode opcode))
358 (emit asm (pack-u8-u24 opcode arg)))
360 (record-label-reference asm label)
363 (emit asm (pack-u8-u8-u16 opcode a (object-address imm))))
365 (emit asm (pack-u8-u12-u12 opcode a b)))
367 (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
369 (define (pack-tail-word asm type)
373 (emit asm (pack-u8-u24 a b)))
375 (record-label-reference asm label)
378 (emit asm (pack-u8-u8-u16 a b (object-address imm))))
380 (emit asm (pack-u8-u12-u12 a b c)))
381 ((U8_U8_U8_U8 a b c d)
382 (emit asm (pack-u8-u8-u8-u8 a b c d)))
386 (let ((val (object-address imm)))
387 (unless (zero? (ash val -32))
388 (error "FIXME: enable truncation of negative fixnums when cross-compiling"))
391 (unless (= (asm-word-size asm) 8)
392 (error "make-long-immediate unavailable for this target"))
393 (emit asm (ash (object-address imm) -32))
394 (emit asm (logand (object-address imm) (1- (ash 1 32)))))
397 (record-far-label-reference asm label)
400 (record-far-label-reference asm label)
403 (record-far-label-reference asm label)
406 (record-far-label-reference asm label
407 (* offset (/ (asm-word-size asm) 4)))
410 (emit asm (pack-u8-u24 0 a)))
412 (emit asm (pack-u8-u12-u12 0 a b)))
414 (record-label-reference asm label)
417 (record-label-reference asm label)
418 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
419 ((B1_U7_L24 a b label)
420 (record-label-reference asm label)
421 (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
423 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
425 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))))
428 ((_ name opcode word0 word* ...)
429 (with-syntax ((((formal0 ...)
431 (pack-first-word #'asm
432 (syntax->datum #'opcode)
433 (syntax->datum #'word0)))
436 (map (lambda (word) (pack-tail-word #'asm word))
437 (syntax->datum #'(word* ...)))))
438 #'(lambda (asm formal0 ... formal* ... ...)
439 (unless (asm? asm) (error "not an asm"))
442 (reset-asm-start! asm)))))))
444 (define assemblers (make-hash-table))
446 (define-syntax define-assembler
449 ((_ name opcode kind arg ...)
450 (with-syntax ((emit (id-append #'name #'emit- #'name)))
453 (let ((emit (assembler name opcode arg ...)))
454 (hashq-set! assemblers 'name emit)
458 (define-syntax visit-opcodes
461 ((visit-opcodes macro arg ...)
462 (with-syntax (((inst ...)
463 (map (lambda (x) (datum->syntax #'macro x))
464 (rtl-instruction-list))))
466 (macro arg ... . inst)
469 (visit-opcodes define-assembler)
471 (define (emit-text asm instructions)
472 "Assemble @var{instructions} using the assembler @var{asm}.
473 @var{instructions} is a sequence of RTL instructions, expressed as a
474 list of lists. This procedure can be called many times before calling
475 @code{link-assembly}."
476 (for-each (lambda (inst)
477 (apply (or (hashq-ref assemblers (car inst))
478 (error 'bad-instruction inst))
486 ;;; The constant table records a topologically sorted set of literal
487 ;;; constants used by a program. For example, a pair uses its car and
488 ;;; cdr, a string uses its stringbuf, etc.
490 ;;; Some things we want to add to the constant table are not actually
491 ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
492 ;;; references, or cache cells for non-closure procedures. For these we
493 ;;; define special record types and add instances of those record types
497 (define-inlinable (immediate? x)
498 "Return @code{#t} if @var{x} is immediate, and @code{#f} otherwise."
499 (not (zero? (logand (object-address x) 6))))
501 (define-record-type <stringbuf>
502 (make-stringbuf string)
504 (string stringbuf-string))
506 (define-record-type <static-procedure>
507 (make-static-procedure code)
509 (code static-procedure-code))
511 (define-record-type <uniform-vector-backing-store>
512 (make-uniform-vector-backing-store bytes)
513 uniform-vector-backing-store?
514 (bytes uniform-vector-backing-store-bytes))
516 (define-record-type <cache-cell>
517 (make-cache-cell scope key)
519 (scope cache-cell-scope)
520 (key cache-cell-key))
522 (define (simple-vector? obj)
524 (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
526 (define (simple-uniform-vector? obj)
528 (symbol? (array-type obj))
529 (equal? (array-shape obj) (list (list 0 (1- (array-length obj)))))))
531 (define (statically-allocatable? x)
532 "Return @code{#t} if a non-immediate constant can be allocated
533 statically, and @code{#f} if it would need some kind of runtime
535 (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
537 (define (intern-constant asm obj)
538 "Add an object to the constant table, and return a label that can be
539 used to reference it. If the object is already present in the constant
540 table, its existing label is used directly."
542 (intern-constant asm obj))
543 (define (field dst n obj)
544 (let ((src (recur obj)))
546 (if (statically-allocatable? obj)
547 `((static-patch! ,dst ,n ,src))
548 `((static-ref 1 ,src)
549 (static-set! 1 ,dst ,n)))
551 (define (intern obj label)
554 (append (field label 0 (car obj))
555 (field label 1 (cdr obj))))
556 ((simple-vector? obj)
557 (let lp ((i 0) (inits '()))
558 (if (< i (vector-length obj))
560 (append-reverse (field label (1+ i) (vector-ref obj i))
563 ((stringbuf? obj) '())
564 ((static-procedure? obj)
565 `((static-patch! ,label 1 ,(static-procedure-code obj))))
566 ((cache-cell? obj) '())
568 `((make-non-immediate 1 ,(recur (symbol->string obj)))
570 (static-set! 1 ,label 0)))
572 `((static-patch! ,label 1 ,(recur (make-stringbuf obj)))))
574 `((static-ref 1 ,(recur (keyword->symbol obj)))
575 (symbol->keyword 1 1)
576 (static-set! 1 ,label 0)))
578 `((make-non-immediate 1 ,(recur (number->string obj)))
580 (static-set! 1 ,label 0)))
581 ((uniform-vector-backing-store? obj) '())
582 ((simple-uniform-vector? obj)
583 `((static-patch! ,label 2
584 ,(recur (make-uniform-vector-backing-store obj)))))
586 (error "don't know how to intern" obj))))
588 ((immediate? obj) #f)
589 ((vhash-assoc obj (asm-constants asm)) => cdr)
591 ;; Note that calling intern may mutate asm-constants and
592 ;; asm-constant-inits.
593 (let* ((label (gensym "constant"))
594 (inits (intern obj label)))
595 (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
596 (set-asm-inits! asm (append-reverse inits (asm-inits asm)))
599 (define (intern-non-immediate asm obj)
600 "Intern a non-immediate into the constant table, and return its
602 (when (immediate? obj)
603 (error "expected a non-immediate" obj))
604 (intern-constant asm obj))
606 (define (intern-cache-cell asm scope key)
607 "Intern a cache cell into the constant table, and return its label.
608 If there is already a cache cell with the given scope and key, it is
610 (intern-constant asm (make-cache-cell scope key)))
612 ;; Return the label of the cell that holds the module for a scope.
613 (define (intern-module-cache-cell asm scope)
614 "Intern a cache cell for a module, and return its label."
615 (intern-cache-cell asm scope #t))
621 ;;; Macro assemblers bridge the gap between primitive instructions and
622 ;;; some higher-level operations.
625 (define-syntax define-macro-assembler
628 ((_ (name arg ...) body body* ...)
629 (with-syntax ((emit (id-append #'name #'emit- #'name)))
632 (let ((emit (lambda (arg ...) body body* ...)))
633 (hashq-set! assemblers 'name emit)
637 (define-macro-assembler (load-constant asm dst obj)
640 (let ((bits (object-address obj)))
642 ((and (< dst 256) (zero? (ash bits -16)))
643 (emit-make-short-immediate asm dst obj))
644 ((zero? (ash bits -32))
645 (emit-make-long-immediate asm dst obj))
647 (emit-make-long-long-immediate asm dst obj)))))
648 ((statically-allocatable? obj)
649 (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
651 (emit-static-ref asm dst (intern-non-immediate asm obj)))))
653 (define-macro-assembler (load-static-procedure asm dst label)
654 (let ((loc (intern-constant asm (make-static-procedure label))))
655 (emit-make-non-immediate asm dst loc)))
657 (define-syntax-rule (define-tc7-macro-assembler name tc7)
658 (define-macro-assembler (name asm slot invert? label)
659 (emit-br-if-tc7 asm slot invert? tc7 label)))
661 ;; Keep in sync with tags.h. Part of Guile's ABI. Currently unused
662 ;; macro assemblers are commented out. See also
663 ;; *branching-primcall-arities* in (language cps primitives), the set of
664 ;; macro-instructions in assembly.scm, and
665 ;; disassembler.scm:code-annotation.
667 ;; FIXME: Define all tc7 values in Scheme in one place, derived from
669 (define-tc7-macro-assembler br-if-symbol 5)
670 (define-tc7-macro-assembler br-if-variable 7)
671 (define-tc7-macro-assembler br-if-vector 13)
672 ;(define-tc7-macro-assembler br-if-weak-vector 13)
673 (define-tc7-macro-assembler br-if-string 21)
674 ;(define-tc7-macro-assembler br-if-heap-number 23)
675 ;(define-tc7-macro-assembler br-if-stringbuf 39)
676 (define-tc7-macro-assembler br-if-bytevector 77)
677 ;(define-tc7-macro-assembler br-if-pointer 31)
678 ;(define-tc7-macro-assembler br-if-hashtable 29)
679 ;(define-tc7-macro-assembler br-if-fluid 37)
680 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
681 ;(define-tc7-macro-assembler br-if-frame 47)
682 ;(define-tc7-macro-assembler br-if-objcode 53)
683 ;(define-tc7-macro-assembler br-if-vm 55)
684 ;(define-tc7-macro-assembler br-if-vm-cont 71)
685 ;(define-tc7-macro-assembler br-if-rtl-program 69)
686 ;(define-tc7-macro-assembler br-if-program 79)
687 ;(define-tc7-macro-assembler br-if-weak-set 85)
688 ;(define-tc7-macro-assembler br-if-weak-table 87)
689 ;(define-tc7-macro-assembler br-if-array 93)
690 ;(define-tc7-macro-assembler br-if-bitvector 95)
691 ;(define-tc7-macro-assembler br-if-port 125)
692 ;(define-tc7-macro-assembler br-if-smob 127)
694 (define-macro-assembler (begin-program asm label properties)
695 (emit-label asm label)
696 (let ((meta (make-meta label properties (asm-start asm))))
697 (set-asm-meta! asm (cons meta (asm-meta asm)))))
699 (define-macro-assembler (end-program asm)
700 (let ((meta (car (asm-meta asm))))
701 (set-meta-high-pc! meta (asm-start asm))
702 (set-meta-arities! meta (reverse (meta-arities meta)))))
704 (define-macro-assembler (begin-standard-arity asm req nlocals alternate)
705 (emit-begin-opt-arity asm req '() #f nlocals alternate))
707 (define-macro-assembler (begin-opt-arity asm req opt rest nlocals alternate)
708 (emit-begin-kw-arity asm req opt rest '() #f nlocals alternate))
710 (define-macro-assembler (begin-kw-arity asm req opt rest kw-indices
711 allow-other-keys? nlocals alternate)
712 (assert-match req ((? symbol?) ...) "list of symbols")
713 (assert-match opt ((? symbol?) ...) "list of symbols")
714 (assert-match rest (or #f (? symbol?)) "#f or symbol")
715 (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
716 "alist of keyword -> integer")
717 (assert-match allow-other-keys? (? boolean?) "boolean")
718 (assert-match nlocals (? integer?) "integer")
719 (assert-match alternate (or #f (? symbol?)) "#f or symbol")
720 (let* ((meta (car (asm-meta asm)))
721 (arity (make-arity req opt rest kw-indices allow-other-keys?
723 ;; The procedure itself is in slot 0, in the standard calling
724 ;; convention. For procedure prologues, nreq includes the
725 ;; procedure, so here we add 1.
726 (nreq (1+ (length req)))
728 (rest? (->bool rest)))
729 (set-meta-arities! meta (cons arity (meta-arities meta)))
731 ((or allow-other-keys? (pair? kw-indices))
732 (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
734 ((or rest? (pair? opt))
735 (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
737 (emit-standard-prelude asm nreq nlocals alternate)))))
739 (define-macro-assembler (end-arity asm)
740 (let ((arity (car (meta-arities (car (asm-meta asm))))))
741 (set-arity-high-pc! arity (asm-start asm))))
743 (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
746 (emit-br-if-nargs-ne asm nreq alternate)
747 (emit-alloc-frame asm nlocals))
748 ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
749 (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
751 (emit-assert-nargs-ee asm nreq)
752 (emit-alloc-frame asm nlocals))))
754 (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
756 (emit-br-if-nargs-lt asm nreq alternate)
757 (emit-assert-nargs-ge asm nreq))
760 (emit-bind-rest asm (+ nreq nopt)))
762 (emit-br-if-nargs-gt asm (+ nreq nopt) alternate))
764 (emit-assert-nargs-le asm (+ nreq nopt))))
765 (emit-alloc-frame asm nlocals))
767 (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
768 allow-other-keys? nlocals alternate)
770 (emit-br-if-nargs-lt asm nreq alternate)
771 (emit-assert-nargs-ge asm nreq))
772 (let ((ntotal (fold (lambda (kw ntotal)
774 (((? keyword?) . idx)
775 (max (1+ idx) ntotal))))
776 (+ nreq nopt) kw-indices)))
777 ;; FIXME: port 581f410f
778 (emit-bind-kwargs asm nreq
779 (pack-flags allow-other-keys? rest?)
782 (intern-constant asm kw-indices))
783 (emit-alloc-frame asm nlocals)))
785 (define-macro-assembler (label asm sym)
786 (set-asm-labels! asm (acons sym (asm-start asm) (asm-labels asm))))
788 (define-macro-assembler (source asm source)
789 (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
791 (define-macro-assembler (cache-current-module! asm module scope)
792 (let ((mod-label (intern-module-cache-cell asm scope)))
793 (emit-static-set! asm module mod-label 0)))
795 (define-macro-assembler (cached-toplevel-box asm dst scope sym bound?)
796 (let ((sym-label (intern-non-immediate asm sym))
797 (mod-label (intern-module-cache-cell asm scope))
798 (cell-label (intern-cache-cell asm scope sym)))
799 (emit-toplevel-box asm dst cell-label mod-label sym-label bound?)))
801 (define-macro-assembler (cached-module-box asm dst module-name sym public? bound?)
802 (let* ((sym-label (intern-non-immediate asm sym))
803 (key (cons public? module-name))
804 (mod-name-label (intern-constant asm key))
805 (cell-label (intern-cache-cell asm key sym)))
806 (emit-module-box asm dst cell-label mod-name-label sym-label bound?)))
812 ;;; Helper for linking objects.
815 (define (make-object asm name bv relocs labels . kwargs)
816 "Make a linker object. This helper handles interning the name in the
817 shstrtab, assigning the size, allocating a fresh index, and defining a
818 corresponding linker symbol for the start of the section."
819 (let ((name-idx (intern-section-name! asm (symbol->string name)))
820 (index (asm-next-section-number asm)))
821 (set-asm-next-section-number! asm (1+ index))
822 (make-linker-object (apply make-elf-section
825 #:size (bytevector-length bv)
828 (cons (make-linker-symbol name 0) labels))))
834 ;;; Linking the constant table. This code is somewhat intertwingled
835 ;;; with the intern-constant code above, as that procedure also
836 ;;; residualizes instructions to initialize constants at load time.
839 (define (write-immediate asm buf pos x)
840 (let ((val (object-address x))
841 (endianness (asm-endianness asm)))
842 (case (asm-word-size asm)
843 ((4) (bytevector-u32-set! buf pos val endianness))
844 ((8) (bytevector-u64-set! buf pos val endianness))
845 (else (error "bad word size" asm)))))
847 (define (emit-init-constants asm)
848 "If there is writable data that needs initialization at runtime, emit
849 a procedure to do that and return its label. Otherwise return
851 (let ((inits (asm-inits asm)))
852 (and (not (null? inits))
853 (let ((label (gensym "init-constants")))
855 `((begin-program ,label ())
856 (assert-nargs-ee/locals 1 1)
858 (load-constant 1 ,*unspecified*)
863 (define (link-data asm data name)
864 "Link the static data for a program into the @var{name} section (which
865 should be .data or .rodata), and return the resulting linker object.
866 @var{data} should be a vhash mapping objects to labels."
867 (define (align address alignment)
869 (modulo (- alignment (modulo address alignment)) alignment)))
871 (define tc7-vector 13)
872 (define stringbuf-shared-flag #x100)
873 (define stringbuf-wide-flag #x400)
874 (define tc7-stringbuf 39)
875 (define tc7-narrow-stringbuf
876 (+ tc7-stringbuf stringbuf-shared-flag))
877 (define tc7-wide-stringbuf
878 (+ tc7-stringbuf stringbuf-shared-flag stringbuf-wide-flag))
879 (define tc7-ro-string (+ 21 #x200))
880 (define tc7-rtl-program 69)
881 (define tc7-bytevector 77)
883 (let ((word-size (asm-word-size asm))
884 (endianness (asm-endianness asm)))
885 (define (byte-length x)
888 (let ((x (stringbuf-string x)))
890 (case (string-bytes-per-char x)
891 ((1) (1+ (string-length x)))
892 ((4) (* (1+ (string-length x)) 4))
893 (else (error "bad string bytes per char" x))))))
894 ((static-procedure? x)
901 (* (1+ (vector-length x)) word-size))
902 ((simple-uniform-vector? x)
904 ((uniform-vector-backing-store? x)
905 (bytevector-length (uniform-vector-backing-store-bytes x)))
909 (define (write-constant-reference buf pos x)
910 ;; The asm-inits will fix up any reference to a non-immediate.
911 (write-immediate asm buf pos (if (immediate? x) x #f)))
913 (define (write buf pos obj)
916 (let* ((x (stringbuf-string obj))
917 (len (string-length x))
918 (tag (if (= (string-bytes-per-char x) 1)
920 tc7-wide-stringbuf)))
923 (bytevector-u32-set! buf pos tag endianness)
924 (bytevector-u32-set! buf (+ pos 4) len endianness))
926 (bytevector-u64-set! buf pos tag endianness)
927 (bytevector-u64-set! buf (+ pos 8) len endianness))
929 (error "bad word size" asm)))
930 (let ((pos (+ pos (* word-size 2))))
931 (case (string-bytes-per-char x)
935 (let ((u8 (char->integer (string-ref x i))))
936 (bytevector-u8-set! buf (+ pos i) u8)
938 (bytevector-u8-set! buf (+ pos i) 0))))
942 (let ((u32 (char->integer (string-ref x i))))
943 (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
945 (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
946 (else (error "bad string bytes per char" x))))))
948 ((static-procedure? obj)
951 (bytevector-u32-set! buf pos tc7-rtl-program endianness)
952 (bytevector-u32-set! buf (+ pos 4) 0 endianness))
954 (bytevector-u64-set! buf pos tc7-rtl-program endianness)
955 (bytevector-u64-set! buf (+ pos 8) 0 endianness))
956 (else (error "bad word size"))))
959 (write-immediate asm buf pos #f))
962 (let ((tag (logior tc7-ro-string (ash (string-length obj) 8))))
965 (bytevector-u32-set! buf pos tc7-ro-string endianness)
966 (write-immediate asm buf (+ pos 4) #f) ; stringbuf
967 (bytevector-u32-set! buf (+ pos 8) 0 endianness)
968 (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
970 (bytevector-u64-set! buf pos tc7-ro-string endianness)
971 (write-immediate asm buf (+ pos 8) #f) ; stringbuf
972 (bytevector-u64-set! buf (+ pos 16) 0 endianness)
973 (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
974 (else (error "bad word size")))))
977 (write-constant-reference buf pos (car obj))
978 (write-constant-reference buf (+ pos word-size) (cdr obj)))
980 ((simple-vector? obj)
981 (let* ((len (vector-length obj))
982 (tag (logior tc7-vector (ash len 8))))
984 ((4) (bytevector-u32-set! buf pos tag endianness))
985 ((8) (bytevector-u64-set! buf pos tag endianness))
986 (else (error "bad word size")))
988 (when (< i (vector-length obj))
989 (let ((pos (+ pos word-size (* i word-size)))
990 (elt (vector-ref obj i)))
991 (write-constant-reference buf pos elt)
995 (write-immediate asm buf pos #f))
998 (write-immediate asm buf pos #f))
1001 (write-immediate asm buf pos #f))
1003 ((simple-uniform-vector? obj)
1004 (let ((tag (logior tc7-bytevector
1005 (ash (uniform-vector-element-type-code obj) 7))))
1008 (bytevector-u32-set! buf pos tag endianness)
1009 (bytevector-u32-set! buf (+ pos 4) (bytevector-length obj)
1010 endianness) ; length
1011 (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
1012 (write-immediate asm buf (+ pos 12) #f)) ; owner
1014 (bytevector-u64-set! buf pos tag endianness)
1015 (bytevector-u64-set! buf (+ pos 8) (bytevector-length obj)
1016 endianness) ; length
1017 (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
1018 (write-immediate asm buf (+ pos 24) #f)) ; owner
1019 (else (error "bad word size")))))
1021 ((uniform-vector-backing-store? obj)
1022 (let ((bv (uniform-vector-backing-store-bytes obj)))
1023 (bytevector-copy! bv 0 buf pos (bytevector-length bv))
1024 (unless (or (= 1 (uniform-vector-element-size bv))
1025 (eq? endianness (native-endianness)))
1026 ;; Need to swap units of element-size bytes
1027 (error "FIXME: Implement byte order swap"))))
1030 (error "unrecognized object" obj))))
1033 ((vlist-null? data) #f)
1035 (let* ((byte-len (vhash-fold (lambda (k v len)
1036 (+ (byte-length k) (align len 8)))
1038 (buf (make-bytevector byte-len 0)))
1039 (let lp ((i 0) (pos 0) (labels '()))
1040 (if (< i (vlist-length data))
1041 (let* ((pair (vlist-ref data i))
1043 (obj-label (cdr pair)))
1046 (align (+ (byte-length obj) pos) 8)
1047 (cons (make-linker-symbol obj-label pos) labels)))
1048 (make-object asm name buf '() labels
1050 ('.data (logior SHF_ALLOC SHF_WRITE))
1051 ('.rodata SHF_ALLOC))))))))))
1053 (define (link-constants asm)
1054 "Link sections to hold constants needed by the program text emitted
1057 Returns three values: an object for the .rodata section, an object for
1058 the .data section, and a label for an initialization procedure. Any of
1059 these may be @code{#f}."
1060 (define (shareable? x)
1064 (and (immediate? (car x)) (immediate? (cdr x))))
1067 (or (= i (vector-length x))
1068 (and (immediate? (vector-ref x i))
1070 ((uniform-vector-backing-store? x) #t)
1072 (let* ((constants (asm-constants asm))
1073 (len (vlist-length constants)))
1078 (values (link-data asm ro '.rodata)
1079 (link-data asm rw '.data)
1080 (emit-init-constants asm))
1081 (let ((pair (vlist-ref constants i)))
1082 (if (shareable? (car pair))
1083 (lp (1+ i) (vhash-consq (car pair) (cdr pair) ro) rw)
1084 (lp (1+ i) ro (vhash-consq (car pair) (cdr pair) rw))))))))
1089 ;;; Linking program text.
1092 (define (process-relocs buf relocs labels)
1093 "Patch up internal x8-s24 relocations, and any s32 relocations that
1094 reference symbols in the text section. Return a list of linker
1095 relocations for references to symbols defined outside the text section."
1097 (lambda (reloc tail)
1099 ((type label base word)
1100 (let ((abs (assq-ref labels label))
1101 (dst (+ base word)))
1105 (let ((rel (- abs base)))
1106 (s32-set! buf dst rel)
1108 (cons (make-linker-reloc 'rel32/4 (* dst 4) word label)
1112 (error "unbound near relocation" reloc))
1113 (let ((rel (- abs base))
1114 (u32 (u32-ref buf dst)))
1115 (u32-set! buf dst (pack-u8-s24 (logand u32 #xff) rel))
1117 (else (error "bad relocation kind" reloc)))))))
1121 (define (process-labels labels)
1122 "Define linker symbols for the label-offset pairs in @var{labels}.
1123 The offsets are expected to be expressed in words."
1125 (make-linker-symbol (car pair) (* (cdr pair) 4)))
1128 (define (swap-bytes! buf)
1129 "Patch up the text buffer @var{buf}, swapping the endianness of each
1131 (unless (zero? (modulo (bytevector-length buf) 4))
1132 (error "unexpected length"))
1133 (let ((byte-len (bytevector-length buf)))
1135 (unless (= pos byte-len)
1136 (bytevector-u32-set!
1138 (bytevector-u32-ref buf pos (endianness big))
1139 (endianness little))
1142 (define (link-text-object asm)
1143 "Link the .rtl-text section, swapping the endianness of the bytes if
1145 (let ((buf (make-u32vector (asm-pos asm))))
1146 (let lp ((pos 0) (prev (reverse (asm-prev asm))))
1148 (let ((byte-size (* (asm-idx asm) 4)))
1149 (bytevector-copy! (asm-cur asm) 0 buf pos byte-size)
1150 (unless (eq? (asm-endianness asm) (native-endianness))
1152 (make-object asm '.rtl-text
1154 (process-relocs buf (asm-relocs asm)
1156 (process-labels (asm-labels asm))))
1157 (let ((len (* *block-size* 4)))
1158 (bytevector-copy! (car prev) 0 buf pos len)
1159 (lp (+ pos len) (cdr prev)))))))
1165 ;;; Linking other sections of the ELF file, like the dynamic segment,
1166 ;;; the symbol table, etc.
1169 (define (link-dynamic-section asm text rw rw-init)
1170 "Link the dynamic section for an ELF image with RTL text, given the
1171 writable data section @var{rw} needing fixup from the procedure with
1172 label @var{rw-init}. @var{rw-init} may be false. If @var{rw} is true,
1173 it will be added to the GC roots at runtime."
1174 (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
1175 (let* ((endianness (asm-endianness asm))
1176 (bv (make-bytevector (* word-size (if rw (if rw-init 12 10) 6)) 0))
1179 (%set-uword! bv (* i word-size) uword endianness)))
1183 (set! relocs (cons (make-linker-reloc 'reloc-type
1184 (* i word-size) 0 label)
1186 (%set-uword! bv (* i word-size) 0 endianness))))
1187 (set-uword! 0 DT_GUILE_RTL_VERSION)
1188 (set-uword! 1 #x02020000)
1189 (set-uword! 2 DT_GUILE_ENTRY)
1190 (set-label! 3 '.rtl-text)
1194 (set-uword! 4 DT_GUILE_GC_ROOT)
1195 (set-label! 5 '.data)
1196 (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
1197 (set-uword! 7 (bytevector-length (linker-object-bv rw)))
1200 (set-uword! 8 DT_INIT) ; constants
1201 (set-label! 9 rw-init)
1202 (set-uword! 10 DT_NULL)
1205 (set-uword! 8 DT_NULL)
1208 (set-uword! 4 DT_NULL)
1210 (make-object asm '.dynamic bv relocs '()
1211 #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
1212 (case (asm-word-size asm)
1213 ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
1214 ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
1215 (else (error "bad word size" asm))))
1217 (define (link-shstrtab asm)
1218 "Link the string table for the section headers."
1219 (intern-section-name! asm ".shstrtab")
1220 (make-object asm '.shstrtab
1221 (link-string-table! (asm-shstrtab asm))
1223 #:type SHT_STRTAB #:flags 0))
1225 (define (link-symtab text-section asm)
1226 (let* ((endianness (asm-endianness asm))
1227 (word-size (asm-word-size asm))
1228 (size (elf-symbol-len word-size))
1229 (meta (reverse (asm-meta asm)))
1231 (strtab (make-string-table))
1232 (bv (make-bytevector (* n size) 0)))
1233 (define (intern-string! name)
1234 (string-table-intern! strtab (if name (symbol->string name) "")))
1237 (let ((name (intern-string! (meta-name meta))))
1238 (write-elf-symbol bv (* n size) endianness word-size
1241 ;; Symbol value and size are measured in
1243 #:value (* 4 (meta-low-pc meta))
1244 #:size (* 4 (- (meta-high-pc meta)
1245 (meta-low-pc meta)))
1247 #:visibility STV_HIDDEN
1248 #:shndx (elf-section-index text-section)))))
1250 (let ((strtab (make-object asm '.strtab
1251 (link-string-table! strtab)
1253 #:type SHT_STRTAB #:flags 0)))
1254 (values (make-object asm '.symtab
1257 #:type SHT_SYMTAB #:flags 0 #:entsize size
1258 #:link (elf-section-index
1259 (linker-object-section strtab)))
1262 ;;; The .guile.arities section describes the arities that a function can
1263 ;;; have. It is in two parts: a sorted array of headers describing
1264 ;;; basic arities, and an array of links out to a string table (and in
1265 ;;; the case of keyword arguments, to the data section) for argument
1266 ;;; names. The whole thing is prefixed by a uint32 indicating the
1267 ;;; offset of the end of the headers array.
1269 ;;; The arity headers array is a packed array of structures of the form:
1271 ;;; struct arity_header {
1272 ;;; uint32_t low_pc;
1273 ;;; uint32_t high_pc;
1274 ;;; uint32_t offset;
1280 ;;; All of the offsets and addresses are 32 bits. We can expand in the
1281 ;;; future to use 64-bit offsets if appropriate, but there are other
1282 ;;; aspects of RTL that constrain us to a total image that fits in 32
1283 ;;; bits, so for the moment we'll simplify the problem space.
1285 ;;; The following flags values are defined:
1288 ;;; #x2: allow-other-keys?
1289 ;;; #x4: has-keyword-args?
1290 ;;; #x8: is-case-lambda?
1292 ;;; Functions with a single arity specify their number of required and
1293 ;;; optional arguments in nreq and nopt, and do not have the
1294 ;;; is-case-lambda? flag set. Their "offset" member links to an array
1295 ;;; of pointers into the associated .guile.arities.strtab string table,
1296 ;;; identifying the argument names. This offset is relative to the
1297 ;;; start of the .guile.arities section. Links for required arguments
1298 ;;; are first, in order, as uint32 values. Next follow the optionals,
1299 ;;; then the rest link if has-rest? is set, then a link to the "keyword
1300 ;;; indices" literal if has-keyword-args? is set. Unlike the other
1301 ;;; links, the kw-indices link points into the data section, and is
1302 ;;; relative to the ELF image as a whole.
1304 ;;; Functions with no arities have no arities information present in the
1305 ;;; .guile.arities section.
1307 ;;; Functions with multiple arities are preceded by a header with
1308 ;;; is-case-lambda? set. All other fields are 0, except low-pc and
1309 ;;; high-pc which should be the bounds of the whole function. Headers
1310 ;;; for the individual arities follow. In this way the whole headers
1311 ;;; array is sorted in increasing low-pc order, and case-lambda clauses
1312 ;;; are contained within the [low-pc, high-pc] of the case-lambda
1315 ;; Length of the prefix to the arities section, in bytes.
1316 (define arities-prefix-len 4)
1318 ;; Length of an arity header, in bytes.
1319 (define arity-header-len (* 6 4))
1321 ;; The offset of "offset" within arity header, in bytes.
1322 (define arity-header-offset-offset (* 2 4))
1324 (define-syntax-rule (pack-arity-flags has-rest? allow-other-keys?
1325 has-keyword-args? is-case-lambda?)
1326 (logior (if has-rest? (ash 1 0) 0)
1327 (if allow-other-keys? (ash 1 1) 0)
1328 (if has-keyword-args? (ash 1 2) 0)
1329 (if is-case-lambda? (ash 1 3) 0)))
1331 (define (meta-arities-size meta)
1332 (define (lambda-size arity)
1334 (* 4 ;; name pointers
1335 (+ (length (arity-req arity))
1336 (length (arity-opt arity))
1337 (if (arity-rest arity) 1 0)
1338 (if (pair? (arity-kw-indices arity)) 1 0)))))
1339 (define (case-lambda-size arities)
1341 arity-header-len ;; case-lambda header
1342 (map lambda-size arities))) ;; the cases
1343 (match (meta-arities meta)
1345 ((arity) (lambda-size arity))
1346 (arities (case-lambda-size arities))))
1348 (define (write-arity-headers metas bv endianness)
1349 (define (write-arity-header* pos low-pc high-pc flags nreq nopt)
1350 (bytevector-u32-set! bv pos low-pc endianness)
1351 (bytevector-u32-set! bv (+ pos 4) high-pc endianness)
1352 (bytevector-u32-set! bv (+ pos 8) 0 endianness) ; offset
1353 (bytevector-u32-set! bv (+ pos 12) flags endianness)
1354 (bytevector-u32-set! bv (+ pos 16) nreq endianness)
1355 (bytevector-u32-set! bv (+ pos 20) nopt endianness))
1356 (define (write-arity-header pos arity)
1357 (write-arity-header* pos (arity-low-pc arity)
1358 (arity-high-pc arity)
1359 (pack-arity-flags (arity-rest arity)
1360 (arity-allow-other-keys? arity)
1361 (pair? (arity-kw-indices arity))
1363 (length (arity-req arity))
1364 (length (arity-opt arity))))
1365 (let lp ((metas metas) (pos arities-prefix-len) (offsets '()))
1368 ;; Fill in the prefix.
1369 (bytevector-u32-set! bv 0 pos endianness)
1370 (values pos (reverse offsets)))
1372 (match (meta-arities meta)
1373 (() (lp metas pos offsets))
1375 (write-arity-header pos arity)
1377 (+ pos arity-header-len)
1378 (acons arity (+ pos arity-header-offset-offset) offsets)))
1380 ;; Write a case-lambda header, then individual arities.
1381 ;; The case-lambda header's offset link is 0.
1382 (write-arity-header* pos (meta-low-pc meta) (meta-high-pc meta)
1383 (pack-arity-flags #f #f #f #t) 0 0)
1384 (let lp* ((arities arities) (pos (+ pos arity-header-len))
1387 (() (lp metas pos offsets))
1389 (write-arity-header pos arity)
1391 (+ pos arity-header-len)
1393 (+ pos arity-header-offset-offset)
1396 (define (write-arity-links asm bv pos arity-offset-pairs strtab)
1397 (define (write-symbol sym pos)
1398 (bytevector-u32-set! bv pos
1399 (string-table-intern! strtab (symbol->string sym))
1400 (asm-endianness asm))
1402 (define (write-kw-indices pos kw-indices)
1403 ;; FIXME: Assert that kw-indices is already interned.
1404 (make-linker-reloc 'abs32/1 pos 0
1405 (intern-constant asm kw-indices)))
1406 (let lp ((pos pos) (pairs arity-offset-pairs) (relocs '()))
1409 (unless (= pos (bytevector-length bv))
1410 (error "expected to fully fill the bytevector"
1411 pos (bytevector-length bv)))
1413 (((arity . offset) . pairs)
1414 (bytevector-u32-set! bv offset pos (asm-endianness asm))
1415 (let ((pos (fold write-symbol
1417 (append (arity-req arity)
1420 ((arity-rest arity) => list)
1422 (match (arity-kw-indices arity)
1423 (() (lp pos pairs relocs))
1427 (cons (write-kw-indices pos kw-indices) relocs)))))))))
1429 (define (link-arities asm)
1430 (let* ((endianness (asm-endianness asm))
1431 (metas (reverse (asm-meta asm)))
1432 (size (fold (lambda (meta size)
1433 (+ size (meta-arities-size meta)))
1436 (strtab (make-string-table))
1437 (bv (make-bytevector size 0)))
1438 (let ((kw-indices-relocs
1441 (write-arity-headers metas bv endianness))
1442 (lambda (pos arity-offset-pairs)
1443 (write-arity-links asm bv pos arity-offset-pairs strtab)))))
1444 (let ((strtab (make-object asm '.guile.arities.strtab
1445 (link-string-table! strtab)
1447 #:type SHT_STRTAB #:flags 0)))
1448 (values (make-object asm '.guile.arities
1450 kw-indices-relocs '()
1451 #:type SHT_PROGBITS #:flags 0
1452 #:link (elf-section-index
1453 (linker-object-section strtab)))
1457 ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
1458 ;;; values. Pc and str are both 32 bits wide. (Either could change to
1459 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1460 ;;; entry to a program, relative to the start of the text section, and
1461 ;;; str is an index into the associated .guile.docstrs.strtab string
1465 ;; The size of a docstrs entry, in bytes.
1466 (define docstr-size 8)
1468 (define (link-docstrs asm)
1469 (define (find-docstrings)
1470 (filter-map (lambda (meta)
1471 (define (is-documentation? pair)
1472 (eq? (car pair) 'documentation))
1473 (let* ((props (meta-properties meta))
1474 (tail (find-tail is-documentation? props)))
1476 (not (find-tail is-documentation? (cdr tail)))
1477 (string? (cdar tail))
1478 (cons (meta-low-pc meta) (cdar tail)))))
1479 (reverse (asm-meta asm))))
1480 (let* ((endianness (asm-endianness asm))
1481 (docstrings (find-docstrings))
1482 (strtab (make-string-table))
1483 (bv (make-bytevector (* (length docstrings) docstr-size) 0)))
1484 (fold (lambda (pair pos)
1487 (bytevector-u32-set! bv pos pc endianness)
1488 (bytevector-u32-set! bv (+ pos 4)
1489 (string-table-intern! strtab string)
1491 (+ pos docstr-size))))
1494 (let ((strtab (make-object asm '.guile.docstrs.strtab
1495 (link-string-table! strtab)
1497 #:type SHT_STRTAB #:flags 0)))
1498 (values (make-object asm '.guile.docstrs
1501 #:type SHT_PROGBITS #:flags 0
1502 #:link (elf-section-index
1503 (linker-object-section strtab)))
1507 ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
1508 ;;; values. Pc and addr are both 32 bits wide. (Either could change to
1509 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1510 ;;; entry to a program, relative to the start of the text section, and
1511 ;;; addr is the address of the associated properties alist, relative to
1512 ;;; the start of the ELF image.
1514 ;;; Since procedure properties are stored in the data sections, we need
1515 ;;; to link the procedures property section first. (Note that this
1516 ;;; constraint does not apply to the arities section, which may
1517 ;;; reference the data sections via the kw-indices literal, because
1518 ;;; assembling the text section already makes sure that the kw-indices
1522 ;; The size of a procprops entry, in bytes.
1523 (define procprops-size 8)
1525 (define (link-procprops asm)
1526 (define (assoc-remove-one alist key value-pred)
1529 ((((? (lambda (x) (eq? x key))) . value) . alist)
1530 (if (value-pred value)
1532 (acons key value alist)))
1534 (acons k v (assoc-remove-one alist key value-pred)))))
1535 (define (props-without-name-or-docstring meta)
1537 (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
1540 (define (find-procprops)
1541 (filter-map (lambda (meta)
1542 (let ((props (props-without-name-or-docstring meta)))
1544 (cons (meta-low-pc meta) props))))
1545 (reverse (asm-meta asm))))
1546 (let* ((endianness (asm-endianness asm))
1547 (procprops (find-procprops))
1548 (bv (make-bytevector (* (length procprops) procprops-size) 0)))
1549 (let lp ((procprops procprops) (pos 0) (relocs '()))
1552 (make-object asm '.guile.procprops
1555 #:type SHT_PROGBITS #:flags 0))
1556 (((pc . props) . procprops)
1557 (bytevector-u32-set! bv pos pc endianness)
1559 (+ pos procprops-size)
1560 (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
1561 (intern-constant asm props))
1565 ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
1566 ;;; sections provide line number and local variable liveness
1567 ;;; information. Their format is defined by the DWARF
1571 (define (asm-language asm)
1572 ;; FIXME: Plumb language through to the assembler.
1575 ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
1576 (define (link-debug asm)
1577 (define (put-s8 port val)
1578 (let ((bv (make-bytevector 1)))
1579 (bytevector-s8-set! bv 0 val)
1580 (put-bytevector port bv)))
1582 (define (put-u16 port val)
1583 (let ((bv (make-bytevector 2)))
1584 (bytevector-u16-set! bv 0 val (asm-endianness asm))
1585 (put-bytevector port bv)))
1587 (define (put-u32 port val)
1588 (let ((bv (make-bytevector 4)))
1589 (bytevector-u32-set! bv 0 val (asm-endianness asm))
1590 (put-bytevector port bv)))
1592 (define (put-u64 port val)
1593 (let ((bv (make-bytevector 8)))
1594 (bytevector-u64-set! bv 0 val (asm-endianness asm))
1595 (put-bytevector port bv)))
1597 (define (put-uleb128 port val)
1599 (let ((next (ash val -7)))
1603 (put-u8 port (logior #x80 (logand val #x7f)))
1606 (define (put-sleb128 port val)
1608 (if (<= 0 (+ val 64) 128)
1609 (put-u8 port (logand val #x7f))
1611 (put-u8 port (logior #x80 (logand val #x7f)))
1612 (lp (ash val -7))))))
1614 (define (port-position port)
1615 (seek port 0 SEEK_CUR))
1617 (define (meta->subprogram-die meta)
1621 => (lambda (name) `((name ,(symbol->string name)))))
1624 (low-pc ,(meta-label meta))
1625 (high-pc ,(* 4 (- (meta-high-pc meta) (meta-low-pc meta)))))))
1627 (define (make-compile-unit-die asm)
1629 (@ (producer ,(string-append "Guile " (version)))
1630 (language ,(asm-language asm))
1632 (high-pc ,(* 4 (asm-pos asm)))
1634 ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
1636 (let-values (((die-port get-die-bv) (open-bytevector-output-port))
1638 ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
1639 ;; (tag has-kids? attrs forms) -> code
1640 ((abbrevs) vlist-null)
1641 ((strtab) (make-string-table))
1642 ((line-port get-line-bv) (open-bytevector-output-port))
1645 ((files) vlist-null))
1647 (define (write-abbrev code tag has-children? attrs forms)
1648 (put-uleb128 abbrev-port code)
1649 (put-uleb128 abbrev-port (tag-name->code tag))
1650 (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
1651 (for-each (lambda (attr form)
1652 (put-uleb128 abbrev-port (attribute-name->code attr))
1653 (put-uleb128 abbrev-port (form-name->code form)))
1655 (put-uleb128 abbrev-port 0)
1656 (put-uleb128 abbrev-port 0))
1658 (define (intern-abbrev tag has-children? attrs forms)
1659 (let ((key (list tag has-children? attrs forms)))
1660 (match (vhash-assoc key abbrevs)
1662 (#f (let ((code (1+ (vlist-length abbrevs))))
1663 (set! abbrevs (vhash-cons key code abbrevs))
1664 (write-abbrev code tag has-children? attrs forms)
1667 (define (intern-file file)
1668 (match (vhash-assoc file files)
1670 (#f (let ((code (1+ (vlist-length files))))
1671 (set! files (vhash-cons file code files))
1674 (define (write-sources)
1675 ;; Choose line base and line range values that will allow for an
1676 ;; address advance range of 16 words. The special opcode range is
1677 ;; from 10 to 255, so 246 values.
1681 (let lp ((sources (asm-sources asm)) (out '()))
1683 (((pc . s) . sources)
1684 (let ((file (assq-ref s 'filename))
1685 (line (assq-ref s 'line))
1686 (col (assq-ref s 'column)))
1688 ;; Guile line and column numbers are 0-indexed, but
1689 ;; they are 1-indexed for DWARF.
1691 (if file (intern-file file) 0)
1696 ;; Compilation unit header for .debug_line. We write in
1697 ;; DWARF 2 format because more tools understand it than DWARF
1698 ;; 4, which incompatibly adds another field to this header.
1700 (put-u32 line-port 0) ; Length; will patch later.
1701 (put-u16 line-port 2) ; DWARF 2 format.
1702 (put-u32 line-port 0) ; Prologue length; will patch later.
1703 (put-u8 line-port 4) ; Minimum instruction length: 4 bytes.
1704 (put-u8 line-port 1) ; Default is-stmt: true.
1706 (put-s8 line-port base) ; Line base. See the DWARF standard.
1707 (put-u8 line-port range) ; Line range. See the DWARF standard.
1708 (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
1710 ;; A table of the number of uleb128 arguments taken by each
1711 ;; of the standard opcodes.
1712 (put-u8 line-port 0) ; 1: copy
1713 (put-u8 line-port 1) ; 2: advance-pc
1714 (put-u8 line-port 1) ; 3: advance-line
1715 (put-u8 line-port 1) ; 4: set-file
1716 (put-u8 line-port 1) ; 5: set-column
1717 (put-u8 line-port 0) ; 6: negate-stmt
1718 (put-u8 line-port 0) ; 7: set-basic-block
1719 (put-u8 line-port 0) ; 8: const-add-pc
1720 (put-u8 line-port 1) ; 9: fixed-advance-pc
1722 ;; Include directories, as a zero-terminated sequence of
1723 ;; nul-terminated strings. Nothing, for the moment.
1724 (put-u8 line-port 0)
1726 ;; File table. For each file that contributes to this
1727 ;; compilation unit, a nul-terminated file name string, and a
1728 ;; uleb128 for each of directory the file was found in, the
1729 ;; modification time, and the file's size in bytes. We pass
1730 ;; zero for the latter three fields.
1731 (vlist-for-each (match-lambda
1733 (put-bytevector line-port (string->utf8 file))
1734 (put-u8 line-port 0)
1735 (put-uleb128 line-port 0) ; directory
1736 (put-uleb128 line-port 0) ; mtime
1737 (put-uleb128 line-port 0) ; size
1740 (put-u8 line-port 0) ; 0 byte terminating file list.
1742 ;; Patch prologue length.
1743 (let ((offset (port-position line-port)))
1744 (seek line-port 6 SEEK_SET)
1745 (put-u32 line-port (- offset 10))
1746 (seek line-port offset SEEK_SET))
1748 ;; Now write the statement program.
1750 (define (extended-op opcode payload-len)
1751 (put-u8 line-port 0) ; extended op
1752 (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
1753 (put-uleb128 line-port opcode))
1754 (define (set-address sym)
1755 (define (add-reloc! kind)
1757 (cons (make-linker-reloc kind
1758 (port-position line-port)
1762 (match (asm-word-size asm)
1765 (add-reloc! 'abs32/1)
1766 (put-u32 line-port 0))
1769 (add-reloc! 'abs64/1)
1770 (put-u64 line-port 0))))
1771 (define (end-sequence pc)
1772 (let ((pc-inc (- (asm-pos asm) pc)))
1773 (put-u8 line-port 2) ; advance-pc
1774 (put-uleb128 line-port pc-inc))
1776 (define (advance-pc pc-inc line-inc)
1777 (let ((spec (+ (- line-inc base) (* pc-inc range) 10)))
1779 ((or (< line-inc base) (>= line-inc (+ base range)))
1780 (advance-line line-inc)
1781 (advance-pc pc-inc 0))
1783 (put-u8 line-port spec))
1785 (put-u8 line-port 8) ; const-advance-pc
1786 (advance-pc (- pc-inc (floor/ (- 255 10) range))
1789 (put-u8 line-port 2) ; advance-pc
1790 (put-uleb128 line-port pc-inc)
1791 (advance-pc 0 line-inc)))))
1792 (define (advance-line inc)
1793 (put-u8 line-port 3)
1794 (put-sleb128 line-port inc))
1795 (define (set-file file)
1796 (put-u8 line-port 4)
1797 (put-uleb128 line-port file))
1798 (define (set-column col)
1799 (put-u8 line-port 5)
1800 (put-uleb128 line-port col))
1802 (set-address '.rtl-text)
1804 (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
1808 ;; There was no source info in the first place. Set
1809 ;; file register to 0 before adding final row.
1812 (((pc* file* line* col*) . in*)
1814 ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
1815 (lp in* pc file line col))
1817 (unless (eqv? col col*)
1819 (unless (eqv? file file*)
1821 (advance-pc (- pc* pc) (- line* line))
1822 (lp in* pc* file* line* col*)))))))))))
1824 (define (compute-code attr val)
1826 ('name (string-table-intern! strtab val))
1829 ('producer (string-table-intern! strtab val))
1830 ('language (language-name->code val))
1833 (define (exact-integer? val)
1834 (and (number? val) (integer? val) (exact? val)))
1836 (define (choose-form attr val code)
1838 ((string? val) 'strp)
1839 ((eq? attr 'stmt-list) 'sec-offset)
1840 ((exact-integer? code)
1842 ((< code 0) 'sleb128)
1843 ((<= code #xff) 'data1)
1844 ((<= code #xffff) 'data2)
1845 ((<= code #xffffffff) 'data4)
1846 ((<= code #xffffffffffffffff) 'data8)
1848 ((symbol? val) 'addr)
1849 (else (error "unhandled case" attr val code))))
1851 (define (add-die-relocation! kind sym)
1853 (cons (make-linker-reloc kind (port-position die-port) 0 sym)
1856 (define (write-value code form)
1858 ('data1 (put-u8 die-port code))
1859 ('data2 (put-u16 die-port code))
1860 ('data4 (put-u32 die-port code))
1861 ('data8 (put-u64 die-port code))
1862 ('uleb128 (put-uleb128 die-port code))
1863 ('sleb128 (put-sleb128 die-port code))
1865 (match (asm-word-size asm)
1867 (add-die-relocation! 'abs32/1 code)
1868 (put-u32 die-port 0))
1870 (add-die-relocation! 'abs64/1 code)
1871 (put-u64 die-port 0))))
1872 ('sec-offset (put-u32 die-port code))
1873 ('strp (put-u32 die-port code))))
1875 (define (write-die die)
1877 ((tag ('@ (attrs vals) ...) children ...)
1878 (let* ((codes (map compute-code attrs vals))
1879 (forms (map choose-form attrs vals codes))
1880 (has-children? (not (null? children)))
1881 (abbrev-code (intern-abbrev tag has-children? attrs forms)))
1882 (put-uleb128 die-port abbrev-code)
1883 (for-each write-value codes forms)
1885 (for-each write-die children)
1886 (put-uleb128 die-port 0))))))
1888 ;; Compilation unit header.
1889 (put-u32 die-port 0) ; Length; will patch later.
1890 (put-u16 die-port 4) ; DWARF 4.
1891 (put-u32 die-port 0) ; Abbrevs offset.
1892 (put-u8 die-port (asm-word-size asm)) ; Address size.
1894 (write-die (make-compile-unit-die asm))
1896 ;; Terminate the abbrevs list.
1897 (put-uleb128 abbrev-port 0)
1901 (values (let ((bv (get-die-bv)))
1902 ;; Patch DWARF32 length.
1903 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
1904 (asm-endianness asm))
1905 (make-object asm '.debug_info bv die-relocs '()
1906 #:type SHT_PROGBITS #:flags 0))
1907 (make-object asm '.debug_abbrev (get-abbrev-bv) '() '()
1908 #:type SHT_PROGBITS #:flags 0)
1909 (make-object asm '.debug_str (link-string-table! strtab) '() '()
1910 #:type SHT_PROGBITS #:flags 0)
1911 (make-object asm '.debug_loc #vu8() '() '()
1912 #:type SHT_PROGBITS #:flags 0)
1913 (let ((bv (get-line-bv)))
1914 ;; Patch DWARF32 length.
1915 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
1916 (asm-endianness asm))
1917 (make-object asm '.debug_line bv line-relocs '()
1918 #:type SHT_PROGBITS #:flags 0)))))
1920 (define (link-objects asm)
1921 (let*-values (;; Link procprops before constants, because it probably
1922 ;; interns more constants.
1923 ((procprops) (link-procprops asm))
1924 ((ro rw rw-init) (link-constants asm))
1925 ;; Link text object after constants, so that the
1926 ;; constants initializer gets included.
1927 ((text) (link-text-object asm))
1928 ((dt) (link-dynamic-section asm text rw rw-init))
1929 ((symtab strtab) (link-symtab (linker-object-section text) asm))
1930 ((arities arities-strtab) (link-arities asm))
1931 ((docstrs docstrs-strtab) (link-docstrs asm))
1932 ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
1933 ;; This needs to be linked last, because linking other
1934 ;; sections adds entries to the string table.
1935 ((shstrtab) (link-shstrtab asm)))
1937 (list text ro rw dt symtab strtab arities arities-strtab
1938 docstrs docstrs-strtab procprops
1939 dinfo dabbrev dstrtab dloc dline
1946 ;;; High-level public interfaces.
1949 (define* (link-assembly asm #:key (page-aligned? #t))
1950 "Produce an ELF image from the code and data emitted into @var{asm}.
1951 The result is a bytevector, by default linked so that read-only and
1952 writable data are on separate pages. Pass @code{#:page-aligned? #f} to
1953 disable this behavior."
1954 (link-elf (link-objects asm) #:page-aligned? page-aligned?))
1956 (define (assemble-program instructions)
1957 "Take the sequence of instructions @var{instructions}, assemble them
1958 into RTL code, link an image, and load that image from memory. Returns
1960 (let ((asm (make-assembler)))
1961 (emit-text asm instructions)
1962 (load-thunk-from-memory (link-assembly asm #:page-aligned? #f))))