as.scm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (mescc as)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (mes guile)
  21. #:use-module (mescc bytevectors)
  22. #:use-module (mescc info)
  23. #:export (as
  24. dec->hex
  25. int->bv8
  26. int->bv16
  27. int->bv32
  28. int->bv64
  29. get-r
  30. get-r0
  31. get-r1
  32. get-r-1))
  33. (define (int->bv64 value)
  34. (let ((bv (make-bytevector 8)))
  35. (bytevector-u64-native-set! bv 0 value)
  36. bv))
  37. (define (int->bv32 value)
  38. (let ((bv (make-bytevector 4)))
  39. (bytevector-u32-native-set! bv 0 value)
  40. bv))
  41. (define (int->bv16 value)
  42. (let ((bv (make-bytevector 2)))
  43. (bytevector-u16-native-set! bv 0 value)
  44. bv))
  45. (define (int->bv8 value)
  46. (let ((bv (make-bytevector 1)))
  47. (bytevector-u8-set! bv 0 value)
  48. bv))
  49. (define (dec->hex o)
  50. (cond ((number? o) (number->string o 16))
  51. ((char? o) (number->string (char->integer o) 16))
  52. (else (format #f "~s" o))))
  53. (define (as info instruction . rest)
  54. (if (pair? instruction)
  55. (append-map (lambda (o) (apply as (cons* info o rest))) instruction)
  56. (let ((proc (assoc-ref (.instructions info) instruction)))
  57. (if (not proc) (error "no such instruction" instruction)
  58. (apply proc info rest)))))
  59. (define (get-r info)
  60. (car (if (pair? (.allocated info)) (.allocated info) (.registers info))))
  61. (define (get-r0 info)
  62. (cadr (.allocated info)))
  63. (define (get-r1 info)
  64. (car (.allocated info)))
  65. (define (get-r-1 info)
  66. (caddr (.allocated info)))