From 9618e3536460864ab8c9f5cda6a0bb162d6501bb Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 11 May 2017 17:53:47 +0000 Subject: [PATCH] Experimental beta expansion code --- scheme/cyclone/cps-optimizations.sld | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/scheme/cyclone/cps-optimizations.sld b/scheme/cyclone/cps-optimizations.sld index 8c9fb409..dc02dbe1 100644 --- a/scheme/cyclone/cps-optimizations.sld +++ b/scheme/cyclone/cps-optimizations.sld @@ -631,6 +631,12 @@ ,(opt:contract (if->else exp)))))) ; Application: ((app? exp) + ;; Hack to test this idea + ;; TODO: was testing this with the fibc program + ;(if (beta-expand? exp) + ; (set! exp (beta-expand exp))) + ;; END + (let* ((fnc (opt:contract (car exp)))) (cond ((and (ast:lambda? fnc) @@ -1149,6 +1155,44 @@ (else (error `(Unexpected expression passed to find inlinable vars ,exp))))) + (define (beta-expand? exp) + (cond + ((and (app? exp) + (ref? (car exp))) + (with-var (car exp) (lambda (var) + (= 1 (adbv:app-fnc-count var))))) ;; TODO: too simplistic + (else #f))) + + (define (beta-expand exp) + (let* ((args (cdr exp)) + (var (adb:get (car exp))) + ;; Function definition, or #f if none + (fnc (adbv:assigned-value var)) + (formals (if (ast:lambda? fnc) (ast:lambda-args fnc) '())) + ;; First formal, or #f if none + (maybe-cont (if (and (list? formals) (pair? formals)) + (car formals) + #f)) + ;; function's continuation symbol, or #f if none + (cont (if maybe-cont + (with-var maybe-cont (lambda (var) + (if (adbv:cont? var) maybe-cont #f))) + #f)) + ) +(trace:error `(JAE beta expand ,exp ,var ,fnc ,formals ,cont)) + (cond + ;; TODO: first arg to the lambda could be a cont, in which + ;; case it needs to be removed from formals list and body + ((and (ast:lambda? fnc) + (or ;(= (length args) (length formals)) + (and (= (length args) (- (length formals) 1)) + cont))) + ;;todo: set up a map, and replace each formal with its corresponding arg + (trace:error `(JAE DEBUG beta expand ,exp)) + exp + ) + (else exp)))) ;; beta expansion failed + (define (analyze-cps exp) (analyze-find-lambdas exp -1) (analyze-lambda-side-effects exp -1)