home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / compiler / machines / C / rulflo.scm < prev    next >
Text File  |  1999-01-02  |  8KB  |  230 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: rulflo.scm,v 1.5 1999/01/02 06:06:43 cph Exp $
  4.  
  5. Copyright (c) 1992-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; LAP Generation Rules: Flonum rules
  23. ;;; package: (compiler lap-syntaxer)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define-rule statement
  28.   ;; convert a floating-point number to a flonum object
  29.   (ASSIGN (REGISTER (? target))
  30.       (FLOAT->OBJECT (REGISTER (? source))))
  31.   (let ((source (standard-source! source 'DOUBLE)))
  32.     (let ((target (standard-target! target 'SCHEME_OBJECT)))
  33.       (LAP "INLINE_DOUBLE_TO_FLONUM (" ,source ", " ,target ");\n\t"))))
  34.  
  35. (define-rule statement
  36.   ;; convert a flonum object to a floating-point number
  37.   (ASSIGN (REGISTER (? target)) (OBJECT->FLOAT (REGISTER (? source))))
  38.   (let ((source (standard-source! source 'SCHEME_OBJECT)))
  39.     (let ((target (standard-target! target 'DOUBLE)))
  40.       (LAP ,target " = (FLONUM_TO_DOUBLE (" ,source "));\n\t"))))
  41.  
  42. ;;;; Floating-point vector support
  43.  
  44. (define-rule statement
  45.   (ASSIGN (REGISTER (? target))
  46.       (FLOAT-OFFSET (REGISTER (? base))
  47.             (MACHINE-CONSTANT (? offset))))
  48.   (standard-unary-conversion
  49.    base 'DOUBLE*
  50.    target 'DOUBLE
  51.    (lambda (base target)
  52.      (LAP ,target " = " ,base "[" ,offset "];\n\t"))))
  53.   
  54. (define-rule statement
  55.   (ASSIGN (FLOAT-OFFSET (REGISTER (? base))
  56.             (MACHINE-CONSTANT (? offset)))
  57.       (REGISTER (? source)))
  58.   (let ((base (standard-source! base 'DOUBLE*))
  59.     (source (standard-source! source 'DOUBLE)))
  60.     (LAP ,base "[" ,offset "] = " ,source ";\n\t")))
  61.  
  62. (define-rule statement
  63.   (ASSIGN (REGISTER (? target))
  64.       (FLOAT-OFFSET (REGISTER (? base)) (REGISTER (? index))))
  65.   (standard-binary-conversion
  66.    base 'DOUBLE*
  67.    index 'LONG
  68.    target 'DOUBLE
  69.    (lambda (base index target)
  70.      (LAP ,target " = " ,base "[" ,index "];\n\t"))))
  71.  
  72. (define-rule statement
  73.   (ASSIGN (FLOAT-OFFSET (REGISTER (? base)) (REGISTER (? index)))
  74.       (REGISTER (? source)))
  75.   (let ((base (standard-source! base 'DOUBLE*))
  76.     (source (standard-source! source 'DOUBLE))
  77.     (index (standard-source! index 'LONG)))
  78.     (LAP ,base "[" ,index "] = " ,source ";\n\t")))
  79.  
  80. (define-rule statement
  81.   (ASSIGN (REGISTER (? target))
  82.       (FLOAT-OFFSET (OFFSET-ADDRESS (REGISTER (? base))
  83.                     (MACHINE-CONSTANT (? w-offset)))
  84.             (MACHINE-CONSTANT (? f-offset))))
  85.   (standard-unary-conversion
  86.    base 'SCHEME_OBJECT*
  87.    target 'DOUBLE
  88.    (lambda (base target)
  89.      (LAP ,target
  90.       " = ((double *) &" ,base "[" ,w-offset "])[" ,f-offset "];\n\t"))))
  91.  
  92. (define-rule statement
  93.   (ASSIGN (FLOAT-OFFSET (OFFSET-ADDRESS (REGISTER (? base))
  94.                     (MACHINE-CONSTANT (? w-offset)))
  95.             (MACHINE-CONSTANT (? f-offset)))
  96.       (REGISTER (? source)))
  97.   (let ((base (standard-source! base 'SCHEME_OBJECT*))
  98.     (source (standard-source! source 'DOUBLE)))
  99.     (LAP "((double *) &" ,base "[" ,w-offset "])[" ,f-offset "] = "
  100.      ,source ";\n\t")))
  101.  
  102. (define-rule statement
  103.   (ASSIGN (REGISTER (? target))
  104.       (FLOAT-OFFSET (OFFSET-ADDRESS (REGISTER (? base))
  105.                     (MACHINE-CONSTANT (? w-offset)))
  106.             (REGISTER (? index))))
  107.   (standard-binary-conversion
  108.    base 'SCHEME_OBJECT*
  109.    index 'LONG
  110.    target 'DOUBLE
  111.    (lambda (base index target)
  112.      (LAP ,target
  113.       " = ((double *) &" ,base "[" ,w-offset "])[" ,index "];\n\t"))))
  114.  
  115. (define-rule statement
  116.   (ASSIGN (FLOAT-OFFSET (OFFSET-ADDRESS (REGISTER (? base))
  117.                     (MACHINE-CONSTANT (? w-offset)))
  118.             (REGISTER (? index)))
  119.       (REGISTER (? source)))
  120.   (let ((base (standard-source! base 'SCHEME_OBJECT*))
  121.     (index (standard-source! index 'LONG))
  122.     (source (standard-source! source 'DOUBLE)))
  123.     (LAP "((double *) &" ,base "[" ,w-offset "])[" ,index "] = "
  124.      ,source ";\n\t")))
  125.  
  126. ;;;; Flonum Arithmetic
  127.  
  128. (define-rule statement
  129.   (ASSIGN (REGISTER (? target))
  130.       (FLONUM-1-ARG (? operation) (REGISTER (? source)) (? overflow?)))
  131.   overflow?                ;ignore
  132.   (let ((source (standard-source! source 'DOUBLE)))
  133.     ((flonum-1-arg/operator operation)
  134.      (standard-target! target 'DOUBLE)
  135.      source)))
  136.  
  137. (define (flonum-1-arg/operator operation)
  138.   (lookup-arithmetic-method operation flonum-methods/1-arg))
  139.  
  140. (define flonum-methods/1-arg
  141.   (list 'FLONUM-METHODS/1-ARG))
  142.  
  143. (define-arithmetic-method 'FLONUM-ABS flonum-methods/1-arg
  144.   (lambda (target source)
  145.     (LAP ,target " =  ((" ,source " >= 0.) ? " ,source " : (-" ,source
  146.      "));\n\t")))
  147.  
  148. (define-arithmetic-method 'FLONUM-NEGATE flonum-methods/1-arg
  149.   (lambda (target source)
  150.     (LAP ,target " = (- " ,source ");\n\t")))
  151.  
  152. (let ((define-use-function
  153.     (lambda (name function)
  154.       (define-arithmetic-method name flonum-methods/1-arg
  155.         (lambda (target source)
  156.           (LAP ,target " = (" ,function " (" ,source "));\n\t"))))))
  157.   (define-use-function 'FLONUM-ACOS "DOUBLE_ACOS")
  158.   (define-use-function 'FLONUM-ASIN "DOUBLE_ASIN")
  159.   (define-use-function 'FLONUM-ATAN "DOUBLE_ATAN")
  160.   (define-use-function 'FLONUM-CEILING "DOUBLE_CEILING")
  161.   (define-use-function 'FLONUM-COS "DOUBLE_COS")
  162.   (define-use-function 'FLONUM-EXP "DOUBLE_EXP")
  163.   (define-use-function 'FLONUM-FLOOR "DOUBLE_FLOOR")
  164.   (define-use-function 'FLONUM-LOG "DOUBLE_LOG")
  165.   (define-use-function 'FLONUM-ROUND "DOUBLE_ROUND")
  166.   (define-use-function 'FLONUM-SIN "DOUBLE_SIN")
  167.   (define-use-function 'FLONUM-SQRT "DOUBLE_SQRT")
  168.   (define-use-function 'FLONUM-TAN "DOUBLE_TAN")
  169.   (define-use-function 'FLONUM-TRUNCATE "DOUBLE_TRUNCATE"))
  170.  
  171. (define-rule statement
  172.   (ASSIGN (REGISTER (? target))
  173.       (FLONUM-2-ARGS (? operation)
  174.              (REGISTER (? source1))
  175.              (REGISTER (? source2))
  176.              (? overflow?)))
  177.   overflow?                ;ignore
  178.   (let ((source1 (standard-source! source1 'DOUBLE))
  179.     (source2 (standard-source! source2 'DOUBLE)))
  180.     ((flonum-2-args/operator operation)
  181.      (standard-target! target 'DOUBLE)
  182.      source1
  183.      source2)))
  184.  
  185. (define (flonum-2-args/operator operation)
  186.   (lookup-arithmetic-method operation flonum-methods/2-args))
  187.  
  188. (define flonum-methods/2-args
  189.   (list 'FLONUM-METHODS/2-ARGS))
  190.  
  191. (let-syntax
  192.     ((define-flonum-operation
  193.        (macro (primitive-name opcode)
  194.      `(define-arithmetic-method ',primitive-name flonum-methods/2-args
  195.         (lambda (target source1 source2)
  196.           (LAP ,',target " = (" ,',source1 ,opcode ,',source2
  197.            ");\n\t"))))))
  198.   (define-flonum-operation flonum-add " + ")
  199.   (define-flonum-operation flonum-subtract " - ")
  200.   (define-flonum-operation flonum-multiply " * ")
  201.   (define-flonum-operation flonum-divide " / "))
  202.  
  203. (define-arithmetic-method 'FLONUM-ATAN2 flonum-methods/2-args
  204.   (lambda (target source1 source2)
  205.     (LAP ,target " = (DOUBLE_ATAN2 (" ,source1 ", " ,source2
  206.      "));\n\t")))
  207.  
  208. ;;;; Flonum Predicates
  209.  
  210. (define-rule predicate
  211.   (FLONUM-PRED-1-ARG (? predicate) (REGISTER (? source)))
  212.   (compare (case predicate
  213.          ((FLONUM-ZERO?) " == ")
  214.          ((FLONUM-NEGATIVE?) " < ")
  215.          ((FLONUM-POSITIVE?) " > ")
  216.          (else (error "unknown flonum predicate" predicate)))
  217.        (standard-source! source 'DOUBLE)
  218.        "0.0"))
  219.  
  220. (define-rule predicate
  221.   (FLONUM-PRED-2-ARGS (? predicate)
  222.               (REGISTER (? source1))
  223.               (REGISTER (? source2)))
  224.   (compare (case predicate
  225.          ((FLONUM-EQUAL?) " == ")
  226.          ((FLONUM-LESS?) " < ")
  227.          ((FLONUM-GREATER?) " > ")
  228.          (else (error "unknown flonum predicate" predicate)))
  229.        (standard-source! source1 'DOUBLE)
  230.        (standard-source! source2 'DOUBLE)))