Commit | Line | Data |
---|---|---|
53e28ed9 AW |
1 | ;;;; test assembly to bytecode compilation -*- scheme -*- |
2 | ;;;; | |
3 | ;;;; This library is free software; you can redistribute it and/or | |
4 | ;;;; modify it under the terms of the GNU Lesser General Public | |
5 | ;;;; License as published by the Free Software Foundation; either | |
53befeb7 | 6 | ;;;; version 3 of the License, or (at your option) any later version. |
53e28ed9 AW |
7 | ;;;; |
8 | ;;;; This library is distributed in the hope that it will be useful, | |
9 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | ;;;; Lesser General Public License for more details. | |
12 | ;;;; | |
13 | ;;;; You should have received a copy of the GNU Lesser General Public | |
14 | ;;;; License along with this library; if not, write to the Free Software | |
15 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | ||
17 | (define-module (test-suite tests asm-to-bytecode) | |
44362a10 | 18 | #:use-module (rnrs bytevector) |
53e28ed9 AW |
19 | #:use-module (test-suite lib) |
20 | #:use-module (system vm instruction) | |
6f787028 | 21 | #:use-module (language assembly compile-bytecode)) |
53e28ed9 | 22 | |
ccf77d95 AW |
23 | (define (->u8-list sym val) |
24 | (let ((entry (assq-ref `((uint16 2 ,bytevector-u16-native-set!) | |
25 | (uint32 4 ,bytevector-u32-native-set!)) | |
26 | sym))) | |
27 | (or entry (error "unknown sym" sym)) | |
28 | (let ((bv (make-bytevector (car entry)))) | |
29 | ((cadr entry) bv 0 val) | |
30 | (bytevector->u8-list bv)))) | |
31 | ||
53e28ed9 | 32 | (define (munge-bytecode v) |
ccf77d95 AW |
33 | (let lp ((i 0) (out '())) |
34 | (if (= i (vector-length v)) | |
35 | (list->u8vector (reverse out)) | |
36 | (let ((x (vector-ref v i))) | |
37 | (cond | |
38 | ((symbol? x) | |
39 | (lp (1+ i) (cons (instruction->opcode x) out))) | |
40 | ((integer? x) | |
41 | (lp (1+ i) (cons x out))) | |
42 | ((pair? x) | |
43 | (lp (1+ i) (append (reverse (apply ->u8-list x)) out))) | |
44 | (else (error "bad test bytecode" x))))))) | |
53e28ed9 AW |
45 | |
46 | (define (comp-test x y) | |
47 | (let* ((y (munge-bytecode y)) | |
48 | (len (u8vector-length y)) | |
49 | (v (make-u8vector len)) | |
50 | (i 0)) | |
51 | (define (write-byte b) (u8vector-set! v i b) (set! i (1+ i))) | |
52 | (define (get-addr) i) | |
53 | (run-test `(length ,x) #t | |
54 | (lambda () | |
6f787028 | 55 | (write-bytecode x write-byte get-addr '()) |
53e28ed9 AW |
56 | (= i len))) |
57 | (run-test `(compile-equal? ,x ,y) #t | |
58 | (lambda () | |
59 | (equal? v y))))) | |
60 | ||
44362a10 | 61 | \f |
53e28ed9 AW |
62 | (with-test-prefix "compiler" |
63 | (with-test-prefix "asm-to-bytecode" | |
64 | ||
65 | (comp-test '(make-int8 3) | |
66 | #(make-int8 3)) | |
67 | ||
53e28ed9 AW |
68 | (comp-test '(load-number "3.14") |
69 | (vector 'load-number 0 0 4 (char->integer #\3) (char->integer #\.) | |
70 | (char->integer #\1) (char->integer #\4))) | |
71 | ||
72 | (comp-test '(load-string "foo") | |
94ff26b9 | 73 | (vector 'load-string 0 0 3 (char->integer #\f) (char->integer #\o) |
53e28ed9 AW |
74 | (char->integer #\o))) |
75 | ||
76 | (comp-test '(load-symbol "foo") | |
94ff26b9 | 77 | (vector 'load-symbol 0 0 3 (char->integer #\f) (char->integer #\o) |
53e28ed9 AW |
78 | (char->integer #\o))) |
79 | ||
56164a5a | 80 | (comp-test '(load-program () 3 #f (make-int8 3) (return)) |
ccf77d95 | 81 | #(load-program |
ccf77d95 AW |
82 | (uint32 3) ;; len |
83 | (uint32 0) ;; metalen | |
84 | make-int8 3 | |
85 | return)) | |
1f1ec13b | 86 | |
28b119ee AW |
87 | ;; the nops are to pad meta to an 8-byte alignment. not strictly |
88 | ;; necessary for this test, but representative of the common case. | |
56164a5a AW |
89 | (comp-test '(load-program () 8 |
90 | (load-program () 3 | |
1f1ec13b AW |
91 | #f |
92 | (make-int8 3) (return)) | |
28b119ee AW |
93 | (make-int8 3) (return) |
94 | (nop) (nop) (nop) (nop) (nop)) | |
ccf77d95 | 95 | #(load-program |
28b119ee | 96 | (uint32 8) ;; len |
56164a5a | 97 | (uint32 11) ;; metalen |
ccf77d95 AW |
98 | make-int8 3 |
99 | return | |
28b119ee | 100 | nop nop nop nop nop |
ccf77d95 AW |
101 | (uint32 3) ;; len |
102 | (uint32 0) ;; metalen | |
103 | make-int8 3 | |
104 | return)))) |