peephole.stk 8.14 KB
Newer Older
eg's avatar
eg committed
1 2
;;;;
;;;; peephole.stk	-- Peephole Optimiser fro the STklos VM
3 4 5 6
;;;;
;;;; Copyright © 2001-2007 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;;
;;;;
eg's avatar
eg committed
7 8 9 10
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
11
;;;;
eg's avatar
eg committed
12 13 14 15
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
16
;;;;
eg's avatar
eg committed
17 18
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
19
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
eg's avatar
eg committed
20
;;;; USA.
21
;;;;
eg's avatar
eg committed
22 23
;;;;           Author: Erick Gallesio [eg@unice.fr]
;;;;    Creation date: 17-Mar-2001 20:32 (eg)
Erick Gallesio's avatar
Erick Gallesio committed
24
;;;; Last file update: 22-Feb-2007 20:06 (eg)
eg's avatar
eg committed
25 26 27
;;;;

; ======================================================================
28
;
eg's avatar
eg committed
29
; 			    PEEPHOLE OPTIMIZER
30
;
eg's avatar
eg committed
31 32 33 34 35 36 37 38 39 40 41 42
; ======================================================================

(define (label? code)      (integer? (car code)))
(define (this-instr code)  (if (label? code) 'NOP (caar code)))
(define (next-instr code)  (if (label? (cdr code)) 'NOP (caadr code)))
(define this-arg1	   cadar)
(define this-arg2	   caddar)
(define next-arg1	   cadadr)
(define next-arg2	   (lambda (x) (car (cddadr x))))


(define (peephole all-code)
43

eg's avatar
eg committed
44 45 46 47 48 49 50 51
  (define (replace-1-instr code instr)
    (set-car! code instr))

  (define (replace-2-instr code instr)
    (set-car! code instr)
    (set-cdr! code (cddr code)))

  (define (optimize-goto code)
52
    ;;//FIXME: normallement all-code ===> mais c'est très lent
eg's avatar
eg committed
53
    ;// on devrait avoir (memq (this-arg1 code) code) pour chercher au-dessus.
54
    (let ((new-code (memq (this-arg1 code) code)))
eg's avatar
eg committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      (if new-code
	  (case (next-instr new-code)
	    ((GOTO)   (replace-1-instr code (copy-tree (cadr new-code))))
	    ((RETURN) (replace-1-instr code (copy-tree (cadr new-code))))
	    (else     (set! code (cdr code))))
	  (set! code (cdr code))))
	;(panic "label ~S is not defined in code ~S" (this-arg1 code) all-code)))
    code)

  (unless (null? all-code)
    (let Loop ((code all-code))
      (if (null? (cdr code))
	  (set! code (cdr code))	;; will stop the loop
	  (let ((i1 (this-instr code))
		(i2 (next-instr code)))
	    (cond
	     ;; [IM-FALSE, PUSH]	=> FALSE-PUSH
	     ;; [IM-TRUE, PUSH]		=> TRUE-PUSH
	     ;; [IM-NIL, PUSH]		=> NIL-PUSH
	     ;; [IM-MINUS1, PUSH]	=> MINUS1-PUSH
	     ;; [IM-ZERO, PUSH]		=> ZERO-PUSH
	     ;; [IM-ONE, PUSH]		=> ONE-PUSH
	     ;; [IM-VOID, PUSH]		=> VOID-PUSH
	     ((and (eq? i2 'PUSH) (memq i1 '(IM-FALSE IM-TRUE IM-NIL IM-MINUS1
					      IM-ZERO IM-ONE IM-VOID)))
	      (replace-2-instr code (list (case i1
					    ((IM-FALSE)  'FALSE-PUSH)
					    ((IM-TRUE)   'TRUE-PUSH)
					    ((IM-NIL)	 'NIL-PUSH)
					    ((IM-MINUS1) 'MINUS1-PUSH)
					    ((IM-ZERO)	 'ZERO-PUSH)
					    ((IM-ONE)	 'ONE-PUSH)
					    ((IM-VOID)	 'VOID-PUSH)))))
;/POURQUOI?  ;; [TAIL-INVOKE, RETURN] => TAIL-INVOKE (eliminate useless return)
;/	     ((and (eq? i1 'TAIL-INVOKE) (eq? i2 'RETURN))
;/	      (format #t "On a ~S\n\n" code)
;/	      (set-cdr! code (cons (list 'NOP) (cddr code)))
;/	      (format #t "     => ~S\n\n" code))

	     ;; [SMALL-INT, PUSH] => INT-PUSH
	     ((and (eq? i1 'SMALL-INT) (eq? i2 'PUSH))
	      (replace-2-instr code (list 'INT-PUSH (this-arg1 code))))

	     ;; [CONSTANT, PUSH] => CONSTANT-PUSH
	     ((and (eq? i1 'CONSTANT) (eq? i2 'PUSH))
	      (replace-2-instr code (list 'CONSTANT-PUSH (this-arg1 code))))

Erick Gallesio's avatar
Erick Gallesio committed
102 103 104
	     ;; [DEEP-LOCAL-REF, PUSH] => DEEP-LOC-REF-PUSH
	     ((and (eq? i1 'DEEP-LOCAL-REF) (eq? i2 'PUSH))
	      (replace-2-instr code (list 'DEEP-LOC-REF-PUSH (this-arg1 code))))
105

Erick Gallesio's avatar
Erick Gallesio committed
106

eg's avatar
eg committed
107 108 109 110
	     ;; [GOTO x], ... ,x: GOTO y => GOTO y
	     ;; [GOTO x], ... ,x: RETURN => RETURN
	     ((eq? i1 'GOTO)
	      (set! code (optimize-goto code)))
111

eg's avatar
eg committed
112 113 114 115 116 117 118 119 120 121 122
	     ;; [IN-NUMEQ ; IN-NOT] 	=> IN-NUMDIFF
	     ;; [IN-NUMDIFF; IN-NOT]	=> IN-EQ
	     ;; [IN-NUMLT ; IN-NOT] 	=> IN-NUMGE
	     ;; [IN-NUMLE ; IN-NOT] 	=> IN-NUMGT
	     ;; [IN-NUMGT ; IN-NOT] 	=> IN-NUMLE
	     ;; [IN-NUMGE ; IN-NOT] 	=> IN-NUMLT
	     ;; [IN-EQ; IN-NOT]		=> IN-NOT-EQ
	     ;; [IN-EQV; IN-NOT]	=> IN-NOT-EQV
	     ;; [IN-EQUAL; IN-NOT]	=> IN-NOT-EQUAL
	     ((and (eq? i2 'IN-NOT) (memq i1 '(IN-NUMEQ IN-NUMDIFF
					       IN-NUMLT IN-NUMGT
123
					       IN-NUMLE IN-NUMGE
eg's avatar
eg committed
124 125 126 127 128 129 130 131 132 133 134 135
					       IN-EQ IN-EQV IN-EQUAL)))
	      (replace-2-instr code
			       (list (case i1
				       ((IN-NUMEQ)   'IN-NUMDIFF)
				       ((IN-NUMDIFF) 'IN-NUMEQ)
				       ((IN-NUMLT)   'IN-NUMGE)
				       ((IN-NUMGT)   'IN-NUMLE)
				       ((IN-NUMLE)   'IN-NUMGT)
				       ((IN-NUMGE)   'IN-NUMLT)
				       ((IN-EQ)      'IN-NOT-EQ)
				       ((IN-EQV)     'IN-NOT-EQV)
				       ((IN-EQUAL)   'IN-NOT-EQUAL)))))
136

eg's avatar
eg committed
137 138 139 140 141 142 143 144 145 146 147 148 149
	     ;; [IN-NUMEQ ; JUMP-FALSE] 	=> JUMP-NUMDIFF
	     ;; [IN-NUMDIFF; JUMP-FALSE]	=> JUMP-NUMEQ
	     ;; [IN-NUMLT ; JUMP-FALSE] 	=> JUMP-NUMGE
	     ;; [IN-NUMLE ; JUMP-FALSE] 	=> JUMP-NUMGT
	     ;; [IN-NUMGT ; JUMP-FALSE] 	=> JUMP-NUMLE
	     ;; [IN-NUMGE ; JUMP-FALSE] 	=> JUMP-NUMLT
	     ;; [IN-EQ; JUMP-FALSE]		=> JUMP-NOT-EQ
	     ;; [IN-EQV; JUMP-FALSE]		=> JUMP-NOT-EQ
	     ;; [IN-EQUAL; JUMP-FALSE]		=> JUMP-NOT-EQUAL
	     ;; [IN-NOT; JUMP-FALSE]		=> JUMP-TRUE
	     ((and (eq? i2 'JUMP-FALSE) (memq i1 '(IN-NUMEQ ; IN-NUMDIFF
						   IN-NUMLT IN-NUMGT
						   IN-NUMLE IN-NUMGE
150
						   IN-EQ IN-EQV IN-EQUAL
eg's avatar
eg committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164
						   IN-NOT)))
	      (replace-2-instr code
			       (list (case i1
				       ((IN-NUMEQ)   'JUMP-NUMDIFF)
				       ((IN-NUMDIFF) 'JUMP-NUMEQ)
				       ((IN-NUMLT)   'JUMP-NUMGE)
				       ((IN-NUMLE)   'JUMP-NUMGT)
				       ((IN-NUMGT)   'JUMP-NUMLE)
				       ((IN-NUMGE)   'JUMP-NUMLT)
				       ((IN-EQ)      'JUMP-NOT-EQ)
				       ((IN-EQV)     'JUMP-NOT-EQV)
				       ((IN-EQUAL)   'JUMP-NOT-EQUAL)
				       ((IN-NOT)     'JUMP-TRUE))
				     (next-arg1 code))))
Erick Gallesio's avatar
.  
Erick Gallesio committed
165 166 167 168 169 170 171 172 173 174
	     ;; [GLOBAL-REF, PUSH] => GLOBAL-REF-PUSH
	     ((and (eq? i1 'GLOBAL-REF) (eq? i2 'PUSH))
	      (replace-2-instr code (list 'GLOBAL-REF-PUSH
					  (this-arg1 code))))


	     ;; [PUSH GLOBAL-REF] => PUSH-GLOBAL-REF
	     ((and (eq? i1 'PUSH) (eq? i2 'GLOBAL-REF))
	      (replace-2-instr code (list 'PUSH-GLOBAL-REF
					  (next-arg1 code))))
eg's avatar
eg committed
175

Erick Gallesio's avatar
.  
Erick Gallesio committed
176 177 178 179 180
	     ;; [PUSH-GLOBAL-REF, INVOKE] => PUSH-GREF-INVOKE
	     ((and (eq? i1 'PUSH-GLOBAL-REF) (eq? i2 'INVOKE))
	      (replace-2-instr code (list 'PUSH-GREF-INVOKE
					  (this-arg1 code)
					  (next-arg1 code))))
Erick Gallesio's avatar
.  
Erick Gallesio committed
181

Erick Gallesio's avatar
.  
Erick Gallesio committed
182 183 184 185 186
	     ;; [PUSH-GLOBAL-REF, TAIL-INVOKE] => PUSH-GREF-TAIL-INV
	     ((and (eq? i1 'PUSH-GLOBAL-REF) (eq? i2 'TAIL-INVOKE))
	      (replace-2-instr code (list 'PUSH-GREF-TAIL-INV
					  (this-arg1 code)
					  (next-arg1 code))))
187

Erick Gallesio's avatar
.  
Erick Gallesio committed
188 189 190 191

	     ;; [PUSH, PREPARE-CALL] => PUSH-PREPARE-CALL
	     ((and (eq? i1 'PUSH) (eq? i2 'PREPARE-CALL))
	      (replace-2-instr code (list 'PUSH-PREPARE-CALL)))
192 193 194



eg's avatar
eg committed
195 196
	     ;; [GLOBAL-REF, INVOKE] => GREF-INVOKE
	     ((and (eq? i1 'GLOBAL-REF) (eq? i2 'INVOKE))
197
	      (replace-2-instr code (list 'GREF-INVOKE
eg's avatar
eg committed
198 199
					  (this-arg1 code)
					  (next-arg1 code))))
200 201 202

	     ;; [GLOBAL-REF, TAIL-INVOKE] => GREF-TAIL(INVOKE
	     ((and (eq? i1 'GLOBAL-REF) (eq? i2 'TAIL-INVOKE))
203
	      (replace-2-instr code (list 'GREF-TAIL-INVOKE
204 205
					  (this-arg1 code)
					  (next-arg1 code))))
206

207 208 209 210 211 212 213 214 215 216 217
	     ;; [LOCAL-REFx, PUSH] => LOCAL-REFx-PUSH
	     ((and (eq? i2 'PUSH) (memq i1 '(LOCAL-REF0 LOCAL-REF1
					     LOCAL-REF2 LOCAL-REF3
					     LOCAL-REF4)))
	      (replace-2-instr code
			       (list (case i1
				       ((LOCAL-REF0) 'LOCAL-REF0-PUSH)
				       ((LOCAL-REF1) 'LOCAL-REF1-PUSH)
				       ((LOCAL-REF2) 'LOCAL-REF2-PUSH)
				       ((LOCAL-REF3) 'LOCAL-REF3-PUSH)
				       ((LOCAL-REF4) 'LOCAL-REF4-PUSH)))))
218 219 220 221

	     ;; [RETURN; RETURN] => [RETURN]
	     ((and (eq? i1 'RETURN) (eq? i2 'RETURN))
	      (replace-2-instr code (list 'RETURN)))
222

eg's avatar
eg committed
223 224 225 226
	     (else ;; No optimization; goto next instruction
	      (set! code (cdr code))))))
      ;; Loop again on the same instruction
      (unless (null? code) (Loop code))))
227

eg's avatar
eg committed
228 229 230 231
  ;; return the optimized code
  all-code)

;(define (peephole c) c)