objcode.scm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ;;; Guile Virtual Machine Object Code
  2. ;; Copyright (C) 2001 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (language objcode)
  18. #:export (encode-length decode-length))
  19. ;;;
  20. ;;; Variable-length interface
  21. ;;;
  22. ;; NOTE: decoded in vm_fetch_length in vm.c as well.
  23. (define (encode-length len)
  24. (cond ((< len 254) (u8vector len))
  25. ((< len (* 256 256))
  26. (u8vector 254 (quotient len 256) (modulo len 256)))
  27. ((< len most-positive-fixnum)
  28. (u8vector 255
  29. (quotient len (* 256 256 256))
  30. (modulo (quotient len (* 256 256)) 256)
  31. (modulo (quotient len 256) 256)
  32. (modulo len 256)))
  33. (else (error "Too long code length:" len))))
  34. (define (decode-length pop)
  35. (let ((x (pop)))
  36. (cond ((< x 254) x)
  37. ((= x 254) (+ (ash x 8) (pop)))
  38. (else
  39. (let* ((b2 (pop))
  40. (b3 (pop))
  41. (b4 (pop)))
  42. (+ (ash x 24) (ash b2 16) (ash b3 8) b4))))))