-
Notifications
You must be signed in to change notification settings - Fork 18
/
image.lisp
1934 lines (1838 loc) · 74.8 KB
/
image.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) 2001,2000, 2002-2005,
;;;; Department of Computer Science, University of Tromso, Norway.
;;;;
;;;; Filename: image.lisp
;;;; Description: Construction of Movitz images.
;;;; Author: Frode Vatvedt Fjeld <[email protected]>
;;;; Created at: Sun Oct 22 00:22:43 2000
;;;; Distribution: See the accompanying file COPYING.
;;;;
;;;; $Id: image.lisp,v 1.126 2008-07-18 13:15:40 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(in-package movitz)
(define-binary-class movitz-run-time-context (movitz-heap-object)
((run-time-context-start :binary-type :label) ; keep this at the top.
(type
:binary-type other-type-byte
:initform :run-time-context)
(padding
:binary-type 3)
(atomically-continuation
:binary-type lu32
:initform 0)
(raw-scratch0 ; A non-GC-root scratch register
:binary-type lu32
:initform 0)
(pointer-start :binary-type :label)
(ret-trampoline
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-commit
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-non-pointer
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-commit-non-pointer
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-non-header
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-commit-non-header
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(cons-pointer
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
;; tag-specific class-of primitive-functions
(fast-class-of :binary-type :label)
(fast-class-of-even-fixnum ; 0000
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-cons ; 1111
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-character ; 2222
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-tag3 ; 3333
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-odd-fixnum ; 4444
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-null ; 5555
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-other ; 6666
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-class-of-symbol ; 7777
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(keyword-search
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function
:binary-type code-vector-word)
(decode-keyargs-default
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function
:binary-type code-vector-word)
(decode-keyargs-foo
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function
:binary-type code-vector-word)
(fast-car
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-cdr
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-cddr
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-car-ebx
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-cdr-ebx
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
;; primitive functions global constants
(pop-current-values
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(dynamic-variable-lookup
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(dynamic-variable-store
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(dynamic-unwind-next
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(dynamic-variable-install
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(dynamic-variable-uninstall
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(assert-1arg
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(assert-2args
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(assert-3args
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(decode-args-1or2
:map-binary-write 'movitz-intern-code-vector
:binary-tag :primitive-function
:map-binary-read-delayed 'movitz-word-code-vector
:binary-type code-vector-word)
(box-u32-ecx
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(unbox-u32
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-cdr-car
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-cons
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(ensure-heap-cons-variable
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-compare-two-reals
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-compare-fixnum-real
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(fast-compare-real-fixnum
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(trampoline-cl-dispatch-1or2
:binary-type code-vector-word
:initform nil
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(dynamic-jump-next
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
(copy-funobj-code-vector-slots
:binary-type code-vector-word
:map-binary-write 'movitz-intern-code-vector
:map-binary-read-delayed 'movitz-word-code-vector
:binary-tag :primitive-function)
;;
(boolean-one :binary-type :label)
(not-nil ; not-nil, t-symbol and not-not-nil must be consecutive.
:binary-type word
:initform nil
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
(boolean-zero :binary-type :label)
(t-symbol
:binary-type word
:initarg :t-symbol
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word)
(not-not-nil
:binary-type word
:initform nil
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word)
;; (null-cons :binary-type :label)
(null-symbol
:binary-type movitz-symbol
:reader movitz-run-time-context-null-symbol
:initarg :null-symbol)
(complicated-eql
:initform 'muerte::complicated-eql
:binary-type word
:binary-tag :global-function
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word)
(complicated-compare
:binary-type word
:binary-tag :global-function
:map-binary-read-delayed 'movitz-word
:map-binary-write 'movitz-intern)
(dynamic-env
:binary-type word
:initform 0)
(scratch1
:binary-type word
:initform 0)
(scratch2
:binary-type word
:initform 0)
(class
:binary-type word
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word
:initarg :class
:accessor run-time-context-class)
(slots
:binary-type word
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed 'movitz-word
:initarg :slots
:initform #(:init nil)
:accessor run-time-context-slots)
(unwind-protect-tag
:binary-type word
:map-binary-read-delayed 'movitz-word
:map-binary-write 'movitz-read-and-intern
:initform 'muerte::unwind-protect-tag)
(restart-tag
:binary-type word
:map-binary-read-delayed 'movitz-word
:map-binary-write 'movitz-read-and-intern
:initform 'muerte::restart-protect-tag)
(new-unbound-value
:binary-type word
:map-binary-read-delayed 'movitz-word
:map-binary-write 'movitz-read-and-intern
:initform 'unbound)
(stack-bottom ; REMEMBER BOCHS!
:binary-type word
:initform #x0ff000)
(stack-top ; stack-top must be right after stack-bottom
:binary-type word ; in order for the bound instruction to work.
:initform #x100000)
(+
:initform 'muerte.cl:+
:binary-type word
:binary-tag :global-function
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word)
(the-class-t
:binary-type word
:initform t
:map-binary-write (lambda (x type)
(declare (ignore type))
(movitz-read-and-intern (funcall 'muerte::movitz-find-class x)
'word))
:map-binary-read-delayed 'movitz-word)
(copy-funobj
:binary-type word
;; :accessor movitz-run-time-context-copy-funobj
:initform 'muerte::copy-funobj
:map-binary-write (lambda (name type)
(declare (ignore type))
(movitz-intern (movitz-env-named-function name))))
(classes ; A vector of class meta-objects.
:initform nil ; The first element is the map of corresponding names
:binary-type word
:map-binary-write (lambda (x type)
(declare (ignore x type))
(let ((map (image-classes-map *image*)))
(movitz-read-and-intern
(apply #'vector
map
(mapcar (lambda (x)
(funcall 'muerte::movitz-find-class x))
map))
'word)))
:map-binary-read-delayed 'movitz-word)
(physical-address-offset
:binary-type lu32
:initform (image-ds-segment-base *image*))
(nursery-space
:binary-type word
:initform nil
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed (lambda (x type)
(declare (ignore x type))
(movitz-read nil)))
(allow-other-keys-symbol
:binary-type word
:initform :allow-other-keys
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed (lambda (x type)
(declare (ignore x type))
(movitz-read nil)))
(self
:binary-type word
:initform 6
:map-binary-read-delayed 'movitz-word)
(complicated-class-of
:binary-type word
:binary-tag :global-function
:map-binary-read-delayed 'movitz-word
:map-binary-write 'movitz-intern)
(stack-vector
:binary-type word
:initform nil
:map-binary-write 'movitz-read-and-intern
:map-binary-read-delayed (lambda (x type)
(declare (ignore x type))
(movitz-read nil)))
(exception-handlers
:binary-type word
:map-binary-write 'movitz-intern
:map-binary-read-delayed 'movitz-word
:initarg :exception-handlers
:accessor movitz-run-time-context-exception-handlers)
(interrupt-descriptor-table
:binary-type word
:accessor movitz-run-time-context-interrupt-descriptor-table
:initform (make-array 256 :initial-element 'muerte::default-interrupt-trampoline)
:map-binary-read-delayed 'movitz-word
:map-binary-write 'map-interrupt-trampolines-to-idt)
(num-values
:binary-type word ; Fixnum
:initform 0)
(values
:binary-type #.(* 4 +movitz-multiple-values-limit+)))
(:slot-align null-symbol -5))
(defun atomically-continuation-simple-pf (pf-name)
(ldb (byte 32 0) (global-constant-offset pf-name))
#+ignore
(bt:enum-value 'movitz::atomically-status
(list* :restart-primitive-function
(cons :reset-status-p
(if reset-status-p 1 0))
(cons :data
(if (not pf-name)
0
(truncate (+ (tag :null)
(bt:slot-offset 'movitz-run-time-context
(intern (symbol-name pf-name)
:movitz)))
4)))
registers)))
(defun atomically-status-jumper-fn (reset-status-p &rest registers)
(assert (not reset-status-p))
(assert (null registers))
(lambda (jumper)
(assert (= 0 (mod jumper 4)))
(bt:enum-value 'movitz::atomically-status
(list* :restart-jumper
(cons :reset-status-p
(if reset-status-p 1 0))
(cons :data (truncate jumper 4))
registers))))
(defmethod movitz-object-offset ((obj movitz-run-time-context)) 0)
(defun global-constant-offset (slot-name)
(check-type slot-name symbol)
(let ((slot-name (find-symbol (symbol-name slot-name) :movitz)))
(assert slot-name)
(if (not (eq slot-name 'unbound-function))
(slot-offset 'movitz-run-time-context slot-name)
(+ (slot-offset 'movitz-run-time-context 'null-symbol)
(slot-offset 'movitz-symbol 'function-value)
(tag :symbol)))))
(defun make-movitz-run-time-context ()
(make-instance 'movitz-run-time-context
:t-symbol (movitz-read 't)
:null-symbol *movitz-nil*))
(defclass movitz-image ()
((ds-segment-base
:initarg :ds-segment-base
:initform #x100000
:accessor image-ds-segment-base)
(cs-segment-base
:initarg :cs-segment-base
:initform #x100000
:accessor image-cs-segment-base)))
(defclass symbolic-image (movitz-image)
((object-hash
:accessor image-object-hash) ; object => address
(address-hash
:accessor image-address-hash) ; address => object
(cons-pointer
:accessor image-cons-pointer)
(read-map-hash
:initform (make-hash-table :test #'eql) ; lisp object => movitz object
:reader image-read-map-hash)
(inverse-read-map-hash
:initform (make-hash-table :test #'eql) ; lisp object => movitz object
:reader image-inverse-read-map-hash)
(oblist
:reader image-oblist
:initform (make-hash-table :test #'eql))
(global-environment
:initform (make-global-movitz-environment)
:reader image-global-environment)
(struct-slot-descriptions
:initform (make-hash-table :test #'eq)
:accessor image-struct-slot-descriptions)
(start-address
:initarg :start-address
:accessor image-start-address)
(symbol-hash-key-counter
:initform 0
:type unsigned-byte
:accessor image-symbol-hash-key-counter)
(nil-word
:accessor image-nil-word)
(nil-object
:initarg :nil-object
:accessor image-nil-object)
(t-symbol
:accessor image-t-symbol)
(bootblock
:accessor image-bootblock)
(movitz-modules
:initarg :movitz-modules
:initform nil
:accessor image-movitz-modules)
(movitz-features
:initarg :movitz-features
:accessor image-movitz-features)
(called-functions
:initarg :called-functions
:initform nil
:accessor image-called-functions)
(toplevel-funobj
:accessor image-toplevel-funobj)
(run-time-context
:accessor image-run-time-context)
(load-time-funobjs
:initform ()
:accessor image-load-time-funobjs)
(compile-time-variables
:initform ()
:accessor image-compile-time-variables)
(string-constants
:initform (make-hash-table :test #'equal)
:reader image-string-constants)
(cons-constants
:initform (make-hash-table :test #'equal)
:reader image-cons-constants)
(multiboot-header
:accessor image-multiboot-header)
(dump-count
:initform 0
:accessor dump-count)
(function-code-sizes
:initform (make-hash-table :test #'equal)
:initarg :function-code-sizes
:reader function-code-sizes)))
(defmethod image-classes-map ((image symbolic-image))
'(muerte.cl:null muerte.cl:cons muerte.cl:fixnum muerte.cl:symbol
muerte.cl:character muerte.cl:function muerte.cl:condition
muerte.cl:integer muerte.cl:ratio muerte.cl:complex
muerte.cl:vector muerte.cl:string muerte.cl:bit-vector muerte.cl:array
muerte.cl:class muerte.cl:standard-class
muerte.cl:standard-generic-function
muerte:run-time-context
muerte.mop:standard-effective-slot-definition
muerte.mop:funcallable-standard-class
muerte::basic-restart
muerte::illegal-object))
(defun class-object-offset (name)
(let ((name (translate-program name :cl :muerte.cl)))
(+ (bt:slot-offset 'movitz-basic-vector 'data)
(* 4 (1+ (or (position name (image-classes-map *image*))
(error "No class named ~S in class-map." name)))))))
(defun unbound-value ()
(declare (special *image*))
(movitz-read (slot-value (image-run-time-context *image*)
'new-unbound-value)))
(defun edi-offset ()
(declare (special *image*))
(- (image-nil-word *image*)))
(defmethod image-intern-object ((image symbolic-image) object &optional (size (sizeof object)))
(assert ; sanity check on "other" storage-types.
(or (not (typep object 'movitz-heap-object-other))
(and (= (- (tag :other))
(slot-offset (type-of object)
(first (binary-record-slot-names (type-of object)))))
(= +other-type-offset+ (slot-offset (type-of object) 'type))))
()
"The MOVITZ-HEAP-OBJECT-OTHER type ~A is malformed!" (type-of object))
(etypecase object
(movitz-null
(image-nil-word image))
(movitz-heap-object
(+ (movitz-object-offset object)
(or (gethash object (image-object-hash image))
(let* ((alignment (movitz-storage-alignment object))
(new-ptr (if (= (movitz-storage-alignment-offset object)
(mod (image-cons-pointer image)
(movitz-storage-alignment object)))
(image-cons-pointer image)
(+ (image-cons-pointer image)
(mod (- (image-cons-pointer image))
alignment)
(movitz-storage-alignment-offset object)))))
(setf (gethash new-ptr (image-address-hash image)) object
(gethash object (image-object-hash image)) new-ptr
(image-cons-pointer image) (+ new-ptr size))
new-ptr))))))
(defmethod image-memref ((image symbolic-image) address &optional (errorp t))
(let ((obj (gethash address (image-address-hash image) :nothing)))
(cond
((not (typep obj 'movitz-object))
(when errorp
(error "Found non-movitz-object at image-address #x~X: ~A" address obj))
nil)
(t obj))))
(defmethod search-image ((image symbolic-image) address)
(loop for a downfrom (logand address -8) by 8
until (gethash a (image-address-hash image))
finally (let ((object (gethash a (image-address-hash image))))
(when (<= address (+ a (sizeof object)))
;; (warn "Found at ~X: ~S" a (gethash a (image-address-hash image)))
(return object)))))
(defun search-image-funobj (address &optional (*image* *image*))
(search-image-funobj-by-image *image* address))
(defmethod search-image-funobj-by-image ((image symbolic-image) address)
(let ((code-vector (search-image image (1- address))))
(unless (and (typep code-vector 'movitz-basic-vector)
(eq :code (movitz-vector-element-type code-vector)))
(error "Not a code-vector at #x~8,'0X: ~S" address code-vector))
(let ((offset (- address (movitz-intern-code-vector code-vector))))
(assert (not (minusp offset)))
(format t "~&;; Matched code-vector at #x~X with offset ~D.~%"
(image-intern-object image code-vector)
offset))
(with-hash-table-iterator (next-object (image-object-hash *image*))
(loop with more-objects and object
do (multiple-value-setq (more-objects object) (next-object))
while more-objects
when (typecase object
(movitz-funobj
(when (eq code-vector (movitz-funobj-code-vector object))
object))
(movitz-symbol
(when (eq code-vector (movitz-symbol-value object))
(movitz-print object))))
collect it))))
(defun search-primitive-function (address &optional (*image* *image*))
(let ((code-vector (search-image *image* address)))
(unless (and (typep code-vector 'movitz-basic-vector)
(eq :u8 (movitz-vector-element-type code-vector)))
(error "Not a code-vector at #x~8,'0X: ~S" address code-vector))
(format t "~&;; Code vector: #x~X" (movitz-intern code-vector))
(loop for pf-name in (binary-record-slot-names 'movitz-run-time-context
:match-tags :primitive-function)
when (= (movitz-intern-code-vector code-vector)
(binary-slot-value (image-run-time-context *image*) pf-name))
do (format t "~&;; #x~X matches global primitive-function ~W with offset ~D."
address pf-name
(- address (movitz-intern-code-vector code-vector)))
and collect pf-name)))
(defun movitz-word (word &optional (type 'word))
"Return the movitz-object corresponding to (the integer) WORD."
(assert (eq type 'word))
(movitz-word-by-image *image* word))
(defun movitz-word-and-print (word &optional (type 'word))
(movitz-print (movitz-word word type)))
(defmethod movitz-word-by-image ((image symbolic-image) word)
(case (extract-tag word)
(#.+fixnum-tags+
(make-movitz-fixnum
(make-instance 'movitz-fixnum :value (fixnum-integer word))))
(:character
(make-instance 'movitz-character :char (code-char (ldb (byte 8 8) word))))
(:null
(image-nil-word image))
(t (image-memref *image* (logand word #xfffffff8) t))))
(defun movitz-intern-code-vector (object &optional (type 'code-vector-word))
"Four ways to denote a code-vector: a vector is that vector,
a symbol is considered a primitive-function and the symbol-value is used,
a movitz-funobj is that funobj's code-vector,
a cons is an offset (the car) from some other code-vector (the cdr)."
(assert (member type '(code-vector-word code-pointer)))
(etypecase object
((or vector movitz-basic-vector)
(+ 2 (movitz-intern object)))
((or symbol movitz-symbol)
(let ((primitive-code-vector (movitz-symbol-value (movitz-read object))))
(check-type primitive-code-vector movitz-basic-vector)
(movitz-intern-code-vector primitive-code-vector type)))
(movitz-funobj
(movitz-intern-code-vector (movitz-funobj-code-vector object) type))
(cons
;; a cons denotes an offset (car) from some funobj's (cdr) code-vector.
(check-type (car object) integer)
(check-type (cdr object) movitz-funobj)
(+ (car object) (movitz-intern-code-vector (cdr object) type)))))
(defun movitz-intern-global-function (object &optional (type 'word))
(assert (eq type 'word))
(check-type object symbol)
(let ((x (movitz-env-named-function object)))
(check-type x movitz-funobj)
(movitz-intern x 'word)))
(defun movitz-word-code-vector (word &optional (type 'code-vector-word))
(assert (eq type 'code-vector-word))
(movitz-word (- word +code-vector-word-offset+)))
(defun copy-hash-table (x)
(let ((y (make-hash-table :test (hash-table-test x))))
(maphash (lambda (k v)
(setf (gethash k y) v))
x)
y))
(defun make-initial-segment-descriptor-table ()
(let ((u32-list
(let ((bt:*endian* :little-endian))
(merge-bytes (with-binary-output-to-list (octet-list)
(mapcar (lambda (init-args)
(write-binary 'segment-descriptor octet-list
(apply #'make-segment-descriptor init-args)))
`(() ; 0
(:base 0 :limit #xfffff ; 1: physical code
:type 14 :dpl 0 :flags (s p d/b g))
(:base 0 :limit #xfffff ; 2: physical data
:type 2 :dpl 3 :flags (s p d/b g))
(:base ,(image-cs-segment-base *image*) ; 3: logical code
:limit #xfff00
:type 14 :dpl 0 :flags (s p d/b g))
(:base ,(image-ds-segment-base *image*) ; 4: logical data
:limit #xfff00
:type 2 :dpl 0 :flags (s p d/b g))
)))
8 32))))
(movitz-read (make-movitz-vector (length u32-list)
:initial-contents u32-list
:element-type '(unsigned-byte 32)))))
(defun make-movitz-image (&rest init-args &key start-address &allow-other-keys)
(let ((*image* (apply #'make-instance 'symbolic-image
:nil-object (make-movitz-nil)
:start-address start-address
:movitz-features '(:movitz)
:function-code-sizes
(if *image*
(copy-hash-table (function-code-sizes *image*))
(make-hash-table :test #'equal))
init-args)))
(setf (image-nil-word *image*)
(+ 5 (- (slot-offset 'movitz-run-time-context 'null-symbol)
(slot-offset 'movitz-run-time-context 'run-time-context-start))
(- start-address
(image-ds-segment-base *image*))))
(format t "~&;; NIL value: #x~X.~%" (image-nil-word *image*))
(assert (eq :null (extract-tag (image-nil-word *image*))) ()
"NIL value #x~X has tag ~D, but it must be ~D."
(image-nil-word *image*)
(ldb (byte 3 0) (image-nil-word *image*))
(tag :null))
(setf (image-run-time-context *image*) (make-movitz-run-time-context))
(setf (image-t-symbol *image*) (movitz-read t))
;; (warn "NIL value: #x~X" (image-nil-word *image*))
*image*))
(defun find-primitive-function (name)
"Given the NAME of a primitive function, look up
that function's code-vector."
(let ((code-vector
(movitz-symbol-value (movitz-read name))))
(unless (and code-vector (not (eq 'unbound code-vector)))
(cerror "Install an empty vector instead."
"Global constant primitive function ~S is not defined!" name)
(setf code-vector
(setf (movitz-symbol-value (movitz-read name))
(movitz-read #()))))
(check-type code-vector movitz-basic-vector)
code-vector))
(defun create-image (&rest init-args
&key (init-file *default-image-init-file*)
(gc t)
;; (start-address #x100000)
&allow-other-keys)
(psetq *image* (let ((*image* (apply #'make-movitz-image
:start-address #x100000
init-args)))
(when init-file
(movitz-compile-file init-file))
*image*)
*i* *image*)
(when gc
#+allegro (setf (sys:gsgc-parameter :generation-spread) 8)
#+allegro (excl:gc :tenure)
#+allegro (excl:gc t)) ; We just thrashed a lot of tenured objects.
*image*)
(defun set-file-position (stream position &optional who)
(declare (ignore who))
(or (ignore-errors (file-position stream position))
(let* ((end (file-position stream :end))
(diff (- position end)))
(dotimes (i diff)
(write-byte 0 stream))
(assert (= position (file-position stream)))))
(values))
(defun dump-image (&key (path *default-image-file*) ((:image *image*) *image*)
(multiboot-p t) ignore-dump-count (qemu-align :floppy))
"When <multiboot-p> is true, include a MultiBoot-compliant header in the image."
(when (and (not ignore-dump-count)
(= 0 (dump-count *image*)))
;; This is a hack to deal with the fact that the first dump won't work
;; because the packages aren't properly set up.
(format t "~&;; Doing initiating dump..")
(dump-image :path path :multiboot-p multiboot-p :ignore-dump-count t
:qemu-align nil)
(assert (plusp (dump-count *image*))))
(setf (movitz-symbol-value (movitz-read 'muerte:*build-number*))
(1+ *bootblock-build*))
(when (eq 'unbound (movitz-symbol-value (movitz-read 'muerte::*initial-segment-descriptor-table*)))
(setf (movitz-symbol-value (movitz-read 'muerte::*initial-segment-descriptor-table*))
(make-initial-segment-descriptor-table)))
(let ((handler (movitz-env-symbol-function 'muerte::interrupt-default-handler)))
(setf (movitz-run-time-context-exception-handlers (image-run-time-context *image*))
(movitz-read (make-array 256 :initial-element handler))))
(setf (movitz-symbol-value (movitz-read 'muerte::*setf-namespace*))
(movitz-read (movitz-environment-setf-function-names *movitz-global-environment*) t))
(setf (run-time-context-class (image-run-time-context *image*))
(muerte::movitz-find-class 'muerte::run-time-context))
;; (setf (run-time-context-slots (image-run-time-context *image*)) #(1 2 3))
(let ((load-address (image-start-address *image*)))
(setf (image-cons-pointer *image*) (- load-address
(image-ds-segment-base *image*))
(image-address-hash *image*) (make-hash-table :test #'eq)
(image-object-hash *image*) (make-hash-table :test #'eq)
(image-multiboot-header *image*) (make-instance 'multiboot-header
:header-address 0
:load-address 0
:load-end-address 0
:entry-address 0))
(assert (= load-address (+ (image-intern-object *image* (image-run-time-context *image*))
(image-ds-segment-base *image*))))
(when multiboot-p
(assert (< (+ (image-intern-object *image* (image-multiboot-header *image*))
(sizeof (image-multiboot-header *image*))
(- load-address))
8192)))
;; make the toplevel-funobj
(unless (image-load-time-funobjs *image*)
(warn "No top-level funobjs!"))
(setf (image-load-time-funobjs *image*)
(stable-sort (copy-list (image-load-time-funobjs *image*)) #'> :key #'third))
(let* ((toplevel-funobj (make-toplevel-funobj *image*)))
(setf (image-toplevel-funobj *image*) toplevel-funobj
#+ignore ((movitz-run-time-context-toplevel-funobj (image-run-time-context *image*)) toplevel-funobj))
(format t "~&;; load-sequence:~%~<~A~>~%" (mapcar #'second (image-load-time-funobjs *image*)))
(movitz-intern toplevel-funobj)
(let ((init-code-address (+ (movitz-intern-code-vector (movitz-funobj-code-vector toplevel-funobj))
(image-cs-segment-base *image*))))
(dolist (cf (image-called-functions *image*))
(unless (typep (movitz-env-named-function (car cf) nil)
'movitz-funobj)
(warn "Function ~S is called (in ~S) but not defined." (car cf) (cdr cf))))
(maphash (lambda (symbol function-value)
(let ((movitz-symbol (movitz-read symbol)))
(etypecase function-value
(movitz-funobj
(setf (movitz-symbol-function-value movitz-symbol) function-value))
(movitz-macro
#+ignore (warn "fv: ~S ~S ~S" symbol function-value (movitz-env-get symbol :macro-expansion))))))
(movitz-environment-function-cells (image-global-environment *image*)))
(let ((run-time-context (image-run-time-context *image*)))
;; pull in functions in run-time-context
(dolist (gcf-name (binary-record-slot-names 'movitz-run-time-context :match-tags :global-function))
(let* ((gcf-movitz-name (movitz-read (intern (symbol-name gcf-name)
':muerte)))
(gcf-funobj (movitz-symbol-function-value gcf-movitz-name)))
(setf (slot-value run-time-context gcf-name) 0)
(cond
((or (not gcf-funobj)
(eq 'unbound gcf-funobj))
(warn "Global constant function ~S is not defined!" gcf-name))
(t (check-type gcf-funobj movitz-funobj)
(setf (slot-value run-time-context gcf-name)
gcf-funobj)))))
;; pull in primitive functions in run-time-context
(dolist (pf-name (binary-record-slot-names 'movitz-run-time-context
:match-tags :primitive-function))
(setf (slot-value run-time-context pf-name)
(find-primitive-function (intern (symbol-name pf-name) :muerte))))
#+ignore
(loop for k being the hash-keys of (movitz-environment-setf-function-names *movitz-global-environment*)
using (hash-value v)
do (assert (eq (symbol-value v) 'muerte::setf-placeholder))
do (when (eq *movitz-nil* (movitz-symbol-function-value (movitz-read v)))
(warn "non-used setf: ~S" v)))
;; symbol plists
(loop for (symbol plist) on (movitz-environment-plists *movitz-global-environment*) by #'cddr
;; do (warn "sp: ~S ~S" symbol plist)
do (let ((x (movitz-read symbol)))
(typecase x
(movitz-null)
(movitz-symbol
(setf (movitz-plist x)
(movitz-read (translate-program (loop for (property value) on plist by #'cddr
unless (member property '(special constantp))
append (list property value))
:cl :muerte.cl))))
(t (warn "not a symbol for plist: ~S has ~S" symbol plist)))))
;; pull in global properties
(loop for (var value) on (image-compile-time-variables *image*) by #'cddr
do (let ((mname (movitz-read var))
(mvalue (movitz-read value)))
(setf (movitz-symbol-value mname) mvalue)))
(setf (movitz-symbol-value (movitz-read 'muerte::*packages*))
(movitz-read (make-packages-hash))))
(with-binary-file (stream path
:check-stream t
:direction :output
:if-exists :supersede
:if-does-not-exist :create)
(set-file-position stream 512) ; leave room for bootblock.
(let* ((stack-vector (make-instance 'movitz-basic-vector
:num-elements #x3ffe
:fill-pointer 0
:symbolic-data nil
:element-type :stack))
(image-start (file-position stream)))
(dump-image-core *image* stream) ; dump the kernel proper.
;; make a stack-vector for the root run-time-context
(let* ((stack-vector-word
(let ((*endian* :little-endian))
(write-binary-record stack-vector stream)
;; Intern as _last_ object in image.
(movitz-intern stack-vector)))
(image-end (file-position stream))
(kernel-size (- image-end image-start)))
(format t "~&;; Kernel size: ~D octets.~%" kernel-size)
(ecase qemu-align
(:floppy
;; QEMU is rather stupid about "auto-detecting" floppy geometries.
(loop for qemu-geo in '(320 360 640 720 720 820 840 1440 1440 1600 1640 1660 1760 2080 2240 2400
2880 2952 2988 3200 3200 3360 3444 3486 3520 3680 3840 5760 6240 6400 7040 7680)
as qemu-size = (* qemu-geo 512)
do (when (>= qemu-size image-end)
(set-file-position stream (1- qemu-size) 'pad-image-tail)
(write-byte #x0 stream)
(return))
finally
(cerror "Never mind, dump the image without any QEMU geometry alignment."
"No matching QEMU floppy geometry for size ~,2F MB."
(/ image-end (* 1024 1024)))))
(:hd (let ((align-image-size (* 512 16 63))) ; Ensure image is multiple of x octets
(unless (zerop (mod image-end align-image-size))
(set-file-position stream (+ image-end (- (1- align-image-size) (mod image-end 512)))
'pad-image-tail)
(write-byte #x0 stream))))
((nil) (let ((align-image-size (* 512 1))) ; Ensure image is multiple of x octets
(unless (zerop (mod image-end align-image-size))
(set-file-position stream (+ image-end (- (1- align-image-size) (mod image-end 512)))
'pad-image-tail)
(write-byte #x0 stream)))))
(format t "~&;; Image file size: ~D octets.~%" image-end)
;; Write simple stage1 bootblock into sector 0..
(format t "~&;; Dump count: ~D." (incf (dump-count *image*)))
(flet ((global-slot-position (slot-name)