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-br-if-logtest* . emit-br-if-logtest)
99 (emit-mov* . emit-mov)
100 (emit-box* . emit-box)
101 (emit-box-ref* . emit-box-ref)
102 (emit-box-set!* . emit-box-set!)
104 (emit-free-ref* . emit-free-ref)
105 (emit-free-set!* . emit-free-set!)
108 (emit-define!* . emit-define!)
112 (emit-wind* . emit-wind)
114 (emit-push-fluid* . emit-push-fluid)
116 (emit-fluid-ref* . emit-fluid-ref)
117 (emit-fluid-set* . emit-fluid-set)
118 (emit-string-length* . emit-string-length)
119 (emit-string-ref* . emit-string-ref)
120 (emit-string->number* . emit-string->number)
121 (emit-string->symbol* . emit-string->symbol)
122 (emit-symbol->keyword* . emit-symbol->keyword)
123 (emit-cons* . emit-cons)
124 (emit-car* . emit-car)
125 (emit-cdr* . emit-cdr)
126 (emit-set-car!* . emit-set-car!)
127 (emit-set-cdr!* . emit-set-cdr!)
128 (emit-add* . emit-add)
129 (emit-add1* . emit-add1)
130 (emit-sub* . emit-sub)
131 (emit-sub1* . emit-sub1)
132 (emit-mul* . emit-mul)
133 (emit-div* . emit-div)
134 (emit-quo* . emit-quo)
135 (emit-rem* . emit-rem)
136 (emit-mod* . emit-mod)
137 (emit-ash* . emit-ash)
138 (emit-logand* . emit-logand)
139 (emit-logior* . emit-logior)
140 (emit-logxor* . emit-logxor)
141 (emit-make-vector* . emit-make-vector)
142 (emit-make-vector/immediate* . emit-make-vector/immediate)
143 (emit-vector-length* . emit-vector-length)
144 (emit-vector-ref* . emit-vector-ref)
145 (emit-vector-ref/immediate* . emit-vector-ref/immediate)
146 (emit-vector-set!* . emit-vector-set!)
147 (emit-vector-set!/immediate* . emit-vector-set!/immediate)
148 (emit-struct-vtable* . emit-struct-vtable)
149 (emit-allocate-struct/immediate* . emit-allocate-struct/immediate)
150 (emit-struct-ref/immediate* . emit-struct-ref/immediate)
151 (emit-struct-set!/immediate* . emit-struct-set!/immediate)
152 (emit-class-of* . emit-class-of)
153 (emit-make-array* . emit-make-array)
154 (emit-bv-u8-ref* . emit-bv-u8-ref)
155 (emit-bv-s8-ref* . emit-bv-s8-ref)
156 (emit-bv-u16-ref* . emit-bv-u16-ref)
157 (emit-bv-s16-ref* . emit-bv-s16-ref)
158 (emit-bv-u32-ref* . emit-bv-u32-ref)
159 (emit-bv-s32-ref* . emit-bv-s32-ref)
160 (emit-bv-u64-ref* . emit-bv-u64-ref)
161 (emit-bv-s64-ref* . emit-bv-s64-ref)
162 (emit-bv-f32-ref* . emit-bv-f32-ref)
163 (emit-bv-f64-ref* . emit-bv-f64-ref)
164 (emit-bv-u8-set!* . emit-bv-u8-set!)
165 (emit-bv-s8-set!* . emit-bv-s8-set!)
166 (emit-bv-u16-set!* . emit-bv-u16-set!)
167 (emit-bv-s16-set!* . emit-bv-s16-set!)
168 (emit-bv-u32-set!* . emit-bv-u32-set!)
169 (emit-bv-s32-set!* . emit-bv-s32-set!)
170 (emit-bv-u64-set!* . emit-bv-u64-set!)
171 (emit-bv-s64-set!* . emit-bv-s64-set!)
172 (emit-bv-f32-set!* . emit-bv-f32-set!)
173 (emit-bv-f64-set!* . emit-bv-f64-set!)
181 ;; Like define-inlinable, but only for first-order uses of the defined
182 ;; routine. Should residualize less code.
184 (define-syntax define-inline
187 ((_ (name arg ...) body ...)
188 (with-syntax (((temp ...) (generate-temporaries #'(arg ...))))
189 #`(eval-when (expand)
190 (define-syntax-rule (name temp ...)
191 (let ((arg temp) ...)
194 ;;; Bytecode consists of 32-bit units, often subdivided in some way.
195 ;;; These helpers create one 32-bit unit from multiple components.
197 (define-inline (pack-u8-u24 x y)
199 (error "out of range" x))
200 (logior x (ash y 8)))
202 (define-inline (pack-u8-s24 x y)
204 (error "out of range" x))
206 ((< 0 (- y) #x800000)
210 (else (error "out of range" y)))
213 (define-inline (pack-u1-u7-u24 x y z)
215 (error "out of range" x))
217 (error "out of range" y))
218 (logior x (ash y 1) (ash z 8)))
220 (define-inline (pack-u8-u12-u12 x y z)
222 (error "out of range" x))
223 (unless (<= 0 y 4095)
224 (error "out of range" y))
225 (logior x (ash y 8) (ash z 20)))
227 (define-inline (pack-u8-u8-u16 x y z)
229 (error "out of range" x))
231 (error "out of range" y))
232 (logior x (ash y 8) (ash z 16)))
234 (define-inline (pack-u8-u8-u8-u8 x y z w)
236 (error "out of range" x))
238 (error "out of range" y))
240 (error "out of range" z))
241 (logior x (ash y 8) (ash z 16) (ash w 24)))
244 (define-syntax pack-flags
246 ;; Add clauses as needed.
247 ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
248 (if f2 (ash 2 0) 0))))))
250 ;;; Helpers to read and write 32-bit units in a buffer.
252 (define-inline (u32-ref buf n)
253 (bytevector-u32-native-ref buf (* n 4)))
255 (define-inline (u32-set! buf n val)
256 (bytevector-u32-native-set! buf (* n 4) val))
258 (define-inline (s32-ref buf n)
259 (bytevector-s32-native-ref buf (* n 4)))
261 (define-inline (s32-set! buf n val)
262 (bytevector-s32-native-set! buf (* n 4) val))
267 ;;; A <meta> entry collects metadata for one procedure. Procedures are
268 ;;; written as contiguous ranges of bytecode.
271 (define-syntax-rule (assert-match arg pattern kind)
273 (unless (match x (pattern #t) (_ #f))
274 (error (string-append "expected " kind) x)))))
276 (define-record-type <meta>
277 (%make-meta label properties low-pc high-pc arities)
280 (properties meta-properties set-meta-properties!)
282 (high-pc meta-high-pc set-meta-high-pc!)
283 (arities meta-arities set-meta-arities!))
285 (define (make-meta label properties low-pc)
286 (assert-match label (or (? exact-integer?) (? symbol?)) "symbol")
287 (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
288 (%make-meta label properties low-pc #f '()))
290 (define (meta-name meta)
291 (assq-ref (meta-properties meta) 'name))
293 ;; Metadata for one <lambda-case>.
294 (define-record-type <arity>
295 (make-arity req opt rest kw-indices allow-other-keys?
296 low-pc high-pc definitions)
301 (kw-indices arity-kw-indices)
302 (allow-other-keys? arity-allow-other-keys?)
303 (low-pc arity-low-pc)
304 (high-pc arity-high-pc set-arity-high-pc!)
305 (definitions arity-definitions set-arity-definitions!))
308 (define-syntax *block-size* (identifier-syntax 32)))
310 ;;; An assembler collects all of the words emitted during assembly, and
311 ;;; also maintains ancillary information such as the constant table, a
312 ;;; relocation list, and so on.
314 ;;; Bytecode consists of 32-bit units. We emit bytecode using native
315 ;;; endianness. If we're targeting a foreign endianness, we byte-swap
316 ;;; the bytevector as a whole instead of conditionalizing each access.
318 (define-record-type <asm>
319 (make-asm cur idx start prev written
323 shstrtab next-section-number
328 ;; We write bytecode into what is logically a growable vector,
329 ;; implemented as a list of blocks. asm-cur is the current block, and
330 ;; asm-idx is the current index into that block, in 32-bit units.
332 (cur asm-cur set-asm-cur!)
333 (idx asm-idx set-asm-idx!)
335 ;; asm-start is an absolute position, indicating the offset of the
336 ;; beginning of an instruction (in u32 units). It is updated after
337 ;; writing all the words for one primitive instruction. It models the
338 ;; position of the instruction pointer during execution, given that
339 ;; the VM updates the IP only at the end of executing the instruction,
340 ;; and is thus useful for computing offsets between two points in a
343 (start asm-start set-asm-start!)
345 ;; The list of previously written blocks.
347 (prev asm-prev set-asm-prev!)
349 ;; The number of u32 words written in asm-prev, which is the same as
350 ;; the offset of the current block.
352 (written asm-written set-asm-written!)
354 ;; An alist of symbol -> position pairs, indicating the labels defined
355 ;; in this compilation unit.
357 (labels asm-labels set-asm-labels!)
359 ;; A list of relocations needed by the program text. We use an
360 ;; internal representation for relocations, and handle textualn
361 ;; relative relocations in the assembler. Other kinds of relocations
362 ;; are later reified as linker relocations and resolved by the linker.
364 (relocs asm-relocs set-asm-relocs!)
366 ;; Target information.
368 (word-size asm-word-size)
369 (endianness asm-endianness)
371 ;; The constant table, as a vhash of object -> label. All constants
372 ;; get de-duplicated and written into separate sections -- either the
373 ;; .rodata section, for read-only data, or .data, for constants that
374 ;; need initialization at load-time (like symbols). Constants can
375 ;; depend on other constants (e.g. a symbol depending on a stringbuf),
376 ;; so order in this table is important.
378 (constants asm-constants set-asm-constants!)
380 ;; A list of instructions needed to initialize the constants. Will
381 ;; run in a thunk with 2 local variables.
383 (inits asm-inits set-asm-inits!)
385 ;; The shstrtab, for section names.
387 (shstrtab asm-shstrtab set-asm-shstrtab!)
389 ;; The section number for the next section to be written.
391 (next-section-number asm-next-section-number set-asm-next-section-number!)
393 ;; A list of <meta>, corresponding to procedure metadata.
395 (meta asm-meta set-asm-meta!)
397 ;; A list of (pos . source) pairs, indicating source information. POS
398 ;; is relative to the beginning of the text section, and SOURCE is in
399 ;; the same format that source-properties returns.
401 (sources asm-sources set-asm-sources!)
403 ;; A list of (pos . dead-slot-map) pairs, indicating dead slot maps.
404 ;; POS is relative to the beginning of the text section.
405 ;; DEAD-SLOT-MAP is a bitfield of slots that are dead at call sites,
408 (dead-slot-maps asm-dead-slot-maps set-asm-dead-slot-maps!))
410 (define-inline (fresh-block)
411 (make-u32vector *block-size*))
413 (define* (make-assembler #:key (word-size (target-word-size))
414 (endianness (target-endianness)))
415 "Create an assembler for a given target @var{word-size} and
416 @var{endianness}, falling back to appropriate values for the configured
418 (make-asm (fresh-block) 0 0 '() 0
419 (make-hash-table) '()
422 (make-string-table) 1
425 (define (intern-section-name! asm string)
426 "Add a string to the section name table (shstrtab)."
427 (string-table-intern! (asm-shstrtab asm) string))
429 (define-inline (asm-pos asm)
430 "The offset of the next word to be written into the code buffer, in
432 (+ (asm-idx asm) (asm-written asm)))
434 (define (allocate-new-block asm)
435 "Close off the current block, and arrange for the next word to be
436 written to a fresh block."
437 (let ((new (fresh-block)))
438 (set-asm-prev! asm (cons (asm-cur asm) (asm-prev asm)))
439 (set-asm-written! asm (asm-pos asm))
440 (set-asm-cur! asm new)
441 (set-asm-idx! asm 0)))
443 (define-inline (emit asm u32)
444 "Emit one 32-bit word into the instruction stream. Assumes that there
445 is space for the word, and ensures that there is space for the next
447 (u32-set! (asm-cur asm) (asm-idx asm) u32)
448 (set-asm-idx! asm (1+ (asm-idx asm)))
449 (if (= (asm-idx asm) *block-size*)
450 (allocate-new-block asm)))
452 (define-inline (make-reloc type label base word)
453 "Make an internal relocation of type @var{type} referencing symbol
454 @var{label}, @var{word} words after position @var{start}. @var{type}
455 may be x8-s24, indicating a 24-bit relative label reference that can be
456 fixed up by the assembler, or s32, indicating a 32-bit relative
457 reference that needs to be fixed up by the linker."
458 (list type label base word))
460 (define-inline (reset-asm-start! asm)
461 "Reset the asm-start after writing the words for one instruction."
462 (set-asm-start! asm (asm-pos asm)))
464 (define (record-label-reference asm label)
465 "Record an x8-s24 local label reference. This value will get patched
466 up later by the assembler."
467 (let* ((start (asm-start asm))
469 (reloc (make-reloc 'x8-s24 label start (- pos start))))
470 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
472 (define* (record-far-label-reference asm label #:optional (offset 0))
473 "Record an s32 far label reference. This value will get patched up
474 later by the linker."
475 (let* ((start (- (asm-start asm) offset))
477 (reloc (make-reloc 's32 label start (- pos start))))
478 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
484 ;;; Primitive assemblers are defined by expanding `assembler' for each
485 ;;; opcode in `(instruction-list)'.
489 (define (id-append ctx a b)
490 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
492 (define-syntax assembler
494 (define-syntax op-case
497 ((_ asm name ((type arg ...) code ...) clause ...)
498 #`(if (eq? name 'type)
499 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
502 (op-case asm name clause ...)))
504 #'(error "unmatched name" name)))))
506 (define (pack-first-word asm opcode type)
507 (with-syntax ((opcode opcode))
513 (emit asm (pack-u8-u24 opcode arg)))
515 (record-label-reference asm label)
518 (emit asm (pack-u8-u8-u16 opcode a (object-address imm))))
520 (emit asm (pack-u8-u12-u12 opcode a b)))
522 (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
524 (define (pack-tail-word asm type)
528 (emit asm (pack-u8-u24 a b)))
530 (record-label-reference asm label)
535 (let ((val (object-address imm)))
536 (unless (zero? (ash val -32))
537 (error "FIXME: enable truncation of negative fixnums when cross-compiling"))
540 (unless (= (asm-word-size asm) 8)
541 (error "make-long-immediate unavailable for this target"))
542 (emit asm (ash (object-address imm) -32))
543 (emit asm (logand (object-address imm) (1- (ash 1 32)))))
546 (record-far-label-reference asm label)
549 (record-far-label-reference asm label)
552 (record-far-label-reference asm label)
555 (record-far-label-reference asm label
556 (* offset (/ (asm-word-size asm) 4)))
559 (emit asm (pack-u8-u24 0 a)))
561 (record-label-reference asm label)
564 (record-label-reference asm label)
565 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
566 ((B1_U7_L24 a b label)
567 (record-label-reference asm label)
568 (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
570 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
572 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))))
575 ((_ name opcode word0 word* ...)
576 (with-syntax ((((formal0 ...)
578 (pack-first-word #'asm
579 (syntax->datum #'opcode)
580 (syntax->datum #'word0)))
583 (map (lambda (word) (pack-tail-word #'asm word))
584 (syntax->datum #'(word* ...)))))
585 #'(lambda (asm formal0 ... formal* ... ...)
586 (unless (asm? asm) (error "not an asm"))
589 (reset-asm-start! asm))))))))
591 (define assemblers (make-hash-table))
594 (define-syntax define-assembler
597 ((_ name opcode kind arg ...)
598 (with-syntax ((emit (id-append #'name #'emit- #'name)))
600 (let ((emit (assembler name opcode arg ...)))
601 (hashq-set! assemblers 'name emit)
604 (define-syntax visit-opcodes
607 ((visit-opcodes macro arg ...)
608 (with-syntax (((inst ...)
609 (map (lambda (x) (datum->syntax #'macro x))
610 (instruction-list))))
612 (macro arg ... . inst)
615 (visit-opcodes define-assembler)
619 ;; Some operands are encoded using a restricted subset of the full
620 ;; 24-bit local address space, in order to make the bytecode more
621 ;; dense in the usual case that there are few live locals. Here we
622 ;; define wrapper emitters that shuffle out-of-range operands into and
623 ;; out of the reserved range of locals [233,255]. This range is
624 ;; sufficient because these restricted operands are only present in
625 ;; the first word of an instruction. Since 8 bits is the smallest
626 ;; slot-addressing operand size, that means we can fit 3 operands in
627 ;; the 24 bits of payload of the first word (the lower 8 bits being
628 ;; taken by the opcode).
630 ;; The result are wrapper emitters with the same arity,
631 ;; e.g. emit-cons* that wraps emit-cons. We expose these wrappers as
632 ;; the public interface for emitting `cons' instructions. That way we
633 ;; solve the problem fully and in just one place. The only manual
634 ;; care that need be taken is in the exports list at the top of the
635 ;; file -- to be sure that we export the wrapper and not the wrapped
638 (define (shuffling-assembler name kind word0 word*)
639 (define (analyze-first-word)
640 (define-syntax op-case
642 ((_ type ((%type %kind arg ...) values) clause ...)
643 (if (and (eq? type '%type) (eq? kind '%kind))
644 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
645 #'((arg ...) values))
646 (op-case type clause ...)))
652 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
654 ((U8_U8_I16 <- a imm)
655 (values (if (< a (ash 1 8)) a 253)
658 (values (if (< a (ash 1 12)) a (begin (emit-mov* asm 253 a) 253))
659 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
661 (values (if (< a (ash 1 12)) a 253)
662 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
663 ((U8_U8_U8_U8 ! a b c)
664 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
665 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
666 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))
667 ((U8_U8_U8_U8 <- a b c)
668 (values (if (< a (ash 1 8)) a 253)
669 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
670 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))))
672 (define (tail-formals type)
673 (define-syntax op-case
675 ((op-case type (%type arg ...) clause ...)
676 (if (eq? type '%type)
677 (generate-temporaries #'(arg ...))
678 (op-case type clause ...)))
680 (error "unmatched type" type))))
695 (B1_U7_L24 a b label)
699 (define (shuffle-up dst)
700 (define-syntax op-case
702 ((_ type ((%type ...) exp) clause ...)
703 (if (memq type '(%type ...))
705 (op-case type clause ...)))
707 (error "unexpected type" type))))
708 (with-syntax ((dst dst))
711 ((U8_U8_I16 U8_U8_U8_U8)
712 (unless (< dst (ash 1 8))
713 (emit-mov* asm dst 253)))
715 (unless (< dst (ash 1 12))
716 (emit-mov* asm dst 253))))))
720 (lambda (formals+shuffle)
721 (with-syntax ((emit-name (id-append name #'emit- name))
722 (((formal0 ...) shuffle) formals+shuffle)
723 (((formal* ...) ...) (map tail-formals word*)))
724 (with-syntax (((shuffle-up-dst ...)
726 (syntax-case #'(formal0 ...) ()
728 (list (shuffle-up #'dst))))
730 #'(lambda (asm formal0 ... formal* ... ...)
731 (call-with-values (lambda () shuffle)
732 (lambda (formal0 ...)
733 (emit-name asm formal0 ... formal* ... ...)))
734 shuffle-up-dst ...))))))
736 (define-syntax define-shuffling-assembler
739 ((_ #:except (except ...) name opcode kind word0 word* ...)
741 ((or-map (lambda (op) (eq? (syntax->datum #'name) op))
742 (map syntax->datum #'(except ...)))
744 ((shuffling-assembler #'name (syntax->datum #'kind)
745 (syntax->datum #'word0)
746 (map syntax->datum #'(word* ...)))
748 (with-syntax ((emit (id-append #'name
749 (id-append #'name #'emit- #'name)
754 (hashq-set! assemblers 'name emit)
756 (else #'(begin))))))))
758 (visit-opcodes define-shuffling-assembler #:except (receive mov))
760 ;; Mov and receive are two special cases that can work without wrappers.
761 ;; Indeed it is important that they do so.
763 (define (emit-mov* asm dst src)
764 (if (and (< dst (ash 1 12)) (< src (ash 1 12)))
765 (emit-mov asm dst src)
766 (emit-long-mov asm dst src)))
768 (define (emit-receive* asm dst proc nlocals)
769 (if (and (< dst (ash 1 12)) (< proc (ash 1 12)))
770 (emit-receive asm dst proc nlocals)
772 (emit-receive-values asm proc #t 1)
773 (emit-mov* asm dst (1+ proc))
774 (emit-reset-frame asm nlocals))))
776 (define (emit-text asm instructions)
777 "Assemble @var{instructions} using the assembler @var{asm}.
778 @var{instructions} is a sequence of instructions, expressed as a list of
779 lists. This procedure can be called many times before calling
780 @code{link-assembly}."
781 (for-each (lambda (inst)
782 (apply (or (hashq-ref assemblers (car inst))
783 (error 'bad-instruction inst))
791 ;;; The constant table records a topologically sorted set of literal
792 ;;; constants used by a program. For example, a pair uses its car and
793 ;;; cdr, a string uses its stringbuf, etc.
795 ;;; Some things we want to add to the constant table are not actually
796 ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
797 ;;; references, or cache cells for non-closure procedures. For these we
798 ;;; define special record types and add instances of those record types
802 (define-inline (immediate? x)
803 "Return @code{#t} if @var{x} is immediate, and @code{#f} otherwise."
804 (not (zero? (logand (object-address x) 6))))
806 (define-record-type <stringbuf>
807 (make-stringbuf string)
809 (string stringbuf-string))
811 (define-record-type <static-procedure>
812 (make-static-procedure code)
814 (code static-procedure-code))
816 (define-record-type <uniform-vector-backing-store>
817 (make-uniform-vector-backing-store bytes element-size)
818 uniform-vector-backing-store?
819 (bytes uniform-vector-backing-store-bytes)
820 (element-size uniform-vector-backing-store-element-size))
822 (define-record-type <cache-cell>
823 (make-cache-cell scope key)
825 (scope cache-cell-scope)
826 (key cache-cell-key))
828 (define (simple-vector? obj)
830 (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
832 (define (simple-uniform-vector? obj)
834 (symbol? (array-type obj))
835 (equal? (array-shape obj) (list (list 0 (1- (array-length obj)))))))
837 (define (statically-allocatable? x)
838 "Return @code{#t} if a non-immediate constant can be allocated
839 statically, and @code{#f} if it would need some kind of runtime
841 (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
843 (define (intern-constant asm obj)
844 "Add an object to the constant table, and return a label that can be
845 used to reference it. If the object is already present in the constant
846 table, its existing label is used directly."
848 (intern-constant asm obj))
849 (define (field dst n obj)
850 (let ((src (recur obj)))
852 (if (statically-allocatable? obj)
853 `((static-patch! ,dst ,n ,src))
854 `((static-ref 1 ,src)
855 (static-set! 1 ,dst ,n)))
857 (define (intern obj label)
860 (append (field label 0 (car obj))
861 (field label 1 (cdr obj))))
862 ((simple-vector? obj)
863 (let lp ((i 0) (inits '()))
864 (if (< i (vector-length obj))
866 (append-reverse (field label (1+ i) (vector-ref obj i))
869 ((stringbuf? obj) '())
870 ((static-procedure? obj)
871 `((static-patch! ,label 1 ,(static-procedure-code obj))))
872 ((cache-cell? obj) '())
874 `((make-non-immediate 1 ,(recur (symbol->string obj)))
876 (static-set! 1 ,label 0)))
878 `((static-patch! ,label 1 ,(recur (make-stringbuf obj)))))
880 `((static-ref 1 ,(recur (keyword->symbol obj)))
881 (symbol->keyword 1 1)
882 (static-set! 1 ,label 0)))
884 `((make-non-immediate 1 ,(recur (number->string obj)))
886 (static-set! 1 ,label 0)))
887 ((uniform-vector-backing-store? obj) '())
888 ((simple-uniform-vector? obj)
889 (let ((width (case (array-type obj)
892 ;; Bitvectors are addressed in 32-bit units.
893 ;; Although a complex number is 8 or 16 bytes wide,
894 ;; it should be byteswapped in 4 or 8 byte units.
895 ((u32 s32 f32 c32 b) 4)
896 ((u64 s64 f64 c64) 8)
898 (error "unhandled array type" obj)))))
899 `((static-patch! ,label 2
900 ,(recur (make-uniform-vector-backing-store
901 (uniform-array->bytevector obj)
904 (error "don't know how to intern" obj))))
906 ((immediate? obj) #f)
907 ((vhash-assoc obj (asm-constants asm)) => cdr)
909 ;; Note that calling intern may mutate asm-constants and
910 ;; asm-constant-inits.
911 (let* ((label (gensym "constant"))
912 (inits (intern obj label)))
913 (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
914 (set-asm-inits! asm (append-reverse inits (asm-inits asm)))
917 (define (intern-non-immediate asm obj)
918 "Intern a non-immediate into the constant table, and return its
920 (when (immediate? obj)
921 (error "expected a non-immediate" obj))
922 (intern-constant asm obj))
924 (define (intern-cache-cell asm scope key)
925 "Intern a cache cell into the constant table, and return its label.
926 If there is already a cache cell with the given scope and key, it is
928 (intern-constant asm (make-cache-cell scope key)))
930 ;; Return the label of the cell that holds the module for a scope.
931 (define (intern-module-cache-cell asm scope)
932 "Intern a cache cell for a module, and return its label."
933 (intern-cache-cell asm scope #t))
939 ;;; Macro assemblers bridge the gap between primitive instructions and
940 ;;; some higher-level operations.
944 (define-syntax define-macro-assembler
947 ((_ (name arg ...) body body* ...)
948 (with-syntax ((emit (id-append #'name #'emit- #'name)))
951 (let ((emit (lambda (arg ...) body body* ...)))
952 (hashq-set! assemblers 'name emit)
956 (define-macro-assembler (load-constant asm dst obj)
959 (let ((bits (object-address obj)))
961 ((and (< dst 256) (zero? (ash bits -16)))
962 (emit-make-short-immediate asm dst obj))
963 ((zero? (ash bits -32))
964 (emit-make-long-immediate asm dst obj))
966 (emit-make-long-long-immediate asm dst obj)))))
967 ((statically-allocatable? obj)
968 (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
970 (emit-static-ref asm dst (intern-non-immediate asm obj)))))
972 (define-macro-assembler (load-static-procedure asm dst label)
973 (let ((loc (intern-constant asm (make-static-procedure label))))
974 (emit-make-non-immediate asm dst loc)))
976 (define-syntax-rule (define-tc7-macro-assembler name tc7)
977 (define-macro-assembler (name asm slot invert? label)
978 (emit-br-if-tc7 asm slot invert? tc7 label)))
980 ;; Keep in sync with tags.h. Part of Guile's ABI. Currently unused
981 ;; macro assemblers are commented out. See also
982 ;; *branching-primcall-arities* in (language cps primitives), the set of
983 ;; macro-instructions in assembly.scm, and
984 ;; disassembler.scm:code-annotation.
986 ;; FIXME: Define all tc7 values in Scheme in one place, derived from
988 (define-tc7-macro-assembler br-if-symbol 5)
989 (define-tc7-macro-assembler br-if-variable 7)
990 (define-tc7-macro-assembler br-if-vector 13)
991 ;(define-tc7-macro-assembler br-if-weak-vector 13)
992 (define-tc7-macro-assembler br-if-string 21)
993 ;(define-tc7-macro-assembler br-if-heap-number 23)
994 ;(define-tc7-macro-assembler br-if-stringbuf 39)
995 (define-tc7-macro-assembler br-if-bytevector 77)
996 ;(define-tc7-macro-assembler br-if-pointer 31)
997 ;(define-tc7-macro-assembler br-if-hashtable 29)
998 ;(define-tc7-macro-assembler br-if-fluid 37)
999 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
1000 ;(define-tc7-macro-assembler br-if-frame 47)
1001 ;(define-tc7-macro-assembler br-if-vm 55)
1002 ;(define-tc7-macro-assembler br-if-vm-cont 71)
1003 ;(define-tc7-macro-assembler br-if-rtl-program 69)
1004 ;(define-tc7-macro-assembler br-if-weak-set 85)
1005 ;(define-tc7-macro-assembler br-if-weak-table 87)
1006 ;(define-tc7-macro-assembler br-if-array 93)
1007 (define-tc7-macro-assembler br-if-bitvector 95)
1008 ;(define-tc7-macro-assembler br-if-port 125)
1009 ;(define-tc7-macro-assembler br-if-smob 127)
1011 (define-macro-assembler (begin-program asm label properties)
1012 (emit-label asm label)
1013 (let ((meta (make-meta label properties (asm-start asm))))
1014 (set-asm-meta! asm (cons meta (asm-meta asm)))))
1016 (define-macro-assembler (end-program asm)
1017 (let ((meta (car (asm-meta asm))))
1018 (set-meta-high-pc! meta (asm-start asm))
1019 (set-meta-arities! meta (reverse (meta-arities meta)))))
1021 (define-macro-assembler (begin-standard-arity asm req nlocals alternate)
1022 (emit-begin-opt-arity asm req '() #f nlocals alternate))
1024 (define-macro-assembler (begin-opt-arity asm req opt rest nlocals alternate)
1025 (emit-begin-kw-arity asm req opt rest '() #f nlocals alternate))
1027 (define-macro-assembler (begin-kw-arity asm req opt rest kw-indices
1028 allow-other-keys? nlocals alternate)
1029 (assert-match req ((? symbol?) ...) "list of symbols")
1030 (assert-match opt ((? symbol?) ...) "list of symbols")
1031 (assert-match rest (or #f (? symbol?)) "#f or symbol")
1032 (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
1033 "alist of keyword -> integer")
1034 (assert-match allow-other-keys? (? boolean?) "boolean")
1035 (assert-match nlocals (? integer?) "integer")
1036 (assert-match alternate (or #f (? exact-integer?) (? symbol?)) "#f or symbol")
1037 (let* ((meta (car (asm-meta asm)))
1038 (arity (make-arity req opt rest kw-indices allow-other-keys?
1039 (asm-start asm) #f '()))
1040 ;; The procedure itself is in slot 0, in the standard calling
1041 ;; convention. For procedure prologues, nreq includes the
1042 ;; procedure, so here we add 1.
1043 (nreq (1+ (length req)))
1045 (rest? (->bool rest)))
1046 (set-meta-arities! meta (cons arity (meta-arities meta)))
1048 ((or allow-other-keys? (pair? kw-indices))
1049 (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
1051 ((or rest? (pair? opt))
1052 (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
1054 (emit-standard-prelude asm nreq nlocals alternate)))))
1056 (define-macro-assembler (end-arity asm)
1057 (let ((arity (car (meta-arities (car (asm-meta asm))))))
1058 (set-arity-definitions! arity (reverse (arity-definitions arity)))
1059 (set-arity-high-pc! arity (asm-start asm))))
1061 ;; As noted above, we reserve locals 253 through 255 for shuffling large
1062 ;; operands. However the calling convention has all arguments passed in
1063 ;; a contiguous block. This helper, called after the clause has been
1064 ;; chosen and the keyword/optional/rest arguments have been processed,
1065 ;; shuffles up arguments from slot 253 and higher into their final
1068 (define (shuffle-up-args asm nargs)
1070 (let ((slot (1- nargs)))
1071 (emit-mov asm (+ slot 3) slot)
1072 (shuffle-up-args asm (1- nargs)))))
1074 (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
1077 (emit-br-if-nargs-ne asm nreq alternate)
1078 (emit-alloc-frame asm nlocals))
1079 ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
1080 (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
1082 (emit-assert-nargs-ee asm nreq)
1083 (emit-alloc-frame asm nlocals)))
1084 (shuffle-up-args asm nreq))
1086 (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
1088 (emit-br-if-nargs-lt asm nreq alternate)
1089 (emit-assert-nargs-ge asm nreq))
1092 (emit-bind-rest asm (+ nreq nopt)))
1094 (emit-br-if-nargs-gt asm (+ nreq nopt) alternate))
1096 (emit-assert-nargs-le asm (+ nreq nopt))))
1097 (emit-alloc-frame asm nlocals)
1098 (shuffle-up-args asm (+ nreq nopt (if rest? 1 0))))
1100 (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
1101 allow-other-keys? nlocals alternate)
1104 (emit-br-if-nargs-lt asm nreq alternate)
1106 (emit-br-if-npos-gt asm nreq (+ nreq nopt) alternate)))
1107 (emit-assert-nargs-ge asm nreq))
1108 (let ((ntotal (fold (lambda (kw ntotal)
1110 (((? keyword?) . idx)
1111 (max (1+ idx) ntotal))))
1112 (+ nreq nopt) kw-indices)))
1113 ;; FIXME: port 581f410f
1114 (emit-bind-kwargs asm nreq
1115 (pack-flags allow-other-keys? rest?)
1118 (intern-constant asm kw-indices))
1119 (emit-alloc-frame asm nlocals)
1120 (shuffle-up-args asm ntotal)))
1122 (define-macro-assembler (label asm sym)
1123 (hashq-set! (asm-labels asm) sym (asm-start asm)))
1125 (define-macro-assembler (source asm source)
1126 (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
1128 (define-macro-assembler (definition asm name slot)
1129 (let* ((arity (car (meta-arities (car (asm-meta asm)))))
1132 (* (- (asm-start asm) (arity-low-pc arity)) 4))))
1133 (set-arity-definitions! arity (cons def (arity-definitions arity)))))
1135 (define-macro-assembler (cache-current-module! asm module scope)
1136 (let ((mod-label (intern-module-cache-cell asm scope)))
1137 (emit-static-set! asm module mod-label 0)))
1139 (define-macro-assembler (cached-toplevel-box asm dst scope sym bound?)
1140 (let ((sym-label (intern-non-immediate asm sym))
1141 (mod-label (intern-module-cache-cell asm scope))
1142 (cell-label (intern-cache-cell asm scope sym)))
1143 (emit-toplevel-box asm dst cell-label mod-label sym-label bound?)))
1145 (define-macro-assembler (cached-module-box asm dst module-name sym public? bound?)
1146 (let* ((sym-label (intern-non-immediate asm sym))
1147 (key (cons public? module-name))
1148 (mod-name-label (intern-constant asm key))
1149 (cell-label (intern-cache-cell asm key sym)))
1150 (emit-module-box asm dst cell-label mod-name-label sym-label bound?)))
1152 (define-macro-assembler (dead-slot-map asm proc-slot dead-slot-map)
1153 (unless (zero? dead-slot-map)
1154 (set-asm-dead-slot-maps! asm
1156 (cons* (asm-start asm) proc-slot dead-slot-map)
1157 (asm-dead-slot-maps asm)))))
1162 ;;; Helper for linking objects.
1165 (define (make-object asm name bv relocs labels . kwargs)
1166 "Make a linker object. This helper handles interning the name in the
1167 shstrtab, assigning the size, allocating a fresh index, and defining a
1168 corresponding linker symbol for the start of the section."
1169 (let ((name-idx (intern-section-name! asm (symbol->string name)))
1170 (index (asm-next-section-number asm)))
1171 (set-asm-next-section-number! asm (1+ index))
1172 (make-linker-object (apply make-elf-section
1175 #:size (bytevector-length bv)
1178 (cons (make-linker-symbol name 0) labels))))
1184 ;;; Linking the constant table. This code is somewhat intertwingled
1185 ;;; with the intern-constant code above, as that procedure also
1186 ;;; residualizes instructions to initialize constants at load time.
1189 (define (write-immediate asm buf pos x)
1190 (let ((val (object-address x))
1191 (endianness (asm-endianness asm)))
1192 (case (asm-word-size asm)
1193 ((4) (bytevector-u32-set! buf pos val endianness))
1194 ((8) (bytevector-u64-set! buf pos val endianness))
1195 (else (error "bad word size" asm)))))
1197 (define (emit-init-constants asm)
1198 "If there is writable data that needs initialization at runtime, emit
1199 a procedure to do that and return its label. Otherwise return
1201 (let ((inits (asm-inits asm)))
1202 (and (not (null? inits))
1203 (let ((label (gensym "init-constants")))
1205 `((begin-program ,label ())
1206 (assert-nargs-ee/locals 1 1)
1208 (load-constant 1 ,*unspecified*)
1213 (define (link-data asm data name)
1214 "Link the static data for a program into the @var{name} section (which
1215 should be .data or .rodata), and return the resulting linker object.
1216 @var{data} should be a vhash mapping objects to labels."
1217 (define (align address alignment)
1219 (modulo (- alignment (modulo address alignment)) alignment)))
1221 (define tc7-vector 13)
1222 (define stringbuf-shared-flag #x100)
1223 (define stringbuf-wide-flag #x400)
1224 (define tc7-stringbuf 39)
1225 (define tc7-narrow-stringbuf
1226 (+ tc7-stringbuf stringbuf-shared-flag))
1227 (define tc7-wide-stringbuf
1228 (+ tc7-stringbuf stringbuf-shared-flag stringbuf-wide-flag))
1229 (define tc7-ro-string (+ 21 #x200))
1230 (define tc7-program 69)
1231 (define tc7-bytevector 77)
1232 (define tc7-bitvector 95)
1234 (let ((word-size (asm-word-size asm))
1235 (endianness (asm-endianness asm)))
1236 (define (byte-length x)
1239 (let ((x (stringbuf-string x)))
1241 (case (string-bytes-per-char x)
1242 ((1) (1+ (string-length x)))
1243 ((4) (* (1+ (string-length x)) 4))
1244 (else (error "bad string bytes per char" x))))))
1245 ((static-procedure? x)
1252 (* (1+ (vector-length x)) word-size))
1253 ((simple-uniform-vector? x)
1255 ((uniform-vector-backing-store? x)
1256 (bytevector-length (uniform-vector-backing-store-bytes x)))
1260 (define (write-constant-reference buf pos x)
1261 ;; The asm-inits will fix up any reference to a non-immediate.
1262 (write-immediate asm buf pos (if (immediate? x) x #f)))
1264 (define (write buf pos obj)
1267 (let* ((x (stringbuf-string obj))
1268 (len (string-length x))
1269 (tag (if (= (string-bytes-per-char x) 1)
1270 tc7-narrow-stringbuf
1271 tc7-wide-stringbuf)))
1274 (bytevector-u32-set! buf pos tag endianness)
1275 (bytevector-u32-set! buf (+ pos 4) len endianness))
1277 (bytevector-u64-set! buf pos tag endianness)
1278 (bytevector-u64-set! buf (+ pos 8) len endianness))
1280 (error "bad word size" asm)))
1281 (let ((pos (+ pos (* word-size 2))))
1282 (case (string-bytes-per-char x)
1286 (let ((u8 (char->integer (string-ref x i))))
1287 (bytevector-u8-set! buf (+ pos i) u8)
1289 (bytevector-u8-set! buf (+ pos i) 0))))
1293 (let ((u32 (char->integer (string-ref x i))))
1294 (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
1296 (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
1297 (else (error "bad string bytes per char" x))))))
1299 ((static-procedure? obj)
1302 (bytevector-u32-set! buf pos tc7-program endianness)
1303 (bytevector-u32-set! buf (+ pos 4) 0 endianness))
1305 (bytevector-u64-set! buf pos tc7-program endianness)
1306 (bytevector-u64-set! buf (+ pos 8) 0 endianness))
1307 (else (error "bad word size"))))
1310 (write-immediate asm buf pos #f))
1313 (let ((tag (logior tc7-ro-string (ash (string-length obj) 8))))
1316 (bytevector-u32-set! buf pos tc7-ro-string endianness)
1317 (write-immediate asm buf (+ pos 4) #f) ; stringbuf
1318 (bytevector-u32-set! buf (+ pos 8) 0 endianness)
1319 (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
1321 (bytevector-u64-set! buf pos tc7-ro-string endianness)
1322 (write-immediate asm buf (+ pos 8) #f) ; stringbuf
1323 (bytevector-u64-set! buf (+ pos 16) 0 endianness)
1324 (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
1325 (else (error "bad word size")))))
1328 (write-constant-reference buf pos (car obj))
1329 (write-constant-reference buf (+ pos word-size) (cdr obj)))
1331 ((simple-vector? obj)
1332 (let* ((len (vector-length obj))
1333 (tag (logior tc7-vector (ash len 8))))
1335 ((4) (bytevector-u32-set! buf pos tag endianness))
1336 ((8) (bytevector-u64-set! buf pos tag endianness))
1337 (else (error "bad word size")))
1339 (when (< i (vector-length obj))
1340 (let ((pos (+ pos word-size (* i word-size)))
1341 (elt (vector-ref obj i)))
1342 (write-constant-reference buf pos elt)
1346 (write-immediate asm buf pos #f))
1349 (write-immediate asm buf pos #f))
1352 (write-immediate asm buf pos #f))
1354 ((simple-uniform-vector? obj)
1355 (let ((tag (if (bitvector? obj)
1357 (let ((type-code (array-type-code obj)))
1358 (logior tc7-bytevector (ash type-code 7))))))
1361 (bytevector-u32-set! buf pos tag endianness)
1362 (bytevector-u32-set! buf (+ pos 4)
1363 (if (bitvector? obj)
1364 (bitvector-length obj)
1365 (bytevector-length obj))
1366 endianness) ; length
1367 (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
1368 (write-immediate asm buf (+ pos 12) #f)) ; owner
1370 (bytevector-u64-set! buf pos tag endianness)
1371 (bytevector-u64-set! buf (+ pos 8)
1372 (if (bitvector? obj)
1373 (bitvector-length obj)
1374 (bytevector-length obj))
1375 endianness) ; length
1376 (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
1377 (write-immediate asm buf (+ pos 24) #f)) ; owner
1378 (else (error "bad word size")))))
1380 ((uniform-vector-backing-store? obj)
1381 (let ((bv (uniform-vector-backing-store-bytes obj)))
1382 (bytevector-copy! bv 0 buf pos (bytevector-length bv))
1383 (unless (or (= 1 (uniform-vector-backing-store-element-size obj))
1384 (eq? endianness (native-endianness)))
1385 ;; Need to swap units of element-size bytes
1386 (error "FIXME: Implement byte order swap"))))
1389 (error "unrecognized object" obj))))
1392 ((vlist-null? data) #f)
1394 (let* ((byte-len (vhash-fold (lambda (k v len)
1395 (+ (byte-length k) (align len 8)))
1397 (buf (make-bytevector byte-len 0)))
1398 (let lp ((i 0) (pos 0) (symbols '()))
1399 (if (< i (vlist-length data))
1400 (let* ((pair (vlist-ref data i))
1402 (obj-label (cdr pair)))
1405 (align (+ (byte-length obj) pos) 8)
1406 (cons (make-linker-symbol obj-label pos) symbols)))
1407 (make-object asm name buf '() symbols
1409 ('.data (logior SHF_ALLOC SHF_WRITE))
1410 ('.rodata SHF_ALLOC))))))))))
1412 (define (link-constants asm)
1413 "Link sections to hold constants needed by the program text emitted
1416 Returns three values: an object for the .rodata section, an object for
1417 the .data section, and a label for an initialization procedure. Any of
1418 these may be @code{#f}."
1419 (define (shareable? x)
1423 (and (immediate? (car x)) (immediate? (cdr x))))
1426 (or (= i (vector-length x))
1427 (and (immediate? (vector-ref x i))
1429 ((uniform-vector-backing-store? x) #t)
1431 (let* ((constants (asm-constants asm))
1432 (len (vlist-length constants)))
1437 (values (link-data asm ro '.rodata)
1438 (link-data asm rw '.data)
1439 (emit-init-constants asm))
1440 (let ((pair (vlist-ref constants i)))
1441 (if (shareable? (car pair))
1442 (lp (1+ i) (vhash-consq (car pair) (cdr pair) ro) rw)
1443 (lp (1+ i) ro (vhash-consq (car pair) (cdr pair) rw))))))))
1448 ;;; Linking program text.
1451 (define (process-relocs buf relocs labels)
1452 "Patch up internal x8-s24 relocations, and any s32 relocations that
1453 reference symbols in the text section. Return a list of linker
1454 relocations for references to symbols defined outside the text section."
1456 (lambda (reloc tail)
1458 ((type label base word)
1459 (let ((abs (hashq-ref labels label))
1460 (dst (+ base word)))
1464 (let ((rel (- abs base)))
1465 (s32-set! buf dst rel)
1467 (cons (make-linker-reloc 'rel32/4 (* dst 4) word label)
1471 (error "unbound near relocation" reloc))
1472 (let ((rel (- abs base))
1473 (u32 (u32-ref buf dst)))
1474 (u32-set! buf dst (pack-u8-s24 (logand u32 #xff) rel))
1476 (else (error "bad relocation kind" reloc)))))))
1480 (define (process-labels labels)
1481 "Define linker symbols for the label-offset map in @var{labels}.
1482 The offsets are expected to be expressed in words."
1483 (hash-map->list (lambda (label loc)
1484 (make-linker-symbol label (* loc 4)))
1487 (define (swap-bytes! buf)
1488 "Patch up the text buffer @var{buf}, swapping the endianness of each
1490 (unless (zero? (modulo (bytevector-length buf) 4))
1491 (error "unexpected length"))
1492 (let ((byte-len (bytevector-length buf)))
1494 (unless (= pos byte-len)
1495 (bytevector-u32-set!
1497 (bytevector-u32-ref buf pos (endianness big))
1498 (endianness little))
1501 (define (link-text-object asm)
1502 "Link the .rtl-text section, swapping the endianness of the bytes if
1504 (let ((buf (make-u32vector (asm-pos asm))))
1505 (let lp ((pos 0) (prev (reverse (asm-prev asm))))
1507 (let ((byte-size (* (asm-idx asm) 4)))
1508 (bytevector-copy! (asm-cur asm) 0 buf pos byte-size)
1509 (unless (eq? (asm-endianness asm) (native-endianness))
1511 (make-object asm '.rtl-text
1513 (process-relocs buf (asm-relocs asm)
1515 (process-labels (asm-labels asm))))
1516 (let ((len (* *block-size* 4)))
1517 (bytevector-copy! (car prev) 0 buf pos len)
1518 (lp (+ pos len) (cdr prev)))))))
1524 ;;; Create the frame maps. These maps are used by GC to identify dead
1525 ;;; slots in pending call frames, to avoid marking them. We only do
1526 ;;; this when frame makes a non-tail call, as that is the common case.
1527 ;;; Only the topmost frame will see a GC at any other point, but we mark
1528 ;;; top frames conservatively as serializing live slot maps at every
1529 ;;; instruction would take up too much space in the object file.
1532 ;; The .guile.frame-maps section starts with two packed u32 values: one
1533 ;; indicating the offset of the first byte of the .rtl-text section, and
1534 ;; another indicating the relative offset in bytes of the slots data.
1535 (define frame-maps-prefix-len 8)
1537 ;; Each header is 8 bytes: 4 for the offset from .rtl_text, and 4 for
1538 ;; the offset of the slot map from the beginning of the
1539 ;; .guile.frame-maps section. The length of a frame map depends on the
1540 ;; frame size at the call site, and is not encoded into this section as
1541 ;; it is available at run-time.
1542 (define frame-map-header-len 8)
1544 (define (link-frame-maps asm)
1545 (define (map-byte-length proc-slot)
1546 (ceiling-quotient (- proc-slot 2) 8))
1547 (define (make-frame-maps maps count map-len)
1548 (let* ((endianness (asm-endianness asm))
1549 (header-pos frame-maps-prefix-len)
1550 (map-pos (+ header-pos (* count frame-map-header-len)))
1551 (bv (make-bytevector (+ map-pos map-len) 0)))
1552 (bytevector-u32-set! bv 4 map-pos endianness)
1553 (let lp ((maps maps) (header-pos header-pos) (map-pos map-pos))
1556 (make-object asm '.guile.frame-maps bv
1557 (list (make-linker-reloc 'abs32/1 0 0 '.rtl-text))
1558 '() #:type SHT_PROGBITS #:flags SHF_ALLOC))
1559 (((pos proc-slot . map) . maps)
1560 (bytevector-u32-set! bv header-pos (* pos 4) endianness)
1561 (bytevector-u32-set! bv (+ header-pos 4) map-pos endianness)
1562 (let write-bytes ((map-pos map-pos)
1564 (byte-length (map-byte-length proc-slot)))
1565 (if (zero? byte-length)
1566 (lp maps (+ header-pos frame-map-header-len) map-pos)
1568 (bytevector-u8-set! bv map-pos (logand map #xff))
1569 (write-bytes (1+ map-pos) (ash map -8)
1570 (1- byte-length))))))))))
1571 (match (asm-dead-slot-maps asm)
1574 (let lp ((in in) (out '()) (count 0) (map-len 0))
1576 (() (make-frame-maps out count map-len))
1577 (((and head (pos proc-slot . map)) . in)
1578 (lp in (cons head out)
1580 (+ (map-byte-length proc-slot) map-len))))))))
1585 ;;; Linking other sections of the ELF file, like the dynamic segment,
1586 ;;; the symbol table, etc.
1589 ;; FIXME: Define these somewhere central, shared with C.
1590 (define *bytecode-major-version* #x0202)
1591 (define *bytecode-minor-version* 6)
1593 (define (link-dynamic-section asm text rw rw-init frame-maps)
1594 "Link the dynamic section for an ELF image with bytecode @var{text},
1595 given the writable data section @var{rw} needing fixup from the
1596 procedure with label @var{rw-init}. @var{rw-init} may be false. If
1597 @var{rw} is true, it will be added to the GC roots at runtime."
1598 (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
1599 (let* ((endianness (asm-endianness asm))
1601 (words (if rw (+ words 4) words))
1602 (words (if rw-init (+ words 2) words))
1603 (words (if frame-maps (+ words 2) words))
1604 (bv (make-bytevector (* word-size words) 0))
1607 (%set-uword! bv (* i word-size) uword endianness)))
1611 (set! relocs (cons (make-linker-reloc 'reloc-type
1612 (* i word-size) 0 label)
1614 (%set-uword! bv (* i word-size) 0 endianness))))
1615 (set-uword! 0 DT_GUILE_VM_VERSION)
1616 (set-uword! 1 (logior (ash *bytecode-major-version* 16)
1617 *bytecode-minor-version*))
1618 (set-uword! 2 DT_GUILE_ENTRY)
1619 (set-label! 3 '.rtl-text)
1622 (set-uword! 4 DT_GUILE_GC_ROOT)
1623 (set-label! 5 '.data)
1624 (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
1625 (set-uword! 7 (bytevector-length (linker-object-bv rw)))
1627 (set-uword! 8 DT_INIT) ; constants
1628 (set-label! 9 rw-init)))
1630 (set-uword! (- words 4) DT_GUILE_FRAME_MAPS)
1631 (set-label! (- words 3) '.guile.frame-maps))
1632 (set-uword! (- words 2) DT_NULL)
1633 (set-uword! (- words 1) 0)
1634 (make-object asm '.dynamic bv relocs '()
1635 #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
1636 (case (asm-word-size asm)
1637 ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
1638 ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
1639 (else (error "bad word size" asm))))
1641 (define (link-shstrtab asm)
1642 "Link the string table for the section headers."
1643 (intern-section-name! asm ".shstrtab")
1644 (make-object asm '.shstrtab
1645 (link-string-table! (asm-shstrtab asm))
1647 #:type SHT_STRTAB #:flags 0))
1649 (define (link-symtab text-section asm)
1650 (let* ((endianness (asm-endianness asm))
1651 (word-size (asm-word-size asm))
1652 (size (elf-symbol-len word-size))
1653 (meta (reverse (asm-meta asm)))
1655 (strtab (make-string-table))
1656 (bv (make-bytevector (* n size) 0)))
1657 (define (intern-string! name)
1658 (string-table-intern! strtab (if name (symbol->string name) "")))
1661 (let ((name (intern-string! (meta-name meta))))
1662 (write-elf-symbol bv (* n size) endianness word-size
1665 ;; Symbol value and size are measured in
1667 #:value (* 4 (meta-low-pc meta))
1668 #:size (* 4 (- (meta-high-pc meta)
1669 (meta-low-pc meta)))
1671 #:visibility STV_HIDDEN
1672 #:shndx (elf-section-index text-section)))))
1674 (let ((strtab (make-object asm '.strtab
1675 (link-string-table! strtab)
1677 #:type SHT_STRTAB #:flags 0)))
1678 (values (make-object asm '.symtab
1681 #:type SHT_SYMTAB #:flags 0 #:entsize size
1682 #:link (elf-section-index
1683 (linker-object-section strtab)))
1686 ;;; The .guile.arities section describes the arities that a function can
1687 ;;; have. It is in two parts: a sorted array of headers describing
1688 ;;; basic arities, and an array of links out to a string table (and in
1689 ;;; the case of keyword arguments, to the data section) for argument
1690 ;;; names. The whole thing is prefixed by a uint32 indicating the
1691 ;;; offset of the end of the headers array.
1693 ;;; The arity headers array is a packed array of structures of the form:
1695 ;;; struct arity_header {
1696 ;;; uint32_t low_pc;
1697 ;;; uint32_t high_pc;
1698 ;;; uint32_t offset;
1702 ;;; uint32_t nlocals;
1705 ;;; All of the offsets and addresses are 32 bits. We can expand in the
1706 ;;; future to use 64-bit offsets if appropriate, but there are other
1707 ;;; aspects of bytecode that constrain us to a total image that fits in
1708 ;;; 32 bits, so for the moment we'll simplify the problem space.
1710 ;;; The following flags values are defined:
1713 ;;; #x2: allow-other-keys?
1714 ;;; #x4: has-keyword-args?
1715 ;;; #x8: is-case-lambda?
1716 ;;; #x10: is-in-case-lambda?
1718 ;;; Functions with a single arity specify their number of required and
1719 ;;; optional arguments in nreq and nopt, and do not have the
1720 ;;; is-case-lambda? flag set. Their "offset" member links to an array
1721 ;;; of pointers into the associated .guile.arities.strtab string table,
1722 ;;; identifying the argument names. This offset is relative to the
1723 ;;; start of the .guile.arities section.
1725 ;;; If the arity has keyword arguments -- if has-keyword-args? is set in
1726 ;;; the flags -- the first uint32 pointed to by offset encodes a link to
1727 ;;; the "keyword indices" literal, in the data section. Then follow the
1728 ;;; names for all locals, in order, as uleb128 values. The required
1729 ;;; arguments will be the first locals, followed by the optionals,
1730 ;;; followed by the rest argument if if has-rest? is set. The names
1731 ;;; point into the associated string table section.
1733 ;;; Functions with no arities have no arities information present in the
1734 ;;; .guile.arities section.
1736 ;;; Functions with multiple arities are preceded by a header with
1737 ;;; is-case-lambda? set. All other fields are 0, except low-pc and
1738 ;;; high-pc which should be the bounds of the whole function. Headers
1739 ;;; for the individual arities follow, with the is-in-case-lambda? flag
1740 ;;; set. In this way the whole headers array is sorted in increasing
1741 ;;; low-pc order, and case-lambda clauses are contained within the
1742 ;;; [low-pc, high-pc] of the case-lambda header.
1744 ;; Length of the prefix to the arities section, in bytes.
1745 (define arities-prefix-len 4)
1747 ;; Length of an arity header, in bytes.
1748 (define arity-header-len (* 7 4))
1751 (define (put-uleb128 port val)
1753 (let ((next (ash val -7)))
1757 (put-u8 port (logior #x80 (logand val #x7f)))
1760 (define (put-sleb128 port val)
1762 (if (<= 0 (+ val 64) 127)
1763 (put-u8 port (logand val #x7f))
1765 (put-u8 port (logior #x80 (logand val #x7f)))
1766 (lp (ash val -7))))))
1768 (define (port-position port)
1769 (seek port 0 SEEK_CUR))
1771 (define-inline (pack-arity-flags has-rest? allow-other-keys?
1772 has-keyword-args? is-case-lambda?
1774 (logior (if has-rest? (ash 1 0) 0)
1775 (if allow-other-keys? (ash 1 1) 0)
1776 (if has-keyword-args? (ash 1 2) 0)
1777 (if is-case-lambda? (ash 1 3) 0)
1778 (if is-in-case-lambda? (ash 1 4) 0)))
1780 (define (write-arities asm metas headers names-port strtab)
1781 (define (write-header pos low-pc high-pc offset flags nreq nopt nlocals)
1782 (unless (<= (+ nreq nopt) nlocals)
1783 (error "forgot to emit definition instructions?"))
1784 (bytevector-u32-set! headers pos (* low-pc 4) (asm-endianness asm))
1785 (bytevector-u32-set! headers (+ pos 4) (* high-pc 4) (asm-endianness asm))
1786 (bytevector-u32-set! headers (+ pos 8) offset (asm-endianness asm))
1787 (bytevector-u32-set! headers (+ pos 12) flags (asm-endianness asm))
1788 (bytevector-u32-set! headers (+ pos 16) nreq (asm-endianness asm))
1789 (bytevector-u32-set! headers (+ pos 20) nopt (asm-endianness asm))
1790 (bytevector-u32-set! headers (+ pos 24) nlocals (asm-endianness asm)))
1791 (define (write-kw-indices kw-indices relocs)
1792 ;; FIXME: Assert that kw-indices is already interned.
1793 (if (pair? kw-indices)
1794 (let ((pos (+ (bytevector-length headers)
1795 (port-position names-port)))
1796 (label (intern-constant asm kw-indices)))
1797 (put-bytevector names-port #vu8(0 0 0 0))
1798 (cons (make-linker-reloc 'abs32/1 pos 0 label) relocs))
1800 (define (write-arity pos arity in-case-lambda? relocs)
1801 (write-header pos (arity-low-pc arity)
1802 (arity-high-pc arity)
1803 ;; FIXME: Seems silly to add on bytevector-length of
1804 ;; headers, given the arities-prefix.
1805 (+ (bytevector-length headers) (port-position names-port))
1806 (pack-arity-flags (arity-rest arity)
1807 (arity-allow-other-keys? arity)
1808 (pair? (arity-kw-indices arity))
1811 (length (arity-req arity))
1812 (length (arity-opt arity))
1813 (length (arity-definitions arity)))
1814 (let ((relocs (write-kw-indices (arity-kw-indices arity) relocs)))
1815 ;; Write local names.
1816 (let lp ((definitions (arity-definitions arity)))
1819 ((#(name slot def) . definitions)
1820 (let ((sym (if (symbol? name)
1821 (string-table-intern! strtab (symbol->string name))
1823 (put-uleb128 names-port sym)
1824 (lp definitions)))))
1825 ;; Now write their definitions.
1826 (let lp ((definitions (arity-definitions arity)))
1829 ((#(name slot def) . definitions)
1830 (put-uleb128 names-port def)
1831 (put-uleb128 names-port slot)
1832 (lp definitions))))))
1833 (let lp ((metas metas) (pos arities-prefix-len) (relocs '()))
1836 (unless (= pos (bytevector-length headers))
1837 (error "expected to fully fill the bytevector"
1838 pos (bytevector-length headers)))
1841 (match (meta-arities meta)
1842 (() (lp metas pos relocs))
1845 (+ pos arity-header-len)
1846 (write-arity pos arity #f relocs)))
1848 ;; Write a case-lambda header, then individual arities.
1849 ;; The case-lambda header's offset link is 0.
1850 (write-header pos (meta-low-pc meta) (meta-high-pc meta) 0
1851 (pack-arity-flags #f #f #f #t #f) 0 0 0)
1852 (let lp* ((arities arities) (pos (+ pos arity-header-len))
1855 (() (lp metas pos relocs))
1858 (+ pos arity-header-len)
1859 (write-arity pos arity #t relocs)))))))))))
1861 (define (link-arities asm)
1862 (define (meta-arities-header-size meta)
1863 (define (lambda-size arity)
1865 (define (case-lambda-size arities)
1867 arity-header-len ;; case-lambda header
1868 (map lambda-size arities))) ;; the cases
1869 (match (meta-arities meta)
1871 ((arity) (lambda-size arity))
1872 (arities (case-lambda-size arities))))
1874 (define (bytevector-append a b)
1875 (let ((out (make-bytevector (+ (bytevector-length a)
1876 (bytevector-length b)))))
1877 (bytevector-copy! a 0 out 0 (bytevector-length a))
1878 (bytevector-copy! b 0 out (bytevector-length a) (bytevector-length b))
1881 (let* ((endianness (asm-endianness asm))
1882 (metas (reverse (asm-meta asm)))
1883 (header-size (fold (lambda (meta size)
1884 (+ size (meta-arities-header-size meta)))
1887 (strtab (make-string-table))
1888 (headers (make-bytevector header-size 0)))
1889 (bytevector-u32-set! headers 0 (bytevector-length headers) endianness)
1890 (let-values (((names-port get-name-bv) (open-bytevector-output-port)))
1891 (let* ((relocs (write-arities asm metas headers names-port strtab))
1892 (strtab (make-object asm '.guile.arities.strtab
1893 (link-string-table! strtab)
1895 #:type SHT_STRTAB #:flags 0)))
1896 (values (make-object asm '.guile.arities
1897 (bytevector-append headers (get-name-bv))
1899 #:type SHT_PROGBITS #:flags 0
1900 #:link (elf-section-index
1901 (linker-object-section strtab)))
1905 ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
1906 ;;; values. Pc and str are both 32 bits wide. (Either could change to
1907 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1908 ;;; entry to a program, relative to the start of the text section, in
1909 ;;; bytes, and str is an index into the associated .guile.docstrs.strtab
1910 ;;; string table section.
1913 ;; The size of a docstrs entry, in bytes.
1914 (define docstr-size 8)
1916 (define (link-docstrs asm)
1917 (define (find-docstrings)
1918 (filter-map (lambda (meta)
1919 (define (is-documentation? pair)
1920 (eq? (car pair) 'documentation))
1921 (let* ((props (meta-properties meta))
1922 (tail (find-tail is-documentation? props)))
1924 (not (find-tail is-documentation? (cdr tail)))
1925 (string? (cdar tail))
1926 (cons (* 4 (meta-low-pc meta)) (cdar tail)))))
1927 (reverse (asm-meta asm))))
1928 (let* ((endianness (asm-endianness asm))
1929 (docstrings (find-docstrings))
1930 (strtab (make-string-table))
1931 (bv (make-bytevector (* (length docstrings) docstr-size) 0)))
1932 (fold (lambda (pair pos)
1935 (bytevector-u32-set! bv pos pc endianness)
1936 (bytevector-u32-set! bv (+ pos 4)
1937 (string-table-intern! strtab string)
1939 (+ pos docstr-size))))
1942 (let ((strtab (make-object asm '.guile.docstrs.strtab
1943 (link-string-table! strtab)
1945 #:type SHT_STRTAB #:flags 0)))
1946 (values (make-object asm '.guile.docstrs
1949 #:type SHT_PROGBITS #:flags 0
1950 #:link (elf-section-index
1951 (linker-object-section strtab)))
1955 ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
1956 ;;; values. Pc and addr are both 32 bits wide. (Either could change to
1957 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1958 ;;; entry to a program, relative to the start of the text section, and
1959 ;;; addr is the address of the associated properties alist, relative to
1960 ;;; the start of the ELF image.
1962 ;;; Since procedure properties are stored in the data sections, we need
1963 ;;; to link the procedures property section first. (Note that this
1964 ;;; constraint does not apply to the arities section, which may
1965 ;;; reference the data sections via the kw-indices literal, because
1966 ;;; assembling the text section already makes sure that the kw-indices
1970 ;; The size of a procprops entry, in bytes.
1971 (define procprops-size 8)
1973 (define (link-procprops asm)
1974 (define (assoc-remove-one alist key value-pred)
1977 ((((? (lambda (x) (eq? x key))) . value) . alist)
1978 (if (value-pred value)
1980 (acons key value alist)))
1982 (acons k v (assoc-remove-one alist key value-pred)))))
1983 (define (props-without-name-or-docstring meta)
1985 (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
1988 (define (find-procprops)
1989 (filter-map (lambda (meta)
1990 (let ((props (props-without-name-or-docstring meta)))
1992 (cons (* 4 (meta-low-pc meta)) props))))
1993 (reverse (asm-meta asm))))
1994 (let* ((endianness (asm-endianness asm))
1995 (procprops (find-procprops))
1996 (bv (make-bytevector (* (length procprops) procprops-size) 0)))
1997 (let lp ((procprops procprops) (pos 0) (relocs '()))
2000 (make-object asm '.guile.procprops
2003 #:type SHT_PROGBITS #:flags 0))
2004 (((pc . props) . procprops)
2005 (bytevector-u32-set! bv pos pc endianness)
2007 (+ pos procprops-size)
2008 (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
2009 (intern-constant asm props))
2013 ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
2014 ;;; sections provide line number and local variable liveness
2015 ;;; information. Their format is defined by the DWARF
2019 (define (asm-language asm)
2020 ;; FIXME: Plumb language through to the assembler.
2023 ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
2024 (define (link-debug asm)
2025 (define (put-s8 port val)
2026 (let ((bv (make-bytevector 1)))
2027 (bytevector-s8-set! bv 0 val)
2028 (put-bytevector port bv)))
2030 (define (put-u16 port val)
2031 (let ((bv (make-bytevector 2)))
2032 (bytevector-u16-set! bv 0 val (asm-endianness asm))
2033 (put-bytevector port bv)))
2035 (define (put-u32 port val)
2036 (let ((bv (make-bytevector 4)))
2037 (bytevector-u32-set! bv 0 val (asm-endianness asm))
2038 (put-bytevector port bv)))
2040 (define (put-u64 port val)
2041 (let ((bv (make-bytevector 8)))
2042 (bytevector-u64-set! bv 0 val (asm-endianness asm))
2043 (put-bytevector port bv)))
2045 (define (meta->subprogram-die meta)
2049 => (lambda (name) `((name ,(symbol->string name)))))
2052 (low-pc ,(meta-label meta))
2053 (high-pc ,(* 4 (- (meta-high-pc meta) (meta-low-pc meta)))))))
2055 (define (make-compile-unit-die asm)
2057 (@ (producer ,(string-append "Guile " (version)))
2058 (language ,(asm-language asm))
2060 (high-pc ,(* 4 (asm-pos asm)))
2062 ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
2064 (let-values (((die-port get-die-bv) (open-bytevector-output-port))
2066 ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
2067 ;; (tag has-kids? attrs forms) -> code
2068 ((abbrevs) vlist-null)
2069 ((strtab) (make-string-table))
2070 ((line-port get-line-bv) (open-bytevector-output-port))
2073 ((files) vlist-null))
2075 (define (write-abbrev code tag has-children? attrs forms)
2076 (put-uleb128 abbrev-port code)
2077 (put-uleb128 abbrev-port (tag-name->code tag))
2078 (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
2079 (for-each (lambda (attr form)
2080 (put-uleb128 abbrev-port (attribute-name->code attr))
2081 (put-uleb128 abbrev-port (form-name->code form)))
2083 (put-uleb128 abbrev-port 0)
2084 (put-uleb128 abbrev-port 0))
2086 (define (intern-abbrev tag has-children? attrs forms)
2087 (let ((key (list tag has-children? attrs forms)))
2088 (match (vhash-assoc key abbrevs)
2090 (#f (let ((code (1+ (vlist-length abbrevs))))
2091 (set! abbrevs (vhash-cons key code abbrevs))
2092 (write-abbrev code tag has-children? attrs forms)
2095 (define (intern-file file)
2096 (match (vhash-assoc file files)
2098 (#f (let ((code (1+ (vlist-length files))))
2099 (set! files (vhash-cons file code files))
2102 (define (write-sources)
2103 ;; Choose line base and line range values that will allow for an
2104 ;; address advance range of 16 words. The special opcode range is
2105 ;; from 10 to 255, so 246 values.
2109 (let lp ((sources (asm-sources asm)) (out '()))
2111 (((pc . s) . sources)
2112 (let ((file (assq-ref s 'filename))
2113 (line (assq-ref s 'line))
2114 (col (assq-ref s 'column)))
2116 ;; Guile line and column numbers are 0-indexed, but
2117 ;; they are 1-indexed for DWARF.
2119 (if file (intern-file file) 0)
2124 ;; Compilation unit header for .debug_line. We write in
2125 ;; DWARF 2 format because more tools understand it than DWARF
2126 ;; 4, which incompatibly adds another field to this header.
2128 (put-u32 line-port 0) ; Length; will patch later.
2129 (put-u16 line-port 2) ; DWARF 2 format.
2130 (put-u32 line-port 0) ; Prologue length; will patch later.
2131 (put-u8 line-port 4) ; Minimum instruction length: 4 bytes.
2132 (put-u8 line-port 1) ; Default is-stmt: true.
2134 (put-s8 line-port base) ; Line base. See the DWARF standard.
2135 (put-u8 line-port range) ; Line range. See the DWARF standard.
2136 (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
2138 ;; A table of the number of uleb128 arguments taken by each
2139 ;; of the standard opcodes.
2140 (put-u8 line-port 0) ; 1: copy
2141 (put-u8 line-port 1) ; 2: advance-pc
2142 (put-u8 line-port 1) ; 3: advance-line
2143 (put-u8 line-port 1) ; 4: set-file
2144 (put-u8 line-port 1) ; 5: set-column
2145 (put-u8 line-port 0) ; 6: negate-stmt
2146 (put-u8 line-port 0) ; 7: set-basic-block
2147 (put-u8 line-port 0) ; 8: const-add-pc
2148 (put-u8 line-port 1) ; 9: fixed-advance-pc
2150 ;; Include directories, as a zero-terminated sequence of
2151 ;; nul-terminated strings. Nothing, for the moment.
2152 (put-u8 line-port 0)
2154 ;; File table. For each file that contributes to this
2155 ;; compilation unit, a nul-terminated file name string, and a
2156 ;; uleb128 for each of directory the file was found in, the
2157 ;; modification time, and the file's size in bytes. We pass
2158 ;; zero for the latter three fields.
2163 (put-bytevector line-port (string->utf8 file))
2164 (put-u8 line-port 0)
2165 (put-uleb128 line-port 0) ; directory
2166 (put-uleb128 line-port 0) ; mtime
2167 (put-uleb128 line-port 0))) ; size
2171 (put-u8 line-port 0) ; 0 byte terminating file list.
2173 ;; Patch prologue length.
2174 (let ((offset (port-position line-port)))
2175 (seek line-port 6 SEEK_SET)
2176 (put-u32 line-port (- offset 10))
2177 (seek line-port offset SEEK_SET))
2179 ;; Now write the statement program.
2181 (define (extended-op opcode payload-len)
2182 (put-u8 line-port 0) ; extended op
2183 (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
2184 (put-uleb128 line-port opcode))
2185 (define (set-address sym)
2186 (define (add-reloc! kind)
2188 (cons (make-linker-reloc kind
2189 (port-position line-port)
2193 (match (asm-word-size asm)
2196 (add-reloc! 'abs32/1)
2197 (put-u32 line-port 0))
2200 (add-reloc! 'abs64/1)
2201 (put-u64 line-port 0))))
2202 (define (end-sequence pc)
2203 (let ((pc-inc (- (asm-pos asm) pc)))
2204 (put-u8 line-port 2) ; advance-pc
2205 (put-uleb128 line-port pc-inc))
2207 (define (advance-pc pc-inc line-inc)
2208 (let ((spec (+ (- line-inc base) (* pc-inc range) 10)))
2210 ((or (< line-inc base) (>= line-inc (+ base range)))
2211 (advance-line line-inc)
2212 (advance-pc pc-inc 0))
2214 (put-u8 line-port spec))
2216 (put-u8 line-port 8) ; const-advance-pc
2217 (advance-pc (- pc-inc (floor/ (- 255 10) range))
2220 (put-u8 line-port 2) ; advance-pc
2221 (put-uleb128 line-port pc-inc)
2222 (advance-pc 0 line-inc)))))
2223 (define (advance-line inc)
2224 (put-u8 line-port 3)
2225 (put-sleb128 line-port inc))
2226 (define (set-file file)
2227 (put-u8 line-port 4)
2228 (put-uleb128 line-port file))
2229 (define (set-column col)
2230 (put-u8 line-port 5)
2231 (put-uleb128 line-port col))
2233 (set-address '.rtl-text)
2235 (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
2239 ;; There was no source info in the first place. Set
2240 ;; file register to 0 before adding final row.
2243 (((pc* file* line* col*) . in*)
2245 ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
2246 (lp in* pc file line col))
2248 (unless (eqv? col col*)
2250 (unless (eqv? file file*)
2252 (advance-pc (- pc* pc) (- line* line))
2253 (lp in* pc* file* line* col*)))))))))))
2255 (define (compute-code attr val)
2257 ('name (string-table-intern! strtab val))
2260 ('producer (string-table-intern! strtab val))
2261 ('language (language-name->code val))
2264 (define (choose-form attr val code)
2266 ((string? val) 'strp)
2267 ((eq? attr 'stmt-list) 'sec-offset)
2268 ((eq? attr 'low-pc) 'addr)
2269 ((exact-integer? code)
2271 ((< code 0) 'sleb128)
2272 ((<= code #xff) 'data1)
2273 ((<= code #xffff) 'data2)
2274 ((<= code #xffffffff) 'data4)
2275 ((<= code #xffffffffffffffff) 'data8)
2277 (else (error "unhandled case" attr val code))))
2279 (define (add-die-relocation! kind sym)
2281 (cons (make-linker-reloc kind (port-position die-port) 0 sym)
2284 (define (write-value code form)
2286 ('data1 (put-u8 die-port code))
2287 ('data2 (put-u16 die-port code))
2288 ('data4 (put-u32 die-port code))
2289 ('data8 (put-u64 die-port code))
2290 ('uleb128 (put-uleb128 die-port code))
2291 ('sleb128 (put-sleb128 die-port code))
2293 (match (asm-word-size asm)
2295 (add-die-relocation! 'abs32/1 code)
2296 (put-u32 die-port 0))
2298 (add-die-relocation! 'abs64/1 code)
2299 (put-u64 die-port 0))))
2300 ('sec-offset (put-u32 die-port code))
2301 ('strp (put-u32 die-port code))))
2303 (define (write-die die)
2305 ((tag ('@ (attrs vals) ...) children ...)
2306 (let* ((codes (map compute-code attrs vals))
2307 (forms (map choose-form attrs vals codes))
2308 (has-children? (not (null? children)))
2309 (abbrev-code (intern-abbrev tag has-children? attrs forms)))
2310 (put-uleb128 die-port abbrev-code)
2311 (for-each write-value codes forms)
2313 (for-each write-die children)
2314 (put-uleb128 die-port 0))))))
2316 ;; Compilation unit header.
2317 (put-u32 die-port 0) ; Length; will patch later.
2318 (put-u16 die-port 4) ; DWARF 4.
2319 (put-u32 die-port 0) ; Abbrevs offset.
2320 (put-u8 die-port (asm-word-size asm)) ; Address size.
2322 (write-die (make-compile-unit-die asm))
2324 ;; Terminate the abbrevs list.
2325 (put-uleb128 abbrev-port 0)
2329 (values (let ((bv (get-die-bv)))
2330 ;; Patch DWARF32 length.
2331 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2332 (asm-endianness asm))
2333 (make-object asm '.debug_info bv die-relocs '()
2334 #:type SHT_PROGBITS #:flags 0))
2335 (make-object asm '.debug_abbrev (get-abbrev-bv) '() '()
2336 #:type SHT_PROGBITS #:flags 0)
2337 (make-object asm '.debug_str (link-string-table! strtab) '() '()
2338 #:type SHT_PROGBITS #:flags 0)
2339 (make-object asm '.debug_loc #vu8() '() '()
2340 #:type SHT_PROGBITS #:flags 0)
2341 (let ((bv (get-line-bv)))
2342 ;; Patch DWARF32 length.
2343 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2344 (asm-endianness asm))
2345 (make-object asm '.debug_line bv line-relocs '()
2346 #:type SHT_PROGBITS #:flags 0)))))
2348 (define (link-objects asm)
2349 (let*-values (;; Link procprops before constants, because it probably
2350 ;; interns more constants.
2351 ((procprops) (link-procprops asm))
2352 ((ro rw rw-init) (link-constants asm))
2353 ;; Link text object after constants, so that the
2354 ;; constants initializer gets included.
2355 ((text) (link-text-object asm))
2356 ((frame-maps) (link-frame-maps asm))
2357 ((dt) (link-dynamic-section asm text rw rw-init frame-maps))
2358 ((symtab strtab) (link-symtab (linker-object-section text) asm))
2359 ((arities arities-strtab) (link-arities asm))
2360 ((docstrs docstrs-strtab) (link-docstrs asm))
2361 ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
2362 ;; This needs to be linked last, because linking other
2363 ;; sections adds entries to the string table.
2364 ((shstrtab) (link-shstrtab asm)))
2366 (list text ro frame-maps rw dt symtab strtab
2367 arities arities-strtab
2368 docstrs docstrs-strtab procprops
2369 dinfo dabbrev dstrtab dloc dline
2376 ;;; High-level public interfaces.
2379 (define* (link-assembly asm #:key (page-aligned? #t))
2380 "Produce an ELF image from the code and data emitted into @var{asm}.
2381 The result is a bytevector, by default linked so that read-only and
2382 writable data are on separate pages. Pass @code{#:page-aligned? #f} to
2383 disable this behavior."
2384 (link-elf (link-objects asm) #:page-aligned? page-aligned?))