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