forked from cisco/ChezScheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmacros.ss
2564 lines (2379 loc) · 95.8 KB
/
cmacros.ss
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; Copyright 1984-2016 Cisco Systems, Inc.
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.
(define-syntax disable-unbound-warning
(syntax-rules ()
((_ name ...)
(eval-when (compile load eval)
($sputprop 'name 'no-unbound-warning #t) ...))))
(disable-unbound-warning
lookup-constant
flag->mask
construct-name
tc-field-list
)
(define-syntax define-constant
(lambda (x)
(syntax-case x ()
((_ ctype x y)
(and (identifier? #'ctype) (identifier? #'x))
#'(eval-when (compile load eval)
(putprop 'x '*constant-ctype* 'ctype)
(putprop 'x '*constant* y)))
((_ x y)
(identifier? #'x)
#'(eval-when (compile load eval)
(putprop 'x '*constant* y))))))
(eval-when (compile load eval)
(define lookup-constant
(let ([flag (box #f)])
(lambda (x)
(unless (symbol? x)
($oops 'lookup-constant "~s is not a symbol" x))
(let ([v (getprop x '*constant* flag)])
(when (eq? v flag)
($oops 'lookup-constant "undefined constant ~s" x))
v))))
)
(define-syntax constant
(lambda (x)
(syntax-case x ()
((_ x)
(identifier? #'x)
#`'#,(datum->syntax #'x
(lookup-constant (datum x)))))))
(define-syntax constant-case
(syntax-rules (else)
[(_ const [(k ...) e1 e2 ...] ... [else ee1 ee2 ...])
(meta-cond
[(member (constant const) '(k ...)) e1 e2 ...]
...
[else ee1 ee2 ...])]
[(_ const [(k ...) e1 e2 ...] ...)
(meta-cond
[(member (constant const) '(k ...)) e1 e2 ...]
...
[else (syntax-error #'const
(format "unhandled value ~s" (constant const)))])]))
(eval-when (compile load eval)
(define construct-name
(lambda (template-identifier . args)
(datum->syntax
template-identifier
(string->symbol
(apply string-append
(map (lambda (x) (format "~a" (syntax->datum x)))
args))))))
)
(define-syntax macro-define-structure
(lambda (x)
(define constant?
(lambda (x)
(or (let ((x (syntax->datum x)))
(or (boolean? x) (string? x) (char? x) (number? x)))
(syntax-case x (quote)
((quote obj) #t)
(else #f)))))
(syntax-case x ()
((_ (name id1 ...))
(andmap identifier? #'(name id1 ...))
#'(macro-define-structure (name id1 ...) ()))
((_ (name id1 ...) ((id2 init) ...))
(and (andmap identifier? #'(name id1 ... id2 ...))
(andmap constant? #'(init ...)))
(with-syntax
((constructor (construct-name #'name "make-" #'name))
(predicate (construct-name #'name #'name "?"))
((index-name ...)
(map (lambda (x) (construct-name x #'name "-" x "-index"))
#'(id1 ... id2 ...)))
((access ...)
(map (lambda (x) (construct-name x #'name "-" x))
#'(id1 ... id2 ...)))
((assign ...)
(map (lambda (x) (construct-name x "set-" #'name "-" x "!"))
#'(id1 ... id2 ...)))
(structure-length (fx+ (length #'(id1 ... id2 ...)) 1))
((index ...)
(let f ((i 1) (ids #'(id1 ... id2 ...)))
(if (null? ids)
'()
(cons i (f (fx+ i 1) (cdr ids)))))))
#'(begin
(define-syntax constructor
(syntax-rules ()
((_ id1 ...)
(#%vector 'name id1 ... init ...))))
(define-syntax predicate
(syntax-rules ()
((_ x)
(let ((t x))
(and (#%vector? x)
(#3%fx= (#3%vector-length x) structure-length)
(#%eq? (#3%vector-ref x 0) 'name))))))
(define-constant index-name index)
...
(define-syntax access
(syntax-rules ()
((_ x) (#%vector-ref x index))))
...
(define-syntax assign
(syntax-rules ()
((_ x update) (#%vector-set! x index update))))
...))))))
(define-syntax type-case
(syntax-rules (else)
[(_ expr
[(pred1 pred2 ...) e1 e2 ...] ...
[else ee1 ee2 ...])
(let ([t expr])
(cond
[(or (pred1 t) (pred2 t) ...) e1 e2 ...]
...
[else ee1 ee2 ...]))]))
;;; machine-case and float-type-case call eval to pick up the
;;; system value of $target-machine under the assumption that
;;; we'll be in system mode when we expand the macro
(define-syntax machine-case
(lambda (x)
(let ((target-machine (eval '($target-machine))))
(let loop ((x (syntax-case x () ((_ m ...) #'(m ...)))))
(syntax-case x (else)
((((a1 a2 ...) e ...) m1 m2 ...)
(let ((machines (datum (a1 a2 ...))))
(if (memq target-machine machines)
(if (null? #'(e ...))
(begin
(printf "Warning: empty machine-case clause for ~s~%"
machines)
#'($oops 'assembler
"empty machine-case clause for ~s"
'(a1 a2 ...)))
#'(begin e ...))
(loop (cdr x)))))
(((else e1 e2 ...)) #'(begin e1 e2 ...)))))))
(define-syntax float-type-case
(lambda (x)
(syntax-case x (ieee else)
((_ ((ieee tag ...) e1 e2 ...) m ...)
#t ; all currently supported machines are ieee
#'(begin e1 e2 ...))
((_ ((tag1 tag2 ...) e1 e2 ...) m ...)
#'(float-type-case ((tag2 ...) e1 e2 ...) m ...))
((_ (() e1 e2 ...) m ...)
#'(float-type-case m ...))
((_ (else e1 e2 ...))
#'(begin e1 e2 ...)))))
(define-syntax ieee
(lambda (x)
(syntax-error x "misplaced aux keyword")))
;; layout of our flags field:
;; bit 0: needs head space?
;; bit 1 - 9: upper 9 bits of index (lower bit is the needs head space index
;; bit 10 - 12: interface
;; bit 13: closure?
;; bit 14: error?
;; bit 15: has-headroom-version?
(macro-define-structure (libspec name flags))
(define-constant libspec-does-not-expect-headroom-index 0)
(define-constant libspec-index-offset 0)
(define-constant libspec-index-size 10)
(define-constant libspec-index-base-offset 1)
(define-constant libspec-index-base-size 9)
(define-constant libspec-interface-offset 10)
(define-constant libspec-interface-size 3)
(define-constant libspec-closure-index 13)
(define-constant libspec-error-index 14)
(define-constant libspec-has-does-not-expect-headroom-version-index 15)
(define-constant libspec-fake-index 16)
(define-syntax make-libspec-flags
(lambda (x)
(syntax-case x ()
[(_ index-base does-not-expect-headroom? closure? interface error? has-does-not-expect-headroom-version?)
#'(begin
(unless (fx>= (- (expt 2 (constant libspec-index-base-size)) 1) index-base 0)
($oops 'make-libspec-flags "libspec base index exceeds ~s-bit bound: ~s"
(constant libspec-index-base-size) index-base))
(unless (fx>= (- (expt 2 (constant libspec-interface-size)) 1) interface 0)
($oops 'make-libspec-flags "libspec interface exceeds ~s-bit bound: ~s"
(constant libspec-interface-size) interface))
(when (and does-not-expect-headroom? (not has-does-not-expect-headroom-version?))
($oops 'make-libspec-flags
"creating invalid version of libspec that does not expect headroom"))
(fxlogor
(if does-not-expect-headroom?
(fxsll 1 (constant libspec-does-not-expect-headroom-index))
0)
(fxsll index-base (constant libspec-index-base-offset))
(fxsll interface (constant libspec-interface-offset))
(if closure? (fxsll 1 (constant libspec-closure-index)) 0)
(if error? (fxsll 1 (constant libspec-error-index)) 0)
(if has-does-not-expect-headroom-version?
(fxsll 1 (constant libspec-has-does-not-expect-headroom-version-index))
0)))])))
(define-syntax libspec-does-not-expect-headroom?
(syntax-rules ()
[(_ ?libspec)
(fxbit-set? (libspec-flags ?libspec) (constant libspec-does-not-expect-headroom-index))]))
(define-syntax libspec-index
(syntax-rules ()
[(_ ?libspec)
(fxbit-field (libspec-flags ?libspec)
(constant libspec-index-offset)
(fx+ (constant libspec-index-size) (constant libspec-index-offset)))]))
(define-syntax libspec-interface
(syntax-rules ()
[(_ ?libspec)
(fxbit-field (libspec-flags ?libspec)
(constant libspec-interface-offset)
(fx+ (constant libspec-interface-size) (constant libspec-interface-offset)))]))
(define-syntax libspec-closure?
(syntax-rules ()
[(_ ?libspec)
(fxbit-set? (libspec-flags ?libspec) (constant libspec-closure-index))]))
(define-syntax libspec-error?
(syntax-rules ()
[(_ ?libspec)
(fxbit-set? (libspec-flags ?libspec) (constant libspec-error-index))]))
(define-syntax libspec-has-does-not-expect-headroom-version?
(syntax-rules ()
[(_ ?libspec)
(fxbit-set? (libspec-flags ?libspec) (constant libspec-has-does-not-expect-headroom-version-index))]))
(define-syntax libspec->does-not-expect-headroom-libspec
(syntax-rules ()
[(_ ?libspec)
(let ([libspec ?libspec])
(unless (libspec-has-does-not-expect-headroom-version? libspec)
($oops #f "generating invalid libspec for ~s that does not expect headroom"
(libspec-name libspec)))
(make-libspec (libspec-name libspec)
(fxlogor (libspec-flags libspec)
(fxsll 1 (constant libspec-does-not-expect-headroom-index)))))]))
(define-syntax libspec->headroom-libspec
(syntax-rules ()
[(_ ?libspec)
(let ([libspec ?libspec])
(make-libspec (libspec-name libspec)
(fxlogand (libspec-flags libspec)
(fxlognot (fxsll 1 (constant libspec-does-not-expect-headroom-index))))))]))
(define-syntax return-values
(syntax-rules ()
((_ args ...) (values args ...))))
(define-syntax with-values
(syntax-rules ()
((_ producer proc)
(call-with-values (lambda () producer) proc))))
(define-syntax meta-assert
(lambda (x)
(syntax-case x ()
[(_ e)
#`(let-syntax ([t (if e (lambda () #'(void)) #,(#%$make-source-oops #f "failed meta-assertion" #'e))])
(void))])))
(define-syntax features
(lambda (x)
(syntax-case x ()
[(k foo ...)
(with-implicit (k feature-list when-feature unless-feature if-feature)
#'(begin
(define-syntax feature-list
(syntax-rules ()
[(_) '(foo ...)]))
(define-syntax when-feature
(syntax-rules (foo ...)
[(_ foo e1 e2 (... ...)) (begin e1 e2 (... ...))] ...
[(_ bar e1 e2 (... ...)) (void)]))
(define-syntax unless-feature
(syntax-rules (foo ...)
[(_ foo e1 e2 (... ...)) (void)] ...
[(_ bar e1 e2 (... ...)) (begin e1 e2 (... ...))]))
(define-syntax if-feature
(syntax-rules (foo ...)
[(_ foo e1 e2) e1] ...
[(_ bar e1 e2) e2]))))])))
(define-constant scheme-version #x00090400)
(define-syntax define-machine-types
(lambda (x)
(syntax-case x ()
[(_ name ...)
(with-syntax ([(value ...) (enumerate (datum (name ...)))]
[(cname ...)
(map (lambda (name)
(construct-name name "machine-type-" name))
#'(name ...))])
#'(begin
(define-constant cname value) ...
(define-constant machine-type-alist '((value . name) ...))
(define-constant machine-type-limit (+ (max value ...) 1))))])))
(define-machine-types
any
i3le ti3le
i3nt ti3nt
i3fb ti3fb
i3ob ti3ob
i3osx ti3osx
a6le ta6le
a6osx ta6osx
a6ob ta6ob
a6s2 ta6s2
i3s2 ti3s2
a6fb ta6fb
i3nb ti3nb
a6nb ta6nb
a6nt ta6nt
i3qnx ti3qnx
arm32le tarm32le
ppc32le tppc32le
)
(include "machine.def")
(define-constant machine-type-name (cdr (assv (constant machine-type) (constant machine-type-alist))))
(define-syntax log2
(syntax-rules ()
[(_ n) (integer-length (- n 1))]))
; a string-char is a 32-bit equivalent of a ptr char: identical to a
; ptr char on 32-bit machines and the low-order half of a ptr char on
; 64-bit machines.
(define-constant string-char-bits 32)
(define-constant string-char-bytes 4)
(define-constant string-char-offset (log2 (constant string-char-bytes)))
(define-constant ptr-bytes (/ (constant ptr-bits) 8)) ; size in bytes
(define-constant log2-ptr-bytes (log2 (constant ptr-bytes)))
;;; ordinary types must be no more than 8 bits long
(define-constant ordinary-type-bits 8) ; smallest addressable unit
; (typemod = type modulus)
; The typemod defines the range of primary types and is also the
; offset that we subtract off of the actual addresses before adding
; in the primary type tag to obtain a ptr.
;
; The typemod imposes a lower bound on our choice of alignment
; since the low n bits of aligned addresses must be zero so that
; we can steal those bits for type tags.
;
; Leaving the typemod at 8 for 64-bit ports, means that we "waste"
; a bit of primary type space. If we ever attempt to reclaim this
; bit, we must remember that flonums are actually represented by two
; primary type codes, ie. 1xxx and 0xxx, see also the comment under
; byte-alignment.
(define-constant typemod 8)
(define-constant primary-type-bits (log2 (constant typemod)))
; We must have room for forward marker and forward pointer, hence two ptrs.
; We sometimes violate this for flonums since we "extract" the real
; and imag part by returning pointers into the inexactnum structure.
; This is safe since we never forward flonums.
(define-constant byte-alignment
(max (constant typemod) (* 2 (constant ptr-bytes))))
;;; fasl codes---see fasl.c for documentation of representation
(define-constant fasl-type-header 0)
(define-constant fasl-type-box 1)
(define-constant fasl-type-symbol 2)
(define-constant fasl-type-ratnum 3)
(define-constant fasl-type-vector 4)
(define-constant fasl-type-inexactnum 5)
(define-constant fasl-type-closure 6)
(define-constant fasl-type-pair 7)
(define-constant fasl-type-flonum 8)
(define-constant fasl-type-string 9)
(define-constant fasl-type-large-integer 10)
(define-constant fasl-type-code 11)
(define-constant fasl-type-immediate 12)
(define-constant fasl-type-entry 13)
(define-constant fasl-type-library 14)
(define-constant fasl-type-library-code 15)
(define-constant fasl-type-graph 16)
(define-constant fasl-type-graph-def 17)
(define-constant fasl-type-graph-ref 18)
(define-constant fasl-type-gensym 19)
(define-constant fasl-type-exactnum 20)
; 21
(define-constant fasl-type-fasl-size 22)
(define-constant fasl-type-record 23)
(define-constant fasl-type-rtd 24)
(define-constant fasl-type-small-integer 25)
(define-constant fasl-type-base-rtd 26)
(define-constant fasl-type-fxvector 27)
; 28
(define-constant fasl-type-bytevector 29)
(define-constant fasl-type-weak-pair 30)
(define-constant fasl-type-eq-hashtable 31)
(define-constant fasl-type-symbol-hashtable 32)
(define-constant fasl-type-group 33)
(define-constant fasl-type-visit 34)
(define-constant fasl-type-revisit 35)
(define-constant fasl-fld-ptr 0)
(define-constant fasl-fld-u8 1)
(define-constant fasl-fld-i16 2)
(define-constant fasl-fld-i24 3)
(define-constant fasl-fld-i32 4)
(define-constant fasl-fld-i40 5)
(define-constant fasl-fld-i48 6)
(define-constant fasl-fld-i56 7)
(define-constant fasl-fld-i64 8)
(define-constant fasl-fld-single 9)
(define-constant fasl-fld-double 10)
(define-constant fasl-header
(bytevector (constant fasl-type-header) 0 0 0
(char->integer #\c) (char->integer #\h) (char->integer #\e) (char->integer #\z)))
(define-syntax define-enumerated-constants
(lambda (x)
(syntax-case x ()
[(_ reloc-name ...)
(with-syntax ([(i ...) (enumerate #'(reloc-name ...))])
#'(begin
(define-constant reloc-name i)
...))])))
(define-syntax define-reloc-constants
(lambda (x)
(syntax-case x ()
[(_ (all x ...) (arch y ...) ...)
#`(constant-case architecture
[(arch) (define-enumerated-constants x ... y ...)]
...)])))
(define-reloc-constants
(all reloc-abs)
(x86 reloc-rel)
(sparc reloc-sparcabs reloc-sparcrel)
(sparc64 reloc-sparc64abs reloc-sparc64rel)
(ppc reloc-ppccall reloc-ppcload)
(x86_64 reloc-x86_64-call reloc-x86_64-jump)
(arm32 reloc-arm32-abs reloc-arm32-call reloc-arm32-jump)
(ppc32 reloc-ppc32-abs reloc-ppc32-call reloc-ppc32-jump))
(constant-case ptr-bits
[(64)
(define-constant reloc-extended-format #x1)
(define-constant reloc-type-offset 1)
(define-constant reloc-type-mask #x7)
(define-constant reloc-code-offset-offset 4)
(define-constant reloc-code-offset-mask #x3ffffff)
(define-constant reloc-item-offset-offset 30)
(define-constant reloc-item-offset-mask #x3ffffff)]
[(32)
(define-constant reloc-extended-format #x1)
(define-constant reloc-type-offset 1)
(define-constant reloc-type-mask #x7)
(define-constant reloc-code-offset-offset 4)
(define-constant reloc-code-offset-mask #x3ff)
(define-constant reloc-item-offset-offset 14)
(define-constant reloc-item-offset-mask #x3ffff)])
(macro-define-structure (reloc type item-offset code-offset long?))
(define-constant SERROR #x0000)
(define-constant STRVNCATE #x0001) ; V for U to avoid msvc errno.h conflict
(define-constant SREPLACE #x0002)
(define-constant SAPPEND #x0003)
(define-constant SDEFAULT #x0004)
(define-constant OPEN-ERROR-OTHER 0)
(define-constant OPEN-ERROR-PROTECTION 1)
(define-constant OPEN-ERROR-EXISTS 2)
(define-constant OPEN-ERROR-EXISTSNOT 3)
(define-constant SEOF -1)
(define-constant SICONV-DUNNO 0)
(define-constant SICONV-INVALID 1)
(define-constant SICONV-INCOMPLETE 2)
(define-constant SICONV-NOROOM 3)
;;; port flag masks are always single bits
(define-constant port-flag-input #x01)
(define-constant port-flag-output #x02)
(define-constant port-flag-binary #x04)
(define-constant port-flag-closed #x08)
(define-constant port-flag-file #x10)
(define-constant port-flag-compressed #x20)
(define-constant port-flag-exclusive #x40)
(define-constant port-flag-bol #x80)
(define-constant port-flag-eof #x100)
(define-constant port-flag-block-buffered #x200)
(define-constant port-flag-line-buffered #x400)
(define-constant port-flag-input-mode #x800)
(define-constant port-flag-char-positions #x1000)
(define-constant port-flag-r6rs #x2000)
(define-constant port-flag-fold-case #x4000)
(define-constant port-flag-no-fold-case #x8000)
(define-constant port-flags-offset (constant ordinary-type-bits))
;;; allcaps versions are pre-shifted by port-flags-offset
(define-constant PORT-FLAG-INPUT (ash (constant port-flag-input) (constant port-flags-offset)))
(define-constant PORT-FLAG-OUTPUT (ash (constant port-flag-output) (constant port-flags-offset)))
(define-constant PORT-FLAG-BINARY (ash (constant port-flag-binary) (constant port-flags-offset)))
(define-constant PORT-FLAG-CLOSED (ash (constant port-flag-closed) (constant port-flags-offset)))
(define-constant PORT-FLAG-FILE (ash (constant port-flag-file) (constant port-flags-offset)))
(define-constant PORT-FLAG-COMPRESSED (ash (constant port-flag-compressed) (constant port-flags-offset)))
(define-constant PORT-FLAG-EXCLUSIVE (ash (constant port-flag-exclusive) (constant port-flags-offset)))
(define-constant PORT-FLAG-BOL (ash (constant port-flag-bol) (constant port-flags-offset)))
(define-constant PORT-FLAG-EOF (ash (constant port-flag-eof) (constant port-flags-offset)))
(define-constant PORT-FLAG-BLOCK-BUFFERED (ash (constant port-flag-block-buffered) (constant port-flags-offset)))
(define-constant PORT-FLAG-LINE-BUFFERED (ash (constant port-flag-line-buffered) (constant port-flags-offset)))
(define-constant PORT-FLAG-INPUT-MODE (ash (constant port-flag-input-mode) (constant port-flags-offset)))
(define-constant PORT-FLAG-CHAR-POSITIONS (ash (constant port-flag-char-positions) (constant port-flags-offset)))
(define-constant PORT-FLAG-R6RS (ash (constant port-flag-r6rs) (constant port-flags-offset)))
(define-constant PORT-FLAG-FOLD-CASE (ash (constant port-flag-fold-case) (constant port-flags-offset)))
(define-constant PORT-FLAG-NO-FOLD-CASE (ash (constant port-flag-no-fold-case) (constant port-flags-offset)))
;;; c-error codes
(define-constant ERROR_OTHER 0)
(define-constant ERROR_CALL_UNBOUND 1)
(define-constant ERROR_CALL_NONPROCEDURE_SYMBOL 2)
(define-constant ERROR_CALL_NONPROCEDURE 3)
(define-constant ERROR_CALL_ARGUMENT_COUNT 4)
(define-constant ERROR_RESET 5)
(define-constant ERROR_NONCONTINUABLE_INTERRUPT 6)
(define-constant ERROR_VALUES 7)
(define-constant ERROR_MVLET 8)
;;; object-file tags
(define-constant visit-tag 0)
(define-constant revisit-tag 1)
;;; allocation spaces
(define-constant space-locked #x20) ; lock flag
(define-constant space-old #x40) ; oldspace flag
(define-syntax define-alloc-spaces
(lambda (x)
(syntax-case x (real swept unswept unreal)
[(_ (real
(swept
(swept-name swept-cname swept-cchar swept-value)
...
(last-swept-name last-swept-cname last-swept-cchar last-swept-value))
(unswept
(unswept-name unswept-cname unswept-cchar unswept-value)
...
(last-unswept-name last-unswept-cname last-unswept-cchar last-unswept-value)))
(unreal
(unreal-name unreal-cname unreal-cchar unreal-value)
...
(last-unreal-name last-unreal-cname last-unreal-cchar last-unreal-value)))
(with-syntax ([(real-name ...) #'(swept-name ... last-swept-name unswept-name ... last-unswept-name)]
[(real-cname ...) #'(swept-cname ... last-swept-cname unswept-cname ... last-unswept-cname)]
[(real-cchar ...) #'(swept-cchar ... last-swept-cchar unswept-cchar ... last-unswept-cchar)]
[(real-value ...) #'(swept-value ... last-swept-value unswept-value ... last-unswept-value)])
(with-syntax ([(name ...) #'(real-name ... unreal-name ... last-unreal-name)]
[(cname ...) #'(real-cname ... unreal-cname ... last-unreal-cname)]
[(cchar ...) #'(real-cchar ... unreal-cchar ... last-unreal-cchar)]
[(value ...) #'(real-value ... unreal-value ... last-unreal-value)])
(with-syntax ([(space-name ...) (map (lambda (n) (construct-name n "space-" n)) #'(name ...))])
(unless (< (syntax->datum #'last-unreal-value) (constant space-locked))
($oops 'define-alloc-spaces "conflict with space-locked"))
(unless (< (syntax->datum #'last-unreal-value) (constant space-old))
($oops 'define-alloc-spaces "conflict with space-old"))
#'(begin
(define-constant space-name value) ...
(define-constant real-space-alist '((real-name . real-value) ...))
(define-constant space-cname-list '(cname ...))
(define-constant space-char-list '(cchar ...))
(define-constant max-sweep-space last-swept-value)
(define-constant max-real-space last-unswept-value)
(define-constant max-space last-unreal-value)))))])))
(define-alloc-spaces
(real
(swept
(new "new" #\n 0) ; all generation 0 objects allocated here
(impure "impure" #\i 1) ; most mutable objects allocated here (all ptrs)
(symbol "symbol" #\x 2) ;
(port "port" #\q 3) ;
(weakpair "weakpr" #\w 4) ;
(pure "pure" #\p 5) ; swept immutable objects allocated here (all ptrs)
(continuation "cont" #\k 6) ;
(code "code" #\c 7) ;
(pure-typed-object "p-tobj" #\r 8) ;
(impure-record "ip-rec" #\s 9)) ;
(unswept
(data "data" #\d 10))) ; unswept objects allocated here
(unreal
(empty "empty" #\e 11))) ; available segments
;;; enumeration of types for which gc tracks object counts
;;; also update gc.c
(define-constant countof-pair 0)
(define-constant countof-symbol 1)
(define-constant countof-flonum 2)
(define-constant countof-closure 3)
(define-constant countof-continuation 4)
(define-constant countof-bignum 5)
(define-constant countof-ratnum 6)
(define-constant countof-inexactnum 7)
(define-constant countof-exactnum 8)
(define-constant countof-box 9)
(define-constant countof-port 10)
(define-constant countof-code 11)
(define-constant countof-thread 12)
(define-constant countof-tlc 13)
(define-constant countof-rtd-counts 14)
(define-constant countof-stack 15)
(define-constant countof-relocation-table 16)
(define-constant countof-weakpair 17)
(define-constant countof-vector 18)
(define-constant countof-string 19)
(define-constant countof-fxvector 20)
(define-constant countof-bytevector 21)
(define-constant countof-locked 22)
(define-constant countof-guardian 23)
(define-constant countof-oblist 24)
(define-constant countof-types 25)
;;; type-fixnum is assumed to be all zeros by at least by vector, fxvector,
;;; and bytevector index checks
(define-constant type-fixnum 0) ; #b100/#b000 32-bit, #b000 64-bit
(define-constant type-pair #b001)
(define-constant type-flonum #b010)
(define-constant type-symbol #b011)
; #b100 occupied by fixnums on 32-bit machines, unused on 64-bit machines
(define-constant type-closure #b101)
(define-constant type-immediate #b110)
(define-constant type-typed-object #b111)
;;; note: for type-char, leave at least fixnum-offset zeros at top of
;;; type byte to simplify char->integer conversion
(define-constant type-boolean #b00000110)
(define-constant ptr sfalse #b00000110)
(define-constant ptr strue #b00001110)
(define-constant type-char #b00010110)
(define-constant ptr sunbound #b00011110)
(define-constant ptr snil #b00100110)
(define-constant ptr forward-marker #b00101110)
(define-constant ptr seof #b00110110)
(define-constant ptr svoid #b00111110)
(define-constant ptr black-hole #b01000110)
(define-constant ptr sbwp #b01001110)
;;; vector type/length field is a fixnum
;;; (define-constant type-vector (constant type-fixnum))
; #b000 occupied by vectors on 32- and 64-bit machines
(define-constant type-string #b001)
; #b010 unused
(define-constant type-fxvector #b011)
; #b100 occupied by vectors on 32-bit machines, unused on 64-bit machines
(define-constant type-bytevector #b101)
(define-constant type-other-number #b0110) ; bit 3 reset for numbers
(define-constant type-bignum #b00110) ; bit 4 reset for bignums
(define-constant type-positive-bignum #b000110)
(define-constant type-negative-bignum #b100110)
(define-constant type-ratnum #b00010110) ; bit 4 set for non-bignum numbers
(define-constant type-inexactnum #b00110110)
(define-constant type-exactnum #b01010110)
(define-constant type-box #b00001110) ; bit 3 set for non-numbers
(define-constant type-port #b00011110)
; #b00101110 (forward_marker) must not be used
(define-constant type-code #b00111110)
(define-constant type-thread #b01001110)
(define-constant type-tlc #b01011110)
(define-constant type-rtd-counts #b01101110)
(define-constant type-record #b111)
(define-constant code-flag-system #b0001)
(define-constant code-flag-continuation #b0010)
(define-constant fixnum-bits
(case (constant ptr-bits)
[(64) 61]
[(32) 30]
[else ($oops 'fixnum-bits "expected reasonable native bit width (eg. 32 or 64)")]))
(define-constant iptr most-positive-fixnum
(- (expt 2 (- (constant fixnum-bits) 1)) 1))
(define-constant iptr most-negative-fixnum
(- (expt 2 (- (constant fixnum-bits) 1))))
(define-constant fixnum-offset (- (constant ptr-bits) (constant fixnum-bits)))
(define-constant string-length-offset 3)
(define-constant iptr maximum-string-length
(min (- (expt 2 (fx- (constant ptr-bits) (constant string-length-offset))) 1)
(constant most-positive-fixnum)))
(define-constant bignum-sign-offset 5)
(define-constant bignum-length-offset 6)
(define-constant iptr maximum-bignum-length
(min (- (expt 2 (fx- (constant ptr-bits) (constant bignum-length-offset))) 1)
(constant most-positive-fixnum)))
(define-constant bigit-bits 32)
(define-constant bigit-bytes (/ (constant bigit-bits) 8))
; fxvector length field is stored with type
(define-constant fxvector-length-offset 3)
(define-constant iptr maximum-fxvector-length
(min (- (expt 2 (fx- (constant ptr-bits) (constant fxvector-length-offset))) 1)
(constant most-positive-fixnum)))
; bytevector length field is stored with type
(define-constant bytevector-length-offset 3)
(define-constant iptr maximum-bytevector-length
(min (- (expt 2 (fx- (constant ptr-bits) (constant bytevector-length-offset))) 1)
(constant most-positive-fixnum)))
(define-constant code-flags-offset (constant ordinary-type-bits))
(define-constant char-data-offset 8)
(define-constant type-binary-port
(fxlogor (ash (constant port-flag-binary) (constant port-flags-offset))
(constant type-port)))
(define-constant type-textual-port (constant type-port))
(define-constant type-input-port
(fxlogor (ash (constant port-flag-input) (constant port-flags-offset))
(constant type-port)))
(define-constant type-binary-input-port
(fxlogor (ash (constant port-flag-binary) (constant port-flags-offset))
(constant type-input-port)))
(define-constant type-textual-input-port (constant type-input-port))
(define-constant type-output-port
(fxlogor (ash (constant port-flag-output) (constant port-flags-offset))
(constant type-port)))
(define-constant type-binary-output-port
(fxlogor (ash (constant port-flag-binary) (constant port-flags-offset))
(constant type-output-port)))
(define-constant type-textual-output-port (constant type-output-port))
(define-constant type-io-port
(fxlogor (constant type-input-port)
(constant type-output-port)))
(define-constant type-system-code
(fxlogor (constant type-code)
(fxsll (constant code-flag-system)
(constant code-flags-offset))))
(define-constant type-continuation-code
(fxlogor (constant type-code)
(fxsll (constant code-flag-continuation)
(constant code-flags-offset))))
;; type checks are generally performed by applying the mask to the object
;; then comparing against the type code. a mask equal to
;; (constant byte-constant-mask) implies that the object being
;; type-checked must have zeros in all but the low byte if it is to pass
;; the check so that anything between a byte and full word comparison
;; can be used.
(define-constant byte-constant-mask (- (ash 1 (constant ptr-bits)) 1))
;;; mask-fixnum is assumed to be all ones followed by some number of
;;; zeros at least by vector, fxvector, and bytevector index checks
(define-constant mask-fixnum (- (ash 1 (constant fixnum-offset)) 1))
;;; octets are fixnums in the range 0..255
(define-constant mask-octet (lognot (ash #xff (constant fixnum-offset))))
(define-constant type-octet (constant type-fixnum))
(define-constant mask-pair #b111)
(define-constant mask-flonum #b111)
(define-constant mask-symbol #b111)
(define-constant mask-closure #b111)
(define-constant mask-immediate #b111)
(define-constant mask-typed-object #b111)
(define-constant mask-boolean #b11110111)
(define-constant mask-char #xFF)
(define-constant mask-false (constant byte-constant-mask))
(define-constant mask-eof (constant byte-constant-mask))
(define-constant mask-unbound (constant byte-constant-mask))
(define-constant mask-nil (constant byte-constant-mask))
(define-constant mask-bwp (constant byte-constant-mask))
;;; vector type/length field is a fixnum
;;; (define-constant mask-vector (constant mask-fixnum))
(define-constant mask-string #b111)
(define-constant mask-fxvector #b111)
(define-constant mask-bytevector #b111)
(define-constant mask-other-number #b1111)
(define-constant mask-bignum #b11111)
(define-constant mask-bignum-sign #b100000)
(define-constant mask-signed-bignum
(fxlogor
(constant mask-bignum)
(constant mask-bignum-sign)))
(define-constant mask-ratnum (constant byte-constant-mask))
(define-constant mask-inexactnum (constant byte-constant-mask))
(define-constant mask-exactnum (constant byte-constant-mask))
(define-constant mask-rtd-counts (constant byte-constant-mask))
(define-constant mask-record #b111)
(define-constant mask-port #xFF)
(define-constant mask-binary-port
(fxlogor (fxsll (constant port-flag-binary) (constant port-flags-offset))
(constant mask-port)))
(define-constant mask-textual-port (constant mask-binary-port))
(define-constant mask-input-port
(fxlogor (fxsll (constant port-flag-input) (constant port-flags-offset))
(fx- (fxsll 1 (constant port-flags-offset)) 1)))
(define-constant mask-binary-input-port
(fxlogor (fxsll (constant port-flag-binary) (constant port-flags-offset))
(constant mask-input-port)))
(define-constant mask-textual-input-port (constant mask-binary-input-port))
(define-constant mask-output-port
(fxlogor (fxsll (constant port-flag-output) (constant port-flags-offset))
(fx- (fxsll 1 (constant port-flags-offset)) 1)))
(define-constant mask-binary-output-port
(fxlogor (fxsll (constant port-flag-binary) (constant port-flags-offset))
(constant mask-output-port)))
(define-constant mask-textual-output-port (constant mask-binary-output-port))
(define-constant mask-box (constant byte-constant-mask))
(define-constant mask-code #xFF)
(define-constant mask-system-code
(fxlogor (fxsll (constant code-flag-system) (constant code-flags-offset))
(fx- (fxsll 1 (constant code-flags-offset)) 1)))
(define-constant mask-continuation-code
(fxlogor (fxsll (constant code-flag-continuation) (constant code-flags-offset))
(fx- (fxsll 1 (constant code-flags-offset)) 1)))
(define-constant mask-thread (constant byte-constant-mask))
(define-constant mask-tlc (constant byte-constant-mask))
(define-constant mask-positive-fixnum #x80000003)
(define-constant fixnum-factor (expt 2 (constant fixnum-offset)))
(define-constant string-length-factor (expt 2 (constant string-length-offset)))
(define-constant bignum-length-factor (expt 2 (constant bignum-length-offset)))
(define-constant fxvector-length-factor (expt 2 (constant fxvector-length-offset)))
(define-constant bytevector-length-factor (expt 2 (constant bytevector-length-offset)))
(define-constant char-factor (expt 2 (constant char-data-offset)))
;;; record-datatype must be defined before we include layout.ss
;;; (maybe should move into that file??)
;;; We allow Scheme inputs for both signed and unsigned integers to range from
;;; -2^(b-1)..2^b-1, e.g., for 32-bit, -2^31..2^32-1.
(macro-define-structure (fld name mutable? type byte))
(eval-when (compile load eval)
(define-syntax foreign-datatypes
(identifier-syntax
'((scheme-object (constant ptr-bytes) (lambda (x) #t))
(double-float 8 flonum?)
(single-float 4 flonum?)
(integer-8 1 $integer-8?)
(unsigned-8 1 $integer-8?)
(integer-16 2 $integer-16?)
(unsigned-16 2 $integer-16?)
(integer-24 3 $integer-24?)
(unsigned-24 3 $integer-24?)
(integer-32 4 $integer-32?)
(unsigned-32 4 $integer-32?)
(integer-40 5 $integer-40?)
(unsigned-40 5 $integer-40?)
(integer-48 6 $integer-48?)
(unsigned-48 6 $integer-48?)
(integer-56 7 $integer-56?)
(unsigned-56 7 $integer-56?)
(integer-64 8 $integer-64?)
(unsigned-64 8 $integer-64?)
(fixnum (constant ptr-bytes) fixnum?)
(char 1 $foreign-char?)
(wchar (fxsrl (constant wchar-bits) 3) $foreign-wchar?)
(boolean (fxsrl (constant int-bits) 3) (lambda (x) #t)))))
)
(define-syntax record-datatype
(with-syntax ((((type bytes pred) ...)
(datum->syntax #'* foreign-datatypes)))
(lambda (x)
(syntax-case x (list cases)
[(_ list) #''(type ...)]
[(_ cases ty handler else-expr)
#'(case ty
[(type) (handler type bytes pred)]
...
[else else-expr])]))))
(define-syntax c-alloc-align
(syntax-rules ()
((_ n)
(fxlogand (fx+ n (fx- (constant byte-alignment) 1))
(fxlognot (fx- (constant byte-alignment) 1))))))
(eval-when (compile load eval)
(define-syntax filter-foreign-type
; for $object-ref, foreign-ref, etc.
; foreign-procedure and foriegn-callable have their own
; filter-type in syntax.ss
(with-syntax ([alist (datum->syntax #'*
`((ptr . scheme-object)
(iptr .
,(constant-case ptr-bits
[(32) 'integer-32]
[(64) 'integer-64]))
(uptr .
,(constant-case ptr-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(void* .
,(constant-case ptr-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(int .
,(constant-case int-bits
[(32) 'integer-32]
[(64) 'integer-64]))
(unsigned .
,(constant-case int-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(unsigned-int .
,(constant-case int-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(short .
,(constant-case short-bits
[(16) 'integer-16]
[(32) 'integer-32]))
(unsigned-short .
,(constant-case short-bits
[(16) 'unsigned-16]
[(32) 'unsigned-32]))
(long .
,(constant-case long-bits
[(32) 'integer-32]
[(64) 'integer-64]))
(unsigned-long .
,(constant-case long-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(long-long .
,(constant-case long-long-bits
[(64) 'integer-64]))
(unsigned-long-long .
,(constant-case long-long-bits
[(64) 'unsigned-64]))
(wchar_t . wchar)
(size_t .
,(constant-case size_t-bits
[(32) 'unsigned-32]
[(64) 'unsigned-64]))
(ssize_t .
,(constant-case size_t-bits
[(32) 'integer-32]
[(64) 'integer-64]))