mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-17 17:20:29 +02:00
add1 and sub1 instructions
* libguile/vm-i-scheme.c: Add add1 and sub1 instructions. * module/language/tree-il/compile-glil.scm: Compile 1+ and 1- to add1 and sub1. * module/language/tree-il/primitives.scm (define-primitive-expander): Add support for `if' statements in the consequent. (+, -): Compile (- x 1), (+ x 1), and (+ 1 x) to 1- or 1+ as appropriate. (1-): Remove this one. Seems we forgot 1+ before, but we weren't compiling it nicely anyway. * test-suite/tests/tree-il.test ("void"): Fix expected compilation of (+ (void) 1) to allow for add1.
This commit is contained in:
parent
f4863880f5
commit
7382f23e58
4 changed files with 52 additions and 6 deletions
|
@ -85,6 +85,8 @@
|
|||
((>= . 2) . ge?)
|
||||
((+ . 2) . add)
|
||||
((- . 2) . sub)
|
||||
((1+ . 1) . add1)
|
||||
((1- . 1) . sub1)
|
||||
((* . 2) . mul)
|
||||
((/ . 2) . div)
|
||||
((quotient . 2) . quo)
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
;;; Code:
|
||||
|
||||
(define-module (language tree-il primitives)
|
||||
#:use-module (system base pmatch)
|
||||
#:use-module (rnrs bytevector)
|
||||
#:use-module (system base syntax)
|
||||
#:use-module (language tree-il)
|
||||
|
@ -142,8 +143,14 @@
|
|||
(define (consequent exp)
|
||||
(cond
|
||||
((pair? exp)
|
||||
`(make-application src (make-primitive-ref src ',(car exp))
|
||||
,(inline-args (cdr exp))))
|
||||
(pmatch exp
|
||||
((if ,test ,then ,else)
|
||||
`(if ,test
|
||||
,(consequent then)
|
||||
,(consequent else)))
|
||||
(else
|
||||
`(make-application src (make-primitive-ref src ',(car exp))
|
||||
,(inline-args (cdr exp))))))
|
||||
((symbol? exp)
|
||||
;; assume locally bound
|
||||
exp)
|
||||
|
@ -163,6 +170,15 @@
|
|||
(define-primitive-expander +
|
||||
() 0
|
||||
(x) x
|
||||
(x y) (if (and (const? y)
|
||||
(let ((y (const-exp y)))
|
||||
(and (exact? y) (= y 1))))
|
||||
(1+ x)
|
||||
(if (and (const? x)
|
||||
(let ((x (const-exp x)))
|
||||
(and (exact? x) (= x 1))))
|
||||
(1+ y)
|
||||
(+ x y)))
|
||||
(x y z . rest) (+ x (+ y z . rest)))
|
||||
|
||||
(define-primitive-expander *
|
||||
|
@ -172,11 +188,13 @@
|
|||
|
||||
(define-primitive-expander -
|
||||
(x) (- 0 x)
|
||||
(x y) (if (and (const? y)
|
||||
(let ((y (const-exp y)))
|
||||
(and (exact? y) (= y 1))))
|
||||
(1- x)
|
||||
(- x y))
|
||||
(x y z . rest) (- x (+ y z . rest)))
|
||||
|
||||
(define-primitive-expander 1-
|
||||
(x) (- x 1))
|
||||
|
||||
(define-primitive-expander /
|
||||
(x) (/ 1 x)
|
||||
(x y z . rest) (/ x (* y z . rest)))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue