Remove locale u8vector functions
[bpt/guile.git] / test-suite / tests / asm-to-bytecode.test
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
6 ;;;; version 3 of the License, or (at your option) any later version.
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)
18 #:use-module (rnrs bytevector)
19 #:use-module (test-suite lib)
20 #:use-module (system vm instruction)
21 #:use-module (language assembly compile-bytecode))
22
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
32 (define (munge-bytecode v)
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)))))))
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 ()
55 (write-bytecode x write-byte get-addr '())
56 (= i len)))
57 (run-test `(compile-equal? ,x ,y) #t
58 (lambda ()
59 (equal? v y)))))
60
61 \f
62 (with-test-prefix "compiler"
63 (with-test-prefix "asm-to-bytecode"
64
65 (comp-test '(make-int8 3)
66 #(make-int8 3))
67
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")
73 (vector 'load-string 0 0 3 (char->integer #\f) (char->integer #\o)
74 (char->integer #\o)))
75
76 (comp-test '(load-symbol "foo")
77 (vector 'load-symbol 0 0 3 (char->integer #\f) (char->integer #\o)
78 (char->integer #\o)))
79
80 (comp-test '(load-program 3 2 1 () 3 #f (make-int8 3) (return))
81 #(load-program
82 3 2 (uint16 1) ;; nargs, nrest, nlocs
83 (uint32 3) ;; len
84 (uint32 0) ;; metalen
85 (uint32 0) ;; padding
86 make-int8 3
87 return))
88
89 ;; the nops are to pad meta to an 8-byte alignment. not strictly
90 ;; necessary for this test, but representative of the common case.
91 (comp-test '(load-program 3 2 1 () 8
92 (load-program 3 2 1 () 3
93 #f
94 (make-int8 3) (return))
95 (make-int8 3) (return)
96 (nop) (nop) (nop) (nop) (nop))
97 #(load-program
98 3 2 (uint16 1) ;; nargs, nrest, nlocs
99 (uint32 8) ;; len
100 (uint32 19) ;; metalen
101 (uint32 0) ;; padding
102 make-int8 3
103 return
104 nop nop nop nop nop
105 3 2 (uint16 1) ;; nargs, nrest, nlocs
106 (uint32 3) ;; len
107 (uint32 0) ;; metalen
108 (uint32 0) ;; padding
109 make-int8 3
110 return))))