-
Notifications
You must be signed in to change notification settings - Fork 18
/
storage-types.lisp
1344 lines (1203 loc) · 42.6 KB
/
storage-types.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;------------------------------------------------------------------
;;;;
;;;; Copyright (C) 2000-2005,
;;;; Department of Computer Science, University of Tromso, Norway
;;;;
;;;; Filename: storage-types.lisp
;;;; Description: Physical storage structures for Movitz objects.
;;;; Author: Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Created at: Sun Oct 22 00:22:43 2000
;;;; Distribution: See the accompanying file COPYING.
;;;;
;;;; $Id: storage-types.lisp,v 1.65 2008-04-27 19:23:25 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(in-package movitz)
(define-unsigned lu64 8 :little-endian)
(define-bitfield segment-descriptor (lu64)
(((:numeric limit0 16 0))
((:numeric limit1 4 48))
((:numeric base0 24 16))
((:numeric base1 8 56))
((:numeric type 4 40))
((:numeric dpl 2 45))
((:bits)
s 44
p 47
avl 52
reserved 53
d/b 54
g 55)))
(defun make-segment-descriptor (&key (limit 0) (base 0) (type 0) (dpl 0) (flags nil))
(check-type limit (unsigned-byte 20))
(check-type base (unsigned-byte 32))
`((limit0 . ,(ldb (byte 16 0) limit))
(limit1 . ,(ldb (byte 4 16) limit))
(base0 . ,(ldb (byte 24 0) base))
(base1 . ,(ldb (byte 8 24) base))
(type . ,type)
(dpl . ,dpl)
,@flags))
(defmacro with-image-stream-position-remembered (opts &body body)
(declare (ignore opts))
(let ((v (gensym)))
`(let ((,v (file-position (image-stream *image*))))
(unwind-protect (progn ,@body)
(file-position (image-stream *image*) ,v)))))
(define-enum other-type-byte (u8)
:fixnum 0
:even-fixnum 0
:odd-fixnum 4
:cons 1
:character 2
:tag0 0
:tag1 1
:tag2 2
:tag3 3 ; unused
:tag4 4
:tag5 5
:tag6 6
:tag7 7
;; :immediate 4
:null 5
:other 6
:symbol 7
;; The lower 2 bits of these are significant in mysterious ways.
;; 10: Requires GC parsing.
;; 00: Requires no GC parsing (all GC-safe lisp-vals).
;; 11: Illegal/special values
:basic-vector #x22
:defstruct #x2a
:basic-restart #x32
:funobj #x3a
:bignum #x4a
:ratio #x52
:complex #x5a
:std-instance #x40
:run-time-context #x62
:illegal #x13
:infant-object #x23)
(defconstant +fixnum-tags+ '(:even-fixnum :odd-fixnum))
(defparameter +scan-skip-word+ #x00000003)
(defun tag (type &optional (wide-tag 0))
(logior (bt:enum-value 'other-type-byte type)
(ash wide-tag 8)))
(defun tag-name (number)
(find number '(:even-fixnum :odd-fixnum :cons :character :null :other :symbol)
:key 'tag))
(defun extract-tag (word)
(tag-name (ldb (byte 3 0) word)))
(defun extract-pointer (word)
(logand word #xfffffff8))
(defun slot-map (type &optional (offset 0))
(let ((slots (binary-record-slot-names type)))
(loop for slot in slots
as o = (- (bt:slot-offset type slot) offset)
collect (list (intern (symbol-name slot) :muerte)
(intern (symbol-name (binary-slot-type type slot)) :muerte)
(truncate o 4)
(rem o 4)))))
(define-unsigned word 4 :little-endian)
(define-unsigned code-vector-word 4 :little-endian) ; A word that points to a code-vector, +2
(define-unsigned code-pointer 4 :little-endian) ; A pointer anywhere, pointing to code.
(defclass movitz-object ()
((browsed-slots
:initarg :browser-properties
:initform nil
:accessor movitz-object-browser-properties)))
(defclass movitz-immediate-object (movitz-object) ())
(defclass movitz-heap-object (movitz-object)
((word
:accessor movitz-heap-object-word)))
(defclass movitz-heap-object-other (movitz-heap-object) ())
(defmethod movitz-object-offset ((obj movitz-heap-object-other)) 6)
(defmethod movitz-storage-alignment ((obj movitz-heap-object)) 8)
(defmethod movitz-storage-alignment-offset ((obj movitz-heap-object)) 0)
;;;
(defgeneric movitz-references (obj)
(:documentation "Return the objects referenced by OBJ."))
(defmethod movitz-references (obj)
(mapcar #'(lambda (slot)
(slot-value obj slot))
(binary-record-slot-names (find-binary-type (type-of obj)))))
(defmethod movitz-intern ((obj movitz-heap-object) &optional type)
(declare (ignore type))
(image-intern-object *image* obj))
(defmethod movitz-intern ((obj movitz-immediate-object) &optional type)
(declare (ignore type))
(movitz-immediate-value obj))
(defun movitz-read-and-intern (expr type)
(ecase type
(word
(cond
((typep expr 'movitz-object)
(movitz-intern expr))
(t (movitz-intern (movitz-read expr)))))
(code-vector-word
(movitz-intern-code-vector expr))))
(defmethod update-movitz-object ((obj movitz-heap-object) lisp-obj)
(declare (ignore lisp-obj))
(break "Don't know how to update ~W." obj))
(defmethod update-movitz-object ((obj movitz-immediate-object) lisp-obj)
(declare (ignore lisp-obj))
(values))
;;; Fixnums
(eval-when (:compile-toplevel :execute :load-toplevel)
(defparameter +movitz-fixnum-bits+ 30)
(defparameter +movitz-fixnum-shift+ (- 32 +movitz-fixnum-bits+))
(defparameter +movitz-fixnum-factor+ (expt 2 +movitz-fixnum-shift+))
(defparameter +movitz-fixnum-zmask+ (1- +movitz-fixnum-factor+))
(defparameter +movitz-most-positive-fixnum+ (1- (expt 2 (1- +movitz-fixnum-bits+))))
(defparameter +movitz-most-negative-fixnum+ (- (expt 2 (1- +movitz-fixnum-bits+))))
(defparameter +object-pointer-shift+ 0)
(defparameter +other-type-offset+ (- -6 +object-pointer-shift+)))
(defun fixnum-integer (word)
"For a Movitz word, that must be a fixnum, return the corresponding
integer (native lisp) value."
(assert (member (extract-tag word) +fixnum-tags+) (word)
"The word ~W is not a fixnum." word)
(let ((x (ldb (byte (1- +movitz-fixnum-bits+)
(- 32 +movitz-fixnum-bits+))
word)))
(if (logbitp 31 word)
(- (1+ (logxor x +movitz-most-positive-fixnum+)))
x)))
(define-binary-class movitz-fixnum (movitz-immediate-object)
((value :binary-type word
:initarg :value
:reader movitz-fixnum-value)))
(defmethod print-object ((object movitz-fixnum) stream)
(print-unreadable-object (object stream :type t)
(write (movitz-fixnum-value object) :stream stream))
object)
(defun make-movitz-fixnum (value)
(check-type value (signed-byte #.+movitz-fixnum-bits+))
(make-instance 'movitz-fixnum :value value))
(defmethod movitz-immediate-value ((obj movitz-fixnum))
(dpb (movitz-fixnum-value obj)
(byte +movitz-fixnum-bits+ (- 32 +movitz-fixnum-bits+))
0))
(defclass movitz-unboxed-integer (movitz-immediate-object) ())
(defclass movitz-unboxed-integer-u8 (movitz-unboxed-integer) ())
(defclass movitz-unboxed-integer-u32 (movitz-unboxed-integer) ())
;;; Characters
(define-binary-class movitz-character (movitz-immediate-object)
((char :binary-type word
:initarg :char
:type character
:reader movitz-char)))
(defun make-movitz-character (char)
(check-type char character)
(make-instance 'movitz-character :char char))
(defmethod movitz-immediate-value ((obj movitz-character))
(dpb (char-code (movitz-char obj))
(byte 8 8)
(tag :character)))
(defmethod print-object ((x movitz-character) stream)
(print-unreadable-object (x stream)
(format stream "MOVITZ-CHARACTER: ~S" (movitz-char x))))
;;; Code element
(define-binary-class movitz-code (movitz-immediate-object)
((byte :binary-type (define-unsigned code 1)
:reader movitz-code-byte
:initarg byte)))
(defun make-movitz-code (byte)
(make-instance 'movitz-code 'byte byte))
;;; Conses
(define-binary-class movitz-cons (movitz-heap-object)
((car :binary-type word
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word
:initarg :car
:accessor movitz-car)
(cdr :binary-type word
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word
:initarg :cdr
:accessor movitz-cdr))
(:slot-align car #.(- -1 +object-pointer-shift+)))
(defmethod movitz-object-offset ((obj movitz-cons)) 1)
(defmethod update-movitz-object ((movitz-cons movitz-cons) (lisp-cons cons))
(setf (movitz-car movitz-cons) (movitz-read (car lisp-cons))
(movitz-cdr movitz-cons) (movitz-read (cdr lisp-cons))))
(defun make-movitz-cons (car cdr)
(check-type car movitz-object)
(check-type cdr movitz-object)
(make-instance 'movitz-cons
:car car
:cdr cdr))
(defun print-cons (ic stream)
(typecase (movitz-cdr ic)
(movitz-null (format stream "~A" (movitz-car ic)))
(movitz-cons (format stream "~A " (movitz-car ic)))
(t (format stream "~A . ~A" (movitz-car ic) (movitz-cdr ic)))))
(defun movitz-list-length (x)
(etypecase x
(list (list-length x))
(movitz-null 0)
(movitz-cons
(flet ((movitz-endp (x) (eq x *movitz-nil*)))
(do ((n 0 (+ n 2)) ;Counter.
(fast x (movitz-cdr (movitz-cdr fast))) ;Fast pointer: leaps by 2.
(slow x (movitz-cdr slow))) ;Slow pointer: leaps by 1.
(nil)
;; If fast pointer hits the end, return the count.
(when (movitz-endp fast) (return n))
(when (movitz-endp (movitz-cdr fast)) (return (+ n 1)))
;; If fast pointer eventually equals slow pointer,
;; then we must be stuck in a circular list.
;; (A deeper property is the converse: if we are
;; stuck in a circular list, then eventually the
;; fast pointer will equal the slow pointer.
;; That fact justifies this implementation.)
(when (and (eq fast slow) (> n 0))
(warn "Circular list: ~S" x)
(return nil)))))))
(defmethod print-object ((obj movitz-cons) stream)
(format stream "#&(")
(loop for ic = obj then (movitz-cdr ic) as i from 0 to (or *print-length* 100)
while (typep ic 'movitz-cons)
do (print-cons ic stream)
finally (if (>= i 16)
(format stream "...)")
(format stream ")")))
obj)
(defun movitz-nthcdr (n movitz-list)
(if (zerop n)
movitz-list
(movitz-nthcdr (1- n) (movitz-cdr movitz-list))))
(defun (setf movitz-last-cdr) (value movitz-list)
(if (not (typep (movitz-cdr movitz-list) 'movitz-cons))
(setf (movitz-cdr movitz-list) value)
(setf (movitz-last-cdr (movitz-cdr movitz-list)) value)))
;;; movitz-vectors
(define-binary-class movitz-basic-vector (movitz-heap-object-other)
((type
:binary-type other-type-byte
:reader movitz-vector-type
:initform :basic-vector)
(element-type
:binary-type (define-enum movitz-vector-element-type (u8)
:any-t 0
:character 1
:u8 2
:u16 3
:u32 4
:stack 5
:bit 6
:code 7
:indirects 8)
:initarg :element-type
:reader movitz-vector-element-type)
(fill-pointer
:binary-type lu16
:initarg :fill-pointer
:accessor movitz-vector-fill-pointer
:map-binary-write (lambda (x &optional type)
(declare (ignore type))
(check-type x (unsigned-byte 14))
(* x 4))
:map-binary-read (lambda (x &optional type)
(declare (ignore type))
(assert (zerop (mod x 4)))
(truncate x 4)))
(num-elements
:binary-type word
:initarg :num-elements
:reader movitz-vector-num-elements
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word-and-print)
(data
:binary-lisp-type :label) ; data follows physically here
(symbolic-data
:initarg :symbolic-data
:initform nil
:accessor movitz-vector-symbolic-data))
(:slot-align type #.+other-type-offset+))
(defmethod print-object ((object movitz-basic-vector) stream)
(cond
((eq :character (movitz-vector-element-type object))
(print-unreadable-object (object stream :type t :identity nil)
(write (map 'string #'identity (movitz-vector-symbolic-data object))
:stream stream))
object)
(t (call-next-method))))
(defun basic-vector-type-tag (element-type)
(dpb (enum-value 'movitz-vector-element-type element-type)
(byte 8 8)
(enum-value 'other-type-byte :basic-vector)))
(defun movitz-type-word-size (type)
"What's the size of TYPE in words?"
(truncate (sizeof (intern (symbol-name type) :movitz)) 4))
(defun movitz-svref (vector index)
(elt (movitz-vector-symbolic-data vector) index))
(defun movitz-vector-element-type-size (element-type)
(ecase element-type
((:any-t :u32 :stack) 32)
((:character :u8 :code) 8)
(:u16 16)
(:bit 1)))
(defmethod update-movitz-object ((movitz-vector movitz-basic-vector) (vector vector))
(when (eq :any-t (movitz-vector-element-type movitz-vector))
(loop for i from 0 below (length vector)
do (setf (aref (movitz-vector-symbolic-data movitz-vector) i)
(movitz-read (aref vector i)))))
(values))
(defmethod write-binary-record ((obj movitz-basic-vector) stream)
(flet ((write-element (type stream data)
(ecase type
((:u8 :code)(write-binary 'u8 stream data))
(:u16 (write-binary 'u16 stream data))
(:u32 (write-binary 'u32 stream data))
(:character (write-binary 'char8 stream data))
(:any-t (write-binary 'word stream (movitz-read-and-intern data 'word))))))
(+ (call-next-method) ; header
(multiple-value-bind (data type)
(case (movitz-vector-element-type obj)
(:bit (let ((data (movitz-vector-symbolic-data obj)))
(values (loop for byte upfrom 0 below (ceiling (length data) 8)
collect (loop for bit from 0 to 7
sum (* (let ((b (+ (* byte 8) bit)))
(if (< b (length data))
(bit data b)
0))
(expt 2 bit))))
:u8)))
(t (values (movitz-vector-symbolic-data obj)
(movitz-vector-element-type obj))))
(etypecase data
(list
(loop for datum in data
sum (write-element type stream datum)))
(vector
(loop for datum across data
sum (write-element type stream datum))))))))
(defmethod read-binary-record ((type-name (eql 'movitz-basic-vector)) stream &key &allow-other-keys)
(let ((object (call-next-method)))
(setf (movitz-vector-symbolic-data object)
(loop for i from 1 to (movitz-vector-num-elements object)
collecting
(ecase (movitz-vector-element-type object)
((:u8 :code)(read-binary 'u8 stream))
(:u16 (read-binary 'u16 stream))
(:u32 (read-binary 'u32 stream))
(:character (read-binary 'char8 stream))
(:any-t (let ((word (read-binary 'word stream)))
(with-image-stream-position-remembered ()
(movitz-word word)))))))
object))
(defmethod sizeof ((object movitz-basic-vector))
(+ (call-next-method)
(ceiling (* (movitz-vector-element-type-size (slot-value object 'element-type))
(slot-value object 'num-elements))
8)))
(defun movitz-vector-upgrade-type (type)
(cond
((eq type 'code)
(values :code 0))
((subtypep type 'bit)
(values :bit 0))
((subtypep type '(unsigned-byte 8))
(values :u8 0))
((subtypep type '(unsigned-byte 16))
(values :u16 0))
((subtypep type '(unsigned-byte 32))
(values :u32 0))
((subtypep type 'character)
(values :character #\null))
(t (values :any-t nil)))
#+ignore (case type
(movitz-unboxed-integer-u8
(values :u8 0))
(movitz-unboxed-integer-u32
(values :u32 0))
(movitz-character
(values :character #\null))
(movitz-code
(values :code 0))
(t (values :any-t nil))))
(defun make-movitz-vector (size &key (element-type t)
(initial-contents nil)
(initial-element *movitz-nil* initial-element-p)
(alignment 8)
(alignment-offset 0)
(flags nil)
fill-pointer)
(assert (or (null initial-contents)
(= size (length initial-contents))) (size initial-contents)
"The initial-contents must be the same length as SIZE.")
;;; (assert (subtypep element-type 'movitz-object) ()
;;; "ELEMENT-TYPE must be a subtype of MOVITZ-OBJECT.")
;;; (assert (or initial-contents
;;; (not initial-element-p)
;;; (typep initial-element element-type)) ()
;;; "INITIAL-ELEMENT's type ~A is not of ELEMENT-TYPE ~A."
;;; (type-of initial-element) element-type)
(assert (and (>= (log alignment 2) 3)
(zerop (rem (log alignment 2) 1)))
(alignment)
"Illegal alignment: ~A." alignment)
(multiple-value-bind (et default-element)
(movitz-vector-upgrade-type element-type)
(when initial-element-p
(assert (not initial-contents) ()
"Can't provide both initial-element and initial-contents."))
(unless initial-contents
(setf initial-contents
(make-array size :initial-element (or (and initial-element-p initial-element)
default-element))))
(assert (member et '(:any-t :bit :character :u8 :u32 :code)))
(when flags (break "flags: ~S" flags))
(when (and alignment-offset (plusp alignment-offset))
(break "alignment: ~S" alignment-offset))
(make-instance 'movitz-basic-vector
:element-type et
:num-elements size
:symbolic-data (case et
(:any-t
(map 'vector #'movitz-read initial-contents))
(t initial-contents))
:fill-pointer (cond
((not (typep size '(unsigned-byte 14)))
0)
((integerp fill-pointer)
fill-pointer)
(t size)))))
(defun make-movitz-string (string)
(make-movitz-vector (length string)
:element-type 'character
:initial-contents (map 'list #'identity string)))
(defun movitz-stringp (x)
(and (typep x '(or movitz-basic-vector))
(eq :character (movitz-vector-element-type x))))
(deftype movitz-string ()
'(satisfies movitz-stringp))
;;;
(define-binary-class movitz-unbound-value (movitz-immediate-object)
())
(defmethod movitz-intern ((obj movitz-unbound-value) &optional type)
(declare (ignore type))
#x7fffffff)
;;; Symbols
(define-binary-class movitz-symbol (movitz-heap-object)
((function-value
:binary-type word
:accessor movitz-symbol-function-value
:map-binary-write 'movitz-read-and-intern-function-value
:map-binary-read-delayed 'movitz-word
:initarg :function-value
:initform 'muerte::unbound-function)
(value
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:initform 'unbound
:accessor movitz-symbol-value
:initarg :value)
(plist
:binary-type word
:accessor movitz-plist
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:initform nil
:initarg :plist)
(name
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:initarg :name
:accessor movitz-symbol-name)
(package
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:initform nil
:accessor movitz-symbol-package)
(flags
:binary-type (define-bitfield movitz-symbol-flags (lu16)
(((:bits)
:special-variable 3
:constant-variable 4
:setf-placeholder 5)))
:accessor movitz-symbol-flags
:initarg :flags
:initform nil)
(hash-key
:binary-lisp-type lu16
:reader movitz-symbol-hash-key
:initarg :hash-key)
(lisp-symbol
:initform nil
:initarg :lisp-symbol))
(:slot-align function-value -7))
#+ignore
(defmethod write-binary-record :before ((obj movitz-symbol) stream)
(declare (ignore stream))
(setf (movitz-plist obj)
(movitz-read
(translate-program (translate-program (getf (movitz-environment-plists *movitz-global-environment*)
(slot-value obj 'lisp-symbol))
:cl :muerte.cl)
:movitz :muerte))))
(defmethod movitz-object-offset ((obj movitz-symbol)) 7)
(defmethod update-movitz-object ((movitz-symbol movitz-symbol) (symbol symbol))
(setf ;; (movitz-plist movitz-symbol) (movitz-read (symbol-plist symbol))
(movitz-symbol-name movitz-symbol) (movitz-read (symbol-name symbol)))
(values))
(defun make-movitz-symbol (name)
(let ((name-string (image-read-intern-constant *image* (symbol-name name))))
(make-instance 'movitz-symbol
:hash-key (movitz-sxhash name-string)
:name name-string
:lisp-symbol name)))
(defmethod print-object ((object movitz-symbol) stream)
(typecase (movitz-symbol-name object)
(movitz-basic-vector
(print-unreadable-object (object stream :type 'movitz-symbol)
(format stream "|~A|"
(map 'string #'identity
(slot-value (slot-value object 'name) 'symbolic-data))))
object)
(t (call-next-method))))
(defun movitz-read-and-intern-function-value (obj type)
(assert (eq type 'word))
(cond
((typep obj 'movitz-funobj)
(movitz-intern obj))
((symbolp obj)
(let ((x (movitz-env-named-function obj)))
(check-type x movitz-funobj)
(movitz-intern x)))
(t (error "Illegal function value: ~S." obj))))
;;; NIL
(define-binary-class movitz-null (movitz-symbol) ())
(defun make-movitz-nil ()
(make-instance 'movitz-null
:name (symbol-name nil)
:value nil
:plist nil
:hash-key 0
:flags '(:constant-variable)))
(defmethod movitz-intern ((object movitz-null) &optional (type 'word))
(assert (eq 'word type))
(image-nil-word *image*))
(defun movitz-null (x)
(typep x 'movitz-null))
(deftype movitz-list ()
`(or movitz-cons movitz-null))
;;; Compiled funobj
(define-binary-class movitz-funobj (movitz-heap-object-other)
((type
:binary-type other-type-byte
:initform :funobj)
(funobj-type
:binary-type (define-enum movitz-funobj-type (u8)
:standard-function 0
:generic-function 1
:method-function 2
:macro-function 3)
:initform :standard-function
:accessor movitz-funobj-type)
(debug-info
;; Bits 0-4: The value of the start-stack-frame-setup label.
;; Bit 5: The code-vector's uses-stack-frame-p.
:binary-type 'lu16
:initform 0)
(code-vector
:binary-type code-vector-word
:initform 'muerte::no-code-vector
:initarg :code-vector
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:accessor movitz-funobj-code-vector)
(code-vector%1op
:binary-type code-pointer
:initform 'muerte::trampoline-funcall%1op
:initarg :code-vector%1op
:map-binary-write 'movitz-intern-code-vector
:accessor movitz-funobj-code-vector%1op)
(code-vector%2op
:binary-type code-pointer
:initform 'muerte::trampoline-funcall%2op
:initarg :code-vector%2op
:map-binary-write 'movitz-intern-code-vector
:accessor movitz-funobj-code-vector%2op)
(code-vector%3op
:binary-type code-pointer
:initform 'muerte::trampoline-funcall%3op
:initarg :code-vector%3op
:map-binary-write 'movitz-intern-code-vector
:accessor movitz-funobj-code-vector%3op)
(lambda-list
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:reader movitz-funobj-lambda-list
:initarg :lambda-list)
(name
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:accessor movitz-funobj-name
:initarg :name)
(num-jumpers ; how many of the first constants are jumpers.
:binary-type lu16 ; 14 bits, the lower 16 bits of a fixnum.
:initform 0 ; This, in order to see this as a fixnum while
:accessor movitz-funobj-num-jumpers ; GC scanning.
:initarg :num-jumpers
:map-binary-write (lambda (x &optional type)
(declare (ignore type))
(check-type x (unsigned-byte 14))
(* x +movitz-fixnum-factor+))
:map-binary-read (lambda (x &optional type)
(declare (ignore type))
(assert (zerop (ldb (byte 2 0) x)))
(/ x +movitz-fixnum-factor+)))
(num-constants
:binary-type lu16
:initform 0
:initarg :num-constants
:accessor movitz-funobj-num-constants)
;; The funobj's constants follow here..
(constant0
:binary-type :label)
;; A standard-generic-function will have three constants:
;; The class, the slots, and the discriminating-function.
(const-list ;
;; :initform ()
:initarg :const-list
:accessor movitz-funobj-const-list)
(jumpers-map
:initarg :jumpers-map
:accessor movitz-funobj-jumpers-map)
(symbolic-name
:initarg :symbolic-name
:accessor movitz-funobj-symbolic-name)
(symbolic-code
:initarg :symbolic-code
:accessor movitz-funobj-symbolic-code)
(symtab
:initform nil
:accessor movitz-funobj-symtab)
(borrowed-bindings
:initarg :borrowed-bindings
:initform nil
:accessor borrowed-bindings)
(function-envs
:accessor function-envs)
(funobj-env
:initarg :funobj-env
:accessor funobj-env)
(extent
:initarg :extent
:initform :unused
:accessor movitz-funobj-extent)
(allocation
:accessor movitz-allocation)
(usage
:initform nil
:accessor movitz-funobj-usage)
(sub-function-binding-usage ; a plist used during lexical analysis
:initform nil
:accessor sub-function-binding-usage)
(entry-protocol
:initform :default
:initarg :entry-protocol
:reader funobj-entry-protocol)
(headers-on-stack-frame-p
:initform nil
:accessor headers-on-stack-frame-p))
(:slot-align type #.+other-type-offset+))
(defmethod write-binary-record ((obj movitz-funobj) stream)
(declare (special *record-all-funobjs*))
(assert (movitz-funobj-code-vector obj) (obj)
"No code-vector for funobj named ~S." (movitz-funobj-name obj))
#+ignore
(assert (= (movitz-funobj-num-constants obj)
(length (movitz-funobj-const-list obj))))
(+ (call-next-method) ; header
(loop for data in (movitz-funobj-const-list obj)
as pos upfrom 0
summing (if (>= pos (movitz-funobj-num-jumpers obj))
(write-binary 'word stream (movitz-intern data))
(let ((x (cdr (assoc data (movitz-funobj-symtab obj)))))
(assert (integerp x) ()
"Unable to resolve jumper ~S." data)
(write-binary 'u32 stream
(+ x (movitz-intern-code-vector (movitz-funobj-code-vector obj)))))))))
(defmethod print-object ((object movitz-funobj) stream)
(print-unreadable-object (object stream :type t :identity t)
(write (movitz-print (movitz-funobj-name object)) :stream stream)))
(defmethod sizeof ((obj movitz-funobj))
(+ (sizeof (find-binary-type 'movitz-funobj))
(* (movitz-funobj-num-constants obj)
(sizeof 'word))))
(defun make-movitz-funobj (lambda-list &key (name ""))
(check-type name (or symbol cons))
(make-instance 'movitz-funobj
:lambda-list lambda-list
:name name))
(defun funobj-name (x)
(typecase x
(movitz-funobj
(movitz-funobj-name x))))
;;;
(define-binary-class movitz-funobj-standard-gf (movitz-funobj)
;; This class is binary congruent with movitz-funobj.
((type
:binary-type other-type-byte)
(funobj-type
:binary-type movitz-funobj-type
:initform :generic-function)
(debug-info
;; Bits 0-4: The value of the start-stack-frame-setup label.
:binary-type 'lu16
:initform 0)
(code-vector
:binary-type code-vector-word
:initform 'muerte::standard-gf-dispatcher
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector)
(code-vector%1op
:initform 'muerte::standard-gf-dispatcher%1op
:binary-type code-pointer
:map-binary-write 'movitz-intern-code-vector)
(code-vector%2op
:initform 'muerte::standard-gf-dispatcher%2op
:binary-type code-pointer
:map-binary-write 'movitz-intern-code-vector)
(code-vector%3op
:initform 'muerte::standard-gf-dispatcher%3op
:binary-type code-pointer
:map-binary-write 'movitz-intern-code-vector)
(lambda-list
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
(name
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
(num-jumpers
:binary-type lu16
:initform 0
:accessor movitz-funobj-num-jumpers
:map-binary-write (lambda (x &optional type)
(declare (ignore type))
(check-type x (unsigned-byte 14))
(* x +movitz-fixnum-factor+))
:map-binary-read (lambda (x &optional type)
(declare (ignore type))
(assert (zerop (ldb (byte 2 0) x)))
(/ x +movitz-fixnum-factor+)))
(num-constants
:binary-type lu16
:initform (/ (- (sizeof 'movitz-funobj-standard-gf)
(sizeof 'movitz-funobj))
4)) ; XXXXXXX MUST MATCH NUMBER OF WORDS BELOW XXXXXXXXXXX
(standard-gf-function ; a movitz-funobj which is called by dispatcher (in code-vector)
:accessor standard-gf-function
:initarg :function
:initform 'muerte::unbound-function
:binary-type word
:map-binary-write 'movitz-read-and-intern-function-value)
(num-required-arguments
:initarg :num-required-arguments
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word-and-print)
(classes-to-emf-table
:initarg :classes-to-emf-table
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word-and-print)
(eql-specializer-table
:initform nil
:initarg :eql-specializer-table
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word-and-print)
(standard-gf-class
:accessor standard-gf-class
:initarg :class
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
(standard-gf-slots
:accessor standard-gf-slots
:initarg :slots
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
(plist
:initform nil))
(:slot-align type #.+other-type-offset+))
(defmethod movitz-funobj-const-list ((funobj movitz-funobj-standard-gf))
nil)
(defun make-standard-gf (class slots &key lambda-list (name "unnamed")
(function 'muerte::unbound-function)
num-required-arguments
classes-to-emf-table)
(make-instance 'movitz-funobj-standard-gf
:lambda-list lambda-list
:name name
:class class
:slots slots
:function function
:num-required-arguments num-required-arguments
:classes-to-emf-table classes-to-emf-table))
;;;
(define-binary-class movitz-struct (movitz-heap-object-other)
((type
:binary-type other-type-byte
:initform :defstruct)
(pad :binary-lisp-type 1)
(length
:binary-type lu16
:initarg :length
:accessor movitz-struct-length
:map-binary-write (lambda (x &optional type)
(declare (ignore type))
(check-type x (unsigned-byte 14))
(* x 4))
:map-binary-read (lambda (x &optional type)
(declare (ignore type))
(assert (zerop (mod x 4)))
(truncate x 4)))
(class
:binary-type word
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word
:reader movitz-struct-class
:initarg :class)
(slot0 :binary-lisp-type :label) ; the slot values follows here.
(slot-values
:initform '()
:initarg :slot-values
:accessor movitz-struct-slot-values))
(:slot-align type #.+other-type-offset+))
(defmethod update-movitz-object ((movitz-struct movitz-struct) lisp-struct)
(declare (ignore lisp-struct))
(values))
(defmethod sizeof ((obj movitz-struct))
(+ (sizeof 'movitz-struct)
(* 4 (length (movitz-struct-slot-values obj)))))
(defmethod write-binary-record ((obj movitz-struct) stream)
(+ (call-next-method) ; header
(loop for slot-value in (movitz-struct-slot-values obj)
for slot-word = (movitz-read-and-intern slot-value 'word)
summing (write-binary 'word stream slot-word))))
(defmethod read-binary-record ((type-name (eql 'movitz-struct)) stream &key)
(let ((object (call-next-method)))
(setf (movitz-struct-slot-values object)
(loop for i from 1 to (movitz-struct-length object)
collect
(let ((word (read-binary 'word stream)))
(with-image-stream-position-remembered ()