summaryrefslogtreecommitdiff
path: root/test-suite/tests/asm-to-bytecode.test
blob: 0d8fecb5a49b8c65651d6fb69b0da5ef95f1a0ad (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
;;;; Assembly to bytecode compilation -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 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 3 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 (rnrs bytevectors)
  #:use-module ((rnrs io ports) #:select (open-bytevector-output-port))
  #:use-module (test-suite lib)
  #:use-module (system vm instruction)
  #:use-module (language assembly compile-bytecode))

(define write-bytecode
  (@@ (language assembly compile-bytecode) write-bytecode))

(define (->u8-list sym val)
  (let ((entry (assq-ref `((uint16 2 ,bytevector-u16-native-set!)
                           (uint32 4 ,bytevector-u32-native-set!))
                         sym)))
    (or entry (error "unknown sym" sym))
    (let ((bv (make-bytevector (car entry))))
      ((cadr entry) bv 0 val)
      (bytevector->u8-list bv))))

(define (munge-bytecode v)
  (let lp ((i 0) (out '()))
    (if (= i (vector-length v))
        (u8-list->bytevector (reverse out))
        (let ((x (vector-ref v i)))
          (cond
           ((symbol? x)
            (lp (1+ i) (cons (instruction->opcode x) out)))
           ((integer? x)
            (lp (1+ i) (cons x out)))
           ((pair? x)
            (lp (1+ i) (append (reverse (apply ->u8-list x)) out)))
           (else (error "bad test bytecode" x)))))))

(define (comp-test x y)
  (let* ((y   (munge-bytecode y))
         (len (bytevector-length y))
         (v   #f))

    (run-test `(length ,x) #t
              (lambda ()
                (call-with-values open-bytevector-output-port
                  (lambda (port get-bytevector)
                    (write-bytecode x port '() 0 #t)
                    (set! v (get-bytevector))
                    (= (bytevector-length v) 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-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-string "æ") ;; a non-ASCII Latin-1 string
               (vector 'load-string 0 0 1 230))

    (comp-test '(load-wide-string "λ")
               (apply vector 'load-wide-string 0 0 4
                      (if (eq? (native-endianness) (endianness little))
                          '(187 3 0 0)
                          '(0 0 3 187))))

    (comp-test '(load-program () 3 #f (make-int8 3) (return))
               #(load-program
                 (uint32 3)     ;; len
                 (uint32 0)     ;; metalen
                 make-int8 3
                 return))

    ;; the nops are to pad meta to an 8-byte alignment. not strictly
    ;; necessary for this test, but representative of the common case.
    (comp-test '(load-program () 8
                              (load-program () 3
                                            #f
                                            (make-int8 3) (return))
                              (make-int8 3) (return)
                              (nop) (nop) (nop) (nop) (nop))
               #(load-program
                 (uint32 8)     ;; len
                 (uint32 11)    ;; metalen
                 make-int8 3
                 return
                 nop nop nop nop nop
                 (uint32 3)     ;; len
                 (uint32 0)     ;; metalen
                 make-int8 3
                 return))))