Merge commit 'feccd2d3100fd2964d4c2df58ab3da7ce4949a66' into vm-check
[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 2.1 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 (test-suite lib)
19 #:use-module (system vm instruction)
20 #:use-module (language assembly compile-bytecode))
21
22 (define (munge-bytecode v)
23 (let ((newv (make-u8vector (vector-length v))))
24 (let lp ((i 0))
25 (if (= i (vector-length v))
26 newv
27 (let ((x (vector-ref v i)))
28 (u8vector-set! newv i (if (symbol? x)
29 (instruction->opcode x)
30 x))
31 (lp (1+ i)))))))
32
33 (define (comp-test x y)
34 (let* ((y (munge-bytecode y))
35 (len (u8vector-length y))
36 (v (make-u8vector len))
37 (i 0))
38 (define (write-byte b) (u8vector-set! v i b) (set! i (1+ i)))
39 (define (get-addr) i)
40 (run-test `(length ,x) #t
41 (lambda ()
42 (write-bytecode x write-byte get-addr '())
43 (= i len)))
44 (run-test `(compile-equal? ,x ,y) #t
45 (lambda ()
46 (equal? v y)))))
47
48 (with-test-prefix "compiler"
49 (with-test-prefix "asm-to-bytecode"
50
51 (comp-test '(make-int8 3)
52 #(make-int8 3))
53
54 (comp-test `(load-integer ,(string (integer->char 0)))
55 #(load-integer 0 0 1 0))
56
57 (comp-test `(load-integer ,(string (integer->char 255)))
58 #(load-integer 0 0 1 255))
59
60 (comp-test `(load-integer ,(string (integer->char 1) (integer->char 0)))
61 #(load-integer 0 0 2 1 0))
62
63 (comp-test '(load-number "3.14")
64 (vector 'load-number 0 0 4 (char->integer #\3) (char->integer #\.)
65 (char->integer #\1) (char->integer #\4)))
66
67 (comp-test '(load-string "foo")
68 (vector 'load-string 0 0 3 (char->integer #\f) (char->integer #\o)
69 (char->integer #\o)))
70
71 (comp-test '(load-symbol "foo")
72 (vector 'load-symbol 0 0 3 (char->integer #\f) (char->integer #\o)
73 (char->integer #\o)))
74
75 (comp-test '(load-keyword "qux")
76 (vector 'load-keyword 0 0 3 (char->integer #\q) (char->integer #\u)
77 (char->integer #\x)))
78
79 ;; fixme: little-endian test.
80 (comp-test '(load-program 3 2 1 0 () 3 #f (make-int8 3) (return))
81 (vector 'load-program 3 2 1 0 3 0 0 0 0 0 0 0
82 (instruction->opcode 'make-int8) 3
83 (instruction->opcode 'return)))
84
85 ;; fixme: little-endian test.
86 (comp-test '(load-program 3 2 1 0 () 3
87 (load-program 3 2 1 0 () 3
88 #f
89 (make-int8 3) (return))
90 (make-int8 3) (return))
91 (vector 'load-program 3 2 1 0 3 0 0 0 (+ 3 12) 0 0 0
92 (instruction->opcode 'make-int8) 3
93 (instruction->opcode 'return)
94 3 2 1 0 3 0 0 0 0 0 0 0
95 (instruction->opcode 'make-int8) 3
96 (instruction->opcode 'return)))))