-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.scm
67 lines (49 loc) · 1.22 KB
/
utils.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(declare (unit utils))
;; boxed type - call by reference
(define (box v)
(list v))
(define (box-set! box v)
(set-car! box v))
(define (box-ref box)
(car box))
;; key-value table
(define (make-table)
(box '()))
(define (table-get table key)
(cond
((assq key (box-ref table)) => cdr)
(else '())))
(define (table-set table key value)
(box-set! table
(cons (cons key value)
(box-ref table))))
;; A generator function which produces integers which increase by 1 every time.
(eval-when (load compile)
(define (make-count-generator)
(let ((k 0))
(lambda ()
(let ((ret k))
(set! k (+ k 1))
ret)))))
;; size tests for immediates
(define (width x)
(cond
;; convert a negative (and hence known to be two's-complement form) to any unsigned integer that requires the same number of bits
((< x 0)
(width (+ (* 2 (abs x)) 1)))
((<= x #xFF) 8)
((<= x #xFFFF) 16)
((<= x #xFFFFFFFF) 32)
(else 64)))
(define (i8? x)
(and (integer? x)
(= (width x) 8)))
(define (i16? x)
(and (integer? x)
(= (width x) 16)))
(define (i32? x)
(and (integer? x)
(= (width x) 32)))
(define (i64? x)
(and (integer? x)
(= (width x) 64)))