summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2009-02-18 00:54:05 +0100
committerLudovic Courtès <ludo@gnu.org>2009-02-18 00:54:05 +0100
commitb912a1cd6b5d22e1fd3eadca10858d8d0dd9c027 (patch)
treecbd35bdcee9c0d3da264ceaa3ffba2917cbddb53
parent4e29767187774d44a899a82c3d2a7d509ed9e5b9 (diff)
downloadguile-b912a1cd6b5d22e1fd3eadca10858d8d0dd9c027.tar.gz
Add `load-unsigned-integer' instruction.
* libguile/vm-i-loader.c (load_unsigned_integer): New loader. * module/language/assembly.scm (byte-length): Handle `load-unsigned-integer'. * module/language/assembly/compile-bytecode.scm (write-bytecode): Likewise. * module/language/glil/compile-assembly.scm (dump-object): Emit a `load-unsigned-integer' instruction for positive integers. This fixes loading of integers greater than 2^31 - 1. * testsuite/Makefile.am (vm_test_files): Add `t-literal-integers.scm'. * doc/ref/vm.texi (Loading Instructions): Add `load-unsigned-integer'.
-rw-r--r--doc/ref/vm.texi4
-rw-r--r--libguile/vm-i-loader.c20
-rw-r--r--module/language/assembly.scm4
-rw-r--r--module/language/assembly/compile-bytecode.scm3
-rw-r--r--module/language/glil/compile-assembly.scm6
-rw-r--r--testsuite/Makefile.am1
-rw-r--r--testsuite/t-literal-integers.scm5
7 files changed, 37 insertions, 6 deletions
diff --git a/doc/ref/vm.texi b/doc/ref/vm.texi
index 56a2cefdf..042645200 100644
--- a/doc/ref/vm.texi
+++ b/doc/ref/vm.texi
@@ -549,7 +549,9 @@ indicating the size of the embedded data, in bytes. The length itself
may be encoded in 1, 2, or 4 bytes.
@deffn Instruction load-integer length
-Load a 32-bit integer from the instruction stream.
+@deffnx Instruction load-unsigned-integer length
+Load a 32-bit integer (respectively unsigned integer) from the
+instruction stream.
@end deffn
@deffn Instruction load-number length
Load an arbitrary number from the instruction stream. The number is
diff --git a/libguile/vm-i-loader.c b/libguile/vm-i-loader.c
index 66b07b1f5..bba4f4b9c 100644
--- a/libguile/vm-i-loader.c
+++ b/libguile/vm-i-loader.c
@@ -18,12 +18,30 @@
/* This file is included in vm_engine.c */
+VM_DEFINE_LOADER (59, load_unsigned_integer, "load-unsigned-integer")
+{
+ size_t len;
+
+ FETCH_LENGTH (len);
+ if (SCM_LIKELY (len <= 4))
+ {
+ unsigned int val = 0;
+ while (len-- > 0)
+ val = (val << 8U) + FETCH ();
+ SYNC_REGISTER ();
+ PUSH (scm_from_uint (val));
+ NEXT;
+ }
+ else
+ SCM_MISC_ERROR ("load-unsigned-integer: not implemented yet", SCM_EOL);
+}
+
VM_DEFINE_LOADER (60, load_integer, "load-integer")
{
size_t len;
FETCH_LENGTH (len);
- if (len <= 4)
+ if (SCM_LIKELY (len <= 4))
{
int val = 0;
while (len-- > 0)
diff --git a/module/language/assembly.scm b/module/language/assembly.scm
index 42fd5f0e1..28dde1e1a 100644
--- a/module/language/assembly.scm
+++ b/module/language/assembly.scm
@@ -1,6 +1,6 @@
;;; Guile Virtual Machine Assembly
-;; Copyright (C) 2001 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -38,6 +38,8 @@
(pmatch assembly
(,label (guard (not (pair? label)))
0)
+ ((load-unsigned-integer ,str)
+ (+ 1 *len-len* (string-length str)))
((load-integer ,str)
(+ 1 *len-len* (string-length str)))
((load-number ,str)
diff --git a/module/language/assembly/compile-bytecode.scm b/module/language/assembly/compile-bytecode.scm
index a96b56147..6e7e34efc 100644
--- a/module/language/assembly/compile-bytecode.scm
+++ b/module/language/assembly/compile-bytecode.scm
@@ -1,6 +1,6 @@
;;; Guile VM assembler
-;; Copyright (C) 2001 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -107,6 +107,7 @@
(if (> i 0) (write-byte x))))
(get-addr (lambda () i)))
(write-bytecode meta write get-addr '()))))
+ ((load-unsigned-integer ,str) (write-loader str))
((load-integer ,str) (write-loader str))
((load-number ,str) (write-loader str))
((load-string ,str) (write-loader str))
diff --git a/module/language/glil/compile-assembly.scm b/module/language/glil/compile-assembly.scm
index 797c59653..1b1d2dfba 100644
--- a/module/language/glil/compile-assembly.scm
+++ b/module/language/glil/compile-assembly.scm
@@ -1,6 +1,6 @@
;;; Guile VM assembler
-;; Copyright (C) 2001 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2009 Free Software Foundation, Inc.
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -343,7 +343,9 @@
(l '() (cons (modulo n 256) l)))
((= n 0)
(list->string (map integer->char l))))))
- `((load-integer ,str))))
+ (if (< x 0)
+ `((load-integer ,str))
+ `((load-unsigned-integer ,str)))))
((number? x)
`((load-number ,(number->string x))))
((string? x)
diff --git a/testsuite/Makefile.am b/testsuite/Makefile.am
index 8545d86e4..f509348fb 100644
--- a/testsuite/Makefile.am
+++ b/testsuite/Makefile.am
@@ -13,6 +13,7 @@ vm_test_files = \
t-closure3.scm \
t-closure4.scm \
t-do-loop.scm \
+ t-literal-integers.scm \
t-macros.scm \
t-macros2.scm \
t-map.scm \
diff --git a/testsuite/t-literal-integers.scm b/testsuite/t-literal-integers.scm
new file mode 100644
index 000000000..bf015a4ff
--- /dev/null
+++ b/testsuite/t-literal-integers.scm
@@ -0,0 +1,5 @@
+;; Check whether literal integers are correctly signed.
+
+(and (= 4294967295 (- (expt 2 32) 1)) ;; unsigned
+ (= -2147483648 (- (expt 2 31))) ;; signed
+ (= 2147483648 (expt 2 31))) ;; unsigned