* Constant folding
@ 2009-09-08 13:17 Ludovic Courtès
2009-09-16 19:25 ` Andy Wingo
0 siblings, 1 reply; 2+ messages in thread
From: Ludovic Courtès @ 2009-09-08 13:17 UTC (permalink / raw)
To: guile-devel
[-- Attachment #1: Type: text/plain, Size: 114 bytes --]
Hello!
We should implement constant folding in the tree-il->glil pass. A naive
implementation looks like this:
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 2480 bytes --]
diff --git a/module/language/tree-il/compile-glil.scm b/module/language/tree-il/compile-glil.scm
index 86b610f..57a46c8 100644
--- a/module/language/tree-il/compile-glil.scm
+++ b/module/language/tree-il/compile-glil.scm
@@ -22,6 +22,7 @@
#:use-module (system base syntax)
#:use-module (system base pmatch)
#:use-module (system base message)
+ #:use-module (srfi srfi-1)
#:use-module (ice-9 receive)
#:use-module (language glil)
#:use-module (system vm instruction)
@@ -394,20 +395,28 @@
(cons (primitive-ref-name proc) (length args)))
(hash-ref *primcall-ops* (primitive-ref-name proc))))
=> (lambda (op)
- (for-each comp-push args)
- (emit-code src (make-glil-call op (length args)))
- (case (instruction-pushes op)
- ((0)
- (case context
- ((tail push vals) (emit-code #f (make-glil-void))))
- (maybe-emit-return))
- ((1)
- (case context
- ((drop) (emit-code #f (make-glil-call 'drop 1))))
- (maybe-emit-return))
- (else
- (error "bad primitive op: too many pushes"
- op (instruction-pushes op))))))
+ (if (every const? args)
+ (let* ((proc (module-ref the-scm-module
+ (primitive-ref-name proc)))
+ (args (map const-exp args)))
+ ;; constant folding
+ (emit-code src
+ (make-glil-const (apply proc args))))
+ (begin
+ (for-each comp-push args)
+ (emit-code src (make-glil-call op (length args)))
+ (case (instruction-pushes op)
+ ((0)
+ (case context
+ ((tail push vals) (emit-code #f (make-glil-void))))
+ (maybe-emit-return))
+ ((1)
+ (case context
+ ((drop) (emit-code #f (make-glil-call 'drop 1))))
+ (maybe-emit-return))
+ (else
+ (error "bad primitive op: too many pushes"
+ op (instruction-pushes op))))))))
;; da capo al fine
((and (lexical-ref? proc)
[-- Attachment #3: Type: text/plain, Size: 1142 bytes --]
With that we get:
--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,c (+ 2 3)
Disassembly of #<objcode b214e0>:
0 (make-int8 5) ;; 5
--8<---------------cut here---------------end--------------->8---
instead of:
--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,c (+ 2 3)
Disassembly of #<objcode ceb3a0>:
0 (make-int8 2) ;; 2
2 (make-int8 3) ;; 3
4 (add)
5 (return)
--8<---------------cut here---------------end--------------->8---
but:
--8<---------------cut here---------------start------------->8---
scheme@(guile-user)> ,c (+ 2 (+ 2 3) 4)
Disassembly of #<objcode b68ba0>:
0 (make-int8 2) ;; 2
2 (make-int8 5) ;; 5
4 (make-int8 4) ;; 4
6 (add)
7 (add)
8 (return)
--8<---------------cut here---------------end--------------->8---
Thanks,
Ludo’.
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: Constant folding
2009-09-08 13:17 Constant folding Ludovic Courtès
@ 2009-09-16 19:25 ` Andy Wingo
0 siblings, 0 replies; 2+ messages in thread
From: Andy Wingo @ 2009-09-16 19:25 UTC (permalink / raw)
To: Ludovic Courtès; +Cc: guile-devel
On Tue 08 Sep 2009 15:17, ludo@gnu.org (Ludovic Courtès) writes:
> We should implement constant folding in the tree-il->glil pass. A naive
> implementation looks like this:
>
> diff --git a/module/language/tree-il/compile-glil.scm b/module/language/tree-il/compile-glil.scm
> index 86b610f..57a46c8 100644
> --- a/module/language/tree-il/compile-glil.scm
> +++ b/module/language/tree-il/compile-glil.scm
Do it as a tree-il -> tree-il pass, as part of (language tree-il
optimize), and you'll be happer. It will be easier to unit test, too.
I think we should simply implement Waddell's algorithm.
Andy
--
http://wingolog.org/
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-09-16 19:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-08 13:17 Constant folding Ludovic Courtès
2009-09-16 19:25 ` Andy Wingo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).