| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; Copyright (c) 2010, Juan Jose Garcia Ripoll |
| 5 |
;;;; |
| 6 |
;;;; See the file 'LICENSE' for the copyright details. |
| 7 |
;;;; |
| 8 |
|
| 9 |
;;;; Type propagators for numerical expressions. |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 14 |
;;; |
| 15 |
;;; TYPE PROPAGATION |
| 16 |
;;; |
| 17 |
|
| 18 |
(def-type-propagator logand (fname &rest args) |
| 19 |
(values args |
| 20 |
(if args |
| 21 |
(dolist (int-type '((UNSIGNED-BYTE 8) FIXNUM) 'integer) |
| 22 |
(when (loop for value in args |
| 23 |
always (subtypep value int-type *cmp-env*)) |
| 24 |
(return int-type))) |
| 25 |
'fixnum))) |
| 26 |
|
| 27 |
;;; |
| 28 |
;;; The following are type propagators for arithmetic operations. Note |
| 29 |
;;; that some of they have become binary operators. |
| 30 |
;;; |
| 31 |
|
| 32 |
(defun maximum-number-type (type1 type2 &key only-real integer-result) |
| 33 |
;; Computes the output type of an operation between number types T1 |
| 34 |
;; and T2 using the rules of floating point contagion. It returns |
| 35 |
;; the type of the result, and the types of T1 and T2, if they |
| 36 |
;; represent known types, or NUMBER, in other cases. |
| 37 |
(let ((t1-eq nil) |
| 38 |
(t2-eq nil) |
| 39 |
(t1 type1) |
| 40 |
(t2 type2) |
| 41 |
(output nil) |
| 42 |
(complex-t1 nil) |
| 43 |
(complex-t2 nil) |
| 44 |
(default (if only-real 'REAL 'NUMBER)) |
| 45 |
(number-types #(FIXNUM INTEGER RATIONAL SINGLE-FLOAT |
| 46 |
DOUBLE-FLOAT LONG-FLOAT FLOAT REAL))) |
| 47 |
(when (and (consp t1) (eq (first t1) 'COMPLEX)) |
| 48 |
(setf t1 (second t1) complex-t1 t)) |
| 49 |
(when (and (consp t2) (eq (first t2) 'COMPLEX)) |
| 50 |
(setf t2 (second t2) complex-t2 t)) |
| 51 |
(when (and only-real (or complex-t1 complex-t2)) |
| 52 |
(return-from maximum-number-type (values default default default))) |
| 53 |
(loop for i across number-types |
| 54 |
do (when (and (null t1-eq) (type>= i t1 *cmp-env*)) |
| 55 |
(when (equalp t1 t2) |
| 56 |
(setf t2-eq i)) |
| 57 |
(setf t1-eq i output i)) |
| 58 |
(when (and (null t2-eq) (type>= i t2 *cmp-env*)) |
| 59 |
(setf t2-eq i output i))) |
| 60 |
(unless (and t1-eq t2-eq output) |
| 61 |
(setf output default)) |
| 62 |
(when (and integer-result (or (eq output 'FIXNUM) (eq output 'INTEGER))) |
| 63 |
(setf output integer-result)) |
| 64 |
(when (and (or complex-t1 complex-t2) (not (eq output 'NUMBER))) |
| 65 |
(setf output (if (eq output 'REAL) 'COMPLEX `(COMPLEX ,output)))) |
| 66 |
(values output (if t1-eq type1 default) (if t2-eq type2 default)))) |
| 67 |
|
| 68 |
(defun ensure-number-type (general-type &key integer-result) |
| 69 |
(maximum-number-type general-type general-type :integer-result integer-result)) |
| 70 |
|
| 71 |
(defun ensure-nonrational-type (general-type) |
| 72 |
(maximum-number-type general-type 'single-float)) |
| 73 |
|
| 74 |
(defun ensure-real-type (general-type) |
| 75 |
(maximum-number-type general-type 'integer :only-real t)) |
| 76 |
|
| 77 |
(defun arithmetic-propagator (op1-type others integer-result) |
| 78 |
;; Propagates types for an associative operator (we do not care which one). |
| 79 |
;; We collect either the types of the arguments or 'NUMBER, as a generic |
| 80 |
;; expected type. The output type is computed using the rules of floating |
| 81 |
;; point contagion, with the exception that an operation between two |
| 82 |
;; integers has type INTEGER-RESULT (integer for *,-,+ and rational else) |
| 83 |
(multiple-value-bind (result-type op1-type) |
| 84 |
(ensure-number-type op1-type :integer-result integer-result) |
| 85 |
(loop with arg-types = (list op1-type) |
| 86 |
for x in others |
| 87 |
for op2-type = x |
| 88 |
do (progn |
| 89 |
(multiple-value-setq (result-type op1-type op2-type) |
| 90 |
(maximum-number-type result-type op2-type :integer-result integer-result)) |
| 91 |
(setf arg-types (cons op2-type arg-types))) |
| 92 |
finally (return (values (nreverse arg-types) result-type))))) |
| 93 |
|
| 94 |
(def-type-propagator * (fname op1 &rest others) |
| 95 |
(arithmetic-propagator op1 others 'integer)) |
| 96 |
|
| 97 |
(copy-type-propagator '* '(+ -)) |
| 98 |
|
| 99 |
(def-type-propagator / (fname op1 &rest others) |
| 100 |
(arithmetic-propagator op1 others 'rational)) |
| 101 |
|
| 102 |
;;; |
| 103 |
;;; SPECIAL FUNCTIONS |
| 104 |
;;; |
| 105 |
|
| 106 |
(def-type-propagator cos (fname op1-type) |
| 107 |
(multiple-value-bind (output-type op1-type) |
| 108 |
(ensure-nonrational-type op1-type) |
| 109 |
(values (list op1-type) output-type))) |
| 110 |
|
| 111 |
(copy-type-propagator 'cos '(sin tan cosh sinh tanh exp)) |
| 112 |
|
| 113 |
(def-type-propagator acos (fname op1-type) |
| 114 |
(multiple-value-bind (output-type op1-type) |
| 115 |
(ensure-nonrational-type op1-type) |
| 116 |
(declare (ignore output-type)) |
| 117 |
(values (list op1-type) 'NUMBER))) |
| 118 |
|
| 119 |
(def-type-propagator atan (fname op1-type &optional (op2-type t op2-p)) |
| 120 |
(multiple-value-bind (float-t1 t1) |
| 121 |
(ensure-nonrational-type op1-type) |
| 122 |
(if op2-p |
| 123 |
(multiple-value-bind (result t1 t2) |
| 124 |
(maximum-number-type t1 op2-type :only-real t) |
| 125 |
(values (list t1 t2) result)) |
| 126 |
(values (list t1) float-t1)))) |
| 127 |
|
| 128 |
(def-type-propagator expt (fname base exponent) |
| 129 |
;; Rules: |
| 130 |
;; (expt fixnum integer) -> integer |
| 131 |
;; (expt number-type integer) -> number-type |
| 132 |
;; (expt number-type1 number-type2) -> (max-float number-type1 number-type2) |
| 133 |
;; |
| 134 |
(let ((exponent (ensure-real-type exponent))) |
| 135 |
(values (list base exponent) |
| 136 |
(cond ((eql exponent 'integer) |
| 137 |
(if (subtypep base 'fixnum *cmp-env*) |
| 138 |
'integer |
| 139 |
base)) |
| 140 |
((type>= '(real 0 *) base *cmp-env*) |
| 141 |
(let* ((exponent (ensure-nonrational-type exponent))) |
| 142 |
(maximum-number-type exponent base))) |
| 143 |
(t |
| 144 |
'number))))) |
| 145 |
|
| 146 |
(def-type-propagator abs (fname arg) |
| 147 |
(multiple-value-bind (output arg) |
| 148 |
(ensure-number-type arg) |
| 149 |
(values (list arg) |
| 150 |
(or (cdr (assoc output |
| 151 |
'((FIXNUM . (AND FIXNUM (INTEGER 0 *))) |
| 152 |
(INTEGER . (INTEGER 0 *)) |
| 153 |
(RATIONAL . (RATIONAL 0 *)) |
| 154 |
(SHORT-FLOAT . (SHORT-FLOAT 0 *)) |
| 155 |
(SINGLE-FLOAT . (SINGLE-FLOAT 0 *)) |
| 156 |
(DOUBLE-FLOAT . (DOUBLE-FLOAT 0 *)) |
| 157 |
(LONG-FLOAT . (LONG-FLOAT 0 *)) |
| 158 |
(REAL . (REAL 0 *)) |
| 159 |
(NUMBER . (REAL 0 *))))) |
| 160 |
output)))) |
| 161 |
|
| 162 |
(def-type-propagator sqrt (fname arg) |
| 163 |
(multiple-value-bind (output arg) |
| 164 |
(ensure-nonrational-type arg) |
| 165 |
(values (list arg) |
| 166 |
(if (type>= '(REAL 0 *) arg *cmp-env*) output 'NUMBER)))) |
| 167 |
|
| 168 |
(def-type-propagator isqrt (fname arg) |
| 169 |
(if (type>= 'ext:non-negative-fixnum arg *cmp-env*) |
| 170 |
(values '(ext:non-negative-fixnum) |
| 171 |
'ext:non-negative-fixnum) |
| 172 |
(values '((integer 0 *)) '(integer 0 *)))) |