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