mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-17 01:00:20 +02:00
rework the vm support for wide strings
* libguile/_scm.h (SCM_OBJCODE_MINOR_VERSION): Bump. * libguile/vm-engine.c (vm_error_bad_wide_string_length): New error case. * libguile/vm-i-loader.c (load-unsigned-integer, load-integer) (load-keyword): Remove these instructions. The former two are obsoleted by make-int64/make-uint64, the latter via make-keyword. (load-string): Only handle narrow strings. (load-symbol): Only handle narrow symbols. The wide case is handled via make-symbol. (load-wide-string): New instruction, for wide strings. * libguile/vm-i-system.c (define): Move here from loaders.c, as now it just takes a sym on the stack. (make-keyword, make-symbol): New instructions. * module/language/assembly.scm: Remove removed instructions. No more width byte in load-string etc. * module/language/assembly/compile-bytecode.scm (write-bytecode): Adapt to change in instruction set. * module/language/glil/compile-assembly.scm (glil->assembly): Compile define by pushing the sym then emitting (define). (dump-object): Dump narrow and wide strings differently. Use make-keyword and make-symbol as appropriate. * module/language/tree-il/compile-glil.scm (flatten): When compiling a ref to a primitive (not a call), first see if the primitive is actually bound in the root module. (That's not the case with e.g. bytevector-u8-ref). * module/system/xref.scm (program-callee-rev-vars): Don't parse out "nexts". * test-suite/tests/asm-to-bytecode.test ("compiler"): Adapt to bytecode format change.
This commit is contained in:
parent
6cf4830798
commit
94ff26b96b
11 changed files with 101 additions and 201 deletions
|
@ -34,30 +34,21 @@
|
|||
;; lengths are encoded in 3 bytes
|
||||
(define *len-len* 3)
|
||||
|
||||
;; the number of bytes per string character is encoded in 1 byte
|
||||
(define *width-len* 1)
|
||||
|
||||
|
||||
(define (byte-length assembly)
|
||||
(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)
|
||||
(+ 1 *len-len* (string-length str)))
|
||||
((load-string ,str)
|
||||
(+ 1 *len-len* *width-len* (* (string-width str) (string-length str))))
|
||||
(+ 1 *len-len* (string-length str)))
|
||||
((load-wide-string ,str)
|
||||
(+ 1 *len-len* (* 4 (string-length str))))
|
||||
((load-symbol ,str)
|
||||
(+ 1 *len-len* *width-len* (* (string-width str) (string-length str))))
|
||||
((load-keyword ,str)
|
||||
(+ 1 *len-len* *width-len* (* (string-width str) (string-length str))))
|
||||
(+ 1 *len-len* (string-length str)))
|
||||
((load-array ,bv)
|
||||
(+ 1 *len-len* (bytevector-length bv)))
|
||||
((define ,str)
|
||||
(+ 1 *len-len* *width-len* (* (string-width str) (string-length str))))
|
||||
((load-program ,nargs ,nrest ,nlocs ,labels ,len ,meta . ,code)
|
||||
(+ 1 *program-header-len* len (if meta (1- (byte-length meta)) 0)))
|
||||
((,inst . _) (guard (>= (instruction-length inst) 0))
|
||||
|
@ -171,5 +162,4 @@
|
|||
n4)))
|
||||
((load-string ,s) s)
|
||||
((load-symbol ,s) (string->symbol s))
|
||||
((load-keyword ,s) (symbol->keyword (string->symbol s)))
|
||||
(else #f)))
|
||||
|
|
|
@ -65,11 +65,13 @@
|
|||
(write-byte (logand (ash x -8) 255))
|
||||
(write-byte (logand (ash x -16) 255))
|
||||
(write-byte (logand (ash x -24) 255)))
|
||||
(define (write-uint32 x) (case byte-order
|
||||
((1234) (write-uint32-le x))
|
||||
((4321) (write-uint32-be x))
|
||||
(else (error "unknown endianness" byte-order))))
|
||||
(define (write-uint32 x)
|
||||
(case byte-order
|
||||
((1234) (write-uint32-le x))
|
||||
((4321) (write-uint32-be x))
|
||||
(else (error "unknown endianness" byte-order))))
|
||||
(define (write-wide-string s)
|
||||
(write-loader-len (* 4 (string-length s)))
|
||||
(string-for-each (lambda (c) (write-uint32 (char->integer c))) s))
|
||||
(define (write-loader-len len)
|
||||
(write-byte (ash len -16))
|
||||
|
@ -133,14 +135,11 @@
|
|||
;; `scm_c_make_objcode_slice ()'.
|
||||
(write-bytecode meta write get-addr '()))))
|
||||
((make-char32 ,x) (write-uint32-be x))
|
||||
((load-unsigned-integer ,str) (write-loader str))
|
||||
((load-integer ,str) (write-loader str))
|
||||
((load-number ,str) (write-loader str))
|
||||
((load-string ,str) (write-sized-loader str))
|
||||
((load-symbol ,str) (write-sized-loader str))
|
||||
((load-keyword ,str) (write-sized-loader str))
|
||||
((load-string ,str) (write-loader str))
|
||||
((load-wide-string ,str) (write-wide-string str))
|
||||
((load-symbol ,str) (write-loader str))
|
||||
((load-array ,bv) (write-bytevector bv))
|
||||
((define ,str) (write-sized-loader str))
|
||||
((br ,l) (write-break l))
|
||||
((br-if ,l) (write-break l))
|
||||
((br-if-not ,l) (write-break l))
|
||||
|
|
|
@ -96,16 +96,6 @@
|
|||
(lp (cons exp out))))))))))
|
||||
|
||||
(define (decode-bytecode pop)
|
||||
(define (get1 bytes-per-char)
|
||||
(if (= bytes-per-char 1)
|
||||
(pop)
|
||||
(let* ((a (pop))
|
||||
(b (pop))
|
||||
(c (pop))
|
||||
(d (pop)))
|
||||
(if (= byte-order 1234)
|
||||
(+ (ash d 24) (ash c 16) (ash b 8) a)
|
||||
(+ (ash a 24) (ash b 16) (ash c 8) d)))))
|
||||
(and=> (pop)
|
||||
(lambda (opcode)
|
||||
(let ((inst (opcode->instruction opcode)))
|
||||
|
@ -117,29 +107,24 @@
|
|||
;; the negative length indicates a variable length
|
||||
;; instruction
|
||||
(let* ((make-sequence
|
||||
(if (eq? inst 'load-array)
|
||||
(if (or (memq inst '(load-array load-wide-string)))
|
||||
make-bytevector
|
||||
make-string))
|
||||
(sequence-set!
|
||||
(if (eq? inst 'load-array)
|
||||
(if (or (memq inst '(load-array load-wide-string)))
|
||||
bytevector-u8-set!
|
||||
(lambda (str pos value)
|
||||
(string-set! str pos (integer->char value)))))
|
||||
(len (let* ((a (pop)) (b (pop)) (c (pop)))
|
||||
(+ (ash a 16) (ash b 8) c)))
|
||||
(bytes-per-count
|
||||
(if (or (eq? inst 'load-string)
|
||||
(eq? inst 'load-symbol)
|
||||
(eq? inst 'load-keyword)
|
||||
(eq? inst 'define))
|
||||
(pop)
|
||||
1))
|
||||
(seq (make-sequence len)))
|
||||
(let lp ((i 0))
|
||||
(if (= i len)
|
||||
`(,inst ,seq)
|
||||
`(,inst ,(if (eq? inst 'load-wide-string)
|
||||
(utf32->string seq)
|
||||
seq))
|
||||
(begin
|
||||
(sequence-set! seq i (get1 bytes-per-count))
|
||||
(sequence-set! seq i (pop))
|
||||
(lp (1+ i)))))))
|
||||
(else
|
||||
;; fixed length
|
||||
|
|
|
@ -318,8 +318,8 @@
|
|||
,(modulo i 256))))
|
||||
object-alist)))))
|
||||
((define)
|
||||
(emit-code `((define ,(symbol->string name))
|
||||
(variable-set))))
|
||||
(emit-code `(,@(dump-object name addr)
|
||||
(define))))
|
||||
(else
|
||||
(error "unknown toplevel var kind" op name))))
|
||||
|
||||
|
@ -391,11 +391,20 @@
|
|||
((number? x)
|
||||
`((load-number ,(number->string x))))
|
||||
((string? x)
|
||||
`((load-string ,x)))
|
||||
(case (string-width x)
|
||||
((1) `((load-string ,x)))
|
||||
((4) (align-code `(load-wide-string ,x) addr 4 4))
|
||||
(else (error "bad string width" x))))
|
||||
((symbol? x)
|
||||
`((load-symbol ,(symbol->string x))))
|
||||
(let ((str (symbol->string x)))
|
||||
(case (string-width str)
|
||||
((1) `((load-symbol ,str)))
|
||||
((4) `(,@(dump-object str addr)
|
||||
(make-symbol)))
|
||||
(else (error "bad string width" str)))))
|
||||
((keyword? x)
|
||||
`((load-keyword ,(symbol->string (keyword->symbol x)))))
|
||||
`(,@(dump-object (keyword->symbol x) addr)
|
||||
(make-keyword)))
|
||||
((list? x)
|
||||
(let ((tail (let ((len (length x)))
|
||||
(if (>= len 65536) (too-long "list"))
|
||||
|
|
|
@ -492,11 +492,16 @@
|
|||
((tail push vals)
|
||||
(emit-code src (make-glil-toplevel 'ref name))))
|
||||
(maybe-emit-return))
|
||||
(else
|
||||
(pk 'ew-the-badness x (current-module) (fluid-ref *comp-module*))
|
||||
((module-variable the-root-module name)
|
||||
(case context
|
||||
((tail push vals)
|
||||
(emit-code src (make-glil-module 'ref '(guile) name #f))))
|
||||
(maybe-emit-return))
|
||||
(else
|
||||
(case context
|
||||
((tail push vals)
|
||||
(emit-code src (make-glil-module
|
||||
'ref (module-name (fluid-ref *comp-module*)) name #f))))
|
||||
(maybe-emit-return))))
|
||||
|
||||
((<lexical-ref> src name gensym)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue