| 1 |
#!/usr/bin/env ikarus --script |
| 2 |
|
| 3 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 4 |
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum |
| 5 |
;;; |
| 6 |
;;; This program is free software: you can redistribute it and/or modify |
| 7 |
;;; it under the terms of the GNU General Public License version 3 as |
| 8 |
;;; published by the Free Software Foundation. |
| 9 |
;;; |
| 10 |
;;; This program is distributed in the hope that it will be useful, but |
| 11 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
;;; General Public License for more details. |
| 14 |
;;; |
| 15 |
;;; You should have received a copy of the GNU General Public License |
| 16 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
|
| 19 |
(define counter 0) |
| 20 |
(define (asm-test res ls) |
| 21 |
(set! counter (add1 counter)) |
| 22 |
(printf "[~s] Testing:\n" counter) |
| 23 |
(for-each (lambda (x) |
| 24 |
(printf " ~s\n" x)) |
| 25 |
ls) |
| 26 |
(let ([code |
| 27 |
(car (#%list*->code* |
| 28 |
(lambda (x) #f) |
| 29 |
`([0 (label ,(gensym)) . ,ls])))]) |
| 30 |
(let ([proc (#%$code->closure code)]) |
| 31 |
(let ([v (proc)]) |
| 32 |
(printf "running\n") |
| 33 |
(unless (equal? v res) |
| 34 |
(printf "failed!\n") |
| 35 |
(error 'test-asm "expected ~s, got ~s" res v))))) |
| 36 |
(printf "OK\n\n")) |
| 37 |
|
| 38 |
(asm-test 12 |
| 39 |
'([movl 48 %eax] |
| 40 |
[ret])) |
| 41 |
|
| 42 |
(asm-test 12 |
| 43 |
'([movl 16 %eax] |
| 44 |
[orl 32 %eax] |
| 45 |
[ret])) |
| 46 |
|
| 47 |
(asm-test 12 |
| 48 |
'([movl 48 %eax] |
| 49 |
[movl %eax (disp -4 %esp)] |
| 50 |
[movl 0 %eax] |
| 51 |
[movl (disp -4 %esp) %eax] |
| 52 |
[ret])) |
| 53 |
|
| 54 |
(asm-test 12 |
| 55 |
'([movl 16 %eax] |
| 56 |
[movl %eax (disp -4 %esp)] |
| 57 |
[addl 32 (disp -4 %esp)] |
| 58 |
[movl (disp -4 %esp) %eax] |
| 59 |
[ret])) |
| 60 |
|
| 61 |
(asm-test 12 |
| 62 |
'([movl 16 %eax] |
| 63 |
[movl %eax (disp -200 %esp)] |
| 64 |
[addl 32 (disp -200 %esp)] |
| 65 |
[movl (disp -200 %esp) %eax] |
| 66 |
[ret])) |
| 67 |
|
| 68 |
(asm-test 1 |
| 69 |
'([movl 8 %eax] |
| 70 |
[movl %eax (disp -4 %esp)] |
| 71 |
[movl 4 %eax] |
| 72 |
[subl %eax (disp -4 %esp)] |
| 73 |
[movl -4 %eax] |
| 74 |
[movl (disp -4 %esp) %eax] |
| 75 |
[ret])) |
| 76 |
|
| 77 |
(asm-test 1 |
| 78 |
'([movl 1 (disp -4 %esp)] |
| 79 |
[sall 2 (disp -4 %esp)] |
| 80 |
[movl (disp -4 %esp) %eax] |
| 81 |
[ret])) |
| 82 |
|
| 83 |
(asm-test 1 |
| 84 |
'([movl 32 (disp -4 %esp)] |
| 85 |
[sarl 3 (disp -4 %esp)] |
| 86 |
[movl (disp -4 %esp) %eax] |
| 87 |
[ret])) |
| 88 |
|
| 89 |
(asm-test 2 |
| 90 |
'([movl 4 %ebx] |
| 91 |
[movl 4 (disp -8 %esp)] |
| 92 |
[addl %ebx (disp -8 %esp)] |
| 93 |
[movl (disp -8 %esp) %eax] |
| 94 |
[ret])) |
| 95 |
|
| 96 |
(asm-test 2 |
| 97 |
'([movl 4 %eax] |
| 98 |
[movl 4 (disp -8 %esp)] |
| 99 |
[addl %eax (disp -8 %esp)] |
| 100 |
[movl 0 %eax] |
| 101 |
[movl (disp -8 %esp) %eax] |
| 102 |
[ret])) |
| 103 |
|
| 104 |
(asm-test 1 |
| 105 |
'([movl 0 (disp -4 %esp)] |
| 106 |
[movl %esp %eax] |
| 107 |
[movl -4 %ebx] |
| 108 |
[movb 4 (disp %eax %ebx)] |
| 109 |
[movl (disp -4 %esp) %eax] |
| 110 |
[ret])) |
| 111 |
|
| 112 |
(asm-test 2 |
| 113 |
'([movl 12 (disp -8 %esp)] ;;; 12 = 001100 |
| 114 |
[movl 24 %eax] ;;; 24 = 011000 |
| 115 |
[andl %eax (disp -8 %esp)] |
| 116 |
[movl (disp -8 %esp) %eax] |
| 117 |
[ret])) |
| 118 |
|
| 119 |
(asm-test 3 |
| 120 |
'([movl 4 (disp -4 %esp)] |
| 121 |
[orl 8 (disp -4 %esp)] |
| 122 |
[movl (disp -4 %esp) %eax] |
| 123 |
[ret])) |
| 124 |
|
| 125 |
|
| 126 |
(asm-test 3 |
| 127 |
'([movl 4 (disp -4 %esp)] |
| 128 |
[movl 8 %eax] |
| 129 |
[orl %eax (disp -4 %esp)] |
| 130 |
[movl (disp -4 %esp) %eax] |
| 131 |
[ret])) |
| 132 |
|
| 133 |
(asm-test 3 |
| 134 |
'([movl 4 (disp -4 %esp)] |
| 135 |
[movl 8 %ebx] |
| 136 |
[orl %ebx (disp -4 %esp)] |
| 137 |
[movl (disp -4 %esp) %eax] |
| 138 |
[ret])) |
| 139 |
|
| 140 |
|
| 141 |
(asm-test 3 |
| 142 |
'([movl -1 (disp -4 %esp)] |
| 143 |
[andl 12 (disp -4 %esp)] |
| 144 |
[movl (disp -4 %esp) %eax] |
| 145 |
[ret])) |
| 146 |
|
| 147 |
(asm-test 1 |
| 148 |
'([movl (obj (1 2)) (disp -4 %esp)] |
| 149 |
[movl (obj car) %eax] |
| 150 |
[movl (disp 14 %eax) %edi] ;;; symbol-value |
| 151 |
[movl -4 %eax] |
| 152 |
[jmp (disp -3 %edi)])) |
| 153 |
|
| 154 |
(asm-test 1 |
| 155 |
'([movl (obj (1 2)) (disp -4 %esp)] |
| 156 |
[movl (obj car) %eax] |
| 157 |
[movl (disp 14 %eax) %edi] ;;; symbol-value |
| 158 |
[movl (disp -3 %edi) %eax] |
| 159 |
[movl %eax (disp 26 (obj car))] |
| 160 |
[movl -4 %eax] |
| 161 |
[jmp (disp 26 (obj car))])) |
| 162 |
|
| 163 |
(asm-test 1 |
| 164 |
'([movl (obj (1 2)) (disp -4 %esp)] |
| 165 |
[movl (obj car) %eax] |
| 166 |
[movl (disp 14 %eax) %eax] ;;; symbol-value |
| 167 |
[movl (disp -3 %eax) %eax] |
| 168 |
[movl %eax (disp 26 (obj car))] |
| 169 |
[movl -4 %eax] |
| 170 |
[jmp (disp 26 (obj car))])) |
| 171 |
|
| 172 |
|
| 173 |
(asm-test 1 |
| 174 |
'([movl (obj (1 2)) (disp -4 %esp)] |
| 175 |
[movl -4 %eax] |
| 176 |
[jmp (disp 26 (obj car))])) |
| 177 |
|
| 178 |
(asm-test 1 |
| 179 |
'([movl (obj (1 2)) (disp -8 %esp)] |
| 180 |
[movl -4 %eax] |
| 181 |
[call (disp 26 (obj car))] |
| 182 |
[ret])) |
| 183 |
|
| 184 |
(asm-test 8 |
| 185 |
'([movl (obj 1) (disp -8 %esp)] |
| 186 |
[movl 3 %ecx] |
| 187 |
[sall %cl (disp -8 %esp)] |
| 188 |
[movl (disp -8 %esp) %eax] |
| 189 |
[ret])) |
| 190 |
|
| 191 |
(asm-test 1 |
| 192 |
'([movl (obj 8) (disp -8 %esp)] |
| 193 |
[movl 3 %ecx] |
| 194 |
[sarl %cl (disp -8 %esp)] |
| 195 |
[movl (disp -8 %esp) %eax] |
| 196 |
[ret])) |
| 197 |
|
| 198 |
(asm-test 1 |
| 199 |
'([movl (obj 8) (disp -8 %esp)] |
| 200 |
[movl 3 %ecx] |
| 201 |
[shrl %cl (disp -8 %esp)] |
| 202 |
[movl (disp -8 %esp) %eax] |
| 203 |
[ret])) |
| 204 |
|
| 205 |
|
| 206 |
(printf "Happy Happy Joy Joy\n") |
| 207 |
(exit) |