1 ;;; Guile bytecode assembler
3 ;;; Copyright (C) 2001, 2009, 2010, 2012, 2013, 2014 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 ;;; bytecode 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 VM operations. Assemblers
28 ;;; for primitive instructions are generated programmatically from
29 ;;; (instruction-list), which itself is derived from the VM sources.
30 ;;; There are also "macro-instructions" like "label" or "load-constant"
31 ;;; 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 dwarf)
48 #:use-module (system vm elf)
49 #:use-module (system vm linker)
50 #:use-module (language bytecode)
51 #:use-module (rnrs bytevectors)
52 #:use-module (ice-9 binary-ports)
53 #:use-module (ice-9 vlist)
54 #:use-module (ice-9 match)
55 #:use-module (srfi srfi-1)
56 #:use-module (srfi srfi-4)
57 #:use-module (srfi srfi-9)
58 #:use-module (srfi srfi-11)
59 #:export (make-assembler
65 (emit-receive* . emit-receive)
71 (emit-builtin-ref* . emit-builtin-ref)
80 emit-assert-nargs-ee/locals
92 (emit-br-if-eq* . emit-br-if-eq)
93 (emit-br-if-eqv* . emit-br-if-eqv)
94 (emit-br-if-equal* . emit-br-if-equal)
95 (emit-br-if-=* . emit-br-if-=)
96 (emit-br-if-<* . emit-br-if-<)
97 (emit-br-if-<=* . emit-br-if-<=)
98 (emit-mov* . emit-mov)
99 (emit-box* . emit-box)
100 (emit-box-ref* . emit-box-ref)
101 (emit-box-set!* . emit-box-set!)
103 (emit-free-ref* . emit-free-ref)
104 (emit-free-set!* . emit-free-set!)
107 (emit-define!* . emit-define!)
111 (emit-wind* . emit-wind)
113 (emit-push-fluid* . emit-push-fluid)
115 (emit-fluid-ref* . emit-fluid-ref)
116 (emit-fluid-set* . emit-fluid-set)
117 (emit-string-length* . emit-string-length)
118 (emit-string-ref* . emit-string-ref)
119 (emit-string->number* . emit-string->number)
120 (emit-string->symbol* . emit-string->symbol)
121 (emit-symbol->keyword* . emit-symbol->keyword)
122 (emit-cons* . emit-cons)
123 (emit-car* . emit-car)
124 (emit-cdr* . emit-cdr)
125 (emit-set-car!* . emit-set-car!)
126 (emit-set-cdr!* . emit-set-cdr!)
127 (emit-add* . emit-add)
128 (emit-add1* . emit-add1)
129 (emit-sub* . emit-sub)
130 (emit-sub1* . emit-sub1)
131 (emit-mul* . emit-mul)
132 (emit-div* . emit-div)
133 (emit-quo* . emit-quo)
134 (emit-rem* . emit-rem)
135 (emit-mod* . emit-mod)
136 (emit-ash* . emit-ash)
137 (emit-logand* . emit-logand)
138 (emit-logior* . emit-logior)
139 (emit-logxor* . emit-logxor)
140 (emit-make-vector* . emit-make-vector)
141 (emit-make-vector/immediate* . emit-make-vector/immediate)
142 (emit-vector-length* . emit-vector-length)
143 (emit-vector-ref* . emit-vector-ref)
144 (emit-vector-ref/immediate* . emit-vector-ref/immediate)
145 (emit-vector-set!* . emit-vector-set!)
146 (emit-vector-set!/immediate* . emit-vector-set!/immediate)
147 (emit-struct-vtable* . emit-struct-vtable)
148 (emit-allocate-struct/immediate* . emit-allocate-struct/immediate)
149 (emit-struct-ref/immediate* . emit-struct-ref/immediate)
150 (emit-struct-set!/immediate* . emit-struct-set!/immediate)
151 (emit-class-of* . emit-class-of)
152 (emit-make-array* . emit-make-array)
153 (emit-bv-u8-ref* . emit-bv-u8-ref)
154 (emit-bv-s8-ref* . emit-bv-s8-ref)
155 (emit-bv-u16-ref* . emit-bv-u16-ref)
156 (emit-bv-s16-ref* . emit-bv-s16-ref)
157 (emit-bv-u32-ref* . emit-bv-u32-ref)
158 (emit-bv-s32-ref* . emit-bv-s32-ref)
159 (emit-bv-u64-ref* . emit-bv-u64-ref)
160 (emit-bv-s64-ref* . emit-bv-s64-ref)
161 (emit-bv-f32-ref* . emit-bv-f32-ref)
162 (emit-bv-f64-ref* . emit-bv-f64-ref)
163 (emit-bv-u8-set!* . emit-bv-u8-set!)
164 (emit-bv-s8-set!* . emit-bv-s8-set!)
165 (emit-bv-u16-set!* . emit-bv-u16-set!)
166 (emit-bv-s16-set!* . emit-bv-s16-set!)
167 (emit-bv-u32-set!* . emit-bv-u32-set!)
168 (emit-bv-s32-set!* . emit-bv-s32-set!)
169 (emit-bv-u64-set!* . emit-bv-u64-set!)
170 (emit-bv-s64-set!* . emit-bv-s64-set!)
171 (emit-bv-f32-set!* . emit-bv-f32-set!)
172 (emit-bv-f64-set!* . emit-bv-f64-set!)
180 ;; Like define-inlinable, but only for first-order uses of the defined
181 ;; routine. Should residualize less code.
183 (define-syntax define-inline
186 ((_ (name arg ...) body ...)
187 (with-syntax (((temp ...) (generate-temporaries #'(arg ...))))
188 #`(eval-when (expand)
189 (define-syntax-rule (name temp ...)
190 (let ((arg temp) ...)
193 ;;; Bytecode consists of 32-bit units, often subdivided in some way.
194 ;;; These helpers create one 32-bit unit from multiple components.
196 (define-inline (pack-u8-u24 x y)
198 (error "out of range" x))
199 (logior x (ash y 8)))
201 (define-inline (pack-u8-s24 x y)
203 (error "out of range" x))
205 ((< 0 (- y) #x800000)
209 (else (error "out of range" y)))
212 (define-inline (pack-u1-u7-u24 x y z)
214 (error "out of range" x))
216 (error "out of range" y))
217 (logior x (ash y 1) (ash z 8)))
219 (define-inline (pack-u8-u12-u12 x y z)
221 (error "out of range" x))
222 (unless (<= 0 y 4095)
223 (error "out of range" y))
224 (logior x (ash y 8) (ash z 20)))
226 (define-inline (pack-u8-u8-u16 x y z)
228 (error "out of range" x))
230 (error "out of range" y))
231 (logior x (ash y 8) (ash z 16)))
233 (define-inline (pack-u8-u8-u8-u8 x y z w)
235 (error "out of range" x))
237 (error "out of range" y))
239 (error "out of range" z))
240 (logior x (ash y 8) (ash z 16) (ash w 24)))
243 (define-syntax pack-flags
245 ;; Add clauses as needed.
246 ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
247 (if f2 (ash 2 0) 0))))))
249 ;;; Helpers to read and write 32-bit units in a buffer.
251 (define-inline (u32-ref buf n)
252 (bytevector-u32-native-ref buf (* n 4)))
254 (define-inline (u32-set! buf n val)
255 (bytevector-u32-native-set! buf (* n 4) val))
257 (define-inline (s32-ref buf n)
258 (bytevector-s32-native-ref buf (* n 4)))
260 (define-inline (s32-set! buf n val)
261 (bytevector-s32-native-set! buf (* n 4) val))
266 ;;; A <meta> entry collects metadata for one procedure. Procedures are
267 ;;; written as contiguous ranges of bytecode.
270 (define-syntax-rule (assert-match arg pattern kind)
272 (unless (match x (pattern #t) (_ #f))
273 (error (string-append "expected " kind) x)))))
275 (define-record-type <meta>
276 (%make-meta label properties low-pc high-pc arities)
279 (properties meta-properties set-meta-properties!)
281 (high-pc meta-high-pc set-meta-high-pc!)
282 (arities meta-arities set-meta-arities!))
284 (define (make-meta label properties low-pc)
285 (assert-match label (or (? exact-integer?) (? symbol?)) "symbol")
286 (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
287 (%make-meta label properties low-pc #f '()))
289 (define (meta-name meta)
290 (assq-ref (meta-properties meta) 'name))
292 ;; Metadata for one <lambda-case>.
293 (define-record-type <arity>
294 (make-arity req opt rest kw-indices allow-other-keys?
295 low-pc high-pc definitions)
300 (kw-indices arity-kw-indices)
301 (allow-other-keys? arity-allow-other-keys?)
302 (low-pc arity-low-pc)
303 (high-pc arity-high-pc set-arity-high-pc!)
304 (definitions arity-definitions set-arity-definitions!))
307 (define-syntax *block-size* (identifier-syntax 32)))
309 ;;; An assembler collects all of the words emitted during assembly, and
310 ;;; also maintains ancillary information such as the constant table, a
311 ;;; relocation list, and so on.
313 ;;; Bytecode consists of 32-bit units. We emit bytecode using native
314 ;;; endianness. If we're targeting a foreign endianness, we byte-swap
315 ;;; the bytevector as a whole instead of conditionalizing each access.
317 (define-record-type <asm>
318 (make-asm cur idx start prev written
322 shstrtab next-section-number
327 ;; We write bytecode into what is logically a growable vector,
328 ;; implemented as a list of blocks. asm-cur is the current block, and
329 ;; asm-idx is the current index into that block, in 32-bit units.
331 (cur asm-cur set-asm-cur!)
332 (idx asm-idx set-asm-idx!)
334 ;; asm-start is an absolute position, indicating the offset of the
335 ;; beginning of an instruction (in u32 units). It is updated after
336 ;; writing all the words for one primitive instruction. It models the
337 ;; position of the instruction pointer during execution, given that
338 ;; the VM updates the IP only at the end of executing the instruction,
339 ;; and is thus useful for computing offsets between two points in a
342 (start asm-start set-asm-start!)
344 ;; The list of previously written blocks.
346 (prev asm-prev set-asm-prev!)
348 ;; The number of u32 words written in asm-prev, which is the same as
349 ;; the offset of the current block.
351 (written asm-written set-asm-written!)
353 ;; An alist of symbol -> position pairs, indicating the labels defined
354 ;; in this compilation unit.
356 (labels asm-labels set-asm-labels!)
358 ;; A list of relocations needed by the program text. We use an
359 ;; internal representation for relocations, and handle textualn
360 ;; relative relocations in the assembler. Other kinds of relocations
361 ;; are later reified as linker relocations and resolved by the linker.
363 (relocs asm-relocs set-asm-relocs!)
365 ;; Target information.
367 (word-size asm-word-size)
368 (endianness asm-endianness)
370 ;; The constant table, as a vhash of object -> label. All constants
371 ;; get de-duplicated and written into separate sections -- either the
372 ;; .rodata section, for read-only data, or .data, for constants that
373 ;; need initialization at load-time (like symbols). Constants can
374 ;; depend on other constants (e.g. a symbol depending on a stringbuf),
375 ;; so order in this table is important.
377 (constants asm-constants set-asm-constants!)
379 ;; A list of instructions needed to initialize the constants. Will
380 ;; run in a thunk with 2 local variables.
382 (inits asm-inits set-asm-inits!)
384 ;; The shstrtab, for section names.
386 (shstrtab asm-shstrtab set-asm-shstrtab!)
388 ;; The section number for the next section to be written.
390 (next-section-number asm-next-section-number set-asm-next-section-number!)
392 ;; A list of <meta>, corresponding to procedure metadata.
394 (meta asm-meta set-asm-meta!)
396 ;; A list of (pos . source) pairs, indicating source information. POS
397 ;; is relative to the beginning of the text section, and SOURCE is in
398 ;; the same format that source-properties returns.
400 (sources asm-sources set-asm-sources!)
402 ;; A list of (pos . dead-slot-map) pairs, indicating dead slot maps.
403 ;; POS is relative to the beginning of the text section.
404 ;; DEAD-SLOT-MAP is a bitfield of slots that are dead at call sites,
407 (dead-slot-maps asm-dead-slot-maps set-asm-dead-slot-maps!))
409 (define-inline (fresh-block)
410 (make-u32vector *block-size*))
412 (define* (make-assembler #:key (word-size (target-word-size))
413 (endianness (target-endianness)))
414 "Create an assembler for a given target @var{word-size} and
415 @var{endianness}, falling back to appropriate values for the configured
417 (make-asm (fresh-block) 0 0 '() 0
418 (make-hash-table) '()
421 (make-string-table) 1
424 (define (intern-section-name! asm string)
425 "Add a string to the section name table (shstrtab)."
426 (string-table-intern! (asm-shstrtab asm) string))
428 (define-inline (asm-pos asm)
429 "The offset of the next word to be written into the code buffer, in
431 (+ (asm-idx asm) (asm-written asm)))
433 (define (allocate-new-block asm)
434 "Close off the current block, and arrange for the next word to be
435 written to a fresh block."
436 (let ((new (fresh-block)))
437 (set-asm-prev! asm (cons (asm-cur asm) (asm-prev asm)))
438 (set-asm-written! asm (asm-pos asm))
439 (set-asm-cur! asm new)
440 (set-asm-idx! asm 0)))
442 (define-inline (emit asm u32)
443 "Emit one 32-bit word into the instruction stream. Assumes that there
444 is space for the word, and ensures that there is space for the next
446 (u32-set! (asm-cur asm) (asm-idx asm) u32)
447 (set-asm-idx! asm (1+ (asm-idx asm)))
448 (if (= (asm-idx asm) *block-size*)
449 (allocate-new-block asm)))
451 (define-inline (make-reloc type label base word)
452 "Make an internal relocation of type @var{type} referencing symbol
453 @var{label}, @var{word} words after position @var{start}. @var{type}
454 may be x8-s24, indicating a 24-bit relative label reference that can be
455 fixed up by the assembler, or s32, indicating a 32-bit relative
456 reference that needs to be fixed up by the linker."
457 (list type label base word))
459 (define-inline (reset-asm-start! asm)
460 "Reset the asm-start after writing the words for one instruction."
461 (set-asm-start! asm (asm-pos asm)))
463 (define (record-label-reference asm label)
464 "Record an x8-s24 local label reference. This value will get patched
465 up later by the assembler."
466 (let* ((start (asm-start asm))
468 (reloc (make-reloc 'x8-s24 label start (- pos start))))
469 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
471 (define* (record-far-label-reference asm label #:optional (offset 0))
472 "Record an s32 far label reference. This value will get patched up
473 later by the linker."
474 (let* ((start (- (asm-start asm) offset))
476 (reloc (make-reloc 's32 label start (- pos start))))
477 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
483 ;;; Primitive assemblers are defined by expanding `assembler' for each
484 ;;; opcode in `(instruction-list)'.
488 (define (id-append ctx a b)
489 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
491 (define-syntax assembler
493 (define-syntax op-case
496 ((_ asm name ((type arg ...) code ...) clause ...)
497 #`(if (eq? name 'type)
498 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
501 (op-case asm name clause ...)))
503 #'(error "unmatched name" name)))))
505 (define (pack-first-word asm opcode type)
506 (with-syntax ((opcode opcode))
512 (emit asm (pack-u8-u24 opcode arg)))
514 (record-label-reference asm label)
517 (emit asm (pack-u8-u8-u16 opcode a (object-address imm))))
519 (emit asm (pack-u8-u12-u12 opcode a b)))
521 (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
523 (define (pack-tail-word asm type)
527 (emit asm (pack-u8-u24 a b)))
529 (record-label-reference asm label)
534 (let ((val (object-address imm)))
535 (unless (zero? (ash val -32))
536 (error "FIXME: enable truncation of negative fixnums when cross-compiling"))
539 (unless (= (asm-word-size asm) 8)
540 (error "make-long-immediate unavailable for this target"))
541 (emit asm (ash (object-address imm) -32))
542 (emit asm (logand (object-address imm) (1- (ash 1 32)))))
545 (record-far-label-reference asm label)
548 (record-far-label-reference asm label)
551 (record-far-label-reference asm label)
554 (record-far-label-reference asm label
555 (* offset (/ (asm-word-size asm) 4)))
558 (emit asm (pack-u8-u24 0 a)))
560 (record-label-reference asm label)
563 (record-label-reference asm label)
564 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
565 ((B1_U7_L24 a b label)
566 (record-label-reference asm label)
567 (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
569 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
571 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))))
574 ((_ name opcode word0 word* ...)
575 (with-syntax ((((formal0 ...)
577 (pack-first-word #'asm
578 (syntax->datum #'opcode)
579 (syntax->datum #'word0)))
582 (map (lambda (word) (pack-tail-word #'asm word))
583 (syntax->datum #'(word* ...)))))
584 #'(lambda (asm formal0 ... formal* ... ...)
585 (unless (asm? asm) (error "not an asm"))
588 (reset-asm-start! asm))))))))
590 (define assemblers (make-hash-table))
593 (define-syntax define-assembler
596 ((_ name opcode kind arg ...)
597 (with-syntax ((emit (id-append #'name #'emit- #'name)))
599 (let ((emit (assembler name opcode arg ...)))
600 (hashq-set! assemblers 'name emit)
603 (define-syntax visit-opcodes
606 ((visit-opcodes macro arg ...)
607 (with-syntax (((inst ...)
608 (map (lambda (x) (datum->syntax #'macro x))
609 (instruction-list))))
611 (macro arg ... . inst)
614 (visit-opcodes define-assembler)
618 ;; Some operands are encoded using a restricted subset of the full
619 ;; 24-bit local address space, in order to make the bytecode more
620 ;; dense in the usual case that there are few live locals. Here we
621 ;; define wrapper emitters that shuffle out-of-range operands into and
622 ;; out of the reserved range of locals [233,255]. This range is
623 ;; sufficient because these restricted operands are only present in
624 ;; the first word of an instruction. Since 8 bits is the smallest
625 ;; slot-addressing operand size, that means we can fit 3 operands in
626 ;; the 24 bits of payload of the first word (the lower 8 bits being
627 ;; taken by the opcode).
629 ;; The result are wrapper emitters with the same arity,
630 ;; e.g. emit-cons* that wraps emit-cons. We expose these wrappers as
631 ;; the public interface for emitting `cons' instructions. That way we
632 ;; solve the problem fully and in just one place. The only manual
633 ;; care that need be taken is in the exports list at the top of the
634 ;; file -- to be sure that we export the wrapper and not the wrapped
637 (define (shuffling-assembler name kind word0 word*)
638 (define (analyze-first-word)
639 (define-syntax op-case
641 ((_ type ((%type %kind arg ...) values) clause ...)
642 (if (and (eq? type '%type) (eq? kind '%kind))
643 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
644 #'((arg ...) values))
645 (op-case type clause ...)))
651 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
653 ((U8_U8_I16 <- a imm)
654 (values (if (< a (ash 1 8)) a 253)
657 (values (if (< a (ash 1 12)) a (begin (emit-mov* asm 253 a) 253))
658 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
660 (values (if (< a (ash 1 12)) a 253)
661 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
662 ((U8_U8_U8_U8 ! a b c)
663 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
664 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
665 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))
666 ((U8_U8_U8_U8 <- a b c)
667 (values (if (< a (ash 1 8)) a 253)
668 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
669 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))))
671 (define (tail-formals type)
672 (define-syntax op-case
674 ((op-case type (%type arg ...) clause ...)
675 (if (eq? type '%type)
676 (generate-temporaries #'(arg ...))
677 (op-case type clause ...)))
679 (error "unmatched type" type))))
694 (B1_U7_L24 a b label)
698 (define (shuffle-up dst)
699 (define-syntax op-case
701 ((_ type ((%type ...) exp) clause ...)
702 (if (memq type '(%type ...))
704 (op-case type clause ...)))
706 (error "unexpected type" type))))
707 (with-syntax ((dst dst))
710 ((U8_U8_I16 U8_U8_U8_U8)
711 (unless (< dst (ash 1 8))
712 (emit-mov* asm dst 253)))
714 (unless (< dst (ash 1 12))
715 (emit-mov* asm dst 253))))))
719 (lambda (formals+shuffle)
720 (with-syntax ((emit-name (id-append name #'emit- name))
721 (((formal0 ...) shuffle) formals+shuffle)
722 (((formal* ...) ...) (map tail-formals word*)))
723 (with-syntax (((shuffle-up-dst ...)
725 (syntax-case #'(formal0 ...) ()
727 (list (shuffle-up #'dst))))
729 #'(lambda (asm formal0 ... formal* ... ...)
730 (call-with-values (lambda () shuffle)
731 (lambda (formal0 ...)
732 (emit-name asm formal0 ... formal* ... ...)))
733 shuffle-up-dst ...))))))
735 (define-syntax define-shuffling-assembler
738 ((_ #:except (except ...) name opcode kind word0 word* ...)
740 ((or-map (lambda (op) (eq? (syntax->datum #'name) op))
741 (map syntax->datum #'(except ...)))
743 ((shuffling-assembler #'name (syntax->datum #'kind)
744 (syntax->datum #'word0)
745 (map syntax->datum #'(word* ...)))
747 (with-syntax ((emit (id-append #'name
748 (id-append #'name #'emit- #'name)
753 (hashq-set! assemblers 'name emit)
755 (else #'(begin))))))))
757 (visit-opcodes define-shuffling-assembler #:except (receive mov))
759 ;; Mov and receive are two special cases that can work without wrappers.
760 ;; Indeed it is important that they do so.
762 (define (emit-mov* asm dst src)
763 (if (and (< dst (ash 1 12)) (< src (ash 1 12)))
764 (emit-mov asm dst src)
765 (emit-long-mov asm dst src)))
767 (define (emit-receive* asm dst proc nlocals)
768 (if (and (< dst (ash 1 12)) (< proc (ash 1 12)))
769 (emit-receive asm dst proc nlocals)
771 (emit-receive-values asm proc #t 1)
772 (emit-mov* asm dst (1+ proc))
773 (emit-reset-frame asm nlocals))))
775 (define (emit-text asm instructions)
776 "Assemble @var{instructions} using the assembler @var{asm}.
777 @var{instructions} is a sequence of instructions, expressed as a list of
778 lists. This procedure can be called many times before calling
779 @code{link-assembly}."
780 (for-each (lambda (inst)
781 (apply (or (hashq-ref assemblers (car inst))
782 (error 'bad-instruction inst))
790 ;;; The constant table records a topologically sorted set of literal
791 ;;; constants used by a program. For example, a pair uses its car and
792 ;;; cdr, a string uses its stringbuf, etc.
794 ;;; Some things we want to add to the constant table are not actually
795 ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
796 ;;; references, or cache cells for non-closure procedures. For these we
797 ;;; define special record types and add instances of those record types
801 (define-inline (immediate? x)
802 "Return @code{#t} if @var{x} is immediate, and @code{#f} otherwise."
803 (not (zero? (logand (object-address x) 6))))
805 (define-record-type <stringbuf>
806 (make-stringbuf string)
808 (string stringbuf-string))
810 (define-record-type <static-procedure>
811 (make-static-procedure code)
813 (code static-procedure-code))
815 (define-record-type <uniform-vector-backing-store>
816 (make-uniform-vector-backing-store bytes element-size)
817 uniform-vector-backing-store?
818 (bytes uniform-vector-backing-store-bytes)
819 (element-size uniform-vector-backing-store-element-size))
821 (define-record-type <cache-cell>
822 (make-cache-cell scope key)
824 (scope cache-cell-scope)
825 (key cache-cell-key))
827 (define (simple-vector? obj)
829 (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
831 (define (simple-uniform-vector? obj)
833 (symbol? (array-type obj))
834 (equal? (array-shape obj) (list (list 0 (1- (array-length obj)))))))
836 (define (statically-allocatable? x)
837 "Return @code{#t} if a non-immediate constant can be allocated
838 statically, and @code{#f} if it would need some kind of runtime
840 (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
842 (define (intern-constant asm obj)
843 "Add an object to the constant table, and return a label that can be
844 used to reference it. If the object is already present in the constant
845 table, its existing label is used directly."
847 (intern-constant asm obj))
848 (define (field dst n obj)
849 (let ((src (recur obj)))
851 (if (statically-allocatable? obj)
852 `((static-patch! ,dst ,n ,src))
853 `((static-ref 1 ,src)
854 (static-set! 1 ,dst ,n)))
856 (define (intern obj label)
859 (append (field label 0 (car obj))
860 (field label 1 (cdr obj))))
861 ((simple-vector? obj)
862 (let lp ((i 0) (inits '()))
863 (if (< i (vector-length obj))
865 (append-reverse (field label (1+ i) (vector-ref obj i))
868 ((stringbuf? obj) '())
869 ((static-procedure? obj)
870 `((static-patch! ,label 1 ,(static-procedure-code obj))))
871 ((cache-cell? obj) '())
873 `((make-non-immediate 1 ,(recur (symbol->string obj)))
875 (static-set! 1 ,label 0)))
877 `((static-patch! ,label 1 ,(recur (make-stringbuf obj)))))
879 `((static-ref 1 ,(recur (keyword->symbol obj)))
880 (symbol->keyword 1 1)
881 (static-set! 1 ,label 0)))
883 `((make-non-immediate 1 ,(recur (number->string obj)))
885 (static-set! 1 ,label 0)))
886 ((uniform-vector-backing-store? obj) '())
887 ((simple-uniform-vector? obj)
888 (let ((width (case (array-type obj)
891 ;; Bitvectors are addressed in 32-bit units.
892 ;; Although a complex number is 8 or 16 bytes wide,
893 ;; it should be byteswapped in 4 or 8 byte units.
894 ((u32 s32 f32 c32 b) 4)
895 ((u64 s64 f64 c64) 8)
897 (error "unhandled array type" obj)))))
898 `((static-patch! ,label 2
899 ,(recur (make-uniform-vector-backing-store
900 (uniform-array->bytevector obj)
903 (error "don't know how to intern" obj))))
905 ((immediate? obj) #f)
906 ((vhash-assoc obj (asm-constants asm)) => cdr)
908 ;; Note that calling intern may mutate asm-constants and
909 ;; asm-constant-inits.
910 (let* ((label (gensym "constant"))
911 (inits (intern obj label)))
912 (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
913 (set-asm-inits! asm (append-reverse inits (asm-inits asm)))
916 (define (intern-non-immediate asm obj)
917 "Intern a non-immediate into the constant table, and return its
919 (when (immediate? obj)
920 (error "expected a non-immediate" obj))
921 (intern-constant asm obj))
923 (define (intern-cache-cell asm scope key)
924 "Intern a cache cell into the constant table, and return its label.
925 If there is already a cache cell with the given scope and key, it is
927 (intern-constant asm (make-cache-cell scope key)))
929 ;; Return the label of the cell that holds the module for a scope.
930 (define (intern-module-cache-cell asm scope)
931 "Intern a cache cell for a module, and return its label."
932 (intern-cache-cell asm scope #t))
938 ;;; Macro assemblers bridge the gap between primitive instructions and
939 ;;; some higher-level operations.
943 (define-syntax define-macro-assembler
946 ((_ (name arg ...) body body* ...)
947 (with-syntax ((emit (id-append #'name #'emit- #'name)))
950 (let ((emit (lambda (arg ...) body body* ...)))
951 (hashq-set! assemblers 'name emit)
955 (define-macro-assembler (load-constant asm dst obj)
958 (let ((bits (object-address obj)))
960 ((and (< dst 256) (zero? (ash bits -16)))
961 (emit-make-short-immediate asm dst obj))
962 ((zero? (ash bits -32))
963 (emit-make-long-immediate asm dst obj))
965 (emit-make-long-long-immediate asm dst obj)))))
966 ((statically-allocatable? obj)
967 (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
969 (emit-static-ref asm dst (intern-non-immediate asm obj)))))
971 (define-macro-assembler (load-static-procedure asm dst label)
972 (let ((loc (intern-constant asm (make-static-procedure label))))
973 (emit-make-non-immediate asm dst loc)))
975 (define-syntax-rule (define-tc7-macro-assembler name tc7)
976 (define-macro-assembler (name asm slot invert? label)
977 (emit-br-if-tc7 asm slot invert? tc7 label)))
979 ;; Keep in sync with tags.h. Part of Guile's ABI. Currently unused
980 ;; macro assemblers are commented out. See also
981 ;; *branching-primcall-arities* in (language cps primitives), the set of
982 ;; macro-instructions in assembly.scm, and
983 ;; disassembler.scm:code-annotation.
985 ;; FIXME: Define all tc7 values in Scheme in one place, derived from
987 (define-tc7-macro-assembler br-if-symbol 5)
988 (define-tc7-macro-assembler br-if-variable 7)
989 (define-tc7-macro-assembler br-if-vector 13)
990 ;(define-tc7-macro-assembler br-if-weak-vector 13)
991 (define-tc7-macro-assembler br-if-string 21)
992 ;(define-tc7-macro-assembler br-if-heap-number 23)
993 ;(define-tc7-macro-assembler br-if-stringbuf 39)
994 (define-tc7-macro-assembler br-if-bytevector 77)
995 ;(define-tc7-macro-assembler br-if-pointer 31)
996 ;(define-tc7-macro-assembler br-if-hashtable 29)
997 ;(define-tc7-macro-assembler br-if-fluid 37)
998 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
999 ;(define-tc7-macro-assembler br-if-frame 47)
1000 ;(define-tc7-macro-assembler br-if-vm 55)
1001 ;(define-tc7-macro-assembler br-if-vm-cont 71)
1002 ;(define-tc7-macro-assembler br-if-rtl-program 69)
1003 ;(define-tc7-macro-assembler br-if-weak-set 85)
1004 ;(define-tc7-macro-assembler br-if-weak-table 87)
1005 ;(define-tc7-macro-assembler br-if-array 93)
1006 (define-tc7-macro-assembler br-if-bitvector 95)
1007 ;(define-tc7-macro-assembler br-if-port 125)
1008 ;(define-tc7-macro-assembler br-if-smob 127)
1010 (define-macro-assembler (begin-program asm label properties)
1011 (emit-label asm label)
1012 (let ((meta (make-meta label properties (asm-start asm))))
1013 (set-asm-meta! asm (cons meta (asm-meta asm)))))
1015 (define-macro-assembler (end-program asm)
1016 (let ((meta (car (asm-meta asm))))
1017 (set-meta-high-pc! meta (asm-start asm))
1018 (set-meta-arities! meta (reverse (meta-arities meta)))))
1020 (define-macro-assembler (begin-standard-arity asm req nlocals alternate)
1021 (emit-begin-opt-arity asm req '() #f nlocals alternate))
1023 (define-macro-assembler (begin-opt-arity asm req opt rest nlocals alternate)
1024 (emit-begin-kw-arity asm req opt rest '() #f nlocals alternate))
1026 (define-macro-assembler (begin-kw-arity asm req opt rest kw-indices
1027 allow-other-keys? nlocals alternate)
1028 (assert-match req ((? symbol?) ...) "list of symbols")
1029 (assert-match opt ((? symbol?) ...) "list of symbols")
1030 (assert-match rest (or #f (? symbol?)) "#f or symbol")
1031 (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
1032 "alist of keyword -> integer")
1033 (assert-match allow-other-keys? (? boolean?) "boolean")
1034 (assert-match nlocals (? integer?) "integer")
1035 (assert-match alternate (or #f (? exact-integer?) (? symbol?)) "#f or symbol")
1036 (let* ((meta (car (asm-meta asm)))
1037 (arity (make-arity req opt rest kw-indices allow-other-keys?
1038 (asm-start asm) #f '()))
1039 ;; The procedure itself is in slot 0, in the standard calling
1040 ;; convention. For procedure prologues, nreq includes the
1041 ;; procedure, so here we add 1.
1042 (nreq (1+ (length req)))
1044 (rest? (->bool rest)))
1045 (set-meta-arities! meta (cons arity (meta-arities meta)))
1047 ((or allow-other-keys? (pair? kw-indices))
1048 (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
1050 ((or rest? (pair? opt))
1051 (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
1053 (emit-standard-prelude asm nreq nlocals alternate)))))
1055 (define-macro-assembler (end-arity asm)
1056 (let ((arity (car (meta-arities (car (asm-meta asm))))))
1057 (set-arity-definitions! arity (reverse (arity-definitions arity)))
1058 (set-arity-high-pc! arity (asm-start asm))))
1060 ;; As noted above, we reserve locals 253 through 255 for shuffling large
1061 ;; operands. However the calling convention has all arguments passed in
1062 ;; a contiguous block. This helper, called after the clause has been
1063 ;; chosen and the keyword/optional/rest arguments have been processed,
1064 ;; shuffles up arguments from slot 253 and higher into their final
1067 (define (shuffle-up-args asm nargs)
1069 (let ((slot (1- nargs)))
1070 (emit-mov asm (+ slot 3) slot)
1071 (shuffle-up-args asm (1- nargs)))))
1073 (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
1076 (emit-br-if-nargs-ne asm nreq alternate)
1077 (emit-alloc-frame asm nlocals))
1078 ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
1079 (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
1081 (emit-assert-nargs-ee asm nreq)
1082 (emit-alloc-frame asm nlocals)))
1083 (shuffle-up-args asm nreq))
1085 (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
1087 (emit-br-if-nargs-lt asm nreq alternate)
1088 (emit-assert-nargs-ge asm nreq))
1091 (emit-bind-rest asm (+ nreq nopt)))
1093 (emit-br-if-nargs-gt asm (+ nreq nopt) alternate))
1095 (emit-assert-nargs-le asm (+ nreq nopt))))
1096 (emit-alloc-frame asm nlocals)
1097 (shuffle-up-args asm (+ nreq nopt (if rest? 1 0))))
1099 (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
1100 allow-other-keys? nlocals alternate)
1103 (emit-br-if-nargs-lt asm nreq alternate)
1105 (emit-br-if-npos-gt asm nreq (+ nreq nopt) alternate)))
1106 (emit-assert-nargs-ge asm nreq))
1107 (let ((ntotal (fold (lambda (kw ntotal)
1109 (((? keyword?) . idx)
1110 (max (1+ idx) ntotal))))
1111 (+ nreq nopt) kw-indices)))
1112 ;; FIXME: port 581f410f
1113 (emit-bind-kwargs asm nreq
1114 (pack-flags allow-other-keys? rest?)
1117 (intern-constant asm kw-indices))
1118 (emit-alloc-frame asm nlocals)
1119 (shuffle-up-args asm ntotal)))
1121 (define-macro-assembler (label asm sym)
1122 (hashq-set! (asm-labels asm) sym (asm-start asm)))
1124 (define-macro-assembler (source asm source)
1125 (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
1127 (define-macro-assembler (definition asm name slot)
1128 (let* ((arity (car (meta-arities (car (asm-meta asm)))))
1131 (* (- (asm-start asm) (arity-low-pc arity)) 4))))
1132 (set-arity-definitions! arity (cons def (arity-definitions arity)))))
1134 (define-macro-assembler (cache-current-module! asm module scope)
1135 (let ((mod-label (intern-module-cache-cell asm scope)))
1136 (emit-static-set! asm module mod-label 0)))
1138 (define-macro-assembler (cached-toplevel-box asm dst scope sym bound?)
1139 (let ((sym-label (intern-non-immediate asm sym))
1140 (mod-label (intern-module-cache-cell asm scope))
1141 (cell-label (intern-cache-cell asm scope sym)))
1142 (emit-toplevel-box asm dst cell-label mod-label sym-label bound?)))
1144 (define-macro-assembler (cached-module-box asm dst module-name sym public? bound?)
1145 (let* ((sym-label (intern-non-immediate asm sym))
1146 (key (cons public? module-name))
1147 (mod-name-label (intern-constant asm key))
1148 (cell-label (intern-cache-cell asm key sym)))
1149 (emit-module-box asm dst cell-label mod-name-label sym-label bound?)))
1151 (define-macro-assembler (dead-slot-map asm proc-slot dead-slot-map)
1152 (unless (zero? dead-slot-map)
1153 (set-asm-dead-slot-maps! asm
1155 (cons* (asm-start asm) proc-slot dead-slot-map)
1156 (asm-dead-slot-maps asm)))))
1161 ;;; Helper for linking objects.
1164 (define (make-object asm name bv relocs labels . kwargs)
1165 "Make a linker object. This helper handles interning the name in the
1166 shstrtab, assigning the size, allocating a fresh index, and defining a
1167 corresponding linker symbol for the start of the section."
1168 (let ((name-idx (intern-section-name! asm (symbol->string name)))
1169 (index (asm-next-section-number asm)))
1170 (set-asm-next-section-number! asm (1+ index))
1171 (make-linker-object (apply make-elf-section
1174 #:size (bytevector-length bv)
1177 (cons (make-linker-symbol name 0) labels))))
1183 ;;; Linking the constant table. This code is somewhat intertwingled
1184 ;;; with the intern-constant code above, as that procedure also
1185 ;;; residualizes instructions to initialize constants at load time.
1188 (define (write-immediate asm buf pos x)
1189 (let ((val (object-address x))
1190 (endianness (asm-endianness asm)))
1191 (case (asm-word-size asm)
1192 ((4) (bytevector-u32-set! buf pos val endianness))
1193 ((8) (bytevector-u64-set! buf pos val endianness))
1194 (else (error "bad word size" asm)))))
1196 (define (emit-init-constants asm)
1197 "If there is writable data that needs initialization at runtime, emit
1198 a procedure to do that and return its label. Otherwise return
1200 (let ((inits (asm-inits asm)))
1201 (and (not (null? inits))
1202 (let ((label (gensym "init-constants")))
1204 `((begin-program ,label ())
1205 (assert-nargs-ee/locals 1 1)
1207 (load-constant 1 ,*unspecified*)
1212 (define (link-data asm data name)
1213 "Link the static data for a program into the @var{name} section (which
1214 should be .data or .rodata), and return the resulting linker object.
1215 @var{data} should be a vhash mapping objects to labels."
1216 (define (align address alignment)
1218 (modulo (- alignment (modulo address alignment)) alignment)))
1220 (define tc7-vector 13)
1221 (define stringbuf-shared-flag #x100)
1222 (define stringbuf-wide-flag #x400)
1223 (define tc7-stringbuf 39)
1224 (define tc7-narrow-stringbuf
1225 (+ tc7-stringbuf stringbuf-shared-flag))
1226 (define tc7-wide-stringbuf
1227 (+ tc7-stringbuf stringbuf-shared-flag stringbuf-wide-flag))
1228 (define tc7-ro-string (+ 21 #x200))
1229 (define tc7-program 69)
1230 (define tc7-bytevector 77)
1231 (define tc7-bitvector 95)
1233 (let ((word-size (asm-word-size asm))
1234 (endianness (asm-endianness asm)))
1235 (define (byte-length x)
1238 (let ((x (stringbuf-string x)))
1240 (case (string-bytes-per-char x)
1241 ((1) (1+ (string-length x)))
1242 ((4) (* (1+ (string-length x)) 4))
1243 (else (error "bad string bytes per char" x))))))
1244 ((static-procedure? x)
1251 (* (1+ (vector-length x)) word-size))
1252 ((simple-uniform-vector? x)
1254 ((uniform-vector-backing-store? x)
1255 (bytevector-length (uniform-vector-backing-store-bytes x)))
1259 (define (write-constant-reference buf pos x)
1260 ;; The asm-inits will fix up any reference to a non-immediate.
1261 (write-immediate asm buf pos (if (immediate? x) x #f)))
1263 (define (write buf pos obj)
1266 (let* ((x (stringbuf-string obj))
1267 (len (string-length x))
1268 (tag (if (= (string-bytes-per-char x) 1)
1269 tc7-narrow-stringbuf
1270 tc7-wide-stringbuf)))
1273 (bytevector-u32-set! buf pos tag endianness)
1274 (bytevector-u32-set! buf (+ pos 4) len endianness))
1276 (bytevector-u64-set! buf pos tag endianness)
1277 (bytevector-u64-set! buf (+ pos 8) len endianness))
1279 (error "bad word size" asm)))
1280 (let ((pos (+ pos (* word-size 2))))
1281 (case (string-bytes-per-char x)
1285 (let ((u8 (char->integer (string-ref x i))))
1286 (bytevector-u8-set! buf (+ pos i) u8)
1288 (bytevector-u8-set! buf (+ pos i) 0))))
1292 (let ((u32 (char->integer (string-ref x i))))
1293 (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
1295 (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
1296 (else (error "bad string bytes per char" x))))))
1298 ((static-procedure? obj)
1301 (bytevector-u32-set! buf pos tc7-program endianness)
1302 (bytevector-u32-set! buf (+ pos 4) 0 endianness))
1304 (bytevector-u64-set! buf pos tc7-program endianness)
1305 (bytevector-u64-set! buf (+ pos 8) 0 endianness))
1306 (else (error "bad word size"))))
1309 (write-immediate asm buf pos #f))
1312 (let ((tag (logior tc7-ro-string (ash (string-length obj) 8))))
1315 (bytevector-u32-set! buf pos tc7-ro-string endianness)
1316 (write-immediate asm buf (+ pos 4) #f) ; stringbuf
1317 (bytevector-u32-set! buf (+ pos 8) 0 endianness)
1318 (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
1320 (bytevector-u64-set! buf pos tc7-ro-string endianness)
1321 (write-immediate asm buf (+ pos 8) #f) ; stringbuf
1322 (bytevector-u64-set! buf (+ pos 16) 0 endianness)
1323 (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
1324 (else (error "bad word size")))))
1327 (write-constant-reference buf pos (car obj))
1328 (write-constant-reference buf (+ pos word-size) (cdr obj)))
1330 ((simple-vector? obj)
1331 (let* ((len (vector-length obj))
1332 (tag (logior tc7-vector (ash len 8))))
1334 ((4) (bytevector-u32-set! buf pos tag endianness))
1335 ((8) (bytevector-u64-set! buf pos tag endianness))
1336 (else (error "bad word size")))
1338 (when (< i (vector-length obj))
1339 (let ((pos (+ pos word-size (* i word-size)))
1340 (elt (vector-ref obj i)))
1341 (write-constant-reference buf pos elt)
1345 (write-immediate asm buf pos #f))
1348 (write-immediate asm buf pos #f))
1351 (write-immediate asm buf pos #f))
1353 ((simple-uniform-vector? obj)
1354 (let ((tag (if (bitvector? obj)
1356 (let ((type-code (array-type-code obj)))
1357 (logior tc7-bytevector (ash type-code 7))))))
1360 (bytevector-u32-set! buf pos tag endianness)
1361 (bytevector-u32-set! buf (+ pos 4)
1362 (if (bitvector? obj)
1363 (bitvector-length obj)
1364 (bytevector-length obj))
1365 endianness) ; length
1366 (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
1367 (write-immediate asm buf (+ pos 12) #f)) ; owner
1369 (bytevector-u64-set! buf pos tag endianness)
1370 (bytevector-u64-set! buf (+ pos 8)
1371 (if (bitvector? obj)
1372 (bitvector-length obj)
1373 (bytevector-length obj))
1374 endianness) ; length
1375 (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
1376 (write-immediate asm buf (+ pos 24) #f)) ; owner
1377 (else (error "bad word size")))))
1379 ((uniform-vector-backing-store? obj)
1380 (let ((bv (uniform-vector-backing-store-bytes obj)))
1381 (bytevector-copy! bv 0 buf pos (bytevector-length bv))
1382 (unless (or (= 1 (uniform-vector-backing-store-element-size obj))
1383 (eq? endianness (native-endianness)))
1384 ;; Need to swap units of element-size bytes
1385 (error "FIXME: Implement byte order swap"))))
1388 (error "unrecognized object" obj))))
1391 ((vlist-null? data) #f)
1393 (let* ((byte-len (vhash-fold (lambda (k v len)
1394 (+ (byte-length k) (align len 8)))
1396 (buf (make-bytevector byte-len 0)))
1397 (let lp ((i 0) (pos 0) (symbols '()))
1398 (if (< i (vlist-length data))
1399 (let* ((pair (vlist-ref data i))
1401 (obj-label (cdr pair)))
1404 (align (+ (byte-length obj) pos) 8)
1405 (cons (make-linker-symbol obj-label pos) symbols)))
1406 (make-object asm name buf '() symbols
1408 ('.data (logior SHF_ALLOC SHF_WRITE))
1409 ('.rodata SHF_ALLOC))))))))))
1411 (define (link-constants asm)
1412 "Link sections to hold constants needed by the program text emitted
1415 Returns three values: an object for the .rodata section, an object for
1416 the .data section, and a label for an initialization procedure. Any of
1417 these may be @code{#f}."
1418 (define (shareable? x)
1422 (and (immediate? (car x)) (immediate? (cdr x))))
1425 (or (= i (vector-length x))
1426 (and (immediate? (vector-ref x i))
1428 ((uniform-vector-backing-store? x) #t)
1430 (let* ((constants (asm-constants asm))
1431 (len (vlist-length constants)))
1436 (values (link-data asm ro '.rodata)
1437 (link-data asm rw '.data)
1438 (emit-init-constants asm))
1439 (let ((pair (vlist-ref constants i)))
1440 (if (shareable? (car pair))
1441 (lp (1+ i) (vhash-consq (car pair) (cdr pair) ro) rw)
1442 (lp (1+ i) ro (vhash-consq (car pair) (cdr pair) rw))))))))
1447 ;;; Linking program text.
1450 (define (process-relocs buf relocs labels)
1451 "Patch up internal x8-s24 relocations, and any s32 relocations that
1452 reference symbols in the text section. Return a list of linker
1453 relocations for references to symbols defined outside the text section."
1455 (lambda (reloc tail)
1457 ((type label base word)
1458 (let ((abs (hashq-ref labels label))
1459 (dst (+ base word)))
1463 (let ((rel (- abs base)))
1464 (s32-set! buf dst rel)
1466 (cons (make-linker-reloc 'rel32/4 (* dst 4) word label)
1470 (error "unbound near relocation" reloc))
1471 (let ((rel (- abs base))
1472 (u32 (u32-ref buf dst)))
1473 (u32-set! buf dst (pack-u8-s24 (logand u32 #xff) rel))
1475 (else (error "bad relocation kind" reloc)))))))
1479 (define (process-labels labels)
1480 "Define linker symbols for the label-offset map in @var{labels}.
1481 The offsets are expected to be expressed in words."
1482 (hash-map->list (lambda (label loc)
1483 (make-linker-symbol label (* loc 4)))
1486 (define (swap-bytes! buf)
1487 "Patch up the text buffer @var{buf}, swapping the endianness of each
1489 (unless (zero? (modulo (bytevector-length buf) 4))
1490 (error "unexpected length"))
1491 (let ((byte-len (bytevector-length buf)))
1493 (unless (= pos byte-len)
1494 (bytevector-u32-set!
1496 (bytevector-u32-ref buf pos (endianness big))
1497 (endianness little))
1500 (define (link-text-object asm)
1501 "Link the .rtl-text section, swapping the endianness of the bytes if
1503 (let ((buf (make-u32vector (asm-pos asm))))
1504 (let lp ((pos 0) (prev (reverse (asm-prev asm))))
1506 (let ((byte-size (* (asm-idx asm) 4)))
1507 (bytevector-copy! (asm-cur asm) 0 buf pos byte-size)
1508 (unless (eq? (asm-endianness asm) (native-endianness))
1510 (make-object asm '.rtl-text
1512 (process-relocs buf (asm-relocs asm)
1514 (process-labels (asm-labels asm))))
1515 (let ((len (* *block-size* 4)))
1516 (bytevector-copy! (car prev) 0 buf pos len)
1517 (lp (+ pos len) (cdr prev)))))))
1523 ;;; Create the frame maps. These maps are used by GC to identify dead
1524 ;;; slots in pending call frames, to avoid marking them. We only do
1525 ;;; this when frame makes a non-tail call, as that is the common case.
1526 ;;; Only the topmost frame will see a GC at any other point, but we mark
1527 ;;; top frames conservatively as serializing live slot maps at every
1528 ;;; instruction would take up too much space in the object file.
1531 ;; The .guile.frame-maps section starts with two packed u32 values: one
1532 ;; indicating the offset of the first byte of the .rtl-text section, and
1533 ;; another indicating the relative offset in bytes of the slots data.
1534 (define frame-maps-prefix-len 8)
1536 ;; Each header is 8 bytes: 4 for the offset from .rtl_text, and 4 for
1537 ;; the offset of the slot map from the beginning of the
1538 ;; .guile.frame-maps section. The length of a frame map depends on the
1539 ;; frame size at the call site, and is not encoded into this section as
1540 ;; it is available at run-time.
1541 (define frame-map-header-len 8)
1543 (define (link-frame-maps asm)
1544 (define (map-byte-length proc-slot)
1545 (ceiling-quotient (- proc-slot 2) 8))
1546 (define (make-frame-maps maps count map-len)
1547 (let* ((endianness (asm-endianness asm))
1548 (header-pos frame-maps-prefix-len)
1549 (map-pos (+ header-pos (* count frame-map-header-len)))
1550 (bv (make-bytevector (+ map-pos map-len) 0)))
1551 (bytevector-u32-set! bv 4 map-pos endianness)
1552 (let lp ((maps maps) (header-pos header-pos) (map-pos map-pos))
1555 (make-object asm '.guile.frame-maps bv
1556 (list (make-linker-reloc 'abs32/1 0 0 '.rtl-text))
1557 '() #:type SHT_PROGBITS #:flags SHF_ALLOC))
1558 (((pos proc-slot . map) . maps)
1559 (bytevector-u32-set! bv header-pos (* pos 4) endianness)
1560 (bytevector-u32-set! bv (+ header-pos 4) map-pos endianness)
1561 (let write-bytes ((map-pos map-pos)
1563 (byte-length (map-byte-length proc-slot)))
1564 (if (zero? byte-length)
1565 (lp maps (+ header-pos frame-map-header-len) map-pos)
1567 (bytevector-u8-set! bv map-pos (logand map #xff))
1568 (write-bytes (1+ map-pos) (ash map -8)
1569 (1- byte-length))))))))))
1570 (match (asm-dead-slot-maps asm)
1573 (let lp ((in in) (out '()) (count 0) (map-len 0))
1575 (() (make-frame-maps out count map-len))
1576 (((and head (pos proc-slot . map)) . in)
1577 (lp in (cons head out)
1579 (+ (map-byte-length proc-slot) map-len))))))))
1584 ;;; Linking other sections of the ELF file, like the dynamic segment,
1585 ;;; the symbol table, etc.
1588 ;; FIXME: Define these somewhere central, shared with C.
1589 (define *bytecode-major-version* #x0202)
1590 (define *bytecode-minor-version* 6)
1592 (define (link-dynamic-section asm text rw rw-init frame-maps)
1593 "Link the dynamic section for an ELF image with bytecode @var{text},
1594 given the writable data section @var{rw} needing fixup from the
1595 procedure with label @var{rw-init}. @var{rw-init} may be false. If
1596 @var{rw} is true, it will be added to the GC roots at runtime."
1597 (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
1598 (let* ((endianness (asm-endianness asm))
1600 (words (if rw (+ words 4) words))
1601 (words (if rw-init (+ words 2) words))
1602 (words (if frame-maps (+ words 2) words))
1603 (bv (make-bytevector (* word-size words) 0))
1606 (%set-uword! bv (* i word-size) uword endianness)))
1610 (set! relocs (cons (make-linker-reloc 'reloc-type
1611 (* i word-size) 0 label)
1613 (%set-uword! bv (* i word-size) 0 endianness))))
1614 (set-uword! 0 DT_GUILE_VM_VERSION)
1615 (set-uword! 1 (logior (ash *bytecode-major-version* 16)
1616 *bytecode-minor-version*))
1617 (set-uword! 2 DT_GUILE_ENTRY)
1618 (set-label! 3 '.rtl-text)
1621 (set-uword! 4 DT_GUILE_GC_ROOT)
1622 (set-label! 5 '.data)
1623 (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
1624 (set-uword! 7 (bytevector-length (linker-object-bv rw)))
1626 (set-uword! 8 DT_INIT) ; constants
1627 (set-label! 9 rw-init)))
1629 (set-uword! (- words 4) DT_GUILE_FRAME_MAPS)
1630 (set-label! (- words 3) '.guile.frame-maps))
1631 (set-uword! (- words 2) DT_NULL)
1632 (set-uword! (- words 1) 0)
1633 (make-object asm '.dynamic bv relocs '()
1634 #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
1635 (case (asm-word-size asm)
1636 ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
1637 ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
1638 (else (error "bad word size" asm))))
1640 (define (link-shstrtab asm)
1641 "Link the string table for the section headers."
1642 (intern-section-name! asm ".shstrtab")
1643 (make-object asm '.shstrtab
1644 (link-string-table! (asm-shstrtab asm))
1646 #:type SHT_STRTAB #:flags 0))
1648 (define (link-symtab text-section asm)
1649 (let* ((endianness (asm-endianness asm))
1650 (word-size (asm-word-size asm))
1651 (size (elf-symbol-len word-size))
1652 (meta (reverse (asm-meta asm)))
1654 (strtab (make-string-table))
1655 (bv (make-bytevector (* n size) 0)))
1656 (define (intern-string! name)
1657 (string-table-intern! strtab (if name (symbol->string name) "")))
1660 (let ((name (intern-string! (meta-name meta))))
1661 (write-elf-symbol bv (* n size) endianness word-size
1664 ;; Symbol value and size are measured in
1666 #:value (* 4 (meta-low-pc meta))
1667 #:size (* 4 (- (meta-high-pc meta)
1668 (meta-low-pc meta)))
1670 #:visibility STV_HIDDEN
1671 #:shndx (elf-section-index text-section)))))
1673 (let ((strtab (make-object asm '.strtab
1674 (link-string-table! strtab)
1676 #:type SHT_STRTAB #:flags 0)))
1677 (values (make-object asm '.symtab
1680 #:type SHT_SYMTAB #:flags 0 #:entsize size
1681 #:link (elf-section-index
1682 (linker-object-section strtab)))
1685 ;;; The .guile.arities section describes the arities that a function can
1686 ;;; have. It is in two parts: a sorted array of headers describing
1687 ;;; basic arities, and an array of links out to a string table (and in
1688 ;;; the case of keyword arguments, to the data section) for argument
1689 ;;; names. The whole thing is prefixed by a uint32 indicating the
1690 ;;; offset of the end of the headers array.
1692 ;;; The arity headers array is a packed array of structures of the form:
1694 ;;; struct arity_header {
1695 ;;; uint32_t low_pc;
1696 ;;; uint32_t high_pc;
1697 ;;; uint32_t offset;
1701 ;;; uint32_t nlocals;
1704 ;;; All of the offsets and addresses are 32 bits. We can expand in the
1705 ;;; future to use 64-bit offsets if appropriate, but there are other
1706 ;;; aspects of bytecode that constrain us to a total image that fits in
1707 ;;; 32 bits, so for the moment we'll simplify the problem space.
1709 ;;; The following flags values are defined:
1712 ;;; #x2: allow-other-keys?
1713 ;;; #x4: has-keyword-args?
1714 ;;; #x8: is-case-lambda?
1715 ;;; #x10: is-in-case-lambda?
1717 ;;; Functions with a single arity specify their number of required and
1718 ;;; optional arguments in nreq and nopt, and do not have the
1719 ;;; is-case-lambda? flag set. Their "offset" member links to an array
1720 ;;; of pointers into the associated .guile.arities.strtab string table,
1721 ;;; identifying the argument names. This offset is relative to the
1722 ;;; start of the .guile.arities section.
1724 ;;; If the arity has keyword arguments -- if has-keyword-args? is set in
1725 ;;; the flags -- the first uint32 pointed to by offset encodes a link to
1726 ;;; the "keyword indices" literal, in the data section. Then follow the
1727 ;;; names for all locals, in order, as uleb128 values. The required
1728 ;;; arguments will be the first locals, followed by the optionals,
1729 ;;; followed by the rest argument if if has-rest? is set. The names
1730 ;;; point into the associated string table section.
1732 ;;; Functions with no arities have no arities information present in the
1733 ;;; .guile.arities section.
1735 ;;; Functions with multiple arities are preceded by a header with
1736 ;;; is-case-lambda? set. All other fields are 0, except low-pc and
1737 ;;; high-pc which should be the bounds of the whole function. Headers
1738 ;;; for the individual arities follow, with the is-in-case-lambda? flag
1739 ;;; set. In this way the whole headers array is sorted in increasing
1740 ;;; low-pc order, and case-lambda clauses are contained within the
1741 ;;; [low-pc, high-pc] of the case-lambda header.
1743 ;; Length of the prefix to the arities section, in bytes.
1744 (define arities-prefix-len 4)
1746 ;; Length of an arity header, in bytes.
1747 (define arity-header-len (* 7 4))
1750 (define (put-uleb128 port val)
1752 (let ((next (ash val -7)))
1756 (put-u8 port (logior #x80 (logand val #x7f)))
1759 (define (put-sleb128 port val)
1761 (if (<= 0 (+ val 64) 127)
1762 (put-u8 port (logand val #x7f))
1764 (put-u8 port (logior #x80 (logand val #x7f)))
1765 (lp (ash val -7))))))
1767 (define (port-position port)
1768 (seek port 0 SEEK_CUR))
1770 (define-inline (pack-arity-flags has-rest? allow-other-keys?
1771 has-keyword-args? is-case-lambda?
1773 (logior (if has-rest? (ash 1 0) 0)
1774 (if allow-other-keys? (ash 1 1) 0)
1775 (if has-keyword-args? (ash 1 2) 0)
1776 (if is-case-lambda? (ash 1 3) 0)
1777 (if is-in-case-lambda? (ash 1 4) 0)))
1779 (define (write-arities asm metas headers names-port strtab)
1780 (define (write-header pos low-pc high-pc offset flags nreq nopt nlocals)
1781 (unless (<= (+ nreq nopt) nlocals)
1782 (error "forgot to emit definition instructions?"))
1783 (bytevector-u32-set! headers pos (* low-pc 4) (asm-endianness asm))
1784 (bytevector-u32-set! headers (+ pos 4) (* high-pc 4) (asm-endianness asm))
1785 (bytevector-u32-set! headers (+ pos 8) offset (asm-endianness asm))
1786 (bytevector-u32-set! headers (+ pos 12) flags (asm-endianness asm))
1787 (bytevector-u32-set! headers (+ pos 16) nreq (asm-endianness asm))
1788 (bytevector-u32-set! headers (+ pos 20) nopt (asm-endianness asm))
1789 (bytevector-u32-set! headers (+ pos 24) nlocals (asm-endianness asm)))
1790 (define (write-kw-indices kw-indices relocs)
1791 ;; FIXME: Assert that kw-indices is already interned.
1792 (if (pair? kw-indices)
1793 (let ((pos (+ (bytevector-length headers)
1794 (port-position names-port)))
1795 (label (intern-constant asm kw-indices)))
1796 (put-bytevector names-port #vu8(0 0 0 0))
1797 (cons (make-linker-reloc 'abs32/1 pos 0 label) relocs))
1799 (define (write-arity pos arity in-case-lambda? relocs)
1800 (write-header pos (arity-low-pc arity)
1801 (arity-high-pc arity)
1802 ;; FIXME: Seems silly to add on bytevector-length of
1803 ;; headers, given the arities-prefix.
1804 (+ (bytevector-length headers) (port-position names-port))
1805 (pack-arity-flags (arity-rest arity)
1806 (arity-allow-other-keys? arity)
1807 (pair? (arity-kw-indices arity))
1810 (length (arity-req arity))
1811 (length (arity-opt arity))
1812 (length (arity-definitions arity)))
1813 (let ((relocs (write-kw-indices (arity-kw-indices arity) relocs)))
1814 ;; Write local names.
1815 (let lp ((definitions (arity-definitions arity)))
1818 ((#(name slot def) . definitions)
1819 (let ((sym (if (symbol? name)
1820 (string-table-intern! strtab (symbol->string name))
1822 (put-uleb128 names-port sym)
1823 (lp definitions)))))
1824 ;; Now write their definitions.
1825 (let lp ((definitions (arity-definitions arity)))
1828 ((#(name slot def) . definitions)
1829 (put-uleb128 names-port def)
1830 (put-uleb128 names-port slot)
1831 (lp definitions))))))
1832 (let lp ((metas metas) (pos arities-prefix-len) (relocs '()))
1835 (unless (= pos (bytevector-length headers))
1836 (error "expected to fully fill the bytevector"
1837 pos (bytevector-length headers)))
1840 (match (meta-arities meta)
1841 (() (lp metas pos relocs))
1844 (+ pos arity-header-len)
1845 (write-arity pos arity #f relocs)))
1847 ;; Write a case-lambda header, then individual arities.
1848 ;; The case-lambda header's offset link is 0.
1849 (write-header pos (meta-low-pc meta) (meta-high-pc meta) 0
1850 (pack-arity-flags #f #f #f #t #f) 0 0 0)
1851 (let lp* ((arities arities) (pos (+ pos arity-header-len))
1854 (() (lp metas pos relocs))
1857 (+ pos arity-header-len)
1858 (write-arity pos arity #t relocs)))))))))))
1860 (define (link-arities asm)
1861 (define (meta-arities-header-size meta)
1862 (define (lambda-size arity)
1864 (define (case-lambda-size arities)
1866 arity-header-len ;; case-lambda header
1867 (map lambda-size arities))) ;; the cases
1868 (match (meta-arities meta)
1870 ((arity) (lambda-size arity))
1871 (arities (case-lambda-size arities))))
1873 (define (bytevector-append a b)
1874 (let ((out (make-bytevector (+ (bytevector-length a)
1875 (bytevector-length b)))))
1876 (bytevector-copy! a 0 out 0 (bytevector-length a))
1877 (bytevector-copy! b 0 out (bytevector-length a) (bytevector-length b))
1880 (let* ((endianness (asm-endianness asm))
1881 (metas (reverse (asm-meta asm)))
1882 (header-size (fold (lambda (meta size)
1883 (+ size (meta-arities-header-size meta)))
1886 (strtab (make-string-table))
1887 (headers (make-bytevector header-size 0)))
1888 (bytevector-u32-set! headers 0 (bytevector-length headers) endianness)
1889 (let-values (((names-port get-name-bv) (open-bytevector-output-port)))
1890 (let* ((relocs (write-arities asm metas headers names-port strtab))
1891 (strtab (make-object asm '.guile.arities.strtab
1892 (link-string-table! strtab)
1894 #:type SHT_STRTAB #:flags 0)))
1895 (values (make-object asm '.guile.arities
1896 (bytevector-append headers (get-name-bv))
1898 #:type SHT_PROGBITS #:flags 0
1899 #:link (elf-section-index
1900 (linker-object-section strtab)))
1904 ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
1905 ;;; values. Pc and str are both 32 bits wide. (Either could change to
1906 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1907 ;;; entry to a program, relative to the start of the text section, in
1908 ;;; bytes, and str is an index into the associated .guile.docstrs.strtab
1909 ;;; string table section.
1912 ;; The size of a docstrs entry, in bytes.
1913 (define docstr-size 8)
1915 (define (link-docstrs asm)
1916 (define (find-docstrings)
1917 (filter-map (lambda (meta)
1918 (define (is-documentation? pair)
1919 (eq? (car pair) 'documentation))
1920 (let* ((props (meta-properties meta))
1921 (tail (find-tail is-documentation? props)))
1923 (not (find-tail is-documentation? (cdr tail)))
1924 (string? (cdar tail))
1925 (cons (* 4 (meta-low-pc meta)) (cdar tail)))))
1926 (reverse (asm-meta asm))))
1927 (let* ((endianness (asm-endianness asm))
1928 (docstrings (find-docstrings))
1929 (strtab (make-string-table))
1930 (bv (make-bytevector (* (length docstrings) docstr-size) 0)))
1931 (fold (lambda (pair pos)
1934 (bytevector-u32-set! bv pos pc endianness)
1935 (bytevector-u32-set! bv (+ pos 4)
1936 (string-table-intern! strtab string)
1938 (+ pos docstr-size))))
1941 (let ((strtab (make-object asm '.guile.docstrs.strtab
1942 (link-string-table! strtab)
1944 #:type SHT_STRTAB #:flags 0)))
1945 (values (make-object asm '.guile.docstrs
1948 #:type SHT_PROGBITS #:flags 0
1949 #:link (elf-section-index
1950 (linker-object-section strtab)))
1954 ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
1955 ;;; values. Pc and addr are both 32 bits wide. (Either could change to
1956 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1957 ;;; entry to a program, relative to the start of the text section, and
1958 ;;; addr is the address of the associated properties alist, relative to
1959 ;;; the start of the ELF image.
1961 ;;; Since procedure properties are stored in the data sections, we need
1962 ;;; to link the procedures property section first. (Note that this
1963 ;;; constraint does not apply to the arities section, which may
1964 ;;; reference the data sections via the kw-indices literal, because
1965 ;;; assembling the text section already makes sure that the kw-indices
1969 ;; The size of a procprops entry, in bytes.
1970 (define procprops-size 8)
1972 (define (link-procprops asm)
1973 (define (assoc-remove-one alist key value-pred)
1976 ((((? (lambda (x) (eq? x key))) . value) . alist)
1977 (if (value-pred value)
1979 (acons key value alist)))
1981 (acons k v (assoc-remove-one alist key value-pred)))))
1982 (define (props-without-name-or-docstring meta)
1984 (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
1987 (define (find-procprops)
1988 (filter-map (lambda (meta)
1989 (let ((props (props-without-name-or-docstring meta)))
1991 (cons (* 4 (meta-low-pc meta)) props))))
1992 (reverse (asm-meta asm))))
1993 (let* ((endianness (asm-endianness asm))
1994 (procprops (find-procprops))
1995 (bv (make-bytevector (* (length procprops) procprops-size) 0)))
1996 (let lp ((procprops procprops) (pos 0) (relocs '()))
1999 (make-object asm '.guile.procprops
2002 #:type SHT_PROGBITS #:flags 0))
2003 (((pc . props) . procprops)
2004 (bytevector-u32-set! bv pos pc endianness)
2006 (+ pos procprops-size)
2007 (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
2008 (intern-constant asm props))
2012 ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
2013 ;;; sections provide line number and local variable liveness
2014 ;;; information. Their format is defined by the DWARF
2018 (define (asm-language asm)
2019 ;; FIXME: Plumb language through to the assembler.
2022 ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
2023 (define (link-debug asm)
2024 (define (put-s8 port val)
2025 (let ((bv (make-bytevector 1)))
2026 (bytevector-s8-set! bv 0 val)
2027 (put-bytevector port bv)))
2029 (define (put-u16 port val)
2030 (let ((bv (make-bytevector 2)))
2031 (bytevector-u16-set! bv 0 val (asm-endianness asm))
2032 (put-bytevector port bv)))
2034 (define (put-u32 port val)
2035 (let ((bv (make-bytevector 4)))
2036 (bytevector-u32-set! bv 0 val (asm-endianness asm))
2037 (put-bytevector port bv)))
2039 (define (put-u64 port val)
2040 (let ((bv (make-bytevector 8)))
2041 (bytevector-u64-set! bv 0 val (asm-endianness asm))
2042 (put-bytevector port bv)))
2044 (define (meta->subprogram-die meta)
2048 => (lambda (name) `((name ,(symbol->string name)))))
2051 (low-pc ,(meta-label meta))
2052 (high-pc ,(* 4 (- (meta-high-pc meta) (meta-low-pc meta)))))))
2054 (define (make-compile-unit-die asm)
2056 (@ (producer ,(string-append "Guile " (version)))
2057 (language ,(asm-language asm))
2059 (high-pc ,(* 4 (asm-pos asm)))
2061 ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
2063 (let-values (((die-port get-die-bv) (open-bytevector-output-port))
2065 ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
2066 ;; (tag has-kids? attrs forms) -> code
2067 ((abbrevs) vlist-null)
2068 ((strtab) (make-string-table))
2069 ((line-port get-line-bv) (open-bytevector-output-port))
2072 ((files) vlist-null))
2074 (define (write-abbrev code tag has-children? attrs forms)
2075 (put-uleb128 abbrev-port code)
2076 (put-uleb128 abbrev-port (tag-name->code tag))
2077 (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
2078 (for-each (lambda (attr form)
2079 (put-uleb128 abbrev-port (attribute-name->code attr))
2080 (put-uleb128 abbrev-port (form-name->code form)))
2082 (put-uleb128 abbrev-port 0)
2083 (put-uleb128 abbrev-port 0))
2085 (define (intern-abbrev tag has-children? attrs forms)
2086 (let ((key (list tag has-children? attrs forms)))
2087 (match (vhash-assoc key abbrevs)
2089 (#f (let ((code (1+ (vlist-length abbrevs))))
2090 (set! abbrevs (vhash-cons key code abbrevs))
2091 (write-abbrev code tag has-children? attrs forms)
2094 (define (intern-file file)
2095 (match (vhash-assoc file files)
2097 (#f (let ((code (1+ (vlist-length files))))
2098 (set! files (vhash-cons file code files))
2101 (define (write-sources)
2102 ;; Choose line base and line range values that will allow for an
2103 ;; address advance range of 16 words. The special opcode range is
2104 ;; from 10 to 255, so 246 values.
2108 (let lp ((sources (asm-sources asm)) (out '()))
2110 (((pc . s) . sources)
2111 (let ((file (assq-ref s 'filename))
2112 (line (assq-ref s 'line))
2113 (col (assq-ref s 'column)))
2115 ;; Guile line and column numbers are 0-indexed, but
2116 ;; they are 1-indexed for DWARF.
2118 (if file (intern-file file) 0)
2123 ;; Compilation unit header for .debug_line. We write in
2124 ;; DWARF 2 format because more tools understand it than DWARF
2125 ;; 4, which incompatibly adds another field to this header.
2127 (put-u32 line-port 0) ; Length; will patch later.
2128 (put-u16 line-port 2) ; DWARF 2 format.
2129 (put-u32 line-port 0) ; Prologue length; will patch later.
2130 (put-u8 line-port 4) ; Minimum instruction length: 4 bytes.
2131 (put-u8 line-port 1) ; Default is-stmt: true.
2133 (put-s8 line-port base) ; Line base. See the DWARF standard.
2134 (put-u8 line-port range) ; Line range. See the DWARF standard.
2135 (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
2137 ;; A table of the number of uleb128 arguments taken by each
2138 ;; of the standard opcodes.
2139 (put-u8 line-port 0) ; 1: copy
2140 (put-u8 line-port 1) ; 2: advance-pc
2141 (put-u8 line-port 1) ; 3: advance-line
2142 (put-u8 line-port 1) ; 4: set-file
2143 (put-u8 line-port 1) ; 5: set-column
2144 (put-u8 line-port 0) ; 6: negate-stmt
2145 (put-u8 line-port 0) ; 7: set-basic-block
2146 (put-u8 line-port 0) ; 8: const-add-pc
2147 (put-u8 line-port 1) ; 9: fixed-advance-pc
2149 ;; Include directories, as a zero-terminated sequence of
2150 ;; nul-terminated strings. Nothing, for the moment.
2151 (put-u8 line-port 0)
2153 ;; File table. For each file that contributes to this
2154 ;; compilation unit, a nul-terminated file name string, and a
2155 ;; uleb128 for each of directory the file was found in, the
2156 ;; modification time, and the file's size in bytes. We pass
2157 ;; zero for the latter three fields.
2162 (put-bytevector line-port (string->utf8 file))
2163 (put-u8 line-port 0)
2164 (put-uleb128 line-port 0) ; directory
2165 (put-uleb128 line-port 0) ; mtime
2166 (put-uleb128 line-port 0))) ; size
2170 (put-u8 line-port 0) ; 0 byte terminating file list.
2172 ;; Patch prologue length.
2173 (let ((offset (port-position line-port)))
2174 (seek line-port 6 SEEK_SET)
2175 (put-u32 line-port (- offset 10))
2176 (seek line-port offset SEEK_SET))
2178 ;; Now write the statement program.
2180 (define (extended-op opcode payload-len)
2181 (put-u8 line-port 0) ; extended op
2182 (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
2183 (put-uleb128 line-port opcode))
2184 (define (set-address sym)
2185 (define (add-reloc! kind)
2187 (cons (make-linker-reloc kind
2188 (port-position line-port)
2192 (match (asm-word-size asm)
2195 (add-reloc! 'abs32/1)
2196 (put-u32 line-port 0))
2199 (add-reloc! 'abs64/1)
2200 (put-u64 line-port 0))))
2201 (define (end-sequence pc)
2202 (let ((pc-inc (- (asm-pos asm) pc)))
2203 (put-u8 line-port 2) ; advance-pc
2204 (put-uleb128 line-port pc-inc))
2206 (define (advance-pc pc-inc line-inc)
2207 (let ((spec (+ (- line-inc base) (* pc-inc range) 10)))
2209 ((or (< line-inc base) (>= line-inc (+ base range)))
2210 (advance-line line-inc)
2211 (advance-pc pc-inc 0))
2213 (put-u8 line-port spec))
2215 (put-u8 line-port 8) ; const-advance-pc
2216 (advance-pc (- pc-inc (floor/ (- 255 10) range))
2219 (put-u8 line-port 2) ; advance-pc
2220 (put-uleb128 line-port pc-inc)
2221 (advance-pc 0 line-inc)))))
2222 (define (advance-line inc)
2223 (put-u8 line-port 3)
2224 (put-sleb128 line-port inc))
2225 (define (set-file file)
2226 (put-u8 line-port 4)
2227 (put-uleb128 line-port file))
2228 (define (set-column col)
2229 (put-u8 line-port 5)
2230 (put-uleb128 line-port col))
2232 (set-address '.rtl-text)
2234 (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
2238 ;; There was no source info in the first place. Set
2239 ;; file register to 0 before adding final row.
2242 (((pc* file* line* col*) . in*)
2244 ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
2245 (lp in* pc file line col))
2247 (unless (eqv? col col*)
2249 (unless (eqv? file file*)
2251 (advance-pc (- pc* pc) (- line* line))
2252 (lp in* pc* file* line* col*)))))))))))
2254 (define (compute-code attr val)
2256 ('name (string-table-intern! strtab val))
2259 ('producer (string-table-intern! strtab val))
2260 ('language (language-name->code val))
2263 (define (choose-form attr val code)
2265 ((string? val) 'strp)
2266 ((eq? attr 'stmt-list) 'sec-offset)
2267 ((eq? attr 'low-pc) 'addr)
2268 ((exact-integer? code)
2270 ((< code 0) 'sleb128)
2271 ((<= code #xff) 'data1)
2272 ((<= code #xffff) 'data2)
2273 ((<= code #xffffffff) 'data4)
2274 ((<= code #xffffffffffffffff) 'data8)
2276 (else (error "unhandled case" attr val code))))
2278 (define (add-die-relocation! kind sym)
2280 (cons (make-linker-reloc kind (port-position die-port) 0 sym)
2283 (define (write-value code form)
2285 ('data1 (put-u8 die-port code))
2286 ('data2 (put-u16 die-port code))
2287 ('data4 (put-u32 die-port code))
2288 ('data8 (put-u64 die-port code))
2289 ('uleb128 (put-uleb128 die-port code))
2290 ('sleb128 (put-sleb128 die-port code))
2292 (match (asm-word-size asm)
2294 (add-die-relocation! 'abs32/1 code)
2295 (put-u32 die-port 0))
2297 (add-die-relocation! 'abs64/1 code)
2298 (put-u64 die-port 0))))
2299 ('sec-offset (put-u32 die-port code))
2300 ('strp (put-u32 die-port code))))
2302 (define (write-die die)
2304 ((tag ('@ (attrs vals) ...) children ...)
2305 (let* ((codes (map compute-code attrs vals))
2306 (forms (map choose-form attrs vals codes))
2307 (has-children? (not (null? children)))
2308 (abbrev-code (intern-abbrev tag has-children? attrs forms)))
2309 (put-uleb128 die-port abbrev-code)
2310 (for-each write-value codes forms)
2312 (for-each write-die children)
2313 (put-uleb128 die-port 0))))))
2315 ;; Compilation unit header.
2316 (put-u32 die-port 0) ; Length; will patch later.
2317 (put-u16 die-port 4) ; DWARF 4.
2318 (put-u32 die-port 0) ; Abbrevs offset.
2319 (put-u8 die-port (asm-word-size asm)) ; Address size.
2321 (write-die (make-compile-unit-die asm))
2323 ;; Terminate the abbrevs list.
2324 (put-uleb128 abbrev-port 0)
2328 (values (let ((bv (get-die-bv)))
2329 ;; Patch DWARF32 length.
2330 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2331 (asm-endianness asm))
2332 (make-object asm '.debug_info bv die-relocs '()
2333 #:type SHT_PROGBITS #:flags 0))
2334 (make-object asm '.debug_abbrev (get-abbrev-bv) '() '()
2335 #:type SHT_PROGBITS #:flags 0)
2336 (make-object asm '.debug_str (link-string-table! strtab) '() '()
2337 #:type SHT_PROGBITS #:flags 0)
2338 (make-object asm '.debug_loc #vu8() '() '()
2339 #:type SHT_PROGBITS #:flags 0)
2340 (let ((bv (get-line-bv)))
2341 ;; Patch DWARF32 length.
2342 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2343 (asm-endianness asm))
2344 (make-object asm '.debug_line bv line-relocs '()
2345 #:type SHT_PROGBITS #:flags 0)))))
2347 (define (link-objects asm)
2348 (let*-values (;; Link procprops before constants, because it probably
2349 ;; interns more constants.
2350 ((procprops) (link-procprops asm))
2351 ((ro rw rw-init) (link-constants asm))
2352 ;; Link text object after constants, so that the
2353 ;; constants initializer gets included.
2354 ((text) (link-text-object asm))
2355 ((frame-maps) (link-frame-maps asm))
2356 ((dt) (link-dynamic-section asm text rw rw-init frame-maps))
2357 ((symtab strtab) (link-symtab (linker-object-section text) asm))
2358 ((arities arities-strtab) (link-arities asm))
2359 ((docstrs docstrs-strtab) (link-docstrs asm))
2360 ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
2361 ;; This needs to be linked last, because linking other
2362 ;; sections adds entries to the string table.
2363 ((shstrtab) (link-shstrtab asm)))
2365 (list text ro frame-maps rw dt symtab strtab
2366 arities arities-strtab
2367 docstrs docstrs-strtab procprops
2368 dinfo dabbrev dstrtab dloc dline
2375 ;;; High-level public interfaces.
2378 (define* (link-assembly asm #:key (page-aligned? #t))
2379 "Produce an ELF image from the code and data emitted into @var{asm}.
2380 The result is a bytevector, by default linked so that read-only and
2381 writable data are on separate pages. Pass @code{#:page-aligned? #f} to
2382 disable this behavior."
2383 (link-elf (link-objects asm) #:page-aligned? page-aligned?))