temporarily disable elisp exception tests
[bpt/guile.git] / module / system / vm / assembler.scm
1 ;;; Guile bytecode assembler
2
3 ;;; Copyright (C) 2001, 2009, 2010, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
4 ;;;
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.
9 ;;;
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.
14 ;;;
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
18
19 ;;; Commentary:
20 ;;;
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.
26 ;;;
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.
32 ;;;
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.
37 ;;;
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.
42 ;;;
43 ;;; Code:
44
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
60
61 emit-call
62 emit-call-label
63 emit-tail-call
64 emit-tail-call-label
65 (emit-receive* . emit-receive)
66 emit-receive-values
67 emit-return
68 emit-return-values
69 emit-call/cc
70 emit-abort
71 (emit-builtin-ref* . emit-builtin-ref)
72 emit-br-if-nargs-ne
73 emit-br-if-nargs-lt
74 emit-br-if-nargs-gt
75 emit-assert-nargs-ee
76 emit-assert-nargs-ge
77 emit-assert-nargs-le
78 emit-alloc-frame
79 emit-reset-frame
80 emit-assert-nargs-ee/locals
81 emit-br-if-npos-gt
82 emit-bind-kwargs
83 emit-bind-rest
84 emit-br
85 emit-br-if-true
86 emit-br-if-null
87 emit-br-if-nil
88 emit-br-if-pair
89 emit-br-if-struct
90 emit-br-if-char
91 emit-br-if-tc7
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!)
103 emit-make-closure
104 (emit-free-ref* . emit-free-ref)
105 (emit-free-set!* . emit-free-set!)
106 emit-current-module
107 emit-resolve
108 (emit-define!* . emit-define!)
109 emit-toplevel-box
110 emit-module-box
111 emit-prompt
112 (emit-wind* . emit-wind)
113 emit-unwind
114 (emit-push-fluid* . emit-push-fluid)
115 emit-pop-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-allocate-struct* . emit-allocate-struct)
153 (emit-struct-ref* . emit-struct-ref)
154 (emit-struct-set!* . emit-struct-set!)
155 (emit-class-of* . emit-class-of)
156 (emit-make-array* . emit-make-array)
157 (emit-bv-u8-ref* . emit-bv-u8-ref)
158 (emit-bv-s8-ref* . emit-bv-s8-ref)
159 (emit-bv-u16-ref* . emit-bv-u16-ref)
160 (emit-bv-s16-ref* . emit-bv-s16-ref)
161 (emit-bv-u32-ref* . emit-bv-u32-ref)
162 (emit-bv-s32-ref* . emit-bv-s32-ref)
163 (emit-bv-u64-ref* . emit-bv-u64-ref)
164 (emit-bv-s64-ref* . emit-bv-s64-ref)
165 (emit-bv-f32-ref* . emit-bv-f32-ref)
166 (emit-bv-f64-ref* . emit-bv-f64-ref)
167 (emit-bv-u8-set!* . emit-bv-u8-set!)
168 (emit-bv-s8-set!* . emit-bv-s8-set!)
169 (emit-bv-u16-set!* . emit-bv-u16-set!)
170 (emit-bv-s16-set!* . emit-bv-s16-set!)
171 (emit-bv-u32-set!* . emit-bv-u32-set!)
172 (emit-bv-s32-set!* . emit-bv-s32-set!)
173 (emit-bv-u64-set!* . emit-bv-u64-set!)
174 (emit-bv-s64-set!* . emit-bv-s64-set!)
175 (emit-bv-f32-set!* . emit-bv-f32-set!)
176 (emit-bv-f64-set!* . emit-bv-f64-set!)
177
178 emit-text
179 link-assembly))
180
181
182 \f
183
184 ;; Like define-inlinable, but only for first-order uses of the defined
185 ;; routine. Should residualize less code.
186 (eval-when (expand)
187 (define-syntax define-inline
188 (lambda (x)
189 (syntax-case x ()
190 ((_ (name arg ...) body ...)
191 (with-syntax (((temp ...) (generate-temporaries #'(arg ...))))
192 #`(eval-when (expand)
193 (define-syntax-rule (name temp ...)
194 (let ((arg temp) ...)
195 body ...)))))))))
196
197 ;;; Bytecode consists of 32-bit units, often subdivided in some way.
198 ;;; These helpers create one 32-bit unit from multiple components.
199
200 (define-inline (pack-u8-u24 x y)
201 (unless (<= 0 x 255)
202 (error "out of range" x))
203 (logior x (ash y 8)))
204
205 (define-inline (pack-u8-s24 x y)
206 (unless (<= 0 x 255)
207 (error "out of range" x))
208 (logior x (ash (cond
209 ((< 0 (- y) #x800000)
210 (+ y #x1000000))
211 ((<= 0 y #xffffff)
212 y)
213 (else (error "out of range" y)))
214 8)))
215
216 (define-inline (pack-u1-u7-u24 x y z)
217 (unless (<= 0 x 1)
218 (error "out of range" x))
219 (unless (<= 0 y 127)
220 (error "out of range" y))
221 (logior x (ash y 1) (ash z 8)))
222
223 (define-inline (pack-u8-u12-u12 x y z)
224 (unless (<= 0 x 255)
225 (error "out of range" x))
226 (unless (<= 0 y 4095)
227 (error "out of range" y))
228 (logior x (ash y 8) (ash z 20)))
229
230 (define-inline (pack-u8-u8-u16 x y z)
231 (unless (<= 0 x 255)
232 (error "out of range" x))
233 (unless (<= 0 y 255)
234 (error "out of range" y))
235 (logior x (ash y 8) (ash z 16)))
236
237 (define-inline (pack-u8-u8-u8-u8 x y z w)
238 (unless (<= 0 x 255)
239 (error "out of range" x))
240 (unless (<= 0 y 255)
241 (error "out of range" y))
242 (unless (<= 0 z 255)
243 (error "out of range" z))
244 (logior x (ash y 8) (ash z 16) (ash w 24)))
245
246 (eval-when (expand)
247 (define-syntax pack-flags
248 (syntax-rules ()
249 ;; Add clauses as needed.
250 ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
251 (if f2 (ash 2 0) 0))))))
252
253 ;;; Helpers to read and write 32-bit units in a buffer.
254
255 (define-inline (u32-ref buf n)
256 (bytevector-u32-native-ref buf (* n 4)))
257
258 (define-inline (u32-set! buf n val)
259 (bytevector-u32-native-set! buf (* n 4) val))
260
261 (define-inline (s32-ref buf n)
262 (bytevector-s32-native-ref buf (* n 4)))
263
264 (define-inline (s32-set! buf n val)
265 (bytevector-s32-native-set! buf (* n 4) val))
266
267
268 \f
269
270 ;;; A <meta> entry collects metadata for one procedure. Procedures are
271 ;;; written as contiguous ranges of bytecode.
272 ;;;
273 (eval-when (expand)
274 (define-syntax-rule (assert-match arg pattern kind)
275 (let ((x arg))
276 (unless (match x (pattern #t) (_ #f))
277 (error (string-append "expected " kind) x)))))
278
279 (define-record-type <meta>
280 (%make-meta label properties low-pc high-pc arities)
281 meta?
282 (label meta-label)
283 (properties meta-properties set-meta-properties!)
284 (low-pc meta-low-pc)
285 (high-pc meta-high-pc set-meta-high-pc!)
286 (arities meta-arities set-meta-arities!))
287
288 (define (make-meta label properties low-pc)
289 (assert-match label (or (? exact-integer?) (? symbol?)) "symbol")
290 (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
291 (%make-meta label properties low-pc #f '()))
292
293 (define (meta-name meta)
294 (assq-ref (meta-properties meta) 'name))
295
296 ;; Metadata for one <lambda-case>.
297 (define-record-type <arity>
298 (make-arity req opt rest kw-indices allow-other-keys?
299 low-pc high-pc definitions)
300 arity?
301 (req arity-req)
302 (opt arity-opt)
303 (rest arity-rest)
304 (kw-indices arity-kw-indices)
305 (allow-other-keys? arity-allow-other-keys?)
306 (low-pc arity-low-pc)
307 (high-pc arity-high-pc set-arity-high-pc!)
308 (definitions arity-definitions set-arity-definitions!))
309
310 (eval-when (expand)
311 (define-syntax *block-size* (identifier-syntax 32)))
312
313 ;;; An assembler collects all of the words emitted during assembly, and
314 ;;; also maintains ancillary information such as the constant table, a
315 ;;; relocation list, and so on.
316 ;;;
317 ;;; Bytecode consists of 32-bit units. We emit bytecode using native
318 ;;; endianness. If we're targeting a foreign endianness, we byte-swap
319 ;;; the bytevector as a whole instead of conditionalizing each access.
320 ;;;
321 (define-record-type <asm>
322 (make-asm cur idx start prev written
323 labels relocs
324 word-size endianness
325 constants inits
326 shstrtab next-section-number
327 meta sources
328 dead-slot-maps
329 to-file?)
330 asm?
331
332 ;; We write bytecode into what is logically a growable vector,
333 ;; implemented as a list of blocks. asm-cur is the current block, and
334 ;; asm-idx is the current index into that block, in 32-bit units.
335 ;;
336 (cur asm-cur set-asm-cur!)
337 (idx asm-idx set-asm-idx!)
338
339 ;; asm-start is an absolute position, indicating the offset of the
340 ;; beginning of an instruction (in u32 units). It is updated after
341 ;; writing all the words for one primitive instruction. It models the
342 ;; position of the instruction pointer during execution, given that
343 ;; the VM updates the IP only at the end of executing the instruction,
344 ;; and is thus useful for computing offsets between two points in a
345 ;; program.
346 ;;
347 (start asm-start set-asm-start!)
348
349 ;; The list of previously written blocks.
350 ;;
351 (prev asm-prev set-asm-prev!)
352
353 ;; The number of u32 words written in asm-prev, which is the same as
354 ;; the offset of the current block.
355 ;;
356 (written asm-written set-asm-written!)
357
358 ;; An alist of symbol -> position pairs, indicating the labels defined
359 ;; in this compilation unit.
360 ;;
361 (labels asm-labels set-asm-labels!)
362
363 ;; A list of relocations needed by the program text. We use an
364 ;; internal representation for relocations, and handle textualn
365 ;; relative relocations in the assembler. Other kinds of relocations
366 ;; are later reified as linker relocations and resolved by the linker.
367 ;;
368 (relocs asm-relocs set-asm-relocs!)
369
370 ;; Target information.
371 ;;
372 (word-size asm-word-size)
373 (endianness asm-endianness)
374
375 ;; The constant table, as a vhash of object -> label. All constants
376 ;; get de-duplicated and written into separate sections -- either the
377 ;; .rodata section, for read-only data, or .data, for constants that
378 ;; need initialization at load-time (like symbols). Constants can
379 ;; depend on other constants (e.g. a symbol depending on a stringbuf),
380 ;; so order in this table is important.
381 ;;
382 (constants asm-constants set-asm-constants!)
383
384 ;; A list of instructions needed to initialize the constants. Will
385 ;; run in a thunk with 2 local variables.
386 ;;
387 (inits asm-inits set-asm-inits!)
388
389 ;; The shstrtab, for section names.
390 ;;
391 (shstrtab asm-shstrtab set-asm-shstrtab!)
392
393 ;; The section number for the next section to be written.
394 ;;
395 (next-section-number asm-next-section-number set-asm-next-section-number!)
396
397 ;; A list of <meta>, corresponding to procedure metadata.
398 ;;
399 (meta asm-meta set-asm-meta!)
400
401 ;; A list of (pos . source) pairs, indicating source information. POS
402 ;; is relative to the beginning of the text section, and SOURCE is in
403 ;; the same format that source-properties returns.
404 ;;
405 (sources asm-sources set-asm-sources!)
406
407 ;; A list of (pos . dead-slot-map) pairs, indicating dead slot maps.
408 ;; POS is relative to the beginning of the text section.
409 ;; DEAD-SLOT-MAP is a bitfield of slots that are dead at call sites,
410 ;; as an integer.
411 ;;
412 (dead-slot-maps asm-dead-slot-maps set-asm-dead-slot-maps!)
413 (to-file? asm-to-file?))
414
415 (define-inline (fresh-block)
416 (make-u32vector *block-size*))
417
418 (define* (make-assembler #:key (word-size (target-word-size))
419 (endianness (target-endianness))
420 (to-file? #t))
421 "Create an assembler for a given target @var{word-size} and
422 @var{endianness}, falling back to appropriate values for the configured
423 target."
424 (make-asm (fresh-block) 0 0 '() 0
425 (make-hash-table) '()
426 word-size endianness
427 vlist-null '()
428 (make-string-table) 1
429 '() '() '() to-file?))
430
431 (define (intern-section-name! asm string)
432 "Add a string to the section name table (shstrtab)."
433 (string-table-intern! (asm-shstrtab asm) string))
434
435 (define-inline (asm-pos asm)
436 "The offset of the next word to be written into the code buffer, in
437 32-bit units."
438 (+ (asm-idx asm) (asm-written asm)))
439
440 (define (allocate-new-block asm)
441 "Close off the current block, and arrange for the next word to be
442 written to a fresh block."
443 (let ((new (fresh-block)))
444 (set-asm-prev! asm (cons (asm-cur asm) (asm-prev asm)))
445 (set-asm-written! asm (asm-pos asm))
446 (set-asm-cur! asm new)
447 (set-asm-idx! asm 0)))
448
449 (define-inline (emit asm u32)
450 "Emit one 32-bit word into the instruction stream. Assumes that there
451 is space for the word, and ensures that there is space for the next
452 word."
453 (u32-set! (asm-cur asm) (asm-idx asm) u32)
454 (set-asm-idx! asm (1+ (asm-idx asm)))
455 (if (= (asm-idx asm) *block-size*)
456 (allocate-new-block asm)))
457
458 (define-inline (make-reloc type label base word)
459 "Make an internal relocation of type @var{type} referencing symbol
460 @var{label}, @var{word} words after position @var{start}. @var{type}
461 may be x8-s24, indicating a 24-bit relative label reference that can be
462 fixed up by the assembler, or s32, indicating a 32-bit relative
463 reference that needs to be fixed up by the linker."
464 (list type label base word))
465
466 (define-inline (reset-asm-start! asm)
467 "Reset the asm-start after writing the words for one instruction."
468 (set-asm-start! asm (asm-pos asm)))
469
470 (define (record-label-reference asm label)
471 "Record an x8-s24 local label reference. This value will get patched
472 up later by the assembler."
473 (let* ((start (asm-start asm))
474 (pos (asm-pos asm))
475 (reloc (make-reloc 'x8-s24 label start (- pos start))))
476 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
477
478 (define* (record-far-label-reference asm label #:optional (offset 0))
479 "Record an s32 far label reference. This value will get patched up
480 later by the linker."
481 (let* ((start (- (asm-start asm) offset))
482 (pos (asm-pos asm))
483 (reloc (make-reloc 's32 label start (- pos start))))
484 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
485
486
487 \f
488
489 ;;;
490 ;;; Primitive assemblers are defined by expanding `assembler' for each
491 ;;; opcode in `(instruction-list)'.
492 ;;;
493
494 (eval-when (expand)
495 (define (id-append ctx a b)
496 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b))))
497
498 (define-syntax assembler
499 (lambda (x)
500 (define-syntax op-case
501 (lambda (x)
502 (syntax-case x ()
503 ((_ asm name ((type arg ...) code ...) clause ...)
504 #`(if (eq? name 'type)
505 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
506 #'((arg ...)
507 code ...))
508 (op-case asm name clause ...)))
509 ((_ asm name)
510 #'(error "unmatched name" name)))))
511
512 (define (pack-first-word asm opcode type)
513 (with-syntax ((opcode opcode))
514 (op-case
515 asm type
516 ((U8_X24)
517 (emit asm opcode))
518 ((U8_U24 arg)
519 (emit asm (pack-u8-u24 opcode arg)))
520 ((U8_L24 label)
521 (record-label-reference asm label)
522 (emit asm opcode))
523 ((U8_U8_I16 a imm)
524 (emit asm (pack-u8-u8-u16 opcode a (object-address imm))))
525 ((U8_U12_U12 a b)
526 (emit asm (pack-u8-u12-u12 opcode a b)))
527 ((U8_U8_U8_U8 a b c)
528 (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
529
530 (define (pack-tail-word asm type)
531 (op-case
532 asm type
533 ((U8_U24 a b)
534 (emit asm (pack-u8-u24 a b)))
535 ((U8_L24 a label)
536 (record-label-reference asm label)
537 (emit asm a))
538 ((U32 a)
539 (emit asm a))
540 ((I32 imm)
541 (let ((val (object-address imm)))
542 (unless (zero? (ash val -32))
543 (error "FIXME: enable truncation of negative fixnums when cross-compiling"))
544 (emit asm val)))
545 ((A32 imm)
546 (unless (= (asm-word-size asm) 8)
547 (error "make-long-immediate unavailable for this target"))
548 (emit asm (ash (object-address imm) -32))
549 (emit asm (logand (object-address imm) (1- (ash 1 32)))))
550 ((B32))
551 ((N32 label)
552 (record-far-label-reference asm label)
553 (emit asm 0))
554 ((S32 label)
555 (record-far-label-reference asm label)
556 (emit asm 0))
557 ((L32 label)
558 (record-far-label-reference asm label)
559 (emit asm 0))
560 ((LO32 label offset)
561 (record-far-label-reference asm label
562 (* offset (/ (asm-word-size asm) 4)))
563 (emit asm 0))
564 ((X8_U24 a)
565 (emit asm (pack-u8-u24 0 a)))
566 ((X8_L24 label)
567 (record-label-reference asm label)
568 (emit asm 0))
569 ((B1_X7_L24 a label)
570 (record-label-reference asm label)
571 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
572 ((B1_U7_L24 a b label)
573 (record-label-reference asm label)
574 (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
575 ((B1_X31 a)
576 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
577 ((B1_X7_U24 a b)
578 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))))
579
580 (syntax-case x ()
581 ((_ name opcode word0 word* ...)
582 (with-syntax ((((formal0 ...)
583 code0 ...)
584 (pack-first-word #'asm
585 (syntax->datum #'opcode)
586 (syntax->datum #'word0)))
587 ((((formal* ...)
588 code* ...) ...)
589 (map (lambda (word) (pack-tail-word #'asm word))
590 (syntax->datum #'(word* ...)))))
591 #'(lambda (asm formal0 ... formal* ... ...)
592 (unless (asm? asm) (error "not an asm"))
593 code0 ...
594 code* ... ...
595 (reset-asm-start! asm))))))))
596
597 (define assemblers (make-hash-table))
598
599 (eval-when (expand)
600 (define-syntax define-assembler
601 (lambda (x)
602 (syntax-case x ()
603 ((_ name opcode kind arg ...)
604 (with-syntax ((emit (id-append #'name #'emit- #'name)))
605 #'(define emit
606 (let ((emit (assembler name opcode arg ...)))
607 (hashq-set! assemblers 'name emit)
608 emit)))))))
609
610 (define-syntax visit-opcodes
611 (lambda (x)
612 (syntax-case x ()
613 ((visit-opcodes macro arg ...)
614 (with-syntax (((inst ...)
615 (map (lambda (x) (datum->syntax #'macro x))
616 (instruction-list))))
617 #'(begin
618 (macro arg ... . inst)
619 ...)))))))
620
621 (visit-opcodes define-assembler)
622
623 (eval-when (expand)
624
625 ;; Some operands are encoded using a restricted subset of the full
626 ;; 24-bit local address space, in order to make the bytecode more
627 ;; dense in the usual case that there are few live locals. Here we
628 ;; define wrapper emitters that shuffle out-of-range operands into and
629 ;; out of the reserved range of locals [233,255]. This range is
630 ;; sufficient because these restricted operands are only present in
631 ;; the first word of an instruction. Since 8 bits is the smallest
632 ;; slot-addressing operand size, that means we can fit 3 operands in
633 ;; the 24 bits of payload of the first word (the lower 8 bits being
634 ;; taken by the opcode).
635 ;;
636 ;; The result are wrapper emitters with the same arity,
637 ;; e.g. emit-cons* that wraps emit-cons. We expose these wrappers as
638 ;; the public interface for emitting `cons' instructions. That way we
639 ;; solve the problem fully and in just one place. The only manual
640 ;; care that need be taken is in the exports list at the top of the
641 ;; file -- to be sure that we export the wrapper and not the wrapped
642 ;; emitter.
643
644 (define (shuffling-assembler name kind word0 word*)
645 (define (analyze-first-word)
646 (define-syntax op-case
647 (syntax-rules ()
648 ((_ type ((%type %kind arg ...) values) clause ...)
649 (if (and (eq? type '%type) (eq? kind '%kind))
650 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
651 #'((arg ...) values))
652 (op-case type clause ...)))
653 ((_ type)
654 #f)))
655 (op-case
656 word0
657 ((U8_U8_I16 ! a imm)
658 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
659 imm))
660 ((U8_U8_I16 <- a imm)
661 (values (if (< a (ash 1 8)) a 253)
662 imm))
663 ((U8_U12_U12 ! a b)
664 (values (if (< a (ash 1 12)) a (begin (emit-mov* asm 253 a) 253))
665 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
666 ((U8_U12_U12 <- a b)
667 (values (if (< a (ash 1 12)) a 253)
668 (if (< b (ash 1 12)) b (begin (emit-mov* asm 254 b) 254))))
669 ((U8_U8_U8_U8 ! a b c)
670 (values (if (< a (ash 1 8)) a (begin (emit-mov* asm 253 a) 253))
671 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
672 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))
673 ((U8_U8_U8_U8 <- a b c)
674 (values (if (< a (ash 1 8)) a 253)
675 (if (< b (ash 1 8)) b (begin (emit-mov* asm 254 b) 254))
676 (if (< c (ash 1 8)) c (begin (emit-mov* asm 255 c) 255))))))
677
678 (define (tail-formals type)
679 (define-syntax op-case
680 (syntax-rules ()
681 ((op-case type (%type arg ...) clause ...)
682 (if (eq? type '%type)
683 (generate-temporaries #'(arg ...))
684 (op-case type clause ...)))
685 ((op-case type)
686 (error "unmatched type" type))))
687 (op-case type
688 (U8_U24 a b)
689 (U8_L24 a label)
690 (U32 a)
691 (I32 imm)
692 (A32 imm)
693 (B32)
694 (N32 label)
695 (S32 label)
696 (L32 label)
697 (LO32 label offset)
698 (X8_U24 a)
699 (X8_L24 label)
700 (B1_X7_L24 a label)
701 (B1_U7_L24 a b label)
702 (B1_X31 a)
703 (B1_X7_U24 a b)))
704
705 (define (shuffle-up dst)
706 (define-syntax op-case
707 (syntax-rules ()
708 ((_ type ((%type ...) exp) clause ...)
709 (if (memq type '(%type ...))
710 #'exp
711 (op-case type clause ...)))
712 ((_ type)
713 (error "unexpected type" type))))
714 (with-syntax ((dst dst))
715 (op-case
716 word0
717 ((U8_U8_I16 U8_U8_U8_U8)
718 (unless (< dst (ash 1 8))
719 (emit-mov* asm dst 253)))
720 ((U8_U12_U12)
721 (unless (< dst (ash 1 12))
722 (emit-mov* asm dst 253))))))
723
724 (and=>
725 (analyze-first-word)
726 (lambda (formals+shuffle)
727 (with-syntax ((emit-name (id-append name #'emit- name))
728 (((formal0 ...) shuffle) formals+shuffle)
729 (((formal* ...) ...) (map tail-formals word*)))
730 (with-syntax (((shuffle-up-dst ...)
731 (if (eq? kind '<-)
732 (syntax-case #'(formal0 ...) ()
733 ((dst . _)
734 (list (shuffle-up #'dst))))
735 '())))
736 #'(lambda (asm formal0 ... formal* ... ...)
737 (call-with-values (lambda () shuffle)
738 (lambda (formal0 ...)
739 (emit-name asm formal0 ... formal* ... ...)))
740 shuffle-up-dst ...))))))
741
742 (define-syntax define-shuffling-assembler
743 (lambda (stx)
744 (syntax-case stx ()
745 ((_ #:except (except ...) name opcode kind word0 word* ...)
746 (cond
747 ((or-map (lambda (op) (eq? (syntax->datum #'name) op))
748 (map syntax->datum #'(except ...)))
749 #'(begin))
750 ((shuffling-assembler #'name (syntax->datum #'kind)
751 (syntax->datum #'word0)
752 (map syntax->datum #'(word* ...)))
753 => (lambda (proc)
754 (with-syntax ((emit (id-append #'name
755 (id-append #'name #'emit- #'name)
756 #'*))
757 (proc proc))
758 #'(define emit
759 (let ((emit proc))
760 (hashq-set! assemblers 'name emit)
761 emit)))))
762 (else #'(begin))))))))
763
764 (visit-opcodes define-shuffling-assembler #:except (receive mov))
765
766 ;; Mov and receive are two special cases that can work without wrappers.
767 ;; Indeed it is important that they do so.
768
769 (define (emit-mov* asm dst src)
770 (if (and (< dst (ash 1 12)) (< src (ash 1 12)))
771 (emit-mov asm dst src)
772 (emit-long-mov asm dst src)))
773
774 (define (emit-receive* asm dst proc nlocals)
775 (if (and (< dst (ash 1 12)) (< proc (ash 1 12)))
776 (emit-receive asm dst proc nlocals)
777 (begin
778 (emit-receive-values asm proc #t 1)
779 (emit-mov* asm dst (1+ proc))
780 (emit-reset-frame asm nlocals))))
781
782 (define (emit-text asm instructions)
783 "Assemble @var{instructions} using the assembler @var{asm}.
784 @var{instructions} is a sequence of instructions, expressed as a list of
785 lists. This procedure can be called many times before calling
786 @code{link-assembly}."
787 (for-each (lambda (inst)
788 (apply (or (hashq-ref assemblers (car inst))
789 (error 'bad-instruction inst))
790 asm
791 (cdr inst)))
792 instructions))
793
794 \f
795
796 ;;;
797 ;;; The constant table records a topologically sorted set of literal
798 ;;; constants used by a program. For example, a pair uses its car and
799 ;;; cdr, a string uses its stringbuf, etc.
800 ;;;
801 ;;; Some things we want to add to the constant table are not actually
802 ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
803 ;;; references, or cache cells for non-closure procedures. For these we
804 ;;; define special record types and add instances of those record types
805 ;;; to the table.
806 ;;;
807
808 (define-inline (immediate? x)
809 "Return @code{#t} if @var{x} is immediate, and @code{#f} otherwise."
810 (not (zero? (logand (object-address x) 6))))
811
812 (define-record-type <stringbuf>
813 (make-stringbuf string)
814 stringbuf?
815 (string stringbuf-string))
816
817 (define-record-type <static-procedure>
818 (make-static-procedure code)
819 static-procedure?
820 (code static-procedure-code))
821
822 (define-record-type <uniform-vector-backing-store>
823 (make-uniform-vector-backing-store bytes element-size)
824 uniform-vector-backing-store?
825 (bytes uniform-vector-backing-store-bytes)
826 (element-size uniform-vector-backing-store-element-size))
827
828 (define-record-type <cache-cell>
829 (make-cache-cell scope key)
830 cache-cell?
831 (scope cache-cell-scope)
832 (key cache-cell-key))
833
834 (define (simple-vector? obj)
835 (and (vector? obj)
836 (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
837
838 (define (simple-uniform-vector? obj)
839 (and (array? obj)
840 (symbol? (array-type obj))
841 (equal? (array-shape obj) (list (list 0 (1- (array-length obj)))))))
842
843 (define (statically-allocatable? x)
844 "Return @code{#t} if a non-immediate constant can be allocated
845 statically, and @code{#f} if it would need some kind of runtime
846 allocation."
847 (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
848
849 (define (intern-constant asm obj)
850 "Add an object to the constant table, and return a label that can be
851 used to reference it. If the object is already present in the constant
852 table, its existing label is used directly."
853 (define (recur obj)
854 (intern-constant asm obj))
855 (define (field dst n obj)
856 (let ((src (recur obj)))
857 (if src
858 (if (statically-allocatable? obj)
859 `((static-patch! ,dst ,n ,src))
860 `((static-ref 1 ,src)
861 (static-set! 1 ,dst ,n)))
862 '())))
863 (define (intern obj label)
864 (cond
865 ((pair? obj)
866 (append (field label 0 (car obj))
867 (field label 1 (cdr obj))))
868 ((simple-vector? obj)
869 (let lp ((i 0) (inits '()))
870 (if (< i (vector-length obj))
871 (lp (1+ i)
872 (append-reverse (field label (1+ i) (vector-ref obj i))
873 inits))
874 (reverse inits))))
875 ((stringbuf? obj) '())
876 ((static-procedure? obj)
877 `((static-patch! ,label 1 ,(static-procedure-code obj))))
878 ((cache-cell? obj) '())
879 ((and (symbol? obj) (symbol-interned? obj))
880 `((make-non-immediate 1 ,(recur (symbol->string obj)))
881 (string->symbol 1 1)
882 (static-set! 1 ,label 0)))
883 ((string? obj)
884 `((static-patch! ,label 1 ,(recur (make-stringbuf obj)))))
885 ((keyword? obj)
886 `((static-ref 1 ,(recur (keyword->symbol obj)))
887 (symbol->keyword 1 1)
888 (static-set! 1 ,label 0)))
889 ((number? obj)
890 `((make-non-immediate 1 ,(recur (number->string obj)))
891 (string->number 1 1)
892 (static-set! 1 ,label 0)))
893 ((uniform-vector-backing-store? obj) '())
894 ((simple-uniform-vector? obj)
895 (let ((width (case (array-type obj)
896 ((vu8 u8 s8) 1)
897 ((u16 s16) 2)
898 ;; Bitvectors are addressed in 32-bit units.
899 ;; Although a complex number is 8 or 16 bytes wide,
900 ;; it should be byteswapped in 4 or 8 byte units.
901 ((u32 s32 f32 c32 b) 4)
902 ((u64 s64 f64 c64) 8)
903 (else
904 (error "unhandled array type" obj)))))
905 `((static-patch! ,label 2
906 ,(recur (make-uniform-vector-backing-store
907 (uniform-array->bytevector obj)
908 width))))))
909 ((array? obj)
910 `((static-patch! ,label 1 ,(recur (shared-array-root obj)))))
911 (else
912 (if (asm-to-file? asm)
913 (error "don't know how to intern" obj)
914 `((make-short-immediate 1 ,(vlist-length (asm-constants asm)))
915 (vector-ref 1 0 1)
916 (static-set! 1 ,label 0))))))
917 (cond
918 ((immediate? obj) #f)
919 ((vhash-assoc obj (asm-constants asm)) => cdr)
920 (else
921 ;; Note that calling intern may mutate asm-constants and asm-inits.
922 (let* ((label (gensym "constant"))
923 (inits (intern obj label)))
924 (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
925 (set-asm-inits! asm (append-reverse inits (asm-inits asm)))
926 label))))
927
928 (define (intern-non-immediate asm obj)
929 "Intern a non-immediate into the constant table, and return its
930 label."
931 (when (immediate? obj)
932 (error "expected a non-immediate" obj))
933 (intern-constant asm obj))
934
935 (define (intern-cache-cell asm scope key)
936 "Intern a cache cell into the constant table, and return its label.
937 If there is already a cache cell with the given scope and key, it is
938 returned instead."
939 (intern-constant asm (make-cache-cell scope key)))
940
941 ;; Return the label of the cell that holds the module for a scope.
942 (define (intern-module-cache-cell asm scope)
943 "Intern a cache cell for a module, and return its label."
944 (intern-cache-cell asm scope #t))
945
946
947 \f
948
949 ;;;
950 ;;; Macro assemblers bridge the gap between primitive instructions and
951 ;;; some higher-level operations.
952 ;;;
953
954 (eval-when (expand)
955 (define-syntax define-macro-assembler
956 (lambda (x)
957 (syntax-case x ()
958 ((_ (name arg ...) body body* ...)
959 (with-syntax ((emit (id-append #'name #'emit- #'name)))
960 #'(begin
961 (define emit
962 (let ((emit (lambda (arg ...) body body* ...)))
963 (hashq-set! assemblers 'name emit)
964 emit))
965 (export emit))))))))
966
967 (define-macro-assembler (load-constant asm dst obj)
968 (cond
969 ((immediate? obj)
970 (let ((bits (object-address obj)))
971 (cond
972 ((and (< dst 256) (zero? (ash bits -16)))
973 (emit-make-short-immediate asm dst obj))
974 ((zero? (ash bits -32))
975 (emit-make-long-immediate asm dst obj))
976 (else
977 (emit-make-long-long-immediate asm dst obj)))))
978 ((statically-allocatable? obj)
979 (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
980 (else
981 (emit-static-ref asm dst (intern-non-immediate asm obj)))))
982
983 (define-macro-assembler (load-static-procedure asm dst label)
984 (let ((loc (intern-constant asm (make-static-procedure label))))
985 (emit-make-non-immediate asm dst loc)))
986
987 (define-syntax-rule (define-tc7-macro-assembler name tc7)
988 (define-macro-assembler (name asm slot invert? label)
989 (emit-br-if-tc7 asm slot invert? tc7 label)))
990
991 ;; Keep in sync with tags.h. Part of Guile's ABI. Currently unused
992 ;; macro assemblers are commented out. See also
993 ;; *branching-primcall-arities* in (language cps primitives), the set of
994 ;; macro-instructions in assembly.scm, and
995 ;; disassembler.scm:code-annotation.
996 ;;
997 ;; FIXME: Define all tc7 values in Scheme in one place, derived from
998 ;; tags.h.
999 (define-tc7-macro-assembler br-if-symbol 5)
1000 (define-tc7-macro-assembler br-if-variable 7)
1001 (define-tc7-macro-assembler br-if-vector 13)
1002 ;(define-tc7-macro-assembler br-if-weak-vector 13)
1003 (define-tc7-macro-assembler br-if-string 21)
1004 ;(define-tc7-macro-assembler br-if-heap-number 23)
1005 ;(define-tc7-macro-assembler br-if-stringbuf 39)
1006 (define-tc7-macro-assembler br-if-bytevector 77)
1007 ;(define-tc7-macro-assembler br-if-pointer 31)
1008 ;(define-tc7-macro-assembler br-if-hashtable 29)
1009 ;(define-tc7-macro-assembler br-if-fluid 37)
1010 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
1011 ;(define-tc7-macro-assembler br-if-frame 47)
1012 (define-tc7-macro-assembler br-if-keyword 53)
1013 ;(define-tc7-macro-assembler br-if-vm 55)
1014 ;(define-tc7-macro-assembler br-if-vm-cont 71)
1015 ;(define-tc7-macro-assembler br-if-rtl-program 69)
1016 ;(define-tc7-macro-assembler br-if-weak-set 85)
1017 ;(define-tc7-macro-assembler br-if-weak-table 87)
1018 ;(define-tc7-macro-assembler br-if-array 93)
1019 (define-tc7-macro-assembler br-if-bitvector 95)
1020 ;(define-tc7-macro-assembler br-if-port 125)
1021 ;(define-tc7-macro-assembler br-if-smob 127)
1022
1023 (define-macro-assembler (begin-program asm label properties)
1024 (emit-label asm label)
1025 (let ((meta (make-meta label properties (asm-start asm))))
1026 (set-asm-meta! asm (cons meta (asm-meta asm)))))
1027
1028 (define-macro-assembler (end-program asm)
1029 (let ((meta (car (asm-meta asm))))
1030 (set-meta-high-pc! meta (asm-start asm))
1031 (set-meta-arities! meta (reverse (meta-arities meta)))))
1032
1033 (define-macro-assembler (begin-standard-arity asm req nlocals alternate)
1034 (emit-begin-opt-arity asm req '() #f nlocals alternate))
1035
1036 (define-macro-assembler (begin-opt-arity asm req opt rest nlocals alternate)
1037 (emit-begin-kw-arity asm req opt rest '() #f nlocals alternate))
1038
1039 (define-macro-assembler (begin-kw-arity asm req opt rest kw-indices
1040 allow-other-keys? nlocals alternate)
1041 (assert-match req ((? symbol?) ...) "list of symbols")
1042 (assert-match opt ((? symbol?) ...) "list of symbols")
1043 (assert-match rest (or #f (? symbol?)) "#f or symbol")
1044 (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
1045 "alist of keyword -> integer")
1046 (assert-match allow-other-keys? (? boolean?) "boolean")
1047 (assert-match nlocals (? integer?) "integer")
1048 (assert-match alternate (or #f (? exact-integer?) (? symbol?)) "#f or symbol")
1049 (let* ((meta (car (asm-meta asm)))
1050 (arity (make-arity req opt rest kw-indices allow-other-keys?
1051 (asm-start asm) #f '()))
1052 ;; The procedure itself is in slot 0, in the standard calling
1053 ;; convention. For procedure prologues, nreq includes the
1054 ;; procedure, so here we add 1.
1055 (nreq (1+ (length req)))
1056 (nopt (length opt))
1057 (rest? (->bool rest)))
1058 (set-meta-arities! meta (cons arity (meta-arities meta)))
1059 (cond
1060 ((or allow-other-keys? (pair? kw-indices))
1061 (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
1062 nlocals alternate))
1063 ((or rest? (pair? opt))
1064 (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
1065 (else
1066 (emit-standard-prelude asm nreq nlocals alternate)))))
1067
1068 (define-macro-assembler (end-arity asm)
1069 (let ((arity (car (meta-arities (car (asm-meta asm))))))
1070 (set-arity-definitions! arity (reverse (arity-definitions arity)))
1071 (set-arity-high-pc! arity (asm-start asm))))
1072
1073 ;; As noted above, we reserve locals 253 through 255 for shuffling large
1074 ;; operands. However the calling convention has all arguments passed in
1075 ;; a contiguous block. This helper, called after the clause has been
1076 ;; chosen and the keyword/optional/rest arguments have been processed,
1077 ;; shuffles up arguments from slot 253 and higher into their final
1078 ;; allocations.
1079 ;;
1080 (define (shuffle-up-args asm nargs)
1081 (when (> nargs 253)
1082 (let ((slot (1- nargs)))
1083 (emit-mov asm (+ slot 3) slot)
1084 (shuffle-up-args asm (1- nargs)))))
1085
1086 (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
1087 (cond
1088 (alternate
1089 (emit-br-if-nargs-ne asm nreq alternate)
1090 (emit-alloc-frame asm nlocals))
1091 ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
1092 (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
1093 (else
1094 (emit-assert-nargs-ee asm nreq)
1095 (emit-alloc-frame asm nlocals)))
1096 (shuffle-up-args asm nreq))
1097
1098 (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
1099 (if alternate
1100 (emit-br-if-nargs-lt asm nreq alternate)
1101 (emit-assert-nargs-ge asm nreq))
1102 (cond
1103 (rest?
1104 (emit-bind-rest asm (+ nreq nopt)))
1105 (alternate
1106 (emit-br-if-nargs-gt asm (+ nreq nopt) alternate))
1107 (else
1108 (emit-assert-nargs-le asm (+ nreq nopt))))
1109 (emit-alloc-frame asm nlocals)
1110 (shuffle-up-args asm (+ nreq nopt (if rest? 1 0))))
1111
1112 (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
1113 allow-other-keys? nlocals alternate)
1114 (if alternate
1115 (begin
1116 (emit-br-if-nargs-lt asm nreq alternate)
1117 (unless rest?
1118 (emit-br-if-npos-gt asm nreq (+ nreq nopt) alternate)))
1119 (emit-assert-nargs-ge asm nreq))
1120 (let ((ntotal (fold (lambda (kw ntotal)
1121 (match kw
1122 (((? keyword?) . idx)
1123 (max (1+ idx) ntotal))))
1124 (+ nreq nopt) kw-indices)))
1125 ;; FIXME: port 581f410f
1126 (emit-bind-kwargs asm nreq
1127 (pack-flags allow-other-keys? rest?)
1128 (+ nreq nopt)
1129 ntotal
1130 (intern-constant asm kw-indices))
1131 (emit-alloc-frame asm nlocals)
1132 (shuffle-up-args asm ntotal)))
1133
1134 (define-macro-assembler (label asm sym)
1135 (hashq-set! (asm-labels asm) sym (asm-start asm)))
1136
1137 (define-macro-assembler (source asm source)
1138 (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
1139
1140 (define-macro-assembler (definition asm name slot)
1141 (let* ((arity (car (meta-arities (car (asm-meta asm)))))
1142 (def (vector name
1143 slot
1144 (* (- (asm-start asm) (arity-low-pc arity)) 4))))
1145 (set-arity-definitions! arity (cons def (arity-definitions arity)))))
1146
1147 (define-macro-assembler (cache-current-module! asm module scope)
1148 (let ((mod-label (intern-module-cache-cell asm scope)))
1149 (emit-static-set! asm module mod-label 0)))
1150
1151 (define-macro-assembler (cached-toplevel-box asm dst scope sym bound?)
1152 (let ((sym-label (intern-non-immediate asm sym))
1153 (mod-label (intern-module-cache-cell asm scope))
1154 (cell-label (intern-cache-cell asm scope sym)))
1155 (emit-toplevel-box asm dst cell-label mod-label sym-label bound?)))
1156
1157 (define-macro-assembler (cached-module-box asm dst module-name sym public? bound?)
1158 (let* ((sym-label (intern-non-immediate asm sym))
1159 (key (cons public? module-name))
1160 (mod-name-label (intern-constant asm key))
1161 (cell-label (intern-cache-cell asm key sym)))
1162 (emit-module-box asm dst cell-label mod-name-label sym-label bound?)))
1163
1164 (define-macro-assembler (dead-slot-map asm proc-slot dead-slot-map)
1165 (unless (zero? dead-slot-map)
1166 (set-asm-dead-slot-maps! asm
1167 (cons
1168 (cons* (asm-start asm) proc-slot dead-slot-map)
1169 (asm-dead-slot-maps asm)))))
1170
1171 \f
1172
1173 ;;;
1174 ;;; Helper for linking objects.
1175 ;;;
1176
1177 (define (make-object asm name bv relocs labels . kwargs)
1178 "Make a linker object. This helper handles interning the name in the
1179 shstrtab, assigning the size, allocating a fresh index, and defining a
1180 corresponding linker symbol for the start of the section."
1181 (let ((name-idx (intern-section-name! asm (symbol->string name)))
1182 (index (asm-next-section-number asm)))
1183 (set-asm-next-section-number! asm (1+ index))
1184 (make-linker-object (apply make-elf-section
1185 #:index index
1186 #:name name-idx
1187 #:size (bytevector-length bv)
1188 kwargs)
1189 bv relocs
1190 (cons (make-linker-symbol name 0) labels))))
1191
1192
1193 \f
1194
1195 ;;;
1196 ;;; Linking the constant table. This code is somewhat intertwingled
1197 ;;; with the intern-constant code above, as that procedure also
1198 ;;; residualizes instructions to initialize constants at load time.
1199 ;;;
1200
1201 (define (write-immediate asm buf pos x)
1202 (let ((val (object-address x))
1203 (endianness (asm-endianness asm)))
1204 (case (asm-word-size asm)
1205 ((4) (bytevector-u32-set! buf pos val endianness))
1206 ((8) (bytevector-u64-set! buf pos val endianness))
1207 (else (error "bad word size" asm)))))
1208
1209 (define (emit-init-constants asm)
1210 "If there is writable data that needs initialization at runtime, emit
1211 a procedure to do that and return its label. Otherwise return
1212 @code{#f}."
1213 (let ((inits (asm-inits asm)))
1214 (and (not (null? inits))
1215 (let ((label (gensym "init-constants")))
1216 (emit-text asm
1217 `((begin-program ,label ())
1218 ,@(if (asm-to-file? asm)
1219 '((assert-nargs-ee/locals 1 1))
1220 '((assert-nargs-ee/locals 2 0)
1221 (mov 0 1)))
1222 ,@(reverse inits)
1223 (load-constant 1 ,*unspecified*)
1224 (return 1)
1225 (end-program)))
1226 label))))
1227
1228 (define (link-data asm data name)
1229 "Link the static data for a program into the @var{name} section (which
1230 should be .data or .rodata), and return the resulting linker object.
1231 @var{data} should be a vhash mapping objects to labels."
1232 (define (align address alignment)
1233 (+ address
1234 (modulo (- alignment (modulo address alignment)) alignment)))
1235
1236 (define tc7-vector 13)
1237 (define stringbuf-shared-flag #x100)
1238 (define stringbuf-wide-flag #x400)
1239 (define tc7-stringbuf 39)
1240 (define tc7-narrow-stringbuf
1241 (+ tc7-stringbuf stringbuf-shared-flag))
1242 (define tc7-wide-stringbuf
1243 (+ tc7-stringbuf stringbuf-shared-flag stringbuf-wide-flag))
1244 (define tc7-ro-string (+ 21 #x200))
1245 (define tc7-program 69)
1246 (define tc7-bytevector 77)
1247 (define tc7-bitvector 95)
1248 (define tc7-array 93)
1249
1250 (let ((word-size (asm-word-size asm))
1251 (endianness (asm-endianness asm)))
1252 (define (byte-length x)
1253 (cond
1254 ((stringbuf? x)
1255 (let ((x (stringbuf-string x)))
1256 (+ (* 2 word-size)
1257 (case (string-bytes-per-char x)
1258 ((1) (1+ (string-length x)))
1259 ((4) (* (1+ (string-length x)) 4))
1260 (else (error "bad string bytes per char" x))))))
1261 ((static-procedure? x)
1262 (* 2 word-size))
1263 ((string? x)
1264 (* 4 word-size))
1265 ((pair? x)
1266 (* 2 word-size))
1267 ((simple-vector? x)
1268 (* (1+ (vector-length x)) word-size))
1269 ((simple-uniform-vector? x)
1270 (* 4 word-size))
1271 ((uniform-vector-backing-store? x)
1272 (bytevector-length (uniform-vector-backing-store-bytes x)))
1273 ((array? x)
1274 (* word-size (+ 3 (* 3 (array-rank x)))))
1275 (else
1276 word-size)))
1277
1278 (define (write-constant-reference buf pos x)
1279 ;; The asm-inits will fix up any reference to a non-immediate.
1280 (write-immediate asm buf pos (if (immediate? x) x #f)))
1281
1282 (define (write buf pos obj)
1283 (cond
1284 ((stringbuf? obj)
1285 (let* ((x (stringbuf-string obj))
1286 (len (string-length x))
1287 (tag (if (= (string-bytes-per-char x) 1)
1288 tc7-narrow-stringbuf
1289 tc7-wide-stringbuf)))
1290 (case word-size
1291 ((4)
1292 (bytevector-u32-set! buf pos tag endianness)
1293 (bytevector-u32-set! buf (+ pos 4) len endianness))
1294 ((8)
1295 (bytevector-u64-set! buf pos tag endianness)
1296 (bytevector-u64-set! buf (+ pos 8) len endianness))
1297 (else
1298 (error "bad word size" asm)))
1299 (let ((pos (+ pos (* word-size 2))))
1300 (case (string-bytes-per-char x)
1301 ((1)
1302 (let lp ((i 0))
1303 (if (< i len)
1304 (let ((u8 (char->integer (string-ref x i))))
1305 (bytevector-u8-set! buf (+ pos i) u8)
1306 (lp (1+ i)))
1307 (bytevector-u8-set! buf (+ pos i) 0))))
1308 ((4)
1309 (let lp ((i 0))
1310 (if (< i len)
1311 (let ((u32 (char->integer (string-ref x i))))
1312 (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
1313 (lp (1+ i)))
1314 (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
1315 (else (error "bad string bytes per char" x))))))
1316
1317 ((static-procedure? obj)
1318 (case word-size
1319 ((4)
1320 (bytevector-u32-set! buf pos tc7-program endianness)
1321 (bytevector-u32-set! buf (+ pos 4) 0 endianness))
1322 ((8)
1323 (bytevector-u64-set! buf pos tc7-program endianness)
1324 (bytevector-u64-set! buf (+ pos 8) 0 endianness))
1325 (else (error "bad word size"))))
1326
1327 ((cache-cell? obj)
1328 (write-immediate asm buf pos #f))
1329
1330 ((string? obj)
1331 (let ((tag (logior tc7-ro-string (ash (string-length obj) 8)))) ; FIXME: unused?
1332 (case word-size
1333 ((4)
1334 (bytevector-u32-set! buf pos tc7-ro-string endianness)
1335 (write-immediate asm buf (+ pos 4) #f) ; stringbuf
1336 (bytevector-u32-set! buf (+ pos 8) 0 endianness)
1337 (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
1338 ((8)
1339 (bytevector-u64-set! buf pos tc7-ro-string endianness)
1340 (write-immediate asm buf (+ pos 8) #f) ; stringbuf
1341 (bytevector-u64-set! buf (+ pos 16) 0 endianness)
1342 (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
1343 (else (error "bad word size")))))
1344
1345 ((pair? obj)
1346 (write-constant-reference buf pos (car obj))
1347 (write-constant-reference buf (+ pos word-size) (cdr obj)))
1348
1349 ((simple-vector? obj)
1350 (let* ((len (vector-length obj))
1351 (tag (logior tc7-vector (ash len 8))))
1352 (case word-size
1353 ((4) (bytevector-u32-set! buf pos tag endianness))
1354 ((8) (bytevector-u64-set! buf pos tag endianness))
1355 (else (error "bad word size")))
1356 (let lp ((i 0))
1357 (when (< i (vector-length obj))
1358 (let ((pos (+ pos word-size (* i word-size)))
1359 (elt (vector-ref obj i)))
1360 (write-constant-reference buf pos elt)
1361 (lp (1+ i)))))))
1362
1363 ((and (symbol? obj) (symbol-interned? obj))
1364 (write-immediate asm buf pos #f))
1365
1366 ((keyword? obj)
1367 (write-immediate asm buf pos #f))
1368
1369 ((number? obj)
1370 (write-immediate asm buf pos #f))
1371
1372 ((simple-uniform-vector? obj)
1373 (let ((tag (if (bitvector? obj)
1374 tc7-bitvector
1375 (let ((type-code (array-type-code obj)))
1376 (logior tc7-bytevector (ash type-code 7))))))
1377 (case word-size
1378 ((4)
1379 (bytevector-u32-set! buf pos tag endianness)
1380 (bytevector-u32-set! buf (+ pos 4)
1381 (if (bitvector? obj)
1382 (bitvector-length obj)
1383 (bytevector-length obj))
1384 endianness) ; length
1385 (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
1386 (write-immediate asm buf (+ pos 12) #f)) ; owner
1387 ((8)
1388 (bytevector-u64-set! buf pos tag endianness)
1389 (bytevector-u64-set! buf (+ pos 8)
1390 (if (bitvector? obj)
1391 (bitvector-length obj)
1392 (bytevector-length obj))
1393 endianness) ; length
1394 (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
1395 (write-immediate asm buf (+ pos 24) #f)) ; owner
1396 (else (error "bad word size")))))
1397
1398 ((uniform-vector-backing-store? obj)
1399 (let ((bv (uniform-vector-backing-store-bytes obj)))
1400 (bytevector-copy! bv 0 buf pos (bytevector-length bv))
1401 (unless (or (= 1 (uniform-vector-backing-store-element-size obj))
1402 (eq? endianness (native-endianness)))
1403 ;; Need to swap units of element-size bytes
1404 (error "FIXME: Implement byte order swap"))))
1405
1406 ((array? obj)
1407 (let-values
1408 ;; array tag + rank + contp flag: see libguile/arrays.h .
1409 (((tag) (logior tc7-array (ash (array-rank obj) 17) (ash 1 16)))
1410 ((bv-set! bvs-set!)
1411 (case word-size
1412 ((4) (values bytevector-u32-set! bytevector-s32-set!))
1413 ((8) (values bytevector-u64-set! bytevector-s64-set!))
1414 (else (error "bad word size")))))
1415 (bv-set! buf pos tag endianness)
1416 (write-immediate asm buf (+ pos word-size) #f) ; root vector (fixed later)
1417 (bv-set! buf (+ pos (* word-size 2)) 0 endianness) ; base
1418 (let lp ((pos (+ pos (* word-size 3)))
1419 (bounds (array-shape obj))
1420 (incs (shared-array-increments obj)))
1421 (when (pair? bounds)
1422 (bvs-set! buf pos (first (first bounds)) endianness)
1423 (bvs-set! buf (+ pos word-size) (second (first bounds)) endianness)
1424 (bvs-set! buf (+ pos (* word-size 2)) (first incs) endianness)
1425 (lp (+ pos (* 3 word-size)) (cdr bounds) (cdr incs))))))
1426
1427 (else
1428 (if (asm-to-file? asm)
1429 (error "unrecognized object" obj)
1430 (write-constant-reference buf pos obj)))))
1431
1432 (cond
1433 ((vlist-null? data) #f)
1434 (else
1435 (let* ((byte-len (vhash-fold (lambda (k v len)
1436 (+ (byte-length k) (align len 8)))
1437 0 data))
1438 (buf (make-bytevector byte-len 0)))
1439 (let lp ((i 0) (pos 0) (symbols '()))
1440 (if (< i (vlist-length data))
1441 (let* ((pair (vlist-ref data i))
1442 (obj (car pair))
1443 (obj-label (cdr pair)))
1444 (write buf pos obj)
1445 (lp (1+ i)
1446 (align (+ (byte-length obj) pos) 8)
1447 (cons (make-linker-symbol obj-label pos) symbols)))
1448 (make-object asm name buf '() symbols
1449 #:flags (match name
1450 ('.data (logior SHF_ALLOC SHF_WRITE))
1451 ('.rodata SHF_ALLOC))))))))))
1452
1453 (define (link-constants asm)
1454 "Link sections to hold constants needed by the program text emitted
1455 using @var{asm}.
1456
1457 Returns three values: an object for the .rodata section, an object for
1458 the .data section, and a label for an initialization procedure. Any of
1459 these may be @code{#f}."
1460 (define (shareable? x)
1461 (cond
1462 ((stringbuf? x) #t)
1463 ((pair? x)
1464 (and (immediate? (car x)) (immediate? (cdr x))))
1465 ((simple-vector? x)
1466 (let lp ((i 0))
1467 (or (= i (vector-length x))
1468 (and (immediate? (vector-ref x i))
1469 (lp (1+ i))))))
1470 ((uniform-vector-backing-store? x) #t)
1471 (else #f)))
1472 (let* ((constants (asm-constants asm))
1473 (len (vlist-length constants)))
1474 (let lp ((i 0)
1475 (ro vlist-null)
1476 (rw vlist-null))
1477 (if (= i len)
1478 (values (link-data asm ro '.rodata)
1479 (link-data asm rw '.data)
1480 (emit-init-constants asm))
1481 (let ((pair (vlist-ref constants i)))
1482 (if (shareable? (car pair))
1483 (lp (1+ i) (vhash-consq (car pair) (cdr pair) ro) rw)
1484 (lp (1+ i) ro (vhash-consq (car pair) (cdr pair) rw))))))))
1485
1486 \f
1487
1488 ;;;
1489 ;;; Linking program text.
1490 ;;;
1491
1492 (define (process-relocs buf relocs labels)
1493 "Patch up internal x8-s24 relocations, and any s32 relocations that
1494 reference symbols in the text section. Return a list of linker
1495 relocations for references to symbols defined outside the text section."
1496 (fold
1497 (lambda (reloc tail)
1498 (match reloc
1499 ((type label base word)
1500 (let ((abs (hashq-ref labels label))
1501 (dst (+ base word)))
1502 (case type
1503 ((s32)
1504 (if abs
1505 (let ((rel (- abs base)))
1506 (s32-set! buf dst rel)
1507 tail)
1508 (cons (make-linker-reloc 'rel32/4 (* dst 4) word label)
1509 tail)))
1510 ((x8-s24)
1511 (unless abs
1512 (error "unbound near relocation" reloc))
1513 (let ((rel (- abs base))
1514 (u32 (u32-ref buf dst)))
1515 (u32-set! buf dst (pack-u8-s24 (logand u32 #xff) rel))
1516 tail))
1517 (else (error "bad relocation kind" reloc)))))))
1518 '()
1519 relocs))
1520
1521 (define (process-labels labels)
1522 "Define linker symbols for the label-offset map in @var{labels}.
1523 The offsets are expected to be expressed in words."
1524 (hash-map->list (lambda (label loc)
1525 (make-linker-symbol label (* loc 4)))
1526 labels))
1527
1528 (define (swap-bytes! buf)
1529 "Patch up the text buffer @var{buf}, swapping the endianness of each
1530 32-bit unit."
1531 (unless (zero? (modulo (bytevector-length buf) 4))
1532 (error "unexpected length"))
1533 (let ((byte-len (bytevector-length buf)))
1534 (let lp ((pos 0))
1535 (unless (= pos byte-len)
1536 (bytevector-u32-set!
1537 buf pos
1538 (bytevector-u32-ref buf pos (endianness big))
1539 (endianness little))
1540 (lp (+ pos 4))))))
1541
1542 (define (link-text-object asm)
1543 "Link the .rtl-text section, swapping the endianness of the bytes if
1544 needed."
1545 (let ((buf (make-u32vector (asm-pos asm))))
1546 (let lp ((pos 0) (prev (reverse (asm-prev asm))))
1547 (if (null? prev)
1548 (let ((byte-size (* (asm-idx asm) 4)))
1549 (bytevector-copy! (asm-cur asm) 0 buf pos byte-size)
1550 (unless (eq? (asm-endianness asm) (native-endianness))
1551 (swap-bytes! buf))
1552 (make-object asm '.rtl-text
1553 buf
1554 (process-relocs buf (asm-relocs asm)
1555 (asm-labels asm))
1556 (process-labels (asm-labels asm))))
1557 (let ((len (* *block-size* 4)))
1558 (bytevector-copy! (car prev) 0 buf pos len)
1559 (lp (+ pos len) (cdr prev)))))))
1560
1561
1562 \f
1563
1564 ;;;
1565 ;;; Create the frame maps. These maps are used by GC to identify dead
1566 ;;; slots in pending call frames, to avoid marking them. We only do
1567 ;;; this when frame makes a non-tail call, as that is the common case.
1568 ;;; Only the topmost frame will see a GC at any other point, but we mark
1569 ;;; top frames conservatively as serializing live slot maps at every
1570 ;;; instruction would take up too much space in the object file.
1571 ;;;
1572
1573 ;; The .guile.frame-maps section starts with two packed u32 values: one
1574 ;; indicating the offset of the first byte of the .rtl-text section, and
1575 ;; another indicating the relative offset in bytes of the slots data.
1576 (define frame-maps-prefix-len 8)
1577
1578 ;; Each header is 8 bytes: 4 for the offset from .rtl_text, and 4 for
1579 ;; the offset of the slot map from the beginning of the
1580 ;; .guile.frame-maps section. The length of a frame map depends on the
1581 ;; frame size at the call site, and is not encoded into this section as
1582 ;; it is available at run-time.
1583 (define frame-map-header-len 8)
1584
1585 (define (link-frame-maps asm)
1586 (define (map-byte-length proc-slot)
1587 (ceiling-quotient (- proc-slot 2) 8))
1588 (define (make-frame-maps maps count map-len)
1589 (let* ((endianness (asm-endianness asm))
1590 (header-pos frame-maps-prefix-len)
1591 (map-pos (+ header-pos (* count frame-map-header-len)))
1592 (bv (make-bytevector (+ map-pos map-len) 0)))
1593 (bytevector-u32-set! bv 4 map-pos endianness)
1594 (let lp ((maps maps) (header-pos header-pos) (map-pos map-pos))
1595 (match maps
1596 (()
1597 (make-object asm '.guile.frame-maps bv
1598 (list (make-linker-reloc 'abs32/1 0 0 '.rtl-text))
1599 '() #:type SHT_PROGBITS #:flags SHF_ALLOC))
1600 (((pos proc-slot . map) . maps)
1601 (bytevector-u32-set! bv header-pos (* pos 4) endianness)
1602 (bytevector-u32-set! bv (+ header-pos 4) map-pos endianness)
1603 (let write-bytes ((map-pos map-pos)
1604 (map map)
1605 (byte-length (map-byte-length proc-slot)))
1606 (if (zero? byte-length)
1607 (lp maps (+ header-pos frame-map-header-len) map-pos)
1608 (begin
1609 (bytevector-u8-set! bv map-pos (logand map #xff))
1610 (write-bytes (1+ map-pos) (ash map -8)
1611 (1- byte-length))))))))))
1612 (match (asm-dead-slot-maps asm)
1613 (() #f)
1614 (in
1615 (let lp ((in in) (out '()) (count 0) (map-len 0))
1616 (match in
1617 (() (make-frame-maps out count map-len))
1618 (((and head (pos proc-slot . map)) . in)
1619 (lp in (cons head out)
1620 (1+ count)
1621 (+ (map-byte-length proc-slot) map-len))))))))
1622
1623 \f
1624
1625 ;;;
1626 ;;; Linking other sections of the ELF file, like the dynamic segment,
1627 ;;; the symbol table, etc.
1628 ;;;
1629
1630 ;; FIXME: Define these somewhere central, shared with C.
1631 (define *bytecode-major-version* #x0202)
1632 (define *bytecode-minor-version* 6)
1633
1634 (define (link-dynamic-section asm text rw rw-init frame-maps)
1635 "Link the dynamic section for an ELF image with bytecode @var{text},
1636 given the writable data section @var{rw} needing fixup from the
1637 procedure with label @var{rw-init}. @var{rw-init} may be false. If
1638 @var{rw} is true, it will be added to the GC roots at runtime."
1639 (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
1640 (let* ((endianness (asm-endianness asm))
1641 (words 6)
1642 (words (if rw (+ words 4) words))
1643 (words (if rw-init (+ words 2) words))
1644 (words (if frame-maps (+ words 2) words))
1645 (bv (make-bytevector (* word-size words) 0))
1646 (set-uword!
1647 (lambda (i uword)
1648 (%set-uword! bv (* i word-size) uword endianness)))
1649 (relocs '())
1650 (set-label!
1651 (lambda (i label)
1652 (set! relocs (cons (make-linker-reloc 'reloc-type
1653 (* i word-size) 0 label)
1654 relocs))
1655 (%set-uword! bv (* i word-size) 0 endianness))))
1656 (set-uword! 0 DT_GUILE_VM_VERSION)
1657 (set-uword! 1 (logior (ash *bytecode-major-version* 16)
1658 *bytecode-minor-version*))
1659 (set-uword! 2 DT_GUILE_ENTRY)
1660 (set-label! 3 '.rtl-text)
1661 (when rw
1662 ;; Add roots to GC.
1663 (set-uword! 4 DT_GUILE_GC_ROOT)
1664 (set-label! 5 '.data)
1665 (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
1666 (set-uword! 7 (bytevector-length (linker-object-bv rw)))
1667 (when rw-init
1668 (set-uword! 8 DT_INIT) ; constants
1669 (set-label! 9 rw-init)))
1670 (when frame-maps
1671 (set-uword! (- words 4) DT_GUILE_FRAME_MAPS)
1672 (set-label! (- words 3) '.guile.frame-maps))
1673 (set-uword! (- words 2) DT_NULL)
1674 (set-uword! (- words 1) 0)
1675 (make-object asm '.dynamic bv relocs '()
1676 #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
1677 (case (asm-word-size asm)
1678 ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
1679 ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
1680 (else (error "bad word size" asm))))
1681
1682 (define (link-shstrtab asm)
1683 "Link the string table for the section headers."
1684 (intern-section-name! asm ".shstrtab")
1685 (make-object asm '.shstrtab
1686 (link-string-table! (asm-shstrtab asm))
1687 '() '()
1688 #:type SHT_STRTAB #:flags 0))
1689
1690 (define (link-symtab text-section asm)
1691 (let* ((endianness (asm-endianness asm))
1692 (word-size (asm-word-size asm))
1693 (size (elf-symbol-len word-size))
1694 (meta (reverse (asm-meta asm)))
1695 (n (length meta))
1696 (strtab (make-string-table))
1697 (bv (make-bytevector (* n size) 0)))
1698 (define (intern-string! name)
1699 (string-table-intern! strtab (if name (symbol->string name) "")))
1700 (for-each
1701 (lambda (meta n)
1702 (let ((name (intern-string! (meta-name meta))))
1703 (write-elf-symbol bv (* n size) endianness word-size
1704 (make-elf-symbol
1705 #:name name
1706 ;; Symbol value and size are measured in
1707 ;; bytes, not u32s.
1708 #:value (* 4 (meta-low-pc meta))
1709 #:size (* 4 (- (meta-high-pc meta)
1710 (meta-low-pc meta)))
1711 #:type STT_FUNC
1712 #:visibility STV_HIDDEN
1713 #:shndx (elf-section-index text-section)))))
1714 meta (iota n))
1715 (let ((strtab (make-object asm '.strtab
1716 (link-string-table! strtab)
1717 '() '()
1718 #:type SHT_STRTAB #:flags 0)))
1719 (values (make-object asm '.symtab
1720 bv
1721 '() '()
1722 #:type SHT_SYMTAB #:flags 0 #:entsize size
1723 #:link (elf-section-index
1724 (linker-object-section strtab)))
1725 strtab))))
1726
1727 ;;; The .guile.arities section describes the arities that a function can
1728 ;;; have. It is in two parts: a sorted array of headers describing
1729 ;;; basic arities, and an array of links out to a string table (and in
1730 ;;; the case of keyword arguments, to the data section) for argument
1731 ;;; names. The whole thing is prefixed by a uint32 indicating the
1732 ;;; offset of the end of the headers array.
1733 ;;;
1734 ;;; The arity headers array is a packed array of structures of the form:
1735 ;;;
1736 ;;; struct arity_header {
1737 ;;; uint32_t low_pc;
1738 ;;; uint32_t high_pc;
1739 ;;; uint32_t offset;
1740 ;;; uint32_t flags;
1741 ;;; uint32_t nreq;
1742 ;;; uint32_t nopt;
1743 ;;; uint32_t nlocals;
1744 ;;; }
1745 ;;;
1746 ;;; All of the offsets and addresses are 32 bits. We can expand in the
1747 ;;; future to use 64-bit offsets if appropriate, but there are other
1748 ;;; aspects of bytecode that constrain us to a total image that fits in
1749 ;;; 32 bits, so for the moment we'll simplify the problem space.
1750 ;;;
1751 ;;; The following flags values are defined:
1752 ;;;
1753 ;;; #x1: has-rest?
1754 ;;; #x2: allow-other-keys?
1755 ;;; #x4: has-keyword-args?
1756 ;;; #x8: is-case-lambda?
1757 ;;; #x10: is-in-case-lambda?
1758 ;;;
1759 ;;; Functions with a single arity specify their number of required and
1760 ;;; optional arguments in nreq and nopt, and do not have the
1761 ;;; is-case-lambda? flag set. Their "offset" member links to an array
1762 ;;; of pointers into the associated .guile.arities.strtab string table,
1763 ;;; identifying the argument names. This offset is relative to the
1764 ;;; start of the .guile.arities section.
1765 ;;;
1766 ;;; If the arity has keyword arguments -- if has-keyword-args? is set in
1767 ;;; the flags -- the first uint32 pointed to by offset encodes a link to
1768 ;;; the "keyword indices" literal, in the data section. Then follow the
1769 ;;; names for all locals, in order, as uleb128 values. The required
1770 ;;; arguments will be the first locals, followed by the optionals,
1771 ;;; followed by the rest argument if if has-rest? is set. The names
1772 ;;; point into the associated string table section.
1773 ;;;
1774 ;;; Functions with no arities have no arities information present in the
1775 ;;; .guile.arities section.
1776 ;;;
1777 ;;; Functions with multiple arities are preceded by a header with
1778 ;;; is-case-lambda? set. All other fields are 0, except low-pc and
1779 ;;; high-pc which should be the bounds of the whole function. Headers
1780 ;;; for the individual arities follow, with the is-in-case-lambda? flag
1781 ;;; set. In this way the whole headers array is sorted in increasing
1782 ;;; low-pc order, and case-lambda clauses are contained within the
1783 ;;; [low-pc, high-pc] of the case-lambda header.
1784
1785 ;; Length of the prefix to the arities section, in bytes.
1786 (define arities-prefix-len 4)
1787
1788 ;; Length of an arity header, in bytes.
1789 (define arity-header-len (* 7 4))
1790
1791 ;; Some helpers.
1792 (define (put-uleb128 port val)
1793 (let lp ((val val))
1794 (let ((next (ash val -7)))
1795 (if (zero? next)
1796 (put-u8 port val)
1797 (begin
1798 (put-u8 port (logior #x80 (logand val #x7f)))
1799 (lp next))))))
1800
1801 (define (put-sleb128 port val)
1802 (let lp ((val val))
1803 (if (<= 0 (+ val 64) 127)
1804 (put-u8 port (logand val #x7f))
1805 (begin
1806 (put-u8 port (logior #x80 (logand val #x7f)))
1807 (lp (ash val -7))))))
1808
1809 (define (port-position port)
1810 (seek port 0 SEEK_CUR))
1811
1812 (define-inline (pack-arity-flags has-rest? allow-other-keys?
1813 has-keyword-args? is-case-lambda?
1814 is-in-case-lambda?)
1815 (logior (if has-rest? (ash 1 0) 0)
1816 (if allow-other-keys? (ash 1 1) 0)
1817 (if has-keyword-args? (ash 1 2) 0)
1818 (if is-case-lambda? (ash 1 3) 0)
1819 (if is-in-case-lambda? (ash 1 4) 0)))
1820
1821 (define (write-arities asm metas headers names-port strtab)
1822 (define (write-header pos low-pc high-pc offset flags nreq nopt nlocals)
1823 (unless (<= (+ nreq nopt) nlocals)
1824 (error "forgot to emit definition instructions?"))
1825 (bytevector-u32-set! headers pos (* low-pc 4) (asm-endianness asm))
1826 (bytevector-u32-set! headers (+ pos 4) (* high-pc 4) (asm-endianness asm))
1827 (bytevector-u32-set! headers (+ pos 8) offset (asm-endianness asm))
1828 (bytevector-u32-set! headers (+ pos 12) flags (asm-endianness asm))
1829 (bytevector-u32-set! headers (+ pos 16) nreq (asm-endianness asm))
1830 (bytevector-u32-set! headers (+ pos 20) nopt (asm-endianness asm))
1831 (bytevector-u32-set! headers (+ pos 24) nlocals (asm-endianness asm)))
1832 (define (write-kw-indices kw-indices relocs)
1833 ;; FIXME: Assert that kw-indices is already interned.
1834 (if (pair? kw-indices)
1835 (let ((pos (+ (bytevector-length headers)
1836 (port-position names-port)))
1837 (label (intern-constant asm kw-indices)))
1838 (put-bytevector names-port #vu8(0 0 0 0))
1839 (cons (make-linker-reloc 'abs32/1 pos 0 label) relocs))
1840 relocs))
1841 (define (write-arity pos arity in-case-lambda? relocs)
1842 (write-header pos (arity-low-pc arity)
1843 (arity-high-pc arity)
1844 ;; FIXME: Seems silly to add on bytevector-length of
1845 ;; headers, given the arities-prefix.
1846 (+ (bytevector-length headers) (port-position names-port))
1847 (pack-arity-flags (arity-rest arity)
1848 (arity-allow-other-keys? arity)
1849 (pair? (arity-kw-indices arity))
1850 #f
1851 in-case-lambda?)
1852 (length (arity-req arity))
1853 (length (arity-opt arity))
1854 (length (arity-definitions arity)))
1855 (let ((relocs (write-kw-indices (arity-kw-indices arity) relocs)))
1856 ;; Write local names.
1857 (let lp ((definitions (arity-definitions arity)))
1858 (match definitions
1859 (() relocs)
1860 ((#(name slot def) . definitions)
1861 (let ((sym (if (symbol? name)
1862 (string-table-intern! strtab (symbol->string name))
1863 0)))
1864 (put-uleb128 names-port sym)
1865 (lp definitions)))))
1866 ;; Now write their definitions.
1867 (let lp ((definitions (arity-definitions arity)))
1868 (match definitions
1869 (() relocs)
1870 ((#(name slot def) . definitions)
1871 (put-uleb128 names-port def)
1872 (put-uleb128 names-port slot)
1873 (lp definitions))))))
1874 (let lp ((metas metas) (pos arities-prefix-len) (relocs '()))
1875 (match metas
1876 (()
1877 (unless (= pos (bytevector-length headers))
1878 (error "expected to fully fill the bytevector"
1879 pos (bytevector-length headers)))
1880 relocs)
1881 ((meta . metas)
1882 (match (meta-arities meta)
1883 (() (lp metas pos relocs))
1884 ((arity)
1885 (lp metas
1886 (+ pos arity-header-len)
1887 (write-arity pos arity #f relocs)))
1888 (arities
1889 ;; Write a case-lambda header, then individual arities.
1890 ;; The case-lambda header's offset link is 0.
1891 (write-header pos (meta-low-pc meta) (meta-high-pc meta) 0
1892 (pack-arity-flags #f #f #f #t #f) 0 0 0)
1893 (let lp* ((arities arities) (pos (+ pos arity-header-len))
1894 (relocs relocs))
1895 (match arities
1896 (() (lp metas pos relocs))
1897 ((arity . arities)
1898 (lp* arities
1899 (+ pos arity-header-len)
1900 (write-arity pos arity #t relocs)))))))))))
1901
1902 (define (link-arities asm)
1903 (define (meta-arities-header-size meta)
1904 (define (lambda-size arity)
1905 arity-header-len)
1906 (define (case-lambda-size arities)
1907 (fold +
1908 arity-header-len ;; case-lambda header
1909 (map lambda-size arities))) ;; the cases
1910 (match (meta-arities meta)
1911 (() 0)
1912 ((arity) (lambda-size arity))
1913 (arities (case-lambda-size arities))))
1914
1915 (define (bytevector-append a b)
1916 (let ((out (make-bytevector (+ (bytevector-length a)
1917 (bytevector-length b)))))
1918 (bytevector-copy! a 0 out 0 (bytevector-length a))
1919 (bytevector-copy! b 0 out (bytevector-length a) (bytevector-length b))
1920 out))
1921
1922 (let* ((endianness (asm-endianness asm))
1923 (metas (reverse (asm-meta asm)))
1924 (header-size (fold (lambda (meta size)
1925 (+ size (meta-arities-header-size meta)))
1926 arities-prefix-len
1927 metas))
1928 (strtab (make-string-table))
1929 (headers (make-bytevector header-size 0)))
1930 (bytevector-u32-set! headers 0 (bytevector-length headers) endianness)
1931 (let-values (((names-port get-name-bv) (open-bytevector-output-port)))
1932 (let* ((relocs (write-arities asm metas headers names-port strtab))
1933 (strtab (make-object asm '.guile.arities.strtab
1934 (link-string-table! strtab)
1935 '() '()
1936 #:type SHT_STRTAB #:flags 0)))
1937 (values (make-object asm '.guile.arities
1938 (bytevector-append headers (get-name-bv))
1939 relocs '()
1940 #:type SHT_PROGBITS #:flags 0
1941 #:link (elf-section-index
1942 (linker-object-section strtab)))
1943 strtab)))))
1944
1945 ;;;
1946 ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
1947 ;;; values. Pc and str are both 32 bits wide. (Either could change to
1948 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1949 ;;; entry to a program, relative to the start of the text section, in
1950 ;;; bytes, and str is an index into the associated .guile.docstrs.strtab
1951 ;;; string table section.
1952 ;;;
1953
1954 ;; The size of a docstrs entry, in bytes.
1955 (define docstr-size 8)
1956
1957 (define (link-docstrs asm)
1958 (define (find-docstrings)
1959 (filter-map (lambda (meta)
1960 (define (is-documentation? pair)
1961 (eq? (car pair) 'documentation))
1962 (let* ((props (meta-properties meta))
1963 (tail (find-tail is-documentation? props)))
1964 (and tail
1965 (not (find-tail is-documentation? (cdr tail)))
1966 (string? (cdar tail))
1967 (cons (* 4 (meta-low-pc meta)) (cdar tail)))))
1968 (reverse (asm-meta asm))))
1969 (let* ((endianness (asm-endianness asm))
1970 (docstrings (find-docstrings))
1971 (strtab (make-string-table))
1972 (bv (make-bytevector (* (length docstrings) docstr-size) 0)))
1973 (fold (lambda (pair pos)
1974 (match pair
1975 ((pc . string)
1976 (bytevector-u32-set! bv pos pc endianness)
1977 (bytevector-u32-set! bv (+ pos 4)
1978 (string-table-intern! strtab string)
1979 endianness)
1980 (+ pos docstr-size))))
1981 0
1982 docstrings)
1983 (let ((strtab (make-object asm '.guile.docstrs.strtab
1984 (link-string-table! strtab)
1985 '() '()
1986 #:type SHT_STRTAB #:flags 0)))
1987 (values (make-object asm '.guile.docstrs
1988 bv
1989 '() '()
1990 #:type SHT_PROGBITS #:flags 0
1991 #:link (elf-section-index
1992 (linker-object-section strtab)))
1993 strtab))))
1994
1995 ;;;
1996 ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
1997 ;;; values. Pc and addr are both 32 bits wide. (Either could change to
1998 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1999 ;;; entry to a program, relative to the start of the text section, and
2000 ;;; addr is the address of the associated properties alist, relative to
2001 ;;; the start of the ELF image.
2002 ;;;
2003 ;;; Since procedure properties are stored in the data sections, we need
2004 ;;; to link the procedures property section first. (Note that this
2005 ;;; constraint does not apply to the arities section, which may
2006 ;;; reference the data sections via the kw-indices literal, because
2007 ;;; assembling the text section already makes sure that the kw-indices
2008 ;;; are interned.)
2009 ;;;
2010
2011 ;; The size of a procprops entry, in bytes.
2012 (define procprops-size 8)
2013
2014 (define (link-procprops asm)
2015 (define (assoc-remove-one alist key value-pred)
2016 (match alist
2017 (() '())
2018 ((((? (lambda (x) (eq? x key))) . value) . alist)
2019 (if (value-pred value)
2020 alist
2021 (acons key value alist)))
2022 (((k . v) . alist)
2023 (acons k v (assoc-remove-one alist key value-pred)))))
2024 (define (props-without-name-or-docstring meta)
2025 (assoc-remove-one
2026 (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
2027 'documentation
2028 string?))
2029 (define (find-procprops)
2030 (filter-map (lambda (meta)
2031 (let ((props (props-without-name-or-docstring meta)))
2032 (and (pair? props)
2033 (cons (* 4 (meta-low-pc meta)) props))))
2034 (reverse (asm-meta asm))))
2035 (let* ((endianness (asm-endianness asm))
2036 (procprops (find-procprops))
2037 (bv (make-bytevector (* (length procprops) procprops-size) 0)))
2038 (let lp ((procprops procprops) (pos 0) (relocs '()))
2039 (match procprops
2040 (()
2041 (make-object asm '.guile.procprops
2042 bv
2043 relocs '()
2044 #:type SHT_PROGBITS #:flags 0))
2045 (((pc . props) . procprops)
2046 (bytevector-u32-set! bv pos pc endianness)
2047 (lp procprops
2048 (+ pos procprops-size)
2049 (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
2050 (intern-constant asm props))
2051 relocs)))))))
2052
2053 ;;;
2054 ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
2055 ;;; sections provide line number and local variable liveness
2056 ;;; information. Their format is defined by the DWARF
2057 ;;; specifications.
2058 ;;;
2059
2060 (define (asm-language asm)
2061 ;; FIXME: Plumb language through to the assembler.
2062 'scheme)
2063
2064 ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
2065 (define (link-debug asm)
2066 (define (put-s8 port val)
2067 (let ((bv (make-bytevector 1)))
2068 (bytevector-s8-set! bv 0 val)
2069 (put-bytevector port bv)))
2070
2071 (define (put-u16 port val)
2072 (let ((bv (make-bytevector 2)))
2073 (bytevector-u16-set! bv 0 val (asm-endianness asm))
2074 (put-bytevector port bv)))
2075
2076 (define (put-u32 port val)
2077 (let ((bv (make-bytevector 4)))
2078 (bytevector-u32-set! bv 0 val (asm-endianness asm))
2079 (put-bytevector port bv)))
2080
2081 (define (put-u64 port val)
2082 (let ((bv (make-bytevector 8)))
2083 (bytevector-u64-set! bv 0 val (asm-endianness asm))
2084 (put-bytevector port bv)))
2085
2086 (define (meta->subprogram-die meta)
2087 `(subprogram
2088 (@ ,@(cond
2089 ((meta-name meta)
2090 => (lambda (name) `((name ,(symbol->string name)))))
2091 (else
2092 '()))
2093 (low-pc ,(meta-label meta))
2094 (high-pc ,(* 4 (- (meta-high-pc meta) (meta-low-pc meta)))))))
2095
2096 (define (make-compile-unit-die asm)
2097 `(compile-unit
2098 (@ (producer ,(string-append "Guile " (version)))
2099 (language ,(asm-language asm))
2100 (low-pc .rtl-text)
2101 (high-pc ,(* 4 (asm-pos asm)))
2102 (stmt-list 0))
2103 ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
2104
2105 (let-values (((die-port get-die-bv) (open-bytevector-output-port))
2106 ((die-relocs) '())
2107 ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
2108 ;; (tag has-kids? attrs forms) -> code
2109 ((abbrevs) vlist-null)
2110 ((strtab) (make-string-table))
2111 ((line-port get-line-bv) (open-bytevector-output-port))
2112 ((line-relocs) '())
2113 ;; file -> code
2114 ((files) vlist-null))
2115
2116 (define (write-abbrev code tag has-children? attrs forms)
2117 (put-uleb128 abbrev-port code)
2118 (put-uleb128 abbrev-port (tag-name->code tag))
2119 (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
2120 (for-each (lambda (attr form)
2121 (put-uleb128 abbrev-port (attribute-name->code attr))
2122 (put-uleb128 abbrev-port (form-name->code form)))
2123 attrs forms)
2124 (put-uleb128 abbrev-port 0)
2125 (put-uleb128 abbrev-port 0))
2126
2127 (define (intern-abbrev tag has-children? attrs forms)
2128 (let ((key (list tag has-children? attrs forms)))
2129 (match (vhash-assoc key abbrevs)
2130 ((_ . code) code)
2131 (#f (let ((code (1+ (vlist-length abbrevs))))
2132 (set! abbrevs (vhash-cons key code abbrevs))
2133 (write-abbrev code tag has-children? attrs forms)
2134 code)))))
2135
2136 (define (intern-file file)
2137 (match (vhash-assoc file files)
2138 ((_ . code) code)
2139 (#f (let ((code (1+ (vlist-length files))))
2140 (set! files (vhash-cons file code files))
2141 code))))
2142
2143 (define (write-sources)
2144 ;; Choose line base and line range values that will allow for an
2145 ;; address advance range of 16 words. The special opcode range is
2146 ;; from 10 to 255, so 246 values.
2147 (define base -4)
2148 (define range 15)
2149
2150 (let lp ((sources (asm-sources asm)) (out '()))
2151 (match sources
2152 (((pc . s) . sources)
2153 (let ((file (assq-ref s 'filename))
2154 (line (assq-ref s 'line))
2155 (col (assq-ref s 'column)))
2156 (lp sources
2157 ;; Guile line and column numbers are 0-indexed, but
2158 ;; they are 1-indexed for DWARF.
2159 (if (and line col)
2160 (cons (list pc
2161 (if (string? file) (intern-file file) 0)
2162 (1+ line)
2163 (1+ col))
2164 out)
2165 out))))
2166 (()
2167 ;; Compilation unit header for .debug_line. We write in
2168 ;; DWARF 2 format because more tools understand it than DWARF
2169 ;; 4, which incompatibly adds another field to this header.
2170
2171 (put-u32 line-port 0) ; Length; will patch later.
2172 (put-u16 line-port 2) ; DWARF 2 format.
2173 (put-u32 line-port 0) ; Prologue length; will patch later.
2174 (put-u8 line-port 4) ; Minimum instruction length: 4 bytes.
2175 (put-u8 line-port 1) ; Default is-stmt: true.
2176
2177 (put-s8 line-port base) ; Line base. See the DWARF standard.
2178 (put-u8 line-port range) ; Line range. See the DWARF standard.
2179 (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
2180
2181 ;; A table of the number of uleb128 arguments taken by each
2182 ;; of the standard opcodes.
2183 (put-u8 line-port 0) ; 1: copy
2184 (put-u8 line-port 1) ; 2: advance-pc
2185 (put-u8 line-port 1) ; 3: advance-line
2186 (put-u8 line-port 1) ; 4: set-file
2187 (put-u8 line-port 1) ; 5: set-column
2188 (put-u8 line-port 0) ; 6: negate-stmt
2189 (put-u8 line-port 0) ; 7: set-basic-block
2190 (put-u8 line-port 0) ; 8: const-add-pc
2191 (put-u8 line-port 1) ; 9: fixed-advance-pc
2192
2193 ;; Include directories, as a zero-terminated sequence of
2194 ;; nul-terminated strings. Nothing, for the moment.
2195 (put-u8 line-port 0)
2196
2197 ;; File table. For each file that contributes to this
2198 ;; compilation unit, a nul-terminated file name string, and a
2199 ;; uleb128 for each of directory the file was found in, the
2200 ;; modification time, and the file's size in bytes. We pass
2201 ;; zero for the latter three fields.
2202 (vlist-fold-right
2203 (lambda (pair seed)
2204 (match pair
2205 ((file . code)
2206 (put-bytevector line-port (string->utf8 file))
2207 (put-u8 line-port 0)
2208 (put-uleb128 line-port 0) ; directory
2209 (put-uleb128 line-port 0) ; mtime
2210 (put-uleb128 line-port 0))) ; size
2211 seed)
2212 #f
2213 files)
2214 (put-u8 line-port 0) ; 0 byte terminating file list.
2215
2216 ;; Patch prologue length.
2217 (let ((offset (port-position line-port)))
2218 (seek line-port 6 SEEK_SET)
2219 (put-u32 line-port (- offset 10))
2220 (seek line-port offset SEEK_SET))
2221
2222 ;; Now write the statement program.
2223 (let ()
2224 (define (extended-op opcode payload-len)
2225 (put-u8 line-port 0) ; extended op
2226 (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
2227 (put-uleb128 line-port opcode))
2228 (define (set-address sym)
2229 (define (add-reloc! kind)
2230 (set! line-relocs
2231 (cons (make-linker-reloc kind
2232 (port-position line-port)
2233 0
2234 sym)
2235 line-relocs)))
2236 (match (asm-word-size asm)
2237 (4
2238 (extended-op 2 4)
2239 (add-reloc! 'abs32/1)
2240 (put-u32 line-port 0))
2241 (8
2242 (extended-op 2 8)
2243 (add-reloc! 'abs64/1)
2244 (put-u64 line-port 0))))
2245 (define (end-sequence pc)
2246 (let ((pc-inc (- (asm-pos asm) pc)))
2247 (put-u8 line-port 2) ; advance-pc
2248 (put-uleb128 line-port pc-inc))
2249 (extended-op 1 0))
2250 (define (advance-pc pc-inc line-inc)
2251 (let ((spec (+ (- line-inc base) (* pc-inc range) 10)))
2252 (cond
2253 ((or (< line-inc base) (>= line-inc (+ base range)))
2254 (advance-line line-inc)
2255 (advance-pc pc-inc 0))
2256 ((<= spec 255)
2257 (put-u8 line-port spec))
2258 ((< spec 500)
2259 (put-u8 line-port 8) ; const-advance-pc
2260 (advance-pc (- pc-inc (floor/ (- 255 10) range))
2261 line-inc))
2262 (else
2263 (put-u8 line-port 2) ; advance-pc
2264 (put-uleb128 line-port pc-inc)
2265 (advance-pc 0 line-inc)))))
2266 (define (advance-line inc)
2267 (put-u8 line-port 3)
2268 (put-sleb128 line-port inc))
2269 (define (set-file file)
2270 (put-u8 line-port 4)
2271 (put-uleb128 line-port file))
2272 (define (set-column col)
2273 (put-u8 line-port 5)
2274 (put-uleb128 line-port col))
2275
2276 (set-address '.rtl-text)
2277
2278 (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
2279 (match in
2280 (()
2281 (when (null? out)
2282 ;; There was no source info in the first place. Set
2283 ;; file register to 0 before adding final row.
2284 (set-file 0))
2285 (end-sequence pc))
2286 (((pc* file* line* col*) . in*)
2287 (cond
2288 ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
2289 (lp in* pc file line col))
2290 (else
2291 (unless (eqv? col col*)
2292 (set-column col*))
2293 (unless (eqv? file file*)
2294 (set-file file*))
2295 (advance-pc (- pc* pc) (- line* line))
2296 (lp in* pc* file* line* col*)))))))))))
2297
2298 (define (compute-code attr val)
2299 (match attr
2300 ('name (string-table-intern! strtab val))
2301 ('low-pc val)
2302 ('high-pc val)
2303 ('producer (string-table-intern! strtab val))
2304 ('language (language-name->code val))
2305 ('stmt-list val)))
2306
2307 (define (choose-form attr val code)
2308 (cond
2309 ((string? val) 'strp)
2310 ((eq? attr 'stmt-list) 'sec-offset)
2311 ((eq? attr 'low-pc) 'addr)
2312 ((exact-integer? code)
2313 (cond
2314 ((< code 0) 'sleb128)
2315 ((<= code #xff) 'data1)
2316 ((<= code #xffff) 'data2)
2317 ((<= code #xffffffff) 'data4)
2318 ((<= code #xffffffffffffffff) 'data8)
2319 (else 'uleb128)))
2320 (else (error "unhandled case" attr val code))))
2321
2322 (define (add-die-relocation! kind sym)
2323 (set! die-relocs
2324 (cons (make-linker-reloc kind (port-position die-port) 0 sym)
2325 die-relocs)))
2326
2327 (define (write-value code form)
2328 (match form
2329 ('data1 (put-u8 die-port code))
2330 ('data2 (put-u16 die-port code))
2331 ('data4 (put-u32 die-port code))
2332 ('data8 (put-u64 die-port code))
2333 ('uleb128 (put-uleb128 die-port code))
2334 ('sleb128 (put-sleb128 die-port code))
2335 ('addr
2336 (match (asm-word-size asm)
2337 (4
2338 (add-die-relocation! 'abs32/1 code)
2339 (put-u32 die-port 0))
2340 (8
2341 (add-die-relocation! 'abs64/1 code)
2342 (put-u64 die-port 0))))
2343 ('sec-offset (put-u32 die-port code))
2344 ('strp (put-u32 die-port code))))
2345
2346 (define (write-die die)
2347 (match die
2348 ((tag ('@ (attrs vals) ...) children ...)
2349 (let* ((codes (map compute-code attrs vals))
2350 (forms (map choose-form attrs vals codes))
2351 (has-children? (not (null? children)))
2352 (abbrev-code (intern-abbrev tag has-children? attrs forms)))
2353 (put-uleb128 die-port abbrev-code)
2354 (for-each write-value codes forms)
2355 (when has-children?
2356 (for-each write-die children)
2357 (put-uleb128 die-port 0))))))
2358
2359 ;; Compilation unit header.
2360 (put-u32 die-port 0) ; Length; will patch later.
2361 (put-u16 die-port 4) ; DWARF 4.
2362 (put-u32 die-port 0) ; Abbrevs offset.
2363 (put-u8 die-port (asm-word-size asm)) ; Address size.
2364
2365 (write-die (make-compile-unit-die asm))
2366
2367 ;; Terminate the abbrevs list.
2368 (put-uleb128 abbrev-port 0)
2369
2370 (write-sources)
2371
2372 (values (let ((bv (get-die-bv)))
2373 ;; Patch DWARF32 length.
2374 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2375 (asm-endianness asm))
2376 (make-object asm '.debug_info bv die-relocs '()
2377 #:type SHT_PROGBITS #:flags 0))
2378 (make-object asm '.debug_abbrev (get-abbrev-bv) '() '()
2379 #:type SHT_PROGBITS #:flags 0)
2380 (make-object asm '.debug_str (link-string-table! strtab) '() '()
2381 #:type SHT_PROGBITS #:flags 0)
2382 (make-object asm '.debug_loc #vu8() '() '()
2383 #:type SHT_PROGBITS #:flags 0)
2384 (let ((bv (get-line-bv)))
2385 ;; Patch DWARF32 length.
2386 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
2387 (asm-endianness asm))
2388 (make-object asm '.debug_line bv line-relocs '()
2389 #:type SHT_PROGBITS #:flags 0)))))
2390
2391 (define (link-objects asm)
2392 (let*-values (;; Link procprops before constants, because it probably
2393 ;; interns more constants.
2394 ((procprops) (link-procprops asm))
2395 ((ro rw rw-init) (link-constants asm))
2396 ;; Link text object after constants, so that the
2397 ;; constants initializer gets included.
2398 ((text) (link-text-object asm))
2399 ((frame-maps) (link-frame-maps asm))
2400 ((dt) (link-dynamic-section asm text rw rw-init frame-maps))
2401 ((symtab strtab) (link-symtab (linker-object-section text) asm))
2402 ((arities arities-strtab) (link-arities asm))
2403 ((docstrs docstrs-strtab) (link-docstrs asm))
2404 ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
2405 ;; This needs to be linked last, because linking other
2406 ;; sections adds entries to the string table.
2407 ((shstrtab) (link-shstrtab asm)))
2408 (filter identity
2409 (list text ro frame-maps rw dt symtab strtab
2410 arities arities-strtab
2411 docstrs docstrs-strtab procprops
2412 dinfo dabbrev dstrtab dloc dline
2413 shstrtab))))
2414
2415
2416 \f
2417
2418 ;;;
2419 ;;; High-level public interfaces.
2420 ;;;
2421
2422 (define* (link-assembly asm #:key (page-aligned? #t))
2423 "Produce an ELF image from the code and data emitted into @var{asm}.
2424 The result is a bytevector, by default linked so that read-only and
2425 writable data are on separate pages. Pass @code{#:page-aligned? #f} to
2426 disable this behavior."
2427 (define (asm-constant-vector asm)
2428 (list->vector (reverse (map car (vlist->list (asm-constants asm))))))
2429 (let ((bv (link-elf (link-objects asm) #:page-aligned? page-aligned?)))
2430 (cons bv (if (asm-to-file? asm) #f (asm-constant-vector asm)))))