From 1ee99d97db342ab6c8e3f81f697ebe6caf38478b Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Sat, 1 Aug 2020 22:52:47 +0200 Subject: [PATCH] Baseline compiler emits eq-immediate? as appropriate * module/language/tree-il/compile-bytecode.scm (eq?): Define eq-immediate? as immediate emitter. (canonicalize): Don't fuss so much about eq?; only if commutation is needed. (Perhaps a more generic commutation pass is needed.) (compile-closure): Add support for emit/immediate for branches. --- module/language/tree-il/compile-bytecode.scm | 49 ++++++++++++-------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/module/language/tree-il/compile-bytecode.scm b/module/language/tree-il/compile-bytecode.scm index 59bed8d27..b6569c7dd 100644 --- a/module/language/tree-il/compile-bytecode.scm +++ b/module/language/tree-il/compile-bytecode.scm @@ -295,7 +295,15 @@ (eq? #:nargs 2 #:predicate? #t #:emit (lambda (asm a b kf) (emit-eq? asm a b) - (emit-jne asm kf))) + (emit-jne asm kf)) + #:immediate-in-range? (lambda (x) + (and=> + (scm->immediate-bits x) + (lambda (bits) + (truncate-bits bits 16 x)))) + #:emit/immediate (lambda (asm a b kf) + (emit-eq-immediate? asm a b) + (emit-jne asm kf))) (< #:nargs 2 #:predicate? #t #:emit (lambda (asm a b kf) (emit- src '>= (a b)) (reify-branch src '<= (list b a))) (($ src '> (a b)) (reify-branch src '< (list b a))) - ;; Specialize eq?. - (($ src 'eq? (a b)) - (let ((a (if (const? b) a b)) - (b (if (const? b) b a))) - (define (simplify test) (reify-branch src test (list a))) - (match b - (($ _ '()) (simplify 'eq-null?)) - (($ _ #f) (simplify 'eq-false?)) - (($ _ #t) (simplify 'eq-true?)) - (($ _ #nil) (simplify 'eq-nil?)) - (($ _ (? unspecified?)) (simplify 'unspecified?)) - (($ _ (? eof-object?)) (simplify 'eof-object?)) - (_ (reify-branch src 'eq? (list a b)))))) + ;; For eq? on constants, make the second arg the constant. + (($ src 'eq? ((and a ($ )) + (and b (not ($ ))))) + (reify-branch src 'eq? (list b a))) ;; Simplify "not". (($ src 'not (x)) @@ -873,14 +872,26 @@ in the frame with for the lambda-case clause @var{clause}." (($ src ($ tsrc name args) consequent alternate) (maybe-emit-source tsrc) - (let ((emit (primitive-emitter (lookup-primitive name))) - (args (for-args args env)) + (let ((prim (lookup-primitive name)) (kf (gensym "false")) (kdone (gensym "done"))) - (maybe-emit-source src) + (define (emit/immediate? val) + (and=> (primitive-immediate-in-range-predicate prim) + (lambda (pred) (pred val)))) (match args - ((a) (emit asm a kf)) - ((a b) (emit asm a b kf))) + ((a ($ _ (? emit/immediate? b))) + (let ((emit (primitive-emitter/immediate prim))) + (match (for-args (list a) env) + ((a) + (maybe-emit-source src) + (emit asm a b kf))))) + (_ + (let ((emit (primitive-emitter prim)) + (args (for-args args env))) + (maybe-emit-source src) + (match args + ((a) (emit asm a kf)) + ((a b) (emit asm a b kf)))))) (for-context consequent env ctx) (unless (eq? ctx 'tail) (emit-j asm kdone))