image.lisp 74.8 KB
Newer Older
1 2
;;;;------------------------------------------------------------------
;;;; 
3
;;;;    Copyright (C) 2001,2000, 2002-2005,
4
;;;;    Department of Computer Science, University of Tromso, Norway.
5 6
;;;; 
;;;; Filename:      image.lisp
7
;;;; Description:   Construction of Movitz images.
8 9 10 11
;;;; Author:        Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Created at:    Sun Oct 22 00:22:43 2000
;;;; Distribution:  See the accompanying file COPYING.
;;;;                
12
;;;; $Id: image.lisp,v 1.125 2008/07/09 19:54:56 ffjeld Exp $
13 14 15 16 17
;;;;                
;;;;------------------------------------------------------------------

(in-package movitz)

18 19
(define-binary-class movitz-run-time-context (movitz-heap-object)
  ((run-time-context-start :binary-type :label) ; keep this at the top.
20 21 22 23 24
   (type
    :binary-type other-type-byte
    :initform :run-time-context)
   (padding
    :binary-type 3)
25 26 27 28 29 30 31
   (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)
32
   (ret-trampoline
33 34 35 36
    :binary-type code-vector-word
    :map-binary-write 'movitz-intern-code-vector
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-tag :primitive-function)
37
   (cons-commit
38 39 40 41 42
    :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)
43
   (cons-non-pointer
44 45 46 47
    :binary-type code-vector-word
    :map-binary-write 'movitz-intern-code-vector
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-tag :primitive-function)
48
   (cons-commit-non-pointer
49 50 51 52
    :binary-type code-vector-word
    :map-binary-write 'movitz-intern-code-vector
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-tag :primitive-function)
53 54 55 56 57 58 59 60 61 62 63 64
   (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
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
    :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)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

   (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)
128 129 130
   
   (fast-car
    :binary-type code-vector-word
131
    :initform nil
132 133 134 135 136
    :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
137
    :initform nil
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    :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)
159
   ;; primitive functions global constants
160 161 162 163 164 165
   (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)
166
   (dynamic-variable-lookup
167 168 169 170
    :map-binary-write 'movitz-intern-code-vector
    :binary-tag :primitive-function
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-type code-vector-word)
171
   (dynamic-variable-store
172 173 174 175
    :map-binary-write 'movitz-intern-code-vector
    :binary-tag :primitive-function
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-type code-vector-word)
176 177 178
   (dynamic-unwind-next
    :map-binary-write 'movitz-intern-code-vector
    :binary-tag :primitive-function
179 180
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-type code-vector-word)
181 182 183 184 185 186 187 188 189 190
   (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)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
   (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)
211 212 213 214 215 216
   (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
217 218 219 220
    :binary-type code-vector-word
    :map-binary-write 'movitz-intern-code-vector
    :map-binary-read-delayed 'movitz-word-code-vector
    :binary-tag :primitive-function)   
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
   (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)
257 258 259 260 261 262
   (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)
263 264 265 266 267 268 269 270 271 272 273 274 275 276
   (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.
277
    :binary-type word
278 279 280 281 282 283 284
    :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
285 286
    :map-binary-write 'movitz-intern
    :map-binary-read-delayed 'movitz-word)
287 288 289 290 291 292 293 294 295 296 297
   (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)
   
298 299 300 301 302 303
   (complicated-eql
    :initform 'muerte::complicated-eql
    :binary-type word
    :binary-tag :global-function
    :map-binary-write 'movitz-intern
    :map-binary-read-delayed 'movitz-word)
304 305
   (complicated-compare
    :binary-type word
306 307
    :binary-tag :global-function
    :map-binary-read-delayed 'movitz-word
308
    :map-binary-write 'movitz-intern)
309 310 311
   (dynamic-env
    :binary-type word
    :initform 0)
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
   
   (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)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
   (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))))


376 377
   (classes				; A vector of class meta-objects.
    :initform nil			; The first element is the map of corresponding names
378
    :binary-type word
379 380 381 382 383 384 385 386 387 388 389
    :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)
390 391 392
   (physical-address-offset
    :binary-type lu32
    :initform (image-ds-segment-base *image*))
393 394 395 396 397 398 399
   (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)))
400 401 402 403 404 405 406
   (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)))
407 408 409 410
   (self
    :binary-type word
    :initform 6
    :map-binary-read-delayed 'movitz-word)
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
   (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
434 435 436 437 438 439
    :map-binary-write 'map-interrupt-trampolines-to-idt)
   (num-values
    :binary-type word			; Fixnum
    :initform 0)
   (values
    :binary-type #.(* 4 +movitz-multiple-values-limit+)))
440
  (:slot-align null-symbol -5))
441

442
(defun atomically-continuation-simple-pf (pf-name)
443
  (ldb (byte 32 0) (global-constant-offset pf-name))
444
  #+ignore
445 446 447 448 449
  (bt:enum-value 'movitz::atomically-status
		 (list* :restart-primitive-function
			(cons :reset-status-p
			      (if reset-status-p 1 0))
			(cons :data
450 451 452
			      (if (not pf-name)
				  0
				(truncate (+ (tag :null)
453
					     (bt:slot-offset 'movitz-run-time-context
454 455 456
							     (intern (symbol-name pf-name)
								     :movitz)))
					  4)))
457 458
			registers)))

459
(defun atomically-status-jumper-fn (reset-status-p &rest registers)
460 461
  (assert (not reset-status-p))
  (assert (null registers))
462 463 464 465 466 467 468 469 470
  (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))))

471
(defmethod movitz-object-offset ((obj movitz-run-time-context)) 0)
472 473 474

(defun global-constant-offset (slot-name)
  (check-type slot-name symbol)
475 476 477 478 479
  (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)
480 481
	 (slot-offset 'movitz-symbol 'function-value)
	 (tag :symbol)))))
482

483 484
(defun make-movitz-run-time-context ()
  (make-instance 'movitz-run-time-context
485
    :t-symbol (movitz-read 't)
486
    :null-symbol  *movitz-nil*))
487

488
(defclass movitz-image ()
489
  ((ds-segment-base
490
    :initarg :ds-segment-base
491 492 493
    :initform #x100000
    :accessor image-ds-segment-base)
   (cs-segment-base
494
    :initarg :cs-segment-base
495 496 497
    :initform #x100000
    :accessor image-cs-segment-base)))

498
(defclass symbolic-image (movitz-image)
499 500 501 502 503 504 505
  ((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
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
506
    :initform (make-hash-table :test #'eql) ; lisp object => movitz object
507 508
    :reader image-read-map-hash)
   (inverse-read-map-hash
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
509
    :initform (make-hash-table :test #'eql) ; lisp object => movitz object
510 511 512
    :reader image-inverse-read-map-hash)
   (oblist
    :reader image-oblist
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
513
    :initform (make-hash-table :test #'eql))
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
   (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)
529 530 531
   (nil-object
    :initarg :nil-object
    :accessor image-nil-object)
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
   (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)
549 550
   (run-time-context
    :accessor image-run-time-context)
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
   (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)))

573 574 575
(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
576
    muerte.cl:integer muerte.cl:ratio muerte.cl:complex
577
    muerte.cl:vector muerte.cl:string muerte.cl:bit-vector muerte.cl:array
578 579 580 581 582
    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
583 584
    muerte::basic-restart
    muerte::illegal-object))
585 586

(defun class-object-offset (name)
587
  (let ((name (translate-program name :cl :muerte.cl)))
588
    (+ (bt:slot-offset 'movitz-basic-vector 'data)
589 590
       (* 4 (1+ (or (position name (image-classes-map *image*))
		    (error "No class named ~S in class-map." name)))))))
591

592 593
(defun unbound-value ()
  (declare (special *image*))
594 595
  (movitz-read (slot-value (image-run-time-context *image*)
			   'new-unbound-value)))
596 597 598 599 600 601 602 603

(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))
604 605 606 607
	  (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))))
608 609 610
      ()
    "The MOVITZ-HEAP-OBJECT-OTHER type ~A is malformed!" (type-of object))
  (etypecase object
611
    (movitz-null
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
     (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))))))

630
(defmethod image-memref ((image symbolic-image) address &optional (errorp t))
631
  (let ((obj (gethash address (image-address-hash image) :nothing)))
632 633 634 635 636 637
    (cond
     ((not (typep obj 'movitz-object))
      (when errorp
	(error "Found non-movitz-object at image-address #x~X: ~A" address obj))
      nil)
     (t obj))))
638 639 640 641

(defmethod search-image ((image symbolic-image) address)
  (loop for a downfrom (logand address -8) by 8
      until (gethash a (image-address-hash image))
642 643 644 645
      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)))))
646 647 648 649 650 651

(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))))
652 653
    (unless (and (typep code-vector 'movitz-basic-vector)
		 (eq :code (movitz-vector-element-type code-vector)))
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
      (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)))
675
    (unless (and (typep code-vector 'movitz-basic-vector)
676 677 678
		 (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))
679
    (loop for pf-name in (binary-record-slot-names 'movitz-run-time-context
680 681
						   :match-tags :primitive-function)
	when (= (movitz-intern-code-vector code-vector)
682
		(binary-slot-value (image-run-time-context *image*) pf-name))
683 684 685 686 687 688 689 690 691 692 693 694
	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))

695 696
(defun movitz-word-and-print (word &optional (type 'word))
  (movitz-print (movitz-word word type)))
697 698 699 700 701 702 703 704 705

(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
706
     (image-nil-word image))
707 708 709 710 711 712 713 714 715
    (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
716
    ((or vector movitz-basic-vector)
717 718 719
     (+ 2 (movitz-intern object)))
    ((or symbol movitz-symbol)
     (let ((primitive-code-vector (movitz-symbol-value (movitz-read object))))
720
       (check-type primitive-code-vector movitz-basic-vector)
721 722 723 724 725 726 727 728 729
       (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)))))

730 731 732 733 734 735 736
(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)))

737 738 739 740 741 742 743 744 745 746 747
(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))

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
(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)))))
		     

773 774 775 776 777 778
(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
779
			(if *image*
780
			    (copy-hash-table (function-code-sizes *image*))
781
                            (make-hash-table :test #'equal))
782
			init-args)))
783
    (setf (image-nil-word *image*)
784
      (+ 5 (- (slot-offset 'movitz-run-time-context 'null-symbol)
785 786 787
	      (slot-offset 'movitz-run-time-context 'run-time-context-start))
	 (- start-address
	    (image-ds-segment-base *image*))))
788 789 790 791 792 793
    (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))
794
    (setf (image-run-time-context *image*) (make-movitz-run-time-context))
795 796 797 798 799 800 801 802 803
    (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))))
804
    (unless (and code-vector (not (eq 'unbound code-vector)))
805 806 807 808 809
      (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 #()))))
810
    (check-type code-vector movitz-basic-vector)
811 812
    code-vector))

813 814
(defun create-image (&rest init-args
		     &key (init-file *default-image-init-file*)
815
			  (gc t)
816 817 818 819 820
			  ;; (start-address #x100000)
			  &allow-other-keys)
  (psetq *image* (let ((*image* (apply #'make-movitz-image
				       :start-address #x100000
				       init-args)))
821 822 823
		   (when init-file
		     (movitz-compile-file init-file))
		   *image*)
824
	 *i* *image*)
825 826 827 828
  (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.
829
  *image*)
830

831
(defun set-file-position (stream position &optional who)
832
  (declare (ignore who))
833 834 835 836 837 838 839 840
  (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))

841
(defun dump-image (&key (path *default-image-file*) ((:image *image*) *image*)
842
                   (multiboot-p t) ignore-dump-count (qemu-align :floppy))
843 844 845 846 847 848
  "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..")
849 850
    (dump-image :path path :multiboot-p multiboot-p :ignore-dump-count t
		:qemu-align nil)
851
    (assert (plusp (dump-count *image*))))
852 853
  (setf (movitz-symbol-value (movitz-read 'muerte:*build-number*))
    (1+ *bootblock-build*))
854 855 856
  (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)))
857
  (let ((handler (movitz-env-symbol-function 'muerte::interrupt-default-handler)))
858
    (setf (movitz-run-time-context-exception-handlers (image-run-time-context *image*))
859
      (movitz-read (make-array 256 :initial-element handler))))
860 861
  (setf (movitz-symbol-value (movitz-read 'muerte::*setf-namespace*))
    (movitz-read (movitz-environment-setf-function-names *movitz-global-environment*) t))
862 863
  (setf (run-time-context-class (image-run-time-context *image*))
    (muerte::movitz-find-class 'muerte::run-time-context))
864
  ;;  (setf (run-time-context-slots (image-run-time-context *image*)) #(1 2 3))
865 866 867 868 869 870 871 872 873 874
  (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))
875
    (assert (= load-address (+ (image-intern-object *image* (image-run-time-context *image*))
876 877 878 879 880 881 882 883 884 885 886 887 888
			       (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
889
	    #+ignore ((movitz-run-time-context-toplevel-funobj (image-run-time-context *image*)) toplevel-funobj))
890 891 892 893 894 895 896 897
      (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))))
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
898 899 900 901 902 903 904
	(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))))))
905
		 (movitz-environment-function-cells (image-global-environment *image*)))
906 907 908
	(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))
909
	    (let* ((gcf-movitz-name (movitz-read (intern (symbol-name gcf-name)
910
							 ':muerte)))
911
		   (gcf-funobj (movitz-symbol-function-value gcf-movitz-name)))
912
	      (setf (slot-value run-time-context gcf-name) 0)
913 914
	      (cond
	       ((or (not gcf-funobj)
915
		    (eq 'unbound gcf-funobj))
916 917
		(warn "Global constant function ~S is not defined!" gcf-name))
	       (t (check-type gcf-funobj movitz-funobj)
918
		  (setf (slot-value run-time-context gcf-name)
919
		    gcf-funobj)))))
920 921
	  ;; pull in primitive functions in run-time-context
	  (dolist (pf-name (binary-record-slot-names 'movitz-run-time-context
922
						     :match-tags :primitive-function))
923
	    (setf (slot-value run-time-context pf-name)
924 925 926 927 928 929 930 931 932
	      (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
933
              ;; do (warn "sp: ~S ~S" symbol plist)
934 935
	      do (let ((x (movitz-read symbol)))
		   (typecase x
936
		     (movitz-null)
937 938
		     (movitz-symbol
		      (setf (movitz-plist x)
939 940 941 942
			(movitz-read (translate-program (loop for (property value) on plist by #'cddr
							    unless (member property '(special constantp))
							    append (list property value))
							:cl :muerte.cl))))
943 944
		     (t (warn "not a symbol for plist: ~S has ~S" symbol plist)))))
	  ;; pull in global properties
945
	  (loop for (var value) on (image-compile-time-variables *image*) by #'cddr
946
	      do (let ((mname (movitz-read var))
947
		       (mvalue (movitz-read value)))
948
		   (setf (movitz-symbol-value mname) mvalue)))
949 950
	  (setf (movitz-symbol-value (movitz-read 'muerte::*packages*))
	    (movitz-read (make-packages-hash))))
951 952 953 954 955
	(with-binary-file (stream path
				  :check-stream t
				  :direction :output
				  :if-exists :supersede
				  :if-does-not-exist :create)
956
	  (set-file-position stream 512) ; leave room for bootblock.
957
	  (let* ((stack-vector (make-instance 'movitz-basic-vector
958
				 :num-elements #x3ffe
959 960
				 :fill-pointer 0
				 :symbolic-data nil
961
				 :element-type :stack))
962 963 964 965 966 967 968 969 970 971 972
		 (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)
973 974
              (ecase qemu-align
		(:floppy
975 976 977 978 979 980 981 982 983
                 ;; 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
984 985 986 987 988 989 990 991 992 993 994 995 996
                      (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)))))
997 998 999 1000 1001 1002 1003 1004 1005
	      (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)
		       (+ 512
			  (image-nil-word *image*)
			  (image-ds-segment-base *image*)
			  (global-constant-offset slot-name)
			  (- load-address))))
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
		(with-simple-restart (continue "Don't write a floppy bootloader.")
		  (let ((bootblock (make-bootblock kernel-size
						   load-address
						   init-code-address)))
		    (setf (image-bootblock *image*) bootblock)
		    (set-file-position stream 0)
		    (write-sequence bootblock stream)))
		(let* ((stack-vector-address (+ (image-nil-word *image*)
						(global-constant-offset 'stack-vector)
						(image-ds-segment-base *image*)))
		       (stack-vector-position (- (+ stack-vector-address 512)
						 load-address)))
		  (declare (ignore stack-vector-position))
		  #+ignore(warn "stack-v-pos: ~S => ~S" 
				stack-vector-position
				stack-vector-word)
		  (set-file-position stream (global-slot-position 'stack-vector) 'stack-vector)
		  (write-binary 'word stream stack-vector-word)
		  (set-file-position stream (global-slot-position 'stack-bottom) 'stack-bottom)
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
1025
		  (write-binary 'lu32 stream (+ 8 (* 8 4096) ; cushion
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
						(- stack-vector-word (tag :other))))
		  (set-file-position stream (global-slot-position 'stack-top) 'stack-top)
		  (write-binary 'lu32 stream (+ 8 (- stack-vector-word (tag :other))
						(* 4 (movitz-vector-num-elements stack-vector)))))
		(if (not multiboot-p)
		    (format t "~&;; No multiboot header.")
		  ;; Update multiboot header, symbolic and in the file..
		  (let* ((mb (image-multiboot-header *image*))
			 (mb-address (+ (movitz-intern mb)
					(slot-offset 'multiboot-header 'magic)
					(image-ds-segment-base *image*)))
			 (mb-file-position (- (+ mb-address 512)
					      load-address
					      (slot-offset 'multiboot-header 'magic))))
		    (when (< load-address #x100000)
		      (warn "Multiboot load-address #x~x is below the 1MB mark."
			    load-address))
		    (when (> (+ mb-file-position (sizeof mb)) 8192)
		      (warn "Multiboot header at position ~D is above the 8KB mark, ~
1045
this image will not be Multiboot compatible."
1046 1047 1048 1049 1050 1051 1052 1053 1054
			    (+ mb-file-position (sizeof mb))))
		    (set-file-position stream mb-file-position 'multiboot-header)
		    ;; (format t "~&;; Multiboot load-address: #x~X." load-address)
		    (setf (header-address mb) mb-address
			  (load-address mb) load-address
			  (load-end-address mb) (+ load-address kernel-size)
			  (bss-end-address mb) (+ load-address kernel-size)
			  (entry-address mb) init-code-address)
		    (write-binary-record mb stream))))))))))
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
  (values))

(defun dump-image-core (image stream)
  (let ((*endian* :little-endian)
	(*record-all-funobjs* nil)
	(symbols-size 0)
	(conses-size 0)
	(funobjs-size 0)
	(code-vectors-size 0)
	(strings-size 0)
	(simple-vectors-size 0)
	(total-size 0)
	(symbols-numof 0)
	(gensyms-numof 0)
	(conses-numof 0)
	(funobjs-numof 0)
	(code-vectors-numof 0)
	(strings-numof 0)
	(simple-vectors-numof 0)
	(file-start-position (file-position stream))
	(pad-size 0))
    (declare (special *record-all-funobjs*))
1077 1078
    (loop with prev-obj
	for p upfrom (- (image-start-address image) (image-ds-segment-base image)) by 8
1079 1080
	until (>= p (image-cons-pointer image))
	summing
1081
	  (let ((obj (image-memref image p nil)))
1082
	    (cond
1083
	     ((not obj) 0)		; (+ 1mb (- 1mb)) vs. (+ 0 (- 1mb 1mb))
1084
	     (t (let ((new-pos (+ p file-start-position
1085 1086 1087 1088 1089
				  (- (image-ds-segment-base image)
				     (image-start-address image)))))
		  (let ((pad-delta (- new-pos (file-position stream))))
		    (with-simple-restart (continue "Never mind.")
		      (assert (<= 0 pad-delta 31) ()
1090 1091
			"pad-delta ~S for ~S (prev ~S), p: ~S, new-pos: ~S"
			pad-delta obj prev-obj p new-pos))
1092
		    (incf pad-size pad-delta))
1093
		  (set-file-position stream new-pos obj))
1094 1095 1096 1097 1098
		;; (warn "Dump at address #x~X, filepos #x~X: ~A" p (file-position stream) obj)
		(let ((old-pos (file-position stream))
		      (write-size (write-binary-record obj stream)))
		  (incf total-size write-size)
		  (typecase obj
1099
		    (movitz-basic-vector
1100 1101 1102 1103 1104
		     (case (movitz-vector-element-type obj)
		       (:character (incf strings-numof)
				   (incf strings-size write-size))
		       (:any-t (incf simple-vectors-numof)
			       (incf simple-vectors-size write-size))
1105 1106
		       (:code (incf code-vectors-numof)
			      (incf code-vectors-size write-size))))
1107
		    (movitz-funobj (incf funobjs-numof)
1108
				   (incf funobjs-size write-size))
1109
		    (movitz-symbol (incf symbols-numof)
1110 1111 1112
				   (incf symbols-size write-size)
				   (when (movitz-eql *movitz-nil* (movitz-symbol-package obj))
				     (incf gensyms-numof)))
1113
		    (movitz-cons (incf conses-numof)
1114
				 (incf conses-size write-size)))
1115 1116 1117 1118
		  (assert (= write-size (sizeof obj) (- (file-position stream) old-pos)) ()
		    "Inconsistent write-size(~D)/sizeof(~D)/file-position delta(~D) ~
                       for object ~S."
		    write-size (sizeof obj) (- (file-position stream) old-pos) obj)
1119
		  (setf prev-obj obj)
1120 1121 1122 1123 1124 1125
		  write-size))))
	finally
	  (let ((total-size (file-position stream))
		(sum (+ symbols-size conses-size funobjs-size strings-size
			simple-vectors-size code-vectors-size pad-size)))
	    (format t "~&;;~%;; ~D symbols (~D gensyms) (~,1F KB ~~ ~,1F%), ~D conses (~,1F KB ~~ ~,1F%),
1126 1127 1128 1129
~D funobjs (~,1F KB ~~ ~,1F%), ~D strings (~,1F KB ~~ ~,1F%),
~D simple-vectors (~,1F KB ~~ ~,1F%), ~D code-vectors (~,1F KB ~~ ~,1F%).
~,1F KB (~,1F%) of padding.
In sum this accounts for ~,1F%, or ~D bytes.~%;;~%"
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
		    symbols-numof gensyms-numof
		    (/ symbols-size 1024) (/ (* symbols-size 100) total-size)
		    conses-numof (/ conses-size 1024) (/ (* conses-size 100) total-size)
		    funobjs-numof (/ funobjs-size 1024) (/ (* funobjs-size 100) total-size)
		    strings-numof (/ strings-size 1024) (/ (* strings-size 100) total-size)
		    simple-vectors-numof (/ simple-vectors-size 1024) (/ (* simple-vectors-size 100) total-size)
		    code-vectors-numof (/ code-vectors-size 1024) (/ (* code-vectors-size 100) total-size)
		    (/ pad-size 1024) (/ (* pad-size 100) total-size)
		    (/ (* sum 100) total-size)
		    sum)))))

(defun intern-movitz-symbol (name)
1142 1143 1144 1145 1146 1147 1148
  (assert (not (member (symbol-package name)
		       '(:common-lisp :movitz)
		       :key #'find-package))
      (name)
    "Trying to movitz-intern a symbol in the ~A package: ~S" (symbol-package name) name)
  (assert (not (eq (symbol-package name)
		   (find-package :movitz)))
1149 1150
      (name)
    "Trying to movitz-intern a symbol in the Common-Lisp package: ~S" name)
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
  (or (gethash name (image-oblist *image*))
      (let ((symbol (make-movitz-symbol name)))
	(when (get name :setf-placeholder)
	  (setf (movitz-symbol-flags symbol) '(:setf-placeholder)
		(movitz-symbol-value symbol) (movitz-read (get name :setf-placeholder))))
	(setf (gethash name (image-oblist *image*)) symbol)
	(when (symbol-package name)
	  (let ((p (gethash (symbol-package name) (image-read-map-hash *image*))))
	    (when p
	      (setf (movitz-symbol-package symbol) p))))
	(when (or (eq 'muerte.cl:t name)
		  (keywordp (translate-program name :muerte.cl :cl)))
	  (pushnew :constant-variable (movitz-symbol-flags symbol))
	  (setf (movitz-symbol-value symbol)
	    (movitz-read (translate-program (symbol-value (translate-program name :muerte.cl :cl))
1166
					    :cl :muerte.cl))))
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	symbol)))

(defun make-packages-hash (&optional (*image* *image*))
  (let ((lisp-to-movitz-package (make-hash-table :test #'eq))
	(packages-hash (make-hash-table :test #'equal :size 23)))
    (labels ((movitz-package-name (name &optional symbol)
	       (declare (ignore symbol))
	       (cond
		((string= (string :keyword) name)
		 name)
		((and (< 7 (length name))
		      (string= (string 'muerte.) name :end2 7))
		 (subseq name 7))
		(t #+ignore (warn "Package ~S ~@[for symbol ~S ~]is not a Movitz package."
				  name symbol)
		   name)))
1183
	     (ensure-package (package-name lisp-package &optional context)
Frode Vatvedt Fjeld's avatar
Frode Vatvedt Fjeld committed
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	       (restart-case (assert (not (member (package-name lisp-package)
						  '(common-lisp movitz
						    #+allegro excl
						    #+allegro sys
						    #+allegro aclmop
						    #+sbcl sb-ext)
						  :test #'string=)) ()
						  "I don't think you really want to dump the package ~A ~@[for symbol ~S~] with Movitz."
						  lisp-package context)
		 (use-muerte ()
		   :report "Substitute the muerte pacakge."
		   (return-from ensure-package (ensure-package :muerte (find-package :muerte)))))
1196 1197
	       (setf (gethash lisp-package lisp-to-movitz-package)
		 (or (gethash package-name packages-hash nil)
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
		     (let* ((nicks (mapcar #'movitz-package-name (package-nicknames lisp-package)))
			    (p (funcall 'muerte::make-package-object
					:name package-name
					:shadowing-symbols-list (package-shadowing-symbols lisp-package)
					:external-symbols (make-hash-table :test #'equal)
					:internal-symbols (make-hash-table :test #'equal)
					:nicknames nicks
					:use-list (mapcar #'(lambda (up) 
							      (ensure-package (movitz-package-name (package-name up))
									      up context))
							  (package-use-list lisp-package)))))
1209
		       (setf (gethash package-name packages-hash) p)
1210 1211
		       (dolist (nick nicks)
			 (setf (gethash nick packages-hash) p))
1212
		       p)))))
1213 1214 1215
      (let ((movitz-cl-package (ensure-package (symbol-name :common-lisp)
					       (find-package :muerte.common-lisp))))
	(setf (gethash "NIL" (funcall 'muerte:package-object-external-symbols movitz-cl-package))
1216
	  nil))
1217 1218
      (ensure-package (symbol-name :common-lisp-user)
		      (find-package :muerte.common-lisp-user))
1219 1220 1221 1222 1223
      (loop for symbol being the hash-key of (image-oblist *image*)
	  as lisp-package = (symbol-package symbol)
	  as package-name = (and lisp-package
				 (movitz-package-name (package-name lisp-package) symbol))
	  when package-name
1224
	  do (let* ((movitz-package (ensure-package package-name lisp-package symbol)))
1225 1226 1227 1228 1229
	       (multiple-value-bind (symbol status)
		   (find-symbol (symbol-name symbol) (symbol-package symbol))
		 (ecase status
		   (:internal
		    (setf (gethash (symbol-name symbol)
1230
				   (funcall 'muerte:package-object-internal-symbols movitz-package))
1231 1232 1233 1234
		      symbol))
		   (:external
		    ;; (warn "putting external ~S in ~S" symbol package-name)
		    (setf (gethash (symbol-name symbol)
1235
				   (funcall 'muerte:package-object-external-symbols movitz-package))
1236 1237 1238 1239 1240 1241 1242 1243 1244
		      symbol))
		   (:inherited
		    (warn "inherited symbol: ~S" symbol))))))
;;;    (warn "PA: ~S" packages-hash)
      (let ((movitz-packages (movitz-read packages-hash)))
	(maphash (lambda (lisp-package movitz-package)
		   (setf (gethash lisp-package (image-read-map-hash *image*))
		     (movitz-read movitz-package)))
		 lisp-to-movitz-package)
1245
	(setf (slot-value (movitz-run-time-context-null-symbol (image-run-time-context *image*))
1246 1247 1248
			  'package)
	  (movitz-read (ensure-package (string :common-lisp) :muerte.common-lisp)))
	(loop for symbol being the hash-key of (image-oblist *image*)
Frode Vatvedt Fjeld's avatar
indent.  
Frode Vatvedt Fjeld committed
1249 1250 1251 1252 1253 1254 1255
	   as lisp-package = (symbol-package symbol)
	   as package-name = (and lisp-package
				  (movitz-package-name (package-name lisp-package) symbol))
	   when package-name
	   do (let* ((movitz-package (ensure-package package-name lisp-package symbol)))
		(setf (movitz-symbol-package (movitz-read symbol))
		      (movitz-read movitz-package))))
1256 1257 1258
	movitz-packages))))


1259 1260 1261 1262
(defun run-time-context-find-slot (offset)
  "Return the name of the run-time-context slot located at offset."
  (dolist (slot-name (bt:binary-record-slot-names 'movitz-run-time-context))
    (when (= offset (bt:slot-offset 'movitz-run-time-context slot-name))
1263 1264
      (return slot-name))))

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
#-ia-x86
(defun comment-instruction (instruction funobj pc)
  "Return a list of strings that comments on INSTRUCTION."
  (declare (ignore pc))
  (loop for operand in (asm:instruction-operands instruction)
     when (and (typep operand 'asm:indirect-operand)
	       (member :edi operand)
	       (run-time-context-find-slot (asm:indirect-operand-offset operand))
	       (not (member (asm:instruction-operator instruction)
			    '(:leal :lea))))
     collect (format nil "<Global slot ~A>" 
		     (run-time-context-find-slot (asm:indirect-operand-offset operand)))
;;      when (and (typep operand 'ia-x86::operand-indirect-register)
;; 	       (eq 'ia-x86::edi (ia-x86::operand-register operand))
;; 	       (typep instruction 'ia-x86-instr::lea)
;; 	       (or (not (ia-x86::operand-register2 operand))
;; 		   (eq 'ia-x86::edi (ia-x86::operand-register2 operand))))
;;      collect (let ((x (+ (* (ia-x86::operand-scale operand)
;; 			    (image-nil-word *image*))
;; 			 (ia-x86::operand-offset operand)
;; 			 (ecase (ia-x86::operand-register2 operand)
;; 			   (ia-x86::edi (image-nil-word *image*))
;; 			   ((nil) 0)))))
;; 	       (case (ldb (byte 3 0) x)
;; 		 (#.(tag :character)
;; 		    (format nil "Immediate ~D (char ~S)"
;; 			    x (code-char (ldb (byte 8 8) x))))
;; 		 (#.(mapcar 'tag +fixnum-tags+)
;; 		    (format nil "Immediate ~D (fixnum ~D #x~X)"
;; 			    x
;; 			    (truncate x +movitz-fixnum-factor+)
;; 			    (truncate x +movitz-fixnum-factor+)))
;; 		 (t (format nil "Immediate ~D" x))))
     when (and funobj
	       (typep operand 'asm:indirect-operand)
	       (member :esi operand)
1301
	       (= 2 (length operand))
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335