Changeset 4677
- Timestamp:
- 11/08/03 16:09:17 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/j/src/org/armedbear/lisp/jvm.lisp
r4676 r4677 2 2 ;;; 3 3 ;;; Copyright (C) 2003 Peter Graves 4 ;;; $Id: jvm.lisp,v 1.1 5 2003-11-08 14:41:21piso Exp $4 ;;; $Id: jvm.lisp,v 1.16 2003-11-08 16:09:17 piso Exp $ 5 5 ;;; 6 6 ;;; This program is free software; you can redistribute it and/or … … 464 464 465 465 (defconstant +lisp-class+ "org/armedbear/lisp/Lisp") 466 467 466 (defconstant +lisp-object-class+ "org/armedbear/lisp/LispObject") 468 467 (defconstant +lisp-object+ "Lorg/armedbear/lisp/LispObject;") 469 470 468 (defconstant +lisp-string+ "Lorg/armedbear/lisp/LispString;") 471 469 (defconstant +lisp-symbol-class+ "org/armedbear/lisp/Symbol") 472 470 (defconstant +lisp-thread-class+ "org/armedbear/lisp/LispThread") 473 471 … … 504 502 (setf *thread-var-initialized* t))) 505 503 504 (defun emit-invokevirtual (class-name method-name descriptor) 505 (emit 'invokevirtual class-name method-name descriptor)) 506 506 507 (defun emit-clear-values () 507 508 (ensure-thread-var-initialized) 508 509 (emit 'aload *thread*) 509 (emit 'invokevirtual 510 +lisp-thread-class+ 511 "clearValues" 512 "()V")) 510 ;; (emit 'invokevirtual 511 ;; +lisp-thread-class+ 512 ;; "clearValues" 513 ;; "()V")) 514 (emit-invokevirtual +lisp-thread-class+ "clearValues" "()V")) 513 515 514 516 (defun emit-invoke-method (method-name) 515 517 (unless (remove-store-value) 516 518 (emit-push-value)) 517 (emit 'invokevirtual 518 +lisp-object-class+ 519 method-name 520 "()Lorg/armedbear/lisp/LispObject;") 519 (emit-invokevirtual +lisp-object-class+ 520 method-name 521 "()Lorg/armedbear/lisp/LispObject;") 521 522 (emit-store-value)) 522 523 … … 626 627 (map 'vector #'resolve-args code)) 627 628 628 (defun is-branch-opcode(opcode)629 (defun branch-opcode-p (opcode) 629 630 (member opcode 630 631 '(153 ; IFEQ … … 635 636 ))) 636 637 638 (defun analyze-stack (code) 639 (require-type code vector) 640 ) 641 637 642 ;; CODE is a list of INSTRUCTIONs. 638 643 (defun code-bytes (code) … … 650 655 ;; (dotimes (i (length code)) 651 656 ;; (let ((instruction (svref code i))) 652 ;; (when ( is-branch-opcode(instruction-opcode instruction))657 ;; (when (branch-opcode-p (instruction-opcode instruction)) 653 658 ;; (push branch-targets (car (instruction-args instruction)))))) 654 659 ;; (format t "branch-targets = ~S~%" branch-targets) … … 696 701 (dotimes (i (length code)) 697 702 (let ((instruction (aref code i))) 698 ;; (case (instruction-opcode instruction) 699 ;; ((153 ; IFEQ 700 ;; 154 ; IFNE 701 ;; 166 ; IF_ACMPNE 702 ;; 165 ; IF_ACMPEQ 703 ;; 167 ; GOTO 704 ;; ) 705 (when (is-branch-opcode (instruction-opcode instruction)) 703 (when (branch-opcode-p (instruction-opcode instruction)) 706 704 (let* ((label (car (instruction-args instruction))) 707 705 (offset (- (symbol-value `,label) index))) … … 709 707 (unless (= (instruction-opcode instruction) 202) ; LABEL 710 708 (incf index (opcode-size (instruction-opcode instruction))))))) 709 711 710 ;; FIXME Do stack analysis here! 712 ;; Convert list to vector. 713 (let ((vector (make-array length)) 711 (analyze-stack code) 712 713 ;; Expand instructions into bytes, skipping LABEL pseudo-instructions. 714 (let ((bytes (make-array length)) 714 715 (index 0)) 715 716 (dotimes (i (length code)) 716 717 (let ((instruction (aref code i))) 717 718 (unless (= (instruction-opcode instruction) 202) ; LABEL 718 (setf (svref vectorindex) (instruction-opcode instruction))719 (setf (svref bytes index) (instruction-opcode instruction)) 719 720 (incf index) 720 721 (dolist (byte (instruction-args instruction)) 721 (setf (svref vectorindex) byte)722 (setf (svref bytes index) byte) 722 723 (incf index))))) 723 vector)))724 bytes))) 724 725 725 726 (defun write-u1 (n) … … 914 915 "(Ljava/lang/String;Ljava/lang/String;)Lorg/armedbear/lisp/Symbol;"))) 915 916 (declare-field f "Lorg/armedbear/lisp/LispObject;") 916 (emit 'invokevirtual 917 "org/armedbear/lisp/Symbol" 918 "getSymbolFunctionOrDie" 919 "()Lorg/armedbear/lisp/LispObject;") 917 (emit-invokevirtual +lisp-symbol-class+ 918 "getSymbolFunctionOrDie" 919 "()Lorg/armedbear/lisp/LispObject;") 920 920 (emit 'putstatic 921 921 *this-class* … … 1081 1081 (unless (remove-store-value) 1082 1082 (emit-push-value)) 1083 (emit 'invokevirtual 1084 "org/armedbear/lisp/LispObject" 1085 op 1086 "(Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;") 1083 (emit-invokevirtual +lisp-object-class+ 1084 op 1085 "(Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;") 1087 1086 (emit-store-value)) 1088 1087 … … 1266 1265 g 1267 1266 "Lorg/armedbear/lisp/Symbol;")) 1268 (emit 'invokevirtual 1269 "org/armedbear/lisp/Symbol" 1270 "getSymbolFunctionOrDie" 1271 "()Lorg/armedbear/lisp/LispObject;"))) 1267 (emit-invokevirtual +lisp-symbol-class+ 1268 "getSymbolFunctionOrDie" 1269 "()Lorg/armedbear/lisp/LispObject;"))) 1272 1270 (case numargs 1273 1271 (0 1274 (emit 'invokevirtual 1275 "org/armedbear/lisp/LispObject" 1276 "execute" 1277 "()Lorg/armedbear/lisp/LispObject;")) 1272 (emit-invokevirtual +lisp-object-class+ 1273 "execute" 1274 "()Lorg/armedbear/lisp/LispObject;")) 1278 1275 (1 1279 1276 (compile-form (first args)) 1280 1277 (unless (remove-store-value) 1281 1278 (emit-push-value)) 1282 (emit 'invokevirtual 1283 "org/armedbear/lisp/LispObject" 1284 "execute" 1285 "(Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1279 (emit-invokevirtual +lisp-object-class+ 1280 "execute" 1281 "(Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1286 1282 (2 1287 1283 (compile-form (first args)) … … 1291 1287 (unless (remove-store-value) 1292 1288 (emit-push-value)) 1293 (emit 'invokevirtual 1294 "org/armedbear/lisp/LispObject" 1295 "execute" 1296 "(Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1289 (emit-invokevirtual +lisp-object-class+ 1290 "execute" 1291 "(Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1297 1292 (3 1298 1293 (compile-form (first args)) … … 1305 1300 (unless (remove-store-value) 1306 1301 (emit-push-value)) 1307 (emit 'invokevirtual 1308 "org/armedbear/lisp/LispObject" 1309 "execute" 1310 "(Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1302 (emit-invokevirtual +lisp-object-class+ 1303 "execute" 1304 "(Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;")) 1311 1305 (t 1312 1306 (emit 'sipush (length args)) … … 1322 1316 (incf i))) ; array left on stack here 1323 1317 ;; Stack: function array-ref 1324 (emit 'invokevirtual 1325 "org/armedbear/lisp/LispObject" 1326 "execute" 1327 "([Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;"))) 1318 (emit-invokevirtual +lisp-object-class+ 1319 "execute" 1320 "([Lorg/armedbear/lisp/LispObject;)Lorg/armedbear/lisp/LispObject;"))) 1328 1321 (if for-effect 1329 1322 (emit 'pop) … … 1359 1352 (unless (remove-store-value) 1360 1353 (emit-push-value)) 1361 (emit 'invokevirtual 1362 +lisp-object-class+ 1363 s 1364 "()Z") 1354 (emit-invokevirtual +lisp-object-class+ 1355 s 1356 "()Z") 1365 1357 (return-from compile-test 'ifeq)))) 1366 1358 (3 (when (eq (car form) 'EQ) … … 1390 1382 (unless (remove-store-value) 1391 1383 (emit-push-value)) 1392 (emit 'invokevirtual 1393 +lisp-object-class+ 1394 s 1395 "(Lorg/armedbear/lisp/LispObject;)Z") 1384 (emit-invokevirtual +lisp-object-class+ 1385 s 1386 "(Lorg/armedbear/lisp/LispObject;)Z") 1396 1387 (return-from compile-test 'ifeq)))))) 1397 1388 ;; Otherwise... … … 1445 1436 (ensure-thread-var-initialized) 1446 1437 (emit 'aload *thread*) 1447 (emit 'invokevirtual 1448 +lisp-thread-class+ 1449 "getDynamicEnvironment" 1450 "()Lorg/armedbear/lisp/Environment;") 1438 (emit-invokevirtual +lisp-thread-class+ 1439 "getDynamicEnvironment" 1440 "()Lorg/armedbear/lisp/Environment;") 1451 1441 (emit 'astore env-var)) 1452 1442 (ecase (car form) … … 1464 1454 (emit 'aload *thread*) 1465 1455 (emit 'aload env-var) 1466 (emit 'invokevirtual 1467 +lisp-thread-class+ 1468 "setDynamicEnvironment" 1469 "(Lorg/armedbear/lisp/Environment;)V")) 1456 (emit-invokevirtual +lisp-thread-class+ 1457 "setDynamicEnvironment" 1458 "(Lorg/armedbear/lisp/Environment;)V")) 1470 1459 ;; Restore fill pointer to its saved value so the slots used by these 1471 1460 ;; bindings will again be available. … … 1683 1672 g 1684 1673 "Lorg/armedbear/lisp/Symbol;") 1685 (emit 'invokevirtual 1686 +lisp-object-class+ 1687 "getSymbolFunctionOrDie" 1688 "()Lorg/armedbear/lisp/LispObject;") 1674 (emit-invokevirtual +lisp-object-class+ 1675 "getSymbolFunctionOrDie" 1676 "()Lorg/armedbear/lisp/LispObject;") 1689 1677 (emit-store-value))) 1690 1678 #+nil … … 1776 1764 g 1777 1765 "Lorg/armedbear/lisp/Symbol;") 1778 (emit 'invokevirtual 1779 "org/armedbear/lisp/Symbol" 1780 "symbolValue" 1781 "()Lorg/armedbear/lisp/LispObject;") 1766 (emit-invokevirtual +lisp-symbol-class+ 1767 "symbolValue" 1768 "()Lorg/armedbear/lisp/LispObject;") 1782 1769 (emit-store-value) 1783 1770 (return-from compile-variable-ref))) … … 1901 1888 (emit 'aload_0) 1902 1889 (emit 'aload_1) 1903 (emit 'invokevirtual 1904 *this-class* 1905 "processArgs" 1906 "([Lorg/armedbear/lisp/LispObject;)[Lorg/armedbear/lisp/LispObject;") 1890 (emit-invokevirtual *this-class* 1891 "processArgs" 1892 "([Lorg/armedbear/lisp/LispObject;)[Lorg/armedbear/lisp/LispObject;") 1907 1893 (emit 'astore_1)) 1908 1894 (dolist (f body)
Note: See TracChangeset
for help on using the changeset viewer.