static-patch! for pair and vector fields
[bpt/guile.git] / module / system / vm / assembler.scm
1 ;;; Guile RTL assembler
2
3 ;;; Copyright (C) 2001, 2009, 2010, 2012, 2013 Free Software Foundation, Inc.
4 ;;;
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
9 ;;;
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
14 ;;;
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 ;;; Commentary:
20 ;;;
21 ;;; This module implements an assembler that creates an ELF image from
22 ;;; RTL assembly and macro-assembly. The input can be given in
23 ;;; s-expression form, like ((OP ARG ...) ...). Internally there is a
24 ;;; procedural interface, the emit-OP procedures, but that is not
25 ;;; currently exported.
26 ;;;
27 ;;; "Primitive instructions" correspond to RTL VM operations.
28 ;;; Assemblers for primitive instructions are generated programmatically
29 ;;; from (rtl-instruction-list), which itself is derived from the VM
30 ;;; sources. There are also "macro-instructions" like "label" or
31 ;;; "load-constant" that expand to 0 or more primitive instructions.
32 ;;;
33 ;;; The assembler also handles some higher-level tasks, like creating
34 ;;; the symbol table, other metadata sections, creating a constant table
35 ;;; for the whole compilation unit, and writing the dynamic section of
36 ;;; the ELF file along with the appropriate initialization routines.
37 ;;;
38 ;;; Most compilers will want to use the trio of make-assembler,
39 ;;; emit-text, and link-assembly. That will result in the creation of
40 ;;; an ELF image as a bytevector, which can then be loaded using
41 ;;; load-thunk-from-memory, or written to disk as a .go file.
42 ;;;
43 ;;; Code:
44
45 (define-module (system vm assembler)
46 #:use-module (system base target)
47 #:use-module (system vm instruction)
48 #:use-module (system vm dwarf)
49 #:use-module (system vm elf)
50 #:use-module (system vm linker)
51 #:use-module (system vm objcode)
52 #:use-module (rnrs bytevectors)
53 #:use-module (ice-9 binary-ports)
54 #:use-module (ice-9 vlist)
55 #:use-module (ice-9 match)
56 #:use-module (srfi srfi-1)
57 #:use-module (srfi srfi-4)
58 #:use-module (srfi srfi-9)
59 #:use-module (srfi srfi-11)
60 #:export (make-assembler
61 emit-text
62 link-assembly
63 assemble-program))
64
65
66 \f
67
68 ;;; RTL code consists of 32-bit units, often subdivided in some way.
69 ;;; These helpers create one 32-bit unit from multiple components.
70
71 (define-syntax-rule (pack-u8-u24 x y)
72 (logior x (ash y 8)))
73
74 (define-syntax-rule (pack-u8-s24 x y)
75 (logior x (ash (cond
76 ((< 0 (- y) #x800000)
77 (+ y #x1000000))
78 ((<= 0 y #xffffff)
79 y)
80 (else (error "out of range" y)))
81 8)))
82
83 (define-syntax-rule (pack-u1-u7-u24 x y z)
84 (logior x (ash y 1) (ash z 8)))
85
86 (define-syntax-rule (pack-u8-u12-u12 x y z)
87 (logior x (ash y 8) (ash z 20)))
88
89 (define-syntax-rule (pack-u8-u8-u16 x y z)
90 (logior x (ash y 8) (ash z 16)))
91
92 (define-syntax-rule (pack-u8-u8-u8-u8 x y z w)
93 (logior x (ash y 8) (ash z 16) (ash w 24)))
94
95 (define-syntax pack-flags
96 (syntax-rules ()
97 ;; Add clauses as needed.
98 ((pack-flags f1 f2) (logior (if f1 (ash 1 0) 0)
99 (if f2 (ash 2 0) 0)))))
100
101 ;;; Helpers to read and write 32-bit units in a buffer.
102
103 (define-syntax-rule (u32-ref buf n)
104 (bytevector-u32-native-ref buf (* n 4)))
105
106 (define-syntax-rule (u32-set! buf n val)
107 (bytevector-u32-native-set! buf (* n 4) val))
108
109 (define-syntax-rule (s32-ref buf n)
110 (bytevector-s32-native-ref buf (* n 4)))
111
112 (define-syntax-rule (s32-set! buf n val)
113 (bytevector-s32-native-set! buf (* n 4) val))
114
115
116 \f
117
118 ;;; A <meta> entry collects metadata for one procedure. Procedures are
119 ;;; written as contiguous ranges of RTL code.
120 ;;;
121 (define-syntax-rule (assert-match arg pattern kind)
122 (let ((x arg))
123 (unless (match x (pattern #t) (_ #f))
124 (error (string-append "expected " kind) x))))
125
126 (define-record-type <meta>
127 (%make-meta label properties low-pc high-pc arities)
128 meta?
129 (label meta-label)
130 (properties meta-properties set-meta-properties!)
131 (low-pc meta-low-pc)
132 (high-pc meta-high-pc set-meta-high-pc!)
133 (arities meta-arities set-meta-arities!))
134
135 (define (make-meta label properties low-pc)
136 (assert-match label (? symbol?) "symbol")
137 (assert-match properties (((? symbol?) . _) ...) "alist with symbolic keys")
138 (%make-meta label properties low-pc #f '()))
139
140 (define (meta-name meta)
141 (assq-ref (meta-properties meta) 'name))
142
143 ;; Metadata for one <lambda-case>.
144 (define-record-type <arity>
145 (make-arity req opt rest kw-indices allow-other-keys?
146 low-pc high-pc)
147 arity?
148 (req arity-req)
149 (opt arity-opt)
150 (rest arity-rest)
151 (kw-indices arity-kw-indices)
152 (allow-other-keys? arity-allow-other-keys?)
153 (low-pc arity-low-pc)
154 (high-pc arity-high-pc set-arity-high-pc!))
155
156 (define-syntax *block-size* (identifier-syntax 32))
157
158 ;;; An assembler collects all of the words emitted during assembly, and
159 ;;; also maintains ancillary information such as the constant table, a
160 ;;; relocation list, and so on.
161 ;;;
162 ;;; RTL code consists of 32-bit units. We emit RTL code using native
163 ;;; endianness. If we're targeting a foreign endianness, we byte-swap
164 ;;; the bytevector as a whole instead of conditionalizing each access.
165 ;;;
166 (define-record-type <asm>
167 (make-asm cur idx start prev written
168 labels relocs
169 word-size endianness
170 constants inits
171 shstrtab next-section-number
172 meta sources)
173 asm?
174
175 ;; We write RTL code into what is logically a growable vector,
176 ;; implemented as a list of blocks. asm-cur is the current block, and
177 ;; asm-idx is the current index into that block, in 32-bit units.
178 ;;
179 (cur asm-cur set-asm-cur!)
180 (idx asm-idx set-asm-idx!)
181
182 ;; asm-start is an absolute position, indicating the offset of the
183 ;; beginning of an instruction (in u32 units). It is updated after
184 ;; writing all the words for one primitive instruction. It models the
185 ;; position of the instruction pointer during execution, given that
186 ;; the RTL VM updates the IP only at the end of executing the
187 ;; instruction, and is thus useful for computing offsets between two
188 ;; points in a program.
189 ;;
190 (start asm-start set-asm-start!)
191
192 ;; The list of previously written blocks.
193 ;;
194 (prev asm-prev set-asm-prev!)
195
196 ;; The number of u32 words written in asm-prev, which is the same as
197 ;; the offset of the current block.
198 ;;
199 (written asm-written set-asm-written!)
200
201 ;; An alist of symbol -> position pairs, indicating the labels defined
202 ;; in this compilation unit.
203 ;;
204 (labels asm-labels set-asm-labels!)
205
206 ;; A list of relocations needed by the program text. We use an
207 ;; internal representation for relocations, and handle textualn
208 ;; relative relocations in the assembler. Other kinds of relocations
209 ;; are later reified as linker relocations and resolved by the linker.
210 ;;
211 (relocs asm-relocs set-asm-relocs!)
212
213 ;; Target information.
214 ;;
215 (word-size asm-word-size)
216 (endianness asm-endianness)
217
218 ;; The constant table, as a vhash of object -> label. All constants
219 ;; get de-duplicated and written into separate sections -- either the
220 ;; .rodata section, for read-only data, or .data, for constants that
221 ;; need initialization at load-time (like symbols). Constants can
222 ;; depend on other constants (e.g. a symbol depending on a stringbuf),
223 ;; so order in this table is important.
224 ;;
225 (constants asm-constants set-asm-constants!)
226
227 ;; A list of RTL instructions needed to initialize the constants.
228 ;; Will run in a thunk with 2 local variables.
229 ;;
230 (inits asm-inits set-asm-inits!)
231
232 ;; The shstrtab, for section names.
233 ;;
234 (shstrtab asm-shstrtab set-asm-shstrtab!)
235
236 ;; The section number for the next section to be written.
237 ;;
238 (next-section-number asm-next-section-number set-asm-next-section-number!)
239
240 ;; A list of <meta>, corresponding to procedure metadata.
241 ;;
242 (meta asm-meta set-asm-meta!)
243
244 ;; A list of (pos . source) pairs, indicating source information. POS
245 ;; is relative to the beginning of the text section, and SOURCE is in
246 ;; the same format that source-properties returns.
247 ;;
248 (sources asm-sources set-asm-sources!))
249
250 (define-inlinable (fresh-block)
251 (make-u32vector *block-size*))
252
253 (define* (make-assembler #:key (word-size (target-word-size))
254 (endianness (target-endianness)))
255 "Create an assembler for a given target @var{word-size} and
256 @var{endianness}, falling back to appropriate values for the configured
257 target."
258 (make-asm (fresh-block) 0 0 '() 0
259 '() '()
260 word-size endianness
261 vlist-null '()
262 (make-string-table) 1
263 '() '()))
264
265 (define (intern-section-name! asm string)
266 "Add a string to the section name table (shstrtab)."
267 (string-table-intern! (asm-shstrtab asm) string))
268
269 (define-inlinable (asm-pos asm)
270 "The offset of the next word to be written into the code buffer, in
271 32-bit units."
272 (+ (asm-idx asm) (asm-written asm)))
273
274 (define (allocate-new-block asm)
275 "Close off the current block, and arrange for the next word to be
276 written to a fresh block."
277 (let ((new (fresh-block)))
278 (set-asm-prev! asm (cons (asm-cur asm) (asm-prev asm)))
279 (set-asm-written! asm (asm-pos asm))
280 (set-asm-cur! asm new)
281 (set-asm-idx! asm 0)))
282
283 (define-inlinable (emit asm u32)
284 "Emit one 32-bit word into the instruction stream. Assumes that there
285 is space for the word, and ensures that there is space for the next
286 word."
287 (u32-set! (asm-cur asm) (asm-idx asm) u32)
288 (set-asm-idx! asm (1+ (asm-idx asm)))
289 (if (= (asm-idx asm) *block-size*)
290 (allocate-new-block asm)))
291
292 (define-inlinable (make-reloc type label base word)
293 "Make an internal relocation of type @var{type} referencing symbol
294 @var{label}, @var{word} words after position @var{start}. @var{type}
295 may be x8-s24, indicating a 24-bit relative label reference that can be
296 fixed up by the assembler, or s32, indicating a 32-bit relative
297 reference that needs to be fixed up by the linker."
298 (list type label base word))
299
300 (define-inlinable (reset-asm-start! asm)
301 "Reset the asm-start after writing the words for one instruction."
302 (set-asm-start! asm (asm-pos asm)))
303
304 (define (emit-exported-label asm label)
305 "Define a linker symbol associating @var{label} with the current
306 asm-start."
307 (set-asm-labels! asm (acons label (asm-start asm) (asm-labels asm))))
308
309 (define (record-label-reference asm label)
310 "Record an x8-s24 local label reference. This value will get patched
311 up later by the assembler."
312 (let* ((start (asm-start asm))
313 (pos (asm-pos asm))
314 (reloc (make-reloc 'x8-s24 label start (- pos start))))
315 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
316
317 (define* (record-far-label-reference asm label #:optional (offset 0))
318 "Record an s32 far label reference. This value will get patched up
319 later by the linker."
320 (let* ((start (- (asm-start asm) offset))
321 (pos (asm-pos asm))
322 (reloc (make-reloc 's32 label start (- pos start))))
323 (set-asm-relocs! asm (cons reloc (asm-relocs asm)))))
324
325
326 \f
327
328 ;;;
329 ;;; Primitive assemblers are defined by expanding `assembler' for each
330 ;;; opcode in `(rtl-instruction-list)'.
331 ;;;
332
333 (eval-when (expand compile load eval)
334 (define (id-append ctx a b)
335 (datum->syntax ctx (symbol-append (syntax->datum a) (syntax->datum b)))))
336
337 (define-syntax assembler
338 (lambda (x)
339 (define-syntax op-case
340 (lambda (x)
341 (syntax-case x ()
342 ((_ asm name ((type arg ...) code ...) clause ...)
343 #`(if (eq? name 'type)
344 (with-syntax (((arg ...) (generate-temporaries #'(arg ...))))
345 #'((arg ...)
346 code ...))
347 (op-case asm name clause ...)))
348 ((_ asm name)
349 #'(error "unmatched name" name)))))
350
351 (define (pack-first-word asm opcode type)
352 (with-syntax ((opcode opcode))
353 (op-case
354 asm type
355 ((U8_X24)
356 (emit asm opcode))
357 ((U8_U24 arg)
358 (emit asm (pack-u8-u24 opcode arg)))
359 ((U8_L24 label)
360 (record-label-reference asm label)
361 (emit asm opcode))
362 ((U8_U8_I16 a imm)
363 (emit asm (pack-u8-u8-u16 opcode a (object-address imm))))
364 ((U8_U12_U12 a b)
365 (emit asm (pack-u8-u12-u12 opcode a b)))
366 ((U8_U8_U8_U8 a b c)
367 (emit asm (pack-u8-u8-u8-u8 opcode a b c))))))
368
369 (define (pack-tail-word asm type)
370 (op-case
371 asm type
372 ((U8_U24 a b)
373 (emit asm (pack-u8-u24 a b)))
374 ((U8_L24 a label)
375 (record-label-reference asm label)
376 (emit asm a))
377 ((U8_U8_I16 a b imm)
378 (emit asm (pack-u8-u8-u16 a b (object-address imm))))
379 ((U8_U12_U12 a b)
380 (emit asm (pack-u8-u12-u12 a b c)))
381 ((U8_U8_U8_U8 a b c d)
382 (emit asm (pack-u8-u8-u8-u8 a b c d)))
383 ((U32 a)
384 (emit asm a))
385 ((I32 imm)
386 (let ((val (object-address imm)))
387 (unless (zero? (ash val -32))
388 (error "FIXME: enable truncation of negative fixnums when cross-compiling"))
389 (emit asm val)))
390 ((A32 imm)
391 (unless (= (asm-word-size asm) 8)
392 (error "make-long-immediate unavailable for this target"))
393 (emit asm (ash (object-address imm) -32))
394 (emit asm (logand (object-address imm) (1- (ash 1 32)))))
395 ((B32))
396 ((N32 label)
397 (record-far-label-reference asm label)
398 (emit asm 0))
399 ((S32 label)
400 (record-far-label-reference asm label)
401 (emit asm 0))
402 ((L32 label)
403 (record-far-label-reference asm label)
404 (emit asm 0))
405 ((LO32 label offset)
406 (record-far-label-reference asm label
407 (* offset (/ (asm-word-size asm) 4)))
408 (emit asm 0))
409 ((X8_U24 a)
410 (emit asm (pack-u8-u24 0 a)))
411 ((X8_U12_U12 a b)
412 (emit asm (pack-u8-u12-u12 0 a b)))
413 ((X8_L24 label)
414 (record-label-reference asm label)
415 (emit asm 0))
416 ((B1_X7_L24 a label)
417 (record-label-reference asm label)
418 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
419 ((B1_U7_L24 a b label)
420 (record-label-reference asm label)
421 (emit asm (pack-u1-u7-u24 (if a 1 0) b 0)))
422 ((B1_X31 a)
423 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 0)))
424 ((B1_X7_U24 a b)
425 (emit asm (pack-u1-u7-u24 (if a 1 0) 0 b)))))
426
427 (syntax-case x ()
428 ((_ name opcode word0 word* ...)
429 (with-syntax ((((formal0 ...)
430 code0 ...)
431 (pack-first-word #'asm
432 (syntax->datum #'opcode)
433 (syntax->datum #'word0)))
434 ((((formal* ...)
435 code* ...) ...)
436 (map (lambda (word) (pack-tail-word #'asm word))
437 (syntax->datum #'(word* ...)))))
438 #'(lambda (asm formal0 ... formal* ... ...)
439 (unless (asm? asm) (error "not an asm"))
440 code0 ...
441 code* ... ...
442 (reset-asm-start! asm)))))))
443
444 (define assemblers (make-hash-table))
445
446 (define-syntax define-assembler
447 (lambda (x)
448 (syntax-case x ()
449 ((_ name opcode kind arg ...)
450 (with-syntax ((emit (id-append #'name #'emit- #'name)))
451 #'(begin
452 (define emit
453 (let ((emit (assembler name opcode arg ...)))
454 (hashq-set! assemblers 'name emit)
455 emit))
456 (export emit)))))))
457
458 (define-syntax visit-opcodes
459 (lambda (x)
460 (syntax-case x ()
461 ((visit-opcodes macro arg ...)
462 (with-syntax (((inst ...)
463 (map (lambda (x) (datum->syntax #'macro x))
464 (rtl-instruction-list))))
465 #'(begin
466 (macro arg ... . inst)
467 ...))))))
468
469 (visit-opcodes define-assembler)
470
471 (define (emit-text asm instructions)
472 "Assemble @var{instructions} using the assembler @var{asm}.
473 @var{instructions} is a sequence of RTL instructions, expressed as a
474 list of lists. This procedure can be called many times before calling
475 @code{link-assembly}."
476 (for-each (lambda (inst)
477 (apply (or (hashq-ref assemblers (car inst))
478 (error 'bad-instruction inst))
479 asm
480 (cdr inst)))
481 instructions))
482
483 \f
484
485 ;;;
486 ;;; The constant table records a topologically sorted set of literal
487 ;;; constants used by a program. For example, a pair uses its car and
488 ;;; cdr, a string uses its stringbuf, etc.
489 ;;;
490 ;;; Some things we want to add to the constant table are not actually
491 ;;; Scheme objects: for example, stringbufs, cache cells for toplevel
492 ;;; references, or cache cells for non-closure procedures. For these we
493 ;;; define special record types and add instances of those record types
494 ;;; to the table.
495 ;;;
496
497 (define-inlinable (immediate? x)
498 "Return @code{#t} if @var{x} is immediate, and @code{#f} otherwise."
499 (not (zero? (logand (object-address x) 6))))
500
501 (define-record-type <stringbuf>
502 (make-stringbuf string)
503 stringbuf?
504 (string stringbuf-string))
505
506 (define-record-type <static-procedure>
507 (make-static-procedure code)
508 static-procedure?
509 (code static-procedure-code))
510
511 (define-record-type <uniform-vector-backing-store>
512 (make-uniform-vector-backing-store bytes)
513 uniform-vector-backing-store?
514 (bytes uniform-vector-backing-store-bytes))
515
516 (define-record-type <cache-cell>
517 (make-cache-cell scope key)
518 cache-cell?
519 (scope cache-cell-scope)
520 (key cache-cell-key))
521
522 (define (simple-vector? obj)
523 (and (vector? obj)
524 (equal? (array-shape obj) (list (list 0 (1- (vector-length obj)))))))
525
526 (define (simple-uniform-vector? obj)
527 (and (array? obj)
528 (symbol? (array-type obj))
529 (equal? (array-shape obj) (list (list 0 (1- (array-length obj)))))))
530
531 (define (statically-allocatable? x)
532 "Return @code{#t} if a non-immediate constant can be allocated
533 statically, and @code{#f} if it would need some kind of runtime
534 allocation."
535 (or (pair? x) (string? x) (stringbuf? x) (static-procedure? x) (array? x)))
536
537 (define (intern-constant asm obj)
538 "Add an object to the constant table, and return a label that can be
539 used to reference it. If the object is already present in the constant
540 table, its existing label is used directly."
541 (define (recur obj)
542 (intern-constant asm obj))
543 (define (field dst n obj)
544 (let ((src (recur obj)))
545 (if src
546 (if (statically-allocatable? obj)
547 `((static-patch! ,dst ,n ,src))
548 `((static-ref 1 ,src)
549 (static-set! 1 ,dst ,n)))
550 '())))
551 (define (intern obj label)
552 (cond
553 ((pair? obj)
554 (append (field label 0 (car obj))
555 (field label 1 (cdr obj))))
556 ((simple-vector? obj)
557 (let lp ((i 0) (inits '()))
558 (if (< i (vector-length obj))
559 (lp (1+ i)
560 (append-reverse (field label (1+ i) (vector-ref obj i))
561 inits))
562 (reverse inits))))
563 ((stringbuf? obj) '())
564 ((static-procedure? obj)
565 `((static-patch! ,label 1 ,(static-procedure-code obj))))
566 ((cache-cell? obj) '())
567 ((symbol? obj)
568 `((make-non-immediate 1 ,(recur (symbol->string obj)))
569 (string->symbol 1 1)
570 (static-set! 1 ,label 0)))
571 ((string? obj)
572 `((static-patch! ,label 1 ,(recur (make-stringbuf obj)))))
573 ((keyword? obj)
574 `((static-ref 1 ,(recur (keyword->symbol obj)))
575 (symbol->keyword 1 1)
576 (static-set! 1 ,label 0)))
577 ((number? obj)
578 `((make-non-immediate 1 ,(recur (number->string obj)))
579 (string->number 1 1)
580 (static-set! 1 ,label 0)))
581 ((uniform-vector-backing-store? obj) '())
582 ((simple-uniform-vector? obj)
583 `((static-patch! ,label 2
584 ,(recur (make-uniform-vector-backing-store obj)))))
585 (else
586 (error "don't know how to intern" obj))))
587 (cond
588 ((immediate? obj) #f)
589 ((vhash-assoc obj (asm-constants asm)) => cdr)
590 (else
591 ;; Note that calling intern may mutate asm-constants and
592 ;; asm-constant-inits.
593 (let* ((label (gensym "constant"))
594 (inits (intern obj label)))
595 (set-asm-constants! asm (vhash-cons obj label (asm-constants asm)))
596 (set-asm-inits! asm (append-reverse inits (asm-inits asm)))
597 label))))
598
599 (define (intern-non-immediate asm obj)
600 "Intern a non-immediate into the constant table, and return its
601 label."
602 (when (immediate? obj)
603 (error "expected a non-immediate" obj))
604 (intern-constant asm obj))
605
606 (define (intern-cache-cell asm scope key)
607 "Intern a cache cell into the constant table, and return its label.
608 If there is already a cache cell with the given scope and key, it is
609 returned instead."
610 (intern-constant asm (make-cache-cell scope key)))
611
612 ;; Return the label of the cell that holds the module for a scope.
613 (define (intern-module-cache-cell asm scope)
614 "Intern a cache cell for a module, and return its label."
615 (intern-cache-cell asm scope #t))
616
617
618 \f
619
620 ;;;
621 ;;; Macro assemblers bridge the gap between primitive instructions and
622 ;;; some higher-level operations.
623 ;;;
624
625 (define-syntax define-macro-assembler
626 (lambda (x)
627 (syntax-case x ()
628 ((_ (name arg ...) body body* ...)
629 (with-syntax ((emit (id-append #'name #'emit- #'name)))
630 #'(begin
631 (define emit
632 (let ((emit (lambda (arg ...) body body* ...)))
633 (hashq-set! assemblers 'name emit)
634 emit))
635 (export emit)))))))
636
637 (define-macro-assembler (load-constant asm dst obj)
638 (cond
639 ((immediate? obj)
640 (let ((bits (object-address obj)))
641 (cond
642 ((and (< dst 256) (zero? (ash bits -16)))
643 (emit-make-short-immediate asm dst obj))
644 ((zero? (ash bits -32))
645 (emit-make-long-immediate asm dst obj))
646 (else
647 (emit-make-long-long-immediate asm dst obj)))))
648 ((statically-allocatable? obj)
649 (emit-make-non-immediate asm dst (intern-non-immediate asm obj)))
650 (else
651 (emit-static-ref asm dst (intern-non-immediate asm obj)))))
652
653 (define-macro-assembler (load-static-procedure asm dst label)
654 (let ((loc (intern-constant asm (make-static-procedure label))))
655 (emit-make-non-immediate asm dst loc)))
656
657 (define-syntax-rule (define-tc7-macro-assembler name tc7)
658 (define-macro-assembler (name asm slot invert? label)
659 (emit-br-if-tc7 asm slot invert? tc7 label)))
660
661 ;; Keep in sync with tags.h. Part of Guile's ABI. Currently unused
662 ;; macro assemblers are commented out. See also
663 ;; *branching-primcall-arities* in (language cps primitives), the set of
664 ;; macro-instructions in assembly.scm, and
665 ;; disassembler.scm:code-annotation.
666 ;;
667 ;; FIXME: Define all tc7 values in Scheme in one place, derived from
668 ;; tags.h.
669 (define-tc7-macro-assembler br-if-symbol 5)
670 (define-tc7-macro-assembler br-if-variable 7)
671 (define-tc7-macro-assembler br-if-vector 13)
672 ;(define-tc7-macro-assembler br-if-weak-vector 13)
673 (define-tc7-macro-assembler br-if-string 21)
674 ;(define-tc7-macro-assembler br-if-heap-number 23)
675 ;(define-tc7-macro-assembler br-if-stringbuf 39)
676 (define-tc7-macro-assembler br-if-bytevector 77)
677 ;(define-tc7-macro-assembler br-if-pointer 31)
678 ;(define-tc7-macro-assembler br-if-hashtable 29)
679 ;(define-tc7-macro-assembler br-if-fluid 37)
680 ;(define-tc7-macro-assembler br-if-dynamic-state 45)
681 ;(define-tc7-macro-assembler br-if-frame 47)
682 ;(define-tc7-macro-assembler br-if-objcode 53)
683 ;(define-tc7-macro-assembler br-if-vm 55)
684 ;(define-tc7-macro-assembler br-if-vm-cont 71)
685 ;(define-tc7-macro-assembler br-if-rtl-program 69)
686 ;(define-tc7-macro-assembler br-if-program 79)
687 ;(define-tc7-macro-assembler br-if-weak-set 85)
688 ;(define-tc7-macro-assembler br-if-weak-table 87)
689 ;(define-tc7-macro-assembler br-if-array 93)
690 ;(define-tc7-macro-assembler br-if-bitvector 95)
691 ;(define-tc7-macro-assembler br-if-port 125)
692 ;(define-tc7-macro-assembler br-if-smob 127)
693
694 (define-macro-assembler (begin-program asm label properties)
695 (emit-label asm label)
696 (let ((meta (make-meta label properties (asm-start asm))))
697 (set-asm-meta! asm (cons meta (asm-meta asm)))))
698
699 (define-macro-assembler (end-program asm)
700 (let ((meta (car (asm-meta asm))))
701 (set-meta-high-pc! meta (asm-start asm))
702 (set-meta-arities! meta (reverse (meta-arities meta)))))
703
704 (define-macro-assembler (begin-standard-arity asm req nlocals alternate)
705 (emit-begin-opt-arity asm req '() #f nlocals alternate))
706
707 (define-macro-assembler (begin-opt-arity asm req opt rest nlocals alternate)
708 (emit-begin-kw-arity asm req opt rest '() #f nlocals alternate))
709
710 (define-macro-assembler (begin-kw-arity asm req opt rest kw-indices
711 allow-other-keys? nlocals alternate)
712 (assert-match req ((? symbol?) ...) "list of symbols")
713 (assert-match opt ((? symbol?) ...) "list of symbols")
714 (assert-match rest (or #f (? symbol?)) "#f or symbol")
715 (assert-match kw-indices (((? keyword?) . (? integer?)) ...)
716 "alist of keyword -> integer")
717 (assert-match allow-other-keys? (? boolean?) "boolean")
718 (assert-match nlocals (? integer?) "integer")
719 (assert-match alternate (or #f (? symbol?)) "#f or symbol")
720 (let* ((meta (car (asm-meta asm)))
721 (arity (make-arity req opt rest kw-indices allow-other-keys?
722 (asm-start asm) #f))
723 ;; The procedure itself is in slot 0, in the standard calling
724 ;; convention. For procedure prologues, nreq includes the
725 ;; procedure, so here we add 1.
726 (nreq (1+ (length req)))
727 (nopt (length opt))
728 (rest? (->bool rest)))
729 (set-meta-arities! meta (cons arity (meta-arities meta)))
730 (cond
731 ((or allow-other-keys? (pair? kw-indices))
732 (emit-kw-prelude asm nreq nopt rest? kw-indices allow-other-keys?
733 nlocals alternate))
734 ((or rest? (pair? opt))
735 (emit-opt-prelude asm nreq nopt rest? nlocals alternate))
736 (else
737 (emit-standard-prelude asm nreq nlocals alternate)))))
738
739 (define-macro-assembler (end-arity asm)
740 (let ((arity (car (meta-arities (car (asm-meta asm))))))
741 (set-arity-high-pc! arity (asm-start asm))))
742
743 (define-macro-assembler (standard-prelude asm nreq nlocals alternate)
744 (cond
745 (alternate
746 (emit-br-if-nargs-ne asm nreq alternate)
747 (emit-alloc-frame asm nlocals))
748 ((and (< nreq (ash 1 12)) (< (- nlocals nreq) (ash 1 12)))
749 (emit-assert-nargs-ee/locals asm nreq (- nlocals nreq)))
750 (else
751 (emit-assert-nargs-ee asm nreq)
752 (emit-alloc-frame asm nlocals))))
753
754 (define-macro-assembler (opt-prelude asm nreq nopt rest? nlocals alternate)
755 (if alternate
756 (emit-br-if-nargs-lt asm nreq alternate)
757 (emit-assert-nargs-ge asm nreq))
758 (cond
759 (rest?
760 (emit-bind-rest asm (+ nreq nopt)))
761 (alternate
762 (emit-br-if-nargs-gt asm (+ nreq nopt) alternate))
763 (else
764 (emit-assert-nargs-le asm (+ nreq nopt))))
765 (emit-alloc-frame asm nlocals))
766
767 (define-macro-assembler (kw-prelude asm nreq nopt rest? kw-indices
768 allow-other-keys? nlocals alternate)
769 (if alternate
770 (emit-br-if-nargs-lt asm nreq alternate)
771 (emit-assert-nargs-ge asm nreq))
772 (let ((ntotal (fold (lambda (kw ntotal)
773 (match kw
774 (((? keyword?) . idx)
775 (max (1+ idx) ntotal))))
776 (+ nreq nopt) kw-indices)))
777 ;; FIXME: port 581f410f
778 (emit-bind-kwargs asm nreq
779 (pack-flags allow-other-keys? rest?)
780 (+ nreq nopt)
781 ntotal
782 (intern-constant asm kw-indices))
783 (emit-alloc-frame asm nlocals)))
784
785 (define-macro-assembler (label asm sym)
786 (set-asm-labels! asm (acons sym (asm-start asm) (asm-labels asm))))
787
788 (define-macro-assembler (source asm source)
789 (set-asm-sources! asm (acons (asm-start asm) source (asm-sources asm))))
790
791 (define-macro-assembler (cache-current-module! asm module scope)
792 (let ((mod-label (intern-module-cache-cell asm scope)))
793 (emit-static-set! asm module mod-label 0)))
794
795 (define-macro-assembler (cached-toplevel-box asm dst scope sym bound?)
796 (let ((sym-label (intern-non-immediate asm sym))
797 (mod-label (intern-module-cache-cell asm scope))
798 (cell-label (intern-cache-cell asm scope sym)))
799 (emit-toplevel-box asm dst cell-label mod-label sym-label bound?)))
800
801 (define-macro-assembler (cached-module-box asm dst module-name sym public? bound?)
802 (let* ((sym-label (intern-non-immediate asm sym))
803 (key (cons public? module-name))
804 (mod-name-label (intern-constant asm key))
805 (cell-label (intern-cache-cell asm key sym)))
806 (emit-module-box asm dst cell-label mod-name-label sym-label bound?)))
807
808
809 \f
810
811 ;;;
812 ;;; Helper for linking objects.
813 ;;;
814
815 (define (make-object asm name bv relocs labels . kwargs)
816 "Make a linker object. This helper handles interning the name in the
817 shstrtab, assigning the size, allocating a fresh index, and defining a
818 corresponding linker symbol for the start of the section."
819 (let ((name-idx (intern-section-name! asm (symbol->string name)))
820 (index (asm-next-section-number asm)))
821 (set-asm-next-section-number! asm (1+ index))
822 (make-linker-object (apply make-elf-section
823 #:index index
824 #:name name-idx
825 #:size (bytevector-length bv)
826 kwargs)
827 bv relocs
828 (cons (make-linker-symbol name 0) labels))))
829
830
831 \f
832
833 ;;;
834 ;;; Linking the constant table. This code is somewhat intertwingled
835 ;;; with the intern-constant code above, as that procedure also
836 ;;; residualizes instructions to initialize constants at load time.
837 ;;;
838
839 (define (write-immediate asm buf pos x)
840 (let ((val (object-address x))
841 (endianness (asm-endianness asm)))
842 (case (asm-word-size asm)
843 ((4) (bytevector-u32-set! buf pos val endianness))
844 ((8) (bytevector-u64-set! buf pos val endianness))
845 (else (error "bad word size" asm)))))
846
847 (define (emit-init-constants asm)
848 "If there is writable data that needs initialization at runtime, emit
849 a procedure to do that and return its label. Otherwise return
850 @code{#f}."
851 (let ((inits (asm-inits asm)))
852 (and (not (null? inits))
853 (let ((label (gensym "init-constants")))
854 (emit-text asm
855 `((begin-program ,label ())
856 (assert-nargs-ee/locals 1 1)
857 ,@(reverse inits)
858 (load-constant 1 ,*unspecified*)
859 (return 1)
860 (end-program)))
861 label))))
862
863 (define (link-data asm data name)
864 "Link the static data for a program into the @var{name} section (which
865 should be .data or .rodata), and return the resulting linker object.
866 @var{data} should be a vhash mapping objects to labels."
867 (define (align address alignment)
868 (+ address
869 (modulo (- alignment (modulo address alignment)) alignment)))
870
871 (define tc7-vector 13)
872 (define stringbuf-shared-flag #x100)
873 (define stringbuf-wide-flag #x400)
874 (define tc7-stringbuf 39)
875 (define tc7-narrow-stringbuf
876 (+ tc7-stringbuf stringbuf-shared-flag))
877 (define tc7-wide-stringbuf
878 (+ tc7-stringbuf stringbuf-shared-flag stringbuf-wide-flag))
879 (define tc7-ro-string (+ 21 #x200))
880 (define tc7-rtl-program 69)
881 (define tc7-bytevector 77)
882
883 (let ((word-size (asm-word-size asm))
884 (endianness (asm-endianness asm)))
885 (define (byte-length x)
886 (cond
887 ((stringbuf? x)
888 (let ((x (stringbuf-string x)))
889 (+ (* 2 word-size)
890 (case (string-bytes-per-char x)
891 ((1) (1+ (string-length x)))
892 ((4) (* (1+ (string-length x)) 4))
893 (else (error "bad string bytes per char" x))))))
894 ((static-procedure? x)
895 (* 2 word-size))
896 ((string? x)
897 (* 4 word-size))
898 ((pair? x)
899 (* 2 word-size))
900 ((simple-vector? x)
901 (* (1+ (vector-length x)) word-size))
902 ((simple-uniform-vector? x)
903 (* 4 word-size))
904 ((uniform-vector-backing-store? x)
905 (bytevector-length (uniform-vector-backing-store-bytes x)))
906 (else
907 word-size)))
908
909 (define (write-constant-reference buf pos x)
910 ;; The asm-inits will fix up any reference to a non-immediate.
911 (write-immediate asm buf pos (if (immediate? x) x #f)))
912
913 (define (write buf pos obj)
914 (cond
915 ((stringbuf? obj)
916 (let* ((x (stringbuf-string obj))
917 (len (string-length x))
918 (tag (if (= (string-bytes-per-char x) 1)
919 tc7-narrow-stringbuf
920 tc7-wide-stringbuf)))
921 (case word-size
922 ((4)
923 (bytevector-u32-set! buf pos tag endianness)
924 (bytevector-u32-set! buf (+ pos 4) len endianness))
925 ((8)
926 (bytevector-u64-set! buf pos tag endianness)
927 (bytevector-u64-set! buf (+ pos 8) len endianness))
928 (else
929 (error "bad word size" asm)))
930 (let ((pos (+ pos (* word-size 2))))
931 (case (string-bytes-per-char x)
932 ((1)
933 (let lp ((i 0))
934 (if (< i len)
935 (let ((u8 (char->integer (string-ref x i))))
936 (bytevector-u8-set! buf (+ pos i) u8)
937 (lp (1+ i)))
938 (bytevector-u8-set! buf (+ pos i) 0))))
939 ((4)
940 (let lp ((i 0))
941 (if (< i len)
942 (let ((u32 (char->integer (string-ref x i))))
943 (bytevector-u32-set! buf (+ pos (* i 4)) u32 endianness)
944 (lp (1+ i)))
945 (bytevector-u32-set! buf (+ pos (* i 4)) 0 endianness))))
946 (else (error "bad string bytes per char" x))))))
947
948 ((static-procedure? obj)
949 (case word-size
950 ((4)
951 (bytevector-u32-set! buf pos tc7-rtl-program endianness)
952 (bytevector-u32-set! buf (+ pos 4) 0 endianness))
953 ((8)
954 (bytevector-u64-set! buf pos tc7-rtl-program endianness)
955 (bytevector-u64-set! buf (+ pos 8) 0 endianness))
956 (else (error "bad word size"))))
957
958 ((cache-cell? obj)
959 (write-immediate asm buf pos #f))
960
961 ((string? obj)
962 (let ((tag (logior tc7-ro-string (ash (string-length obj) 8))))
963 (case word-size
964 ((4)
965 (bytevector-u32-set! buf pos tc7-ro-string endianness)
966 (write-immediate asm buf (+ pos 4) #f) ; stringbuf
967 (bytevector-u32-set! buf (+ pos 8) 0 endianness)
968 (bytevector-u32-set! buf (+ pos 12) (string-length obj) endianness))
969 ((8)
970 (bytevector-u64-set! buf pos tc7-ro-string endianness)
971 (write-immediate asm buf (+ pos 8) #f) ; stringbuf
972 (bytevector-u64-set! buf (+ pos 16) 0 endianness)
973 (bytevector-u64-set! buf (+ pos 24) (string-length obj) endianness))
974 (else (error "bad word size")))))
975
976 ((pair? obj)
977 (write-constant-reference buf pos (car obj))
978 (write-constant-reference buf (+ pos word-size) (cdr obj)))
979
980 ((simple-vector? obj)
981 (let* ((len (vector-length obj))
982 (tag (logior tc7-vector (ash len 8))))
983 (case word-size
984 ((4) (bytevector-u32-set! buf pos tag endianness))
985 ((8) (bytevector-u64-set! buf pos tag endianness))
986 (else (error "bad word size")))
987 (let lp ((i 0))
988 (when (< i (vector-length obj))
989 (let ((pos (+ pos word-size (* i word-size)))
990 (elt (vector-ref obj i)))
991 (write-constant-reference buf pos elt)
992 (lp (1+ i)))))))
993
994 ((symbol? obj)
995 (write-immediate asm buf pos #f))
996
997 ((keyword? obj)
998 (write-immediate asm buf pos #f))
999
1000 ((number? obj)
1001 (write-immediate asm buf pos #f))
1002
1003 ((simple-uniform-vector? obj)
1004 (let ((tag (logior tc7-bytevector
1005 (ash (uniform-vector-element-type-code obj) 7))))
1006 (case word-size
1007 ((4)
1008 (bytevector-u32-set! buf pos tag endianness)
1009 (bytevector-u32-set! buf (+ pos 4) (bytevector-length obj)
1010 endianness) ; length
1011 (bytevector-u32-set! buf (+ pos 8) 0 endianness) ; pointer
1012 (write-immediate asm buf (+ pos 12) #f)) ; owner
1013 ((8)
1014 (bytevector-u64-set! buf pos tag endianness)
1015 (bytevector-u64-set! buf (+ pos 8) (bytevector-length obj)
1016 endianness) ; length
1017 (bytevector-u64-set! buf (+ pos 16) 0 endianness) ; pointer
1018 (write-immediate asm buf (+ pos 24) #f)) ; owner
1019 (else (error "bad word size")))))
1020
1021 ((uniform-vector-backing-store? obj)
1022 (let ((bv (uniform-vector-backing-store-bytes obj)))
1023 (bytevector-copy! bv 0 buf pos (bytevector-length bv))
1024 (unless (or (= 1 (uniform-vector-element-size bv))
1025 (eq? endianness (native-endianness)))
1026 ;; Need to swap units of element-size bytes
1027 (error "FIXME: Implement byte order swap"))))
1028
1029 (else
1030 (error "unrecognized object" obj))))
1031
1032 (cond
1033 ((vlist-null? data) #f)
1034 (else
1035 (let* ((byte-len (vhash-fold (lambda (k v len)
1036 (+ (byte-length k) (align len 8)))
1037 0 data))
1038 (buf (make-bytevector byte-len 0)))
1039 (let lp ((i 0) (pos 0) (labels '()))
1040 (if (< i (vlist-length data))
1041 (let* ((pair (vlist-ref data i))
1042 (obj (car pair))
1043 (obj-label (cdr pair)))
1044 (write buf pos obj)
1045 (lp (1+ i)
1046 (align (+ (byte-length obj) pos) 8)
1047 (cons (make-linker-symbol obj-label pos) labels)))
1048 (make-object asm name buf '() labels
1049 #:flags (match name
1050 ('.data (logior SHF_ALLOC SHF_WRITE))
1051 ('.rodata SHF_ALLOC))))))))))
1052
1053 (define (link-constants asm)
1054 "Link sections to hold constants needed by the program text emitted
1055 using @var{asm}.
1056
1057 Returns three values: an object for the .rodata section, an object for
1058 the .data section, and a label for an initialization procedure. Any of
1059 these may be @code{#f}."
1060 (define (shareable? x)
1061 (cond
1062 ((stringbuf? x) #t)
1063 ((pair? x)
1064 (and (immediate? (car x)) (immediate? (cdr x))))
1065 ((simple-vector? x)
1066 (let lp ((i 0))
1067 (or (= i (vector-length x))
1068 (and (immediate? (vector-ref x i))
1069 (lp (1+ i))))))
1070 ((uniform-vector-backing-store? x) #t)
1071 (else #f)))
1072 (let* ((constants (asm-constants asm))
1073 (len (vlist-length constants)))
1074 (let lp ((i 0)
1075 (ro vlist-null)
1076 (rw vlist-null))
1077 (if (= i len)
1078 (values (link-data asm ro '.rodata)
1079 (link-data asm rw '.data)
1080 (emit-init-constants asm))
1081 (let ((pair (vlist-ref constants i)))
1082 (if (shareable? (car pair))
1083 (lp (1+ i) (vhash-consq (car pair) (cdr pair) ro) rw)
1084 (lp (1+ i) ro (vhash-consq (car pair) (cdr pair) rw))))))))
1085
1086 \f
1087
1088 ;;;
1089 ;;; Linking program text.
1090 ;;;
1091
1092 (define (process-relocs buf relocs labels)
1093 "Patch up internal x8-s24 relocations, and any s32 relocations that
1094 reference symbols in the text section. Return a list of linker
1095 relocations for references to symbols defined outside the text section."
1096 (fold
1097 (lambda (reloc tail)
1098 (match reloc
1099 ((type label base word)
1100 (let ((abs (assq-ref labels label))
1101 (dst (+ base word)))
1102 (case type
1103 ((s32)
1104 (if abs
1105 (let ((rel (- abs base)))
1106 (s32-set! buf dst rel)
1107 tail)
1108 (cons (make-linker-reloc 'rel32/4 (* dst 4) word label)
1109 tail)))
1110 ((x8-s24)
1111 (unless abs
1112 (error "unbound near relocation" reloc))
1113 (let ((rel (- abs base))
1114 (u32 (u32-ref buf dst)))
1115 (u32-set! buf dst (pack-u8-s24 (logand u32 #xff) rel))
1116 tail))
1117 (else (error "bad relocation kind" reloc)))))))
1118 '()
1119 relocs))
1120
1121 (define (process-labels labels)
1122 "Define linker symbols for the label-offset pairs in @var{labels}.
1123 The offsets are expected to be expressed in words."
1124 (map (lambda (pair)
1125 (make-linker-symbol (car pair) (* (cdr pair) 4)))
1126 labels))
1127
1128 (define (swap-bytes! buf)
1129 "Patch up the text buffer @var{buf}, swapping the endianness of each
1130 32-bit unit."
1131 (unless (zero? (modulo (bytevector-length buf) 4))
1132 (error "unexpected length"))
1133 (let ((byte-len (bytevector-length buf)))
1134 (let lp ((pos 0))
1135 (unless (= pos byte-len)
1136 (bytevector-u32-set!
1137 buf pos
1138 (bytevector-u32-ref buf pos (endianness big))
1139 (endianness little))
1140 (lp (+ pos 4))))))
1141
1142 (define (link-text-object asm)
1143 "Link the .rtl-text section, swapping the endianness of the bytes if
1144 needed."
1145 (let ((buf (make-u32vector (asm-pos asm))))
1146 (let lp ((pos 0) (prev (reverse (asm-prev asm))))
1147 (if (null? prev)
1148 (let ((byte-size (* (asm-idx asm) 4)))
1149 (bytevector-copy! (asm-cur asm) 0 buf pos byte-size)
1150 (unless (eq? (asm-endianness asm) (native-endianness))
1151 (swap-bytes! buf))
1152 (make-object asm '.rtl-text
1153 buf
1154 (process-relocs buf (asm-relocs asm)
1155 (asm-labels asm))
1156 (process-labels (asm-labels asm))))
1157 (let ((len (* *block-size* 4)))
1158 (bytevector-copy! (car prev) 0 buf pos len)
1159 (lp (+ pos len) (cdr prev)))))))
1160
1161
1162 \f
1163
1164 ;;;
1165 ;;; Linking other sections of the ELF file, like the dynamic segment,
1166 ;;; the symbol table, etc.
1167 ;;;
1168
1169 (define (link-dynamic-section asm text rw rw-init)
1170 "Link the dynamic section for an ELF image with RTL text, given the
1171 writable data section @var{rw} needing fixup from the procedure with
1172 label @var{rw-init}. @var{rw-init} may be false. If @var{rw} is true,
1173 it will be added to the GC roots at runtime."
1174 (define-syntax-rule (emit-dynamic-section word-size %set-uword! reloc-type)
1175 (let* ((endianness (asm-endianness asm))
1176 (bv (make-bytevector (* word-size (if rw (if rw-init 12 10) 6)) 0))
1177 (set-uword!
1178 (lambda (i uword)
1179 (%set-uword! bv (* i word-size) uword endianness)))
1180 (relocs '())
1181 (set-label!
1182 (lambda (i label)
1183 (set! relocs (cons (make-linker-reloc 'reloc-type
1184 (* i word-size) 0 label)
1185 relocs))
1186 (%set-uword! bv (* i word-size) 0 endianness))))
1187 (set-uword! 0 DT_GUILE_RTL_VERSION)
1188 (set-uword! 1 #x02020000)
1189 (set-uword! 2 DT_GUILE_ENTRY)
1190 (set-label! 3 '.rtl-text)
1191 (cond
1192 (rw
1193 ;; Add roots to GC.
1194 (set-uword! 4 DT_GUILE_GC_ROOT)
1195 (set-label! 5 '.data)
1196 (set-uword! 6 DT_GUILE_GC_ROOT_SZ)
1197 (set-uword! 7 (bytevector-length (linker-object-bv rw)))
1198 (cond
1199 (rw-init
1200 (set-uword! 8 DT_INIT) ; constants
1201 (set-label! 9 rw-init)
1202 (set-uword! 10 DT_NULL)
1203 (set-uword! 11 0))
1204 (else
1205 (set-uword! 8 DT_NULL)
1206 (set-uword! 9 0))))
1207 (else
1208 (set-uword! 4 DT_NULL)
1209 (set-uword! 5 0)))
1210 (make-object asm '.dynamic bv relocs '()
1211 #:type SHT_DYNAMIC #:flags SHF_ALLOC)))
1212 (case (asm-word-size asm)
1213 ((4) (emit-dynamic-section 4 bytevector-u32-set! abs32/1))
1214 ((8) (emit-dynamic-section 8 bytevector-u64-set! abs64/1))
1215 (else (error "bad word size" asm))))
1216
1217 (define (link-shstrtab asm)
1218 "Link the string table for the section headers."
1219 (intern-section-name! asm ".shstrtab")
1220 (make-object asm '.shstrtab
1221 (link-string-table! (asm-shstrtab asm))
1222 '() '()
1223 #:type SHT_STRTAB #:flags 0))
1224
1225 (define (link-symtab text-section asm)
1226 (let* ((endianness (asm-endianness asm))
1227 (word-size (asm-word-size asm))
1228 (size (elf-symbol-len word-size))
1229 (meta (reverse (asm-meta asm)))
1230 (n (length meta))
1231 (strtab (make-string-table))
1232 (bv (make-bytevector (* n size) 0)))
1233 (define (intern-string! name)
1234 (string-table-intern! strtab (if name (symbol->string name) "")))
1235 (for-each
1236 (lambda (meta n)
1237 (let ((name (intern-string! (meta-name meta))))
1238 (write-elf-symbol bv (* n size) endianness word-size
1239 (make-elf-symbol
1240 #:name name
1241 ;; Symbol value and size are measured in
1242 ;; bytes, not u32s.
1243 #:value (* 4 (meta-low-pc meta))
1244 #:size (* 4 (- (meta-high-pc meta)
1245 (meta-low-pc meta)))
1246 #:type STT_FUNC
1247 #:visibility STV_HIDDEN
1248 #:shndx (elf-section-index text-section)))))
1249 meta (iota n))
1250 (let ((strtab (make-object asm '.strtab
1251 (link-string-table! strtab)
1252 '() '()
1253 #:type SHT_STRTAB #:flags 0)))
1254 (values (make-object asm '.symtab
1255 bv
1256 '() '()
1257 #:type SHT_SYMTAB #:flags 0 #:entsize size
1258 #:link (elf-section-index
1259 (linker-object-section strtab)))
1260 strtab))))
1261
1262 ;;; The .guile.arities section describes the arities that a function can
1263 ;;; have. It is in two parts: a sorted array of headers describing
1264 ;;; basic arities, and an array of links out to a string table (and in
1265 ;;; the case of keyword arguments, to the data section) for argument
1266 ;;; names. The whole thing is prefixed by a uint32 indicating the
1267 ;;; offset of the end of the headers array.
1268 ;;;
1269 ;;; The arity headers array is a packed array of structures of the form:
1270 ;;;
1271 ;;; struct arity_header {
1272 ;;; uint32_t low_pc;
1273 ;;; uint32_t high_pc;
1274 ;;; uint32_t offset;
1275 ;;; uint32_t flags;
1276 ;;; uint32_t nreq;
1277 ;;; uint32_t nopt;
1278 ;;; }
1279 ;;;
1280 ;;; All of the offsets and addresses are 32 bits. We can expand in the
1281 ;;; future to use 64-bit offsets if appropriate, but there are other
1282 ;;; aspects of RTL that constrain us to a total image that fits in 32
1283 ;;; bits, so for the moment we'll simplify the problem space.
1284 ;;;
1285 ;;; The following flags values are defined:
1286 ;;;
1287 ;;; #x1: has-rest?
1288 ;;; #x2: allow-other-keys?
1289 ;;; #x4: has-keyword-args?
1290 ;;; #x8: is-case-lambda?
1291 ;;;
1292 ;;; Functions with a single arity specify their number of required and
1293 ;;; optional arguments in nreq and nopt, and do not have the
1294 ;;; is-case-lambda? flag set. Their "offset" member links to an array
1295 ;;; of pointers into the associated .guile.arities.strtab string table,
1296 ;;; identifying the argument names. This offset is relative to the
1297 ;;; start of the .guile.arities section. Links for required arguments
1298 ;;; are first, in order, as uint32 values. Next follow the optionals,
1299 ;;; then the rest link if has-rest? is set, then a link to the "keyword
1300 ;;; indices" literal if has-keyword-args? is set. Unlike the other
1301 ;;; links, the kw-indices link points into the data section, and is
1302 ;;; relative to the ELF image as a whole.
1303 ;;;
1304 ;;; Functions with no arities have no arities information present in the
1305 ;;; .guile.arities section.
1306 ;;;
1307 ;;; Functions with multiple arities are preceded by a header with
1308 ;;; is-case-lambda? set. All other fields are 0, except low-pc and
1309 ;;; high-pc which should be the bounds of the whole function. Headers
1310 ;;; for the individual arities follow. In this way the whole headers
1311 ;;; array is sorted in increasing low-pc order, and case-lambda clauses
1312 ;;; are contained within the [low-pc, high-pc] of the case-lambda
1313 ;;; header.
1314
1315 ;; Length of the prefix to the arities section, in bytes.
1316 (define arities-prefix-len 4)
1317
1318 ;; Length of an arity header, in bytes.
1319 (define arity-header-len (* 6 4))
1320
1321 ;; The offset of "offset" within arity header, in bytes.
1322 (define arity-header-offset-offset (* 2 4))
1323
1324 (define-syntax-rule (pack-arity-flags has-rest? allow-other-keys?
1325 has-keyword-args? is-case-lambda?)
1326 (logior (if has-rest? (ash 1 0) 0)
1327 (if allow-other-keys? (ash 1 1) 0)
1328 (if has-keyword-args? (ash 1 2) 0)
1329 (if is-case-lambda? (ash 1 3) 0)))
1330
1331 (define (meta-arities-size meta)
1332 (define (lambda-size arity)
1333 (+ arity-header-len
1334 (* 4 ;; name pointers
1335 (+ (length (arity-req arity))
1336 (length (arity-opt arity))
1337 (if (arity-rest arity) 1 0)
1338 (if (pair? (arity-kw-indices arity)) 1 0)))))
1339 (define (case-lambda-size arities)
1340 (fold +
1341 arity-header-len ;; case-lambda header
1342 (map lambda-size arities))) ;; the cases
1343 (match (meta-arities meta)
1344 (() 0)
1345 ((arity) (lambda-size arity))
1346 (arities (case-lambda-size arities))))
1347
1348 (define (write-arity-headers metas bv endianness)
1349 (define (write-arity-header* pos low-pc high-pc flags nreq nopt)
1350 (bytevector-u32-set! bv pos low-pc endianness)
1351 (bytevector-u32-set! bv (+ pos 4) high-pc endianness)
1352 (bytevector-u32-set! bv (+ pos 8) 0 endianness) ; offset
1353 (bytevector-u32-set! bv (+ pos 12) flags endianness)
1354 (bytevector-u32-set! bv (+ pos 16) nreq endianness)
1355 (bytevector-u32-set! bv (+ pos 20) nopt endianness))
1356 (define (write-arity-header pos arity)
1357 (write-arity-header* pos (arity-low-pc arity)
1358 (arity-high-pc arity)
1359 (pack-arity-flags (arity-rest arity)
1360 (arity-allow-other-keys? arity)
1361 (pair? (arity-kw-indices arity))
1362 #f)
1363 (length (arity-req arity))
1364 (length (arity-opt arity))))
1365 (let lp ((metas metas) (pos arities-prefix-len) (offsets '()))
1366 (match metas
1367 (()
1368 ;; Fill in the prefix.
1369 (bytevector-u32-set! bv 0 pos endianness)
1370 (values pos (reverse offsets)))
1371 ((meta . metas)
1372 (match (meta-arities meta)
1373 (() (lp metas pos offsets))
1374 ((arity)
1375 (write-arity-header pos arity)
1376 (lp metas
1377 (+ pos arity-header-len)
1378 (acons arity (+ pos arity-header-offset-offset) offsets)))
1379 (arities
1380 ;; Write a case-lambda header, then individual arities.
1381 ;; The case-lambda header's offset link is 0.
1382 (write-arity-header* pos (meta-low-pc meta) (meta-high-pc meta)
1383 (pack-arity-flags #f #f #f #t) 0 0)
1384 (let lp* ((arities arities) (pos (+ pos arity-header-len))
1385 (offsets offsets))
1386 (match arities
1387 (() (lp metas pos offsets))
1388 ((arity . arities)
1389 (write-arity-header pos arity)
1390 (lp* arities
1391 (+ pos arity-header-len)
1392 (acons arity
1393 (+ pos arity-header-offset-offset)
1394 offsets)))))))))))
1395
1396 (define (write-arity-links asm bv pos arity-offset-pairs strtab)
1397 (define (write-symbol sym pos)
1398 (bytevector-u32-set! bv pos
1399 (string-table-intern! strtab (symbol->string sym))
1400 (asm-endianness asm))
1401 (+ pos 4))
1402 (define (write-kw-indices pos kw-indices)
1403 ;; FIXME: Assert that kw-indices is already interned.
1404 (make-linker-reloc 'abs32/1 pos 0
1405 (intern-constant asm kw-indices)))
1406 (let lp ((pos pos) (pairs arity-offset-pairs) (relocs '()))
1407 (match pairs
1408 (()
1409 (unless (= pos (bytevector-length bv))
1410 (error "expected to fully fill the bytevector"
1411 pos (bytevector-length bv)))
1412 relocs)
1413 (((arity . offset) . pairs)
1414 (bytevector-u32-set! bv offset pos (asm-endianness asm))
1415 (let ((pos (fold write-symbol
1416 pos
1417 (append (arity-req arity)
1418 (arity-opt arity)
1419 (cond
1420 ((arity-rest arity) => list)
1421 (else '()))))))
1422 (match (arity-kw-indices arity)
1423 (() (lp pos pairs relocs))
1424 (kw-indices
1425 (lp (+ pos 4)
1426 pairs
1427 (cons (write-kw-indices pos kw-indices) relocs)))))))))
1428
1429 (define (link-arities asm)
1430 (let* ((endianness (asm-endianness asm))
1431 (metas (reverse (asm-meta asm)))
1432 (size (fold (lambda (meta size)
1433 (+ size (meta-arities-size meta)))
1434 arities-prefix-len
1435 metas))
1436 (strtab (make-string-table))
1437 (bv (make-bytevector size 0)))
1438 (let ((kw-indices-relocs
1439 (call-with-values
1440 (lambda ()
1441 (write-arity-headers metas bv endianness))
1442 (lambda (pos arity-offset-pairs)
1443 (write-arity-links asm bv pos arity-offset-pairs strtab)))))
1444 (let ((strtab (make-object asm '.guile.arities.strtab
1445 (link-string-table! strtab)
1446 '() '()
1447 #:type SHT_STRTAB #:flags 0)))
1448 (values (make-object asm '.guile.arities
1449 bv
1450 kw-indices-relocs '()
1451 #:type SHT_PROGBITS #:flags 0
1452 #:link (elf-section-index
1453 (linker-object-section strtab)))
1454 strtab)))))
1455
1456 ;;;
1457 ;;; The .guile.docstrs section is a packed, sorted array of (pc, str)
1458 ;;; values. Pc and str are both 32 bits wide. (Either could change to
1459 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1460 ;;; entry to a program, relative to the start of the text section, and
1461 ;;; str is an index into the associated .guile.docstrs.strtab string
1462 ;;; table section.
1463 ;;;
1464
1465 ;; The size of a docstrs entry, in bytes.
1466 (define docstr-size 8)
1467
1468 (define (link-docstrs asm)
1469 (define (find-docstrings)
1470 (filter-map (lambda (meta)
1471 (define (is-documentation? pair)
1472 (eq? (car pair) 'documentation))
1473 (let* ((props (meta-properties meta))
1474 (tail (find-tail is-documentation? props)))
1475 (and tail
1476 (not (find-tail is-documentation? (cdr tail)))
1477 (string? (cdar tail))
1478 (cons (meta-low-pc meta) (cdar tail)))))
1479 (reverse (asm-meta asm))))
1480 (let* ((endianness (asm-endianness asm))
1481 (docstrings (find-docstrings))
1482 (strtab (make-string-table))
1483 (bv (make-bytevector (* (length docstrings) docstr-size) 0)))
1484 (fold (lambda (pair pos)
1485 (match pair
1486 ((pc . string)
1487 (bytevector-u32-set! bv pos pc endianness)
1488 (bytevector-u32-set! bv (+ pos 4)
1489 (string-table-intern! strtab string)
1490 endianness)
1491 (+ pos docstr-size))))
1492 0
1493 docstrings)
1494 (let ((strtab (make-object asm '.guile.docstrs.strtab
1495 (link-string-table! strtab)
1496 '() '()
1497 #:type SHT_STRTAB #:flags 0)))
1498 (values (make-object asm '.guile.docstrs
1499 bv
1500 '() '()
1501 #:type SHT_PROGBITS #:flags 0
1502 #:link (elf-section-index
1503 (linker-object-section strtab)))
1504 strtab))))
1505
1506 ;;;
1507 ;;; The .guile.procprops section is a packed, sorted array of (pc, addr)
1508 ;;; values. Pc and addr are both 32 bits wide. (Either could change to
1509 ;;; 64 bits if appropriate in the future.) Pc is the address of the
1510 ;;; entry to a program, relative to the start of the text section, and
1511 ;;; addr is the address of the associated properties alist, relative to
1512 ;;; the start of the ELF image.
1513 ;;;
1514 ;;; Since procedure properties are stored in the data sections, we need
1515 ;;; to link the procedures property section first. (Note that this
1516 ;;; constraint does not apply to the arities section, which may
1517 ;;; reference the data sections via the kw-indices literal, because
1518 ;;; assembling the text section already makes sure that the kw-indices
1519 ;;; are interned.)
1520 ;;;
1521
1522 ;; The size of a procprops entry, in bytes.
1523 (define procprops-size 8)
1524
1525 (define (link-procprops asm)
1526 (define (assoc-remove-one alist key value-pred)
1527 (match alist
1528 (() '())
1529 ((((? (lambda (x) (eq? x key))) . value) . alist)
1530 (if (value-pred value)
1531 alist
1532 (acons key value alist)))
1533 (((k . v) . alist)
1534 (acons k v (assoc-remove-one alist key value-pred)))))
1535 (define (props-without-name-or-docstring meta)
1536 (assoc-remove-one
1537 (assoc-remove-one (meta-properties meta) 'name (lambda (x) #t))
1538 'documentation
1539 string?))
1540 (define (find-procprops)
1541 (filter-map (lambda (meta)
1542 (let ((props (props-without-name-or-docstring meta)))
1543 (and (pair? props)
1544 (cons (meta-low-pc meta) props))))
1545 (reverse (asm-meta asm))))
1546 (let* ((endianness (asm-endianness asm))
1547 (procprops (find-procprops))
1548 (bv (make-bytevector (* (length procprops) procprops-size) 0)))
1549 (let lp ((procprops procprops) (pos 0) (relocs '()))
1550 (match procprops
1551 (()
1552 (make-object asm '.guile.procprops
1553 bv
1554 relocs '()
1555 #:type SHT_PROGBITS #:flags 0))
1556 (((pc . props) . procprops)
1557 (bytevector-u32-set! bv pos pc endianness)
1558 (lp procprops
1559 (+ pos procprops-size)
1560 (cons (make-linker-reloc 'abs32/1 (+ pos 4) 0
1561 (intern-constant asm props))
1562 relocs)))))))
1563
1564 ;;;
1565 ;;; The DWARF .debug_info, .debug_abbrev, .debug_str, and .debug_loc
1566 ;;; sections provide line number and local variable liveness
1567 ;;; information. Their format is defined by the DWARF
1568 ;;; specifications.
1569 ;;;
1570
1571 (define (asm-language asm)
1572 ;; FIXME: Plumb language through to the assembler.
1573 'scheme)
1574
1575 ;; -> 5 values: .debug_info, .debug_abbrev, .debug_str, .debug_loc, .debug_lines
1576 (define (link-debug asm)
1577 (define (put-s8 port val)
1578 (let ((bv (make-bytevector 1)))
1579 (bytevector-s8-set! bv 0 val)
1580 (put-bytevector port bv)))
1581
1582 (define (put-u16 port val)
1583 (let ((bv (make-bytevector 2)))
1584 (bytevector-u16-set! bv 0 val (asm-endianness asm))
1585 (put-bytevector port bv)))
1586
1587 (define (put-u32 port val)
1588 (let ((bv (make-bytevector 4)))
1589 (bytevector-u32-set! bv 0 val (asm-endianness asm))
1590 (put-bytevector port bv)))
1591
1592 (define (put-u64 port val)
1593 (let ((bv (make-bytevector 8)))
1594 (bytevector-u64-set! bv 0 val (asm-endianness asm))
1595 (put-bytevector port bv)))
1596
1597 (define (put-uleb128 port val)
1598 (let lp ((val val))
1599 (let ((next (ash val -7)))
1600 (if (zero? next)
1601 (put-u8 port val)
1602 (begin
1603 (put-u8 port (logior #x80 (logand val #x7f)))
1604 (lp next))))))
1605
1606 (define (put-sleb128 port val)
1607 (let lp ((val val))
1608 (if (<= 0 (+ val 64) 128)
1609 (put-u8 port (logand val #x7f))
1610 (begin
1611 (put-u8 port (logior #x80 (logand val #x7f)))
1612 (lp (ash val -7))))))
1613
1614 (define (port-position port)
1615 (seek port 0 SEEK_CUR))
1616
1617 (define (meta->subprogram-die meta)
1618 `(subprogram
1619 (@ ,@(cond
1620 ((meta-name meta)
1621 => (lambda (name) `((name ,(symbol->string name)))))
1622 (else
1623 '()))
1624 (low-pc ,(meta-label meta))
1625 (high-pc ,(* 4 (- (meta-high-pc meta) (meta-low-pc meta)))))))
1626
1627 (define (make-compile-unit-die asm)
1628 `(compile-unit
1629 (@ (producer ,(string-append "Guile " (version)))
1630 (language ,(asm-language asm))
1631 (low-pc .rtl-text)
1632 (high-pc ,(* 4 (asm-pos asm)))
1633 (stmt-list 0))
1634 ,@(map meta->subprogram-die (reverse (asm-meta asm)))))
1635
1636 (let-values (((die-port get-die-bv) (open-bytevector-output-port))
1637 ((die-relocs) '())
1638 ((abbrev-port get-abbrev-bv) (open-bytevector-output-port))
1639 ;; (tag has-kids? attrs forms) -> code
1640 ((abbrevs) vlist-null)
1641 ((strtab) (make-string-table))
1642 ((line-port get-line-bv) (open-bytevector-output-port))
1643 ((line-relocs) '())
1644 ;; file -> code
1645 ((files) vlist-null))
1646
1647 (define (write-abbrev code tag has-children? attrs forms)
1648 (put-uleb128 abbrev-port code)
1649 (put-uleb128 abbrev-port (tag-name->code tag))
1650 (put-u8 abbrev-port (children-name->code (if has-children? 'yes 'no)))
1651 (for-each (lambda (attr form)
1652 (put-uleb128 abbrev-port (attribute-name->code attr))
1653 (put-uleb128 abbrev-port (form-name->code form)))
1654 attrs forms)
1655 (put-uleb128 abbrev-port 0)
1656 (put-uleb128 abbrev-port 0))
1657
1658 (define (intern-abbrev tag has-children? attrs forms)
1659 (let ((key (list tag has-children? attrs forms)))
1660 (match (vhash-assoc key abbrevs)
1661 ((_ . code) code)
1662 (#f (let ((code (1+ (vlist-length abbrevs))))
1663 (set! abbrevs (vhash-cons key code abbrevs))
1664 (write-abbrev code tag has-children? attrs forms)
1665 code)))))
1666
1667 (define (intern-file file)
1668 (match (vhash-assoc file files)
1669 ((_ . code) code)
1670 (#f (let ((code (1+ (vlist-length files))))
1671 (set! files (vhash-cons file code files))
1672 code))))
1673
1674 (define (write-sources)
1675 ;; Choose line base and line range values that will allow for an
1676 ;; address advance range of 16 words. The special opcode range is
1677 ;; from 10 to 255, so 246 values.
1678 (define base -4)
1679 (define range 15)
1680
1681 (let lp ((sources (asm-sources asm)) (out '()))
1682 (match sources
1683 (((pc . s) . sources)
1684 (let ((file (assq-ref s 'filename))
1685 (line (assq-ref s 'line))
1686 (col (assq-ref s 'column)))
1687 (lp sources
1688 ;; Guile line and column numbers are 0-indexed, but
1689 ;; they are 1-indexed for DWARF.
1690 (cons (list pc
1691 (if file (intern-file file) 0)
1692 (if line (1+ line))
1693 (if col (1+ col)))
1694 out))))
1695 (()
1696 ;; Compilation unit header for .debug_line. We write in
1697 ;; DWARF 2 format because more tools understand it than DWARF
1698 ;; 4, which incompatibly adds another field to this header.
1699
1700 (put-u32 line-port 0) ; Length; will patch later.
1701 (put-u16 line-port 2) ; DWARF 2 format.
1702 (put-u32 line-port 0) ; Prologue length; will patch later.
1703 (put-u8 line-port 4) ; Minimum instruction length: 4 bytes.
1704 (put-u8 line-port 1) ; Default is-stmt: true.
1705
1706 (put-s8 line-port base) ; Line base. See the DWARF standard.
1707 (put-u8 line-port range) ; Line range. See the DWARF standard.
1708 (put-u8 line-port 10) ; Opcode base: the first "special" opcode.
1709
1710 ;; A table of the number of uleb128 arguments taken by each
1711 ;; of the standard opcodes.
1712 (put-u8 line-port 0) ; 1: copy
1713 (put-u8 line-port 1) ; 2: advance-pc
1714 (put-u8 line-port 1) ; 3: advance-line
1715 (put-u8 line-port 1) ; 4: set-file
1716 (put-u8 line-port 1) ; 5: set-column
1717 (put-u8 line-port 0) ; 6: negate-stmt
1718 (put-u8 line-port 0) ; 7: set-basic-block
1719 (put-u8 line-port 0) ; 8: const-add-pc
1720 (put-u8 line-port 1) ; 9: fixed-advance-pc
1721
1722 ;; Include directories, as a zero-terminated sequence of
1723 ;; nul-terminated strings. Nothing, for the moment.
1724 (put-u8 line-port 0)
1725
1726 ;; File table. For each file that contributes to this
1727 ;; compilation unit, a nul-terminated file name string, and a
1728 ;; uleb128 for each of directory the file was found in, the
1729 ;; modification time, and the file's size in bytes. We pass
1730 ;; zero for the latter three fields.
1731 (vlist-for-each (match-lambda
1732 ((file . code)
1733 (put-bytevector line-port (string->utf8 file))
1734 (put-u8 line-port 0)
1735 (put-uleb128 line-port 0) ; directory
1736 (put-uleb128 line-port 0) ; mtime
1737 (put-uleb128 line-port 0) ; size
1738 ))
1739 files)
1740 (put-u8 line-port 0) ; 0 byte terminating file list.
1741
1742 ;; Patch prologue length.
1743 (let ((offset (port-position line-port)))
1744 (seek line-port 6 SEEK_SET)
1745 (put-u32 line-port (- offset 10))
1746 (seek line-port offset SEEK_SET))
1747
1748 ;; Now write the statement program.
1749 (let ()
1750 (define (extended-op opcode payload-len)
1751 (put-u8 line-port 0) ; extended op
1752 (put-uleb128 line-port (1+ payload-len)) ; payload-len + opcode
1753 (put-uleb128 line-port opcode))
1754 (define (set-address sym)
1755 (define (add-reloc! kind)
1756 (set! line-relocs
1757 (cons (make-linker-reloc kind
1758 (port-position line-port)
1759 0
1760 sym)
1761 line-relocs)))
1762 (match (asm-word-size asm)
1763 (4
1764 (extended-op 2 4)
1765 (add-reloc! 'abs32/1)
1766 (put-u32 line-port 0))
1767 (8
1768 (extended-op 2 8)
1769 (add-reloc! 'abs64/1)
1770 (put-u64 line-port 0))))
1771 (define (end-sequence pc)
1772 (let ((pc-inc (- (asm-pos asm) pc)))
1773 (put-u8 line-port 2) ; advance-pc
1774 (put-uleb128 line-port pc-inc))
1775 (extended-op 1 0))
1776 (define (advance-pc pc-inc line-inc)
1777 (let ((spec (+ (- line-inc base) (* pc-inc range) 10)))
1778 (cond
1779 ((or (< line-inc base) (>= line-inc (+ base range)))
1780 (advance-line line-inc)
1781 (advance-pc pc-inc 0))
1782 ((<= spec 255)
1783 (put-u8 line-port spec))
1784 ((< spec 500)
1785 (put-u8 line-port 8) ; const-advance-pc
1786 (advance-pc (- pc-inc (floor/ (- 255 10) range))
1787 line-inc))
1788 (else
1789 (put-u8 line-port 2) ; advance-pc
1790 (put-uleb128 line-port pc-inc)
1791 (advance-pc 0 line-inc)))))
1792 (define (advance-line inc)
1793 (put-u8 line-port 3)
1794 (put-sleb128 line-port inc))
1795 (define (set-file file)
1796 (put-u8 line-port 4)
1797 (put-uleb128 line-port file))
1798 (define (set-column col)
1799 (put-u8 line-port 5)
1800 (put-uleb128 line-port col))
1801
1802 (set-address '.rtl-text)
1803
1804 (let lp ((in out) (pc 0) (file 1) (line 1) (col 0))
1805 (match in
1806 (()
1807 (when (null? out)
1808 ;; There was no source info in the first place. Set
1809 ;; file register to 0 before adding final row.
1810 (set-file 0))
1811 (end-sequence pc))
1812 (((pc* file* line* col*) . in*)
1813 (cond
1814 ((and (eqv? file file*) (eqv? line line*) (eqv? col col*))
1815 (lp in* pc file line col))
1816 (else
1817 (unless (eqv? col col*)
1818 (set-column col*))
1819 (unless (eqv? file file*)
1820 (set-file file*))
1821 (advance-pc (- pc* pc) (- line* line))
1822 (lp in* pc* file* line* col*)))))))))))
1823
1824 (define (compute-code attr val)
1825 (match attr
1826 ('name (string-table-intern! strtab val))
1827 ('low-pc val)
1828 ('high-pc val)
1829 ('producer (string-table-intern! strtab val))
1830 ('language (language-name->code val))
1831 ('stmt-list val)))
1832
1833 (define (exact-integer? val)
1834 (and (number? val) (integer? val) (exact? val)))
1835
1836 (define (choose-form attr val code)
1837 (cond
1838 ((string? val) 'strp)
1839 ((eq? attr 'stmt-list) 'sec-offset)
1840 ((exact-integer? code)
1841 (cond
1842 ((< code 0) 'sleb128)
1843 ((<= code #xff) 'data1)
1844 ((<= code #xffff) 'data2)
1845 ((<= code #xffffffff) 'data4)
1846 ((<= code #xffffffffffffffff) 'data8)
1847 (else 'uleb128)))
1848 ((symbol? val) 'addr)
1849 (else (error "unhandled case" attr val code))))
1850
1851 (define (add-die-relocation! kind sym)
1852 (set! die-relocs
1853 (cons (make-linker-reloc kind (port-position die-port) 0 sym)
1854 die-relocs)))
1855
1856 (define (write-value code form)
1857 (match form
1858 ('data1 (put-u8 die-port code))
1859 ('data2 (put-u16 die-port code))
1860 ('data4 (put-u32 die-port code))
1861 ('data8 (put-u64 die-port code))
1862 ('uleb128 (put-uleb128 die-port code))
1863 ('sleb128 (put-sleb128 die-port code))
1864 ('addr
1865 (match (asm-word-size asm)
1866 (4
1867 (add-die-relocation! 'abs32/1 code)
1868 (put-u32 die-port 0))
1869 (8
1870 (add-die-relocation! 'abs64/1 code)
1871 (put-u64 die-port 0))))
1872 ('sec-offset (put-u32 die-port code))
1873 ('strp (put-u32 die-port code))))
1874
1875 (define (write-die die)
1876 (match die
1877 ((tag ('@ (attrs vals) ...) children ...)
1878 (let* ((codes (map compute-code attrs vals))
1879 (forms (map choose-form attrs vals codes))
1880 (has-children? (not (null? children)))
1881 (abbrev-code (intern-abbrev tag has-children? attrs forms)))
1882 (put-uleb128 die-port abbrev-code)
1883 (for-each write-value codes forms)
1884 (when has-children?
1885 (for-each write-die children)
1886 (put-uleb128 die-port 0))))))
1887
1888 ;; Compilation unit header.
1889 (put-u32 die-port 0) ; Length; will patch later.
1890 (put-u16 die-port 4) ; DWARF 4.
1891 (put-u32 die-port 0) ; Abbrevs offset.
1892 (put-u8 die-port (asm-word-size asm)) ; Address size.
1893
1894 (write-die (make-compile-unit-die asm))
1895
1896 ;; Terminate the abbrevs list.
1897 (put-uleb128 abbrev-port 0)
1898
1899 (write-sources)
1900
1901 (values (let ((bv (get-die-bv)))
1902 ;; Patch DWARF32 length.
1903 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
1904 (asm-endianness asm))
1905 (make-object asm '.debug_info bv die-relocs '()
1906 #:type SHT_PROGBITS #:flags 0))
1907 (make-object asm '.debug_abbrev (get-abbrev-bv) '() '()
1908 #:type SHT_PROGBITS #:flags 0)
1909 (make-object asm '.debug_str (link-string-table! strtab) '() '()
1910 #:type SHT_PROGBITS #:flags 0)
1911 (make-object asm '.debug_loc #vu8() '() '()
1912 #:type SHT_PROGBITS #:flags 0)
1913 (let ((bv (get-line-bv)))
1914 ;; Patch DWARF32 length.
1915 (bytevector-u32-set! bv 0 (- (bytevector-length bv) 4)
1916 (asm-endianness asm))
1917 (make-object asm '.debug_line bv line-relocs '()
1918 #:type SHT_PROGBITS #:flags 0)))))
1919
1920 (define (link-objects asm)
1921 (let*-values (;; Link procprops before constants, because it probably
1922 ;; interns more constants.
1923 ((procprops) (link-procprops asm))
1924 ((ro rw rw-init) (link-constants asm))
1925 ;; Link text object after constants, so that the
1926 ;; constants initializer gets included.
1927 ((text) (link-text-object asm))
1928 ((dt) (link-dynamic-section asm text rw rw-init))
1929 ((symtab strtab) (link-symtab (linker-object-section text) asm))
1930 ((arities arities-strtab) (link-arities asm))
1931 ((docstrs docstrs-strtab) (link-docstrs asm))
1932 ((dinfo dabbrev dstrtab dloc dline) (link-debug asm))
1933 ;; This needs to be linked last, because linking other
1934 ;; sections adds entries to the string table.
1935 ((shstrtab) (link-shstrtab asm)))
1936 (filter identity
1937 (list text ro rw dt symtab strtab arities arities-strtab
1938 docstrs docstrs-strtab procprops
1939 dinfo dabbrev dstrtab dloc dline
1940 shstrtab))))
1941
1942
1943 \f
1944
1945 ;;;
1946 ;;; High-level public interfaces.
1947 ;;;
1948
1949 (define* (link-assembly asm #:key (page-aligned? #t))
1950 "Produce an ELF image from the code and data emitted into @var{asm}.
1951 The result is a bytevector, by default linked so that read-only and
1952 writable data are on separate pages. Pass @code{#:page-aligned? #f} to
1953 disable this behavior."
1954 (link-elf (link-objects asm) #:page-aligned? page-aligned?))
1955
1956 (define (assemble-program instructions)
1957 "Take the sequence of instructions @var{instructions}, assemble them
1958 into RTL code, link an image, and load that image from memory. Returns
1959 a procedure."
1960 (let ((asm (make-assembler)))
1961 (emit-text asm instructions)
1962 (load-thunk-from-memory (link-assembly asm #:page-aligned? #f))))