From 5ffa085c342ee804a079270f0075362819c0e514 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Sat, 21 May 2016 00:30:47 -0400 Subject: [PATCH] WIP --- scheme/cyclone/cps-optimizations.sld | 49 ++++++++++++++++++++++------ test-opt.scm | 5 +-- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/scheme/cyclone/cps-optimizations.sld b/scheme/cyclone/cps-optimizations.sld index e236ca44..fbcf1398 100644 --- a/scheme/cyclone/cps-optimizations.sld +++ b/scheme/cyclone/cps-optimizations.sld @@ -17,8 +17,8 @@ ; can write initial analyze, but can't get too far without being able ; to uniquely ID each lambda -;(define-library (optimize-cps) -(define-library (scheme cyclone cps-optimizations) +(define-library (cps-optimizations) +;(define-library (scheme cyclone cps-optimizations) (import (scheme base) (scheme cyclone util) (scheme cyclone ast) @@ -38,11 +38,16 @@ adb:make-var %adb:make-var adb:variable? - adbv:global + adbv:global? adbv:set-global! - adbv:defined-by adbv:set-defined-by! - adbv:assigned adbv:set-assigned! - adbv:assigned-locally adbv:set-assigned-locally! + adbv:defined-by + adbv:set-defined-by! + adbv:assigned? + adbv:set-assigned! + adbv:assigned-locally? + adbv:set-assigned-locally! + adbv:const-value? + adbv:set-const-value! adbv:ref-by adbv:set-ref-by! ;; Analyze functions @@ -65,14 +70,16 @@ (define-record-type (%adb:make-var global defined-by assigned assigned-locally) adb:variable? - (global adbv:global adbv:set-global!) + (global adbv:global? adbv:set-global!) (defined-by adbv:defined-by adbv:set-defined-by!) - (assigned adbv:assigned adbv:set-assigned!) - (assigned-locally adbv:assigned-locally adbv:set-assigned-locally!) + (assigned adbv:assigned? adbv:set-assigned!) + (assigned-locally adbv:assigned-locally? adbv:set-assigned-locally!) + (const adbv:const? adbv:set-const!) + (const-value adbv:const-value? adbv:set-const-value!) (ref-by adbv:ref-by adbv:set-ref-by!) ) (define (adb:make-var) - (%adb:make-var '? '? '? '? '())) + (%adb:make-var '? '? '? '? #f #f '())) (define-record-type (%adb:make-fnc simple unused-params) @@ -83,6 +90,20 @@ (define (adb:make-fnc) (%adb:make-fnc '? '?)) + ;; A constant value that cannot be mutated + ;; A variable only ever assigned to one of these could have all + ;; instances of itself replaced with the value. + (define (const-atomic? exp) + (or (integer? exp) + (real? exp) + ;(string? exp) + ;(vector? exp) + ;(bytevector? exp) + (char? exp) + (boolean? exp))) + +;; TODO: check app for const/const-value, also (for now) reset them +;; if the variable is modified via set/define (define (analyze exp lid) ;(tre:error `(analyze ,lid ,exp)) (cond @@ -129,6 +150,14 @@ ; Application: ((app? exp) + + ;; TODO: if ast-lambda (car), + ;; for each arg + ;; if arg is const-atomic + ;; mark the parameter (variable) as const and give it const-val + ;; + ;; obviously need to add code later on to reset const if mutated + (for-each (lambda (e) (analyze e lid)) diff --git a/test-opt.scm b/test-opt.scm index c2dc4e29..7072b4a0 100644 --- a/test-opt.scm +++ b/test-opt.scm @@ -8,10 +8,11 @@ (import (scheme base) (scheme write)) -(let ((x 1)) +(let ((x 1) (z 3)) (define y 2) + (define w 4) (write - (+ x y))) + (+ w x y z))) (define (test a) (write 3))