summaryrefslogtreecommitdiff
path: root/test-suite/tests/asm-to-bytecode.test
blob: 2af3152fffc27da23deb0faf8c98769da2545404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
;;;; test assembly to bytecode compilation -*- scheme -*-
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2.1 of the License, or (at your option) any later version.
;;;; 
;;;; This library 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
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite tests asm-to-bytecode)
  #:use-module (test-suite lib)
  #:use-module (system vm instruction)
  #:use-module (language assembly compile-bytecode))

(define (munge-bytecode v)
  (let ((newv (make-u8vector (vector-length v))))
    (let lp ((i 0))
      (if (= i (vector-length v))
          newv
          (let ((x (vector-ref v i)))
            (u8vector-set! newv i (if (symbol? x)
                                      (instruction->opcode x)
                                      x))
            (lp (1+ i)))))))

(define (comp-test x y)
  (let* ((y (munge-bytecode y))
         (len (u8vector-length y))
         (v (make-u8vector len))
         (i 0))
    (define (write-byte b) (u8vector-set! v i b) (set! i (1+ i)))
    (define (get-addr) i)
    (run-test `(length ,x) #t
              (lambda ()
                (write-bytecode x write-byte get-addr '())
                (= i len)))
    (run-test `(compile-equal? ,x ,y) #t
              (lambda ()
                (equal? v y)))))

(with-test-prefix "compiler"
  (with-test-prefix "asm-to-bytecode"

    (comp-test '(make-int8 3)
               #(make-int8 3))
    
    (comp-test `(load-integer ,(string (integer->char 0)))
               #(load-integer 0 0 1 0))
    
    (comp-test `(load-integer ,(string (integer->char 255)))
               #(load-integer 0 0 1 255))
    
    (comp-test `(load-integer ,(string (integer->char 1) (integer->char 0)))
               #(load-integer 0 0 2 1 0))
    
    (comp-test '(load-number "3.14")
               (vector 'load-number 0 0 4 (char->integer #\3) (char->integer #\.)
                       (char->integer #\1) (char->integer #\4)))
    
    (comp-test '(load-string "foo")
               (vector 'load-string 0 0 3 (char->integer #\f) (char->integer #\o)
                       (char->integer #\o)))
    
    (comp-test '(load-symbol "foo")
               (vector 'load-symbol 0 0 3 (char->integer #\f) (char->integer #\o)
                       (char->integer #\o)))
    
    (comp-test '(load-keyword "qux")
               (vector 'load-keyword 0 0 3 (char->integer #\q) (char->integer #\u)
                       (char->integer #\x)))
    
    ;; fixme: little-endian test.
    (comp-test '(load-program 3 2 1 0 () 3 #f (make-int8 3) (return))
               (vector 'load-program 3 2 1 0 3 0 0 0 0 0 0 0
                       (instruction->opcode 'make-int8) 3
                       (instruction->opcode 'return)))

    ;; fixme: little-endian test.
    (comp-test '(load-program 3 2 1 0 () 3
                              (load-program 3 2 1 0 () 3
                                            #f
                                            (make-int8 3) (return))
                              (make-int8 3) (return))
               (vector 'load-program 3 2 1 0 3 0 0 0 (+ 3 12) 0 0 0
                       (instruction->opcode 'make-int8) 3
                       (instruction->opcode 'return)
                       3 2 1 0 3 0 0 0 0 0 0 0
                       (instruction->opcode 'make-int8) 3
                       (instruction->opcode 'return)))))