1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-15 10:10:21 +02:00
guile/test-suite/tests/asm-to-bytecode.test
Ludovic Courtès 44362a1086 Fix tests that assumed little endian.
* test-suite/tests/asm-to-bytecode.test (u32->u8-list): New procedure.
  ("compiler")[(load-program 3 2 1 0 () 3 #f (make-int8 3) (return)),
  (load-program 3 2 1 0 () 3 (load-program 3 2 1 0 ...))]: Make these
  tests work on hosts whose endianness is not little endian.
2009-07-14 16:07:13 +02:00

113 lines
4.3 KiB
Scheme
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;;; 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 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 bytevector)
#: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)))))
(define (u32->u8-list x)
;; Return a 4 uint8 list corresponding to the host's native representation
;; of X, a uint32.
(let ((bv (make-bytevector 4)))
(bytevector-u32-native-set! bv 0 x)
(bytevector->u8-list bv)))
(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)))
(comp-test '(load-program 3 2 1 0 () 3 #f (make-int8 3) (return))
(list->vector
`(load-program
3 2 1 0 ;; nargs, nrest, nlocs, nexts
,@(u32->u8-list 3) ;; len
,@(u32->u8-list 0) ;; metalen
make-int8 3
return)))
(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))
(list->vector
`(load-program
3 2 1 0 ;; nargs, nrest, nlocs, nexts
,@(u32->u8-list 3) ;; len
,@(u32->u8-list (+ 3 12)) ;; metalen
make-int8 3
return
3 2 1 0 ;; nargs, nrest, nlocs, nexts
,@(u32->u8-list 3) ;; len
,@(u32->u8-list 0) ;; metalen
make-int8 3
return)))))