-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathgoal-lib.gc
More file actions
1342 lines (1124 loc) · 35 KB
/
Copy pathgoal-lib.gc
File metadata and controls
1342 lines (1124 loc) · 35 KB
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
;;-*-Lisp-*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; BUILD SYSTEM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro m (file)
"Make: compile a file fully and save the result"
`(asm-file ,file :color :write)
)
(defmacro md (file &rest path)
"Make Debug: make + print disassembly for a file"
(if (null? path)
`(asm-file ,file :color :write :disassemble)
`(asm-file ,file :color :write :disassemble ,(first path))
)
)
(defmacro mda (file &rest path)
"Make Debug Asm Only: make + print disassembly (asm-only mode) for a file"
(if (null? path)
`(asm-file ,file :color :write :disassemble :disasm-code-only)
`(asm-file ,file :color :write :disassemble ,(first path) :disasm-code-only)
)
)
(defmacro ml (file)
"Make Load: make and load the file through the listener"
`(asm-file ,file :color :load :write)
)
(desfun make-build-command (file)
`(asm-file ,file :color :write)
)
(desfun run-frontend-command (file)
`(asm-file ,file :no-code)
)
(defmacro load-imports ()
`(begin
,@(apply run-frontend-command all-import-files)
)
)
(defmacro build-kernel ()
"Build kernel and create the KERNEL CGO"
`(make-cgo "KERNEL")
)
(defmacro build-game ()
"Build all game code and all game CGOs"
`(make-group "all-code")
)
(defmacro blg ()
"Build engine and kernel CGOs (code only, no data for now) and load them in the listener
Uses the blocking dgo-load."
`(begin
(build-game)
(dgo-load "kernel" global (link-flag output-load-msg output-load-true-msg execute-login print-login) #x200000)
(load-package "game" global)
)
)
(defmacro lg ()
"Load an already built game."
`(load-package "game" global)
)
(defmacro tc ()
"Typecheck against the all-types file"
`(m "decompiler/config/jak1/all-types.gc")
)
(defmacro e ()
"Exit the compiler"
`(:exit)
)
(defmacro dbc ()
"Put the compiler in debug mode"
`(begin
(set-config! print-ir #t)
(set-config! print-regalloc #t)
)
)
(defmacro import (file-name)
`(asm-file ,file-name :no-code)
)
;; enum for text file encoding versions
(defenum game-text-version
(jak1-v1 10)
(jak1-v2 11)
(jak2 20)
(jak3 30)
(jakx 40)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CONDITIONAL COMPILATION
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro #when (clause &rest body)
`(#cond (,clause ,@body))
)
(defmacro #unless (clause &rest body)
`(#cond ((not ,clause) ,@body))
)
(defmacro #if (clause true false)
`(#cond (,clause ,true) (#t ,false))
)
(defmacro move-if-not-zero (result value check original)
`(if (!= ,check 0)
(set! ,result (the-as int ,value))
(set! ,result (the-as int ,original))
)
)
(defmacro set-on-less-than (dest src1 src2)
"dest = src1 < src2 ? 1 : 0 -- Compare as Signed Integers"
`(if (< (the int ,src1) (the int ,src2))
(set! ,dest 1)
(set! ,dest 0)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; TARGET CONTROL
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro lt (&rest args)
"Listen to target. Opens a connection and flushes buffers"
`(begin
(listen-to-target ,@args)
(:status)
)
)
(defmacro r (&rest args)
"Reset the target state and reconnect."
`(begin
;; connect, so we can send reset. if we're already connected, does nothing
(listen-to-target ,@args)
;; send a reset message, disconnecting us
(reset-target)
;; establish connection again
(listen-to-target ,@args)
;; flush buffers
(:status)
)
)
(defmacro shutdown-target ()
"Make the target exit. The runtime itself will exit and not restart automatically."
`(begin
(reset-target :shutdown)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; DEBUGGER MACROS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro db (&rest args)
"Print bytes."
`(:pm 1 ,@args)
)
(defmacro dh (&rest args)
"Print halfwords (16-bits)"
`(:pm 2 ,@args)
)
(defmacro dw (&rest args)
"Print words (32-bits)"
`(:pm 4 ,@args)
)
(defmacro dd (&rest args)
"Print doublewords (64-bits)"
`(:pm 8 ,@args)
)
(defmacro df (&rest args)
"Print floats (32-bit)"
`(:pm 4 ,@args :print-mode float)
)
(defmacro segfault ()
"Dereference the GOAL 0 pointer, which should be a segfault"
`(-> (the (pointer int) 0))
)
(defmacro fpe ()
"Trigger a SIGFPE by doing integer division by zero"
`(/ 0 0)
)
(defmacro break! ()
"A breakpoint. Todo - should we use int3 instead?"
`(/ 0 0)
)
(defmacro crash! ()
"Cause a crash by attempting to deference 0x0"
`(-> (the (pointer uint8) 0))
)
;;;;;;;;;;;;;;;;;;;
;; GOAL Syntax
;;;;;;;;;;;;;;;;;;;
;; Bind vars in body
(defmacro let (bindings &rest body)
`((lambda :immediate #t ,(apply first bindings) ,@body)
,@(apply second bindings)))
;; Let, but recursive, allowing you to define variables in terms of others.
(defmacro let* (bindings &rest body)
(if (null? bindings)
`(begin ,@body)
`((lambda :immediate #t (,(caar bindings))
(let* ,(cdr bindings) ,@body))
,(car (cdar bindings))
)
)
)
;; mlet, but recursive, allowing you to define variables in terms of others.
(defmacro mlet* (bindings &rest body)
(if (null? bindings)
`(begin ,@body)
`((lambda :immediate #t (,(caar bindings))
(mlet* ,(cdr bindings) ,@body))
,(car (cdar bindings))
)
)
)
;; Define a new function
(defmacro defun (name bindings &rest body)
(let ((docstring ""))
(when (and (> (length body) 1) (string? (first body)))
(set! docstring (first body))
(set! body (cdr body)))
`(define ,name ,docstring (lambda :name ,name ,bindings ,@body))))
;; the compiler can't figure out types of a recursive function without
;; first knowing the return type, so we use this form to forward declare
;; and define a function.
(defmacro defun-recursive (name return-type bindings &rest body)
`(begin
(define-extern ,name
(function ,@(apply (lambda (x)
(if (pair? x)
(second x)
'object)
)
bindings)
,return-type))
(defun ,name ,bindings ,@body)
)
)
(defmacro defun-extern (function-name &rest type-info)
`(define-extern ,function-name (function ,@type-info))
)
(defmacro def-event-handler (name type)
`(define-extern ,name (function process int symbol event-message-block object :behavior ,type))
)
(defmacro defun-debug (name bindings &rest body)
"Define a function which is only present in debug mode. Otherwise the function becomes nothing"
`(if *debug-segment*
,(if (and
(> (length body) 1) ;; more than one thing in function
(string? (first body)) ;; first thing is a string
)
;; then it's a docstring and we ignore it.
`(define ,name (lambda :name ,name :segment debug ,bindings ,@(cdr body)))
;; otherwise don't ignore it.
`(define ,name (lambda :name ,name :segment debug ,bindings ,@body))
)
;; function not loaded, set function to the nothing function.
;; we don't typecheck this.
(define :no-typecheck #t ,name nothing)
)
)
(defmacro defun-debug-recursive (name return-type bindings &rest body)
`(begin
(define-extern ,name
(function ,@(apply (lambda (x)
(if (pair? x)
(second x)
'object)
)
bindings)
,return-type))
(if *debug-segment*
(defun-debug ,name ,bindings ,@body)
(define :no-typecheck #t ,name nothing))
)
)
(defmacro define-once (name value)
"define once. Does not set the symbol if it already has a value. It must have been at least forward-declared first!"
`(begin
(if (or (not ,name) (zero? ,name))
(set! ,name ,value)
)
)
)
(defmacro define-perm (name type value)
"Define 'permanent', meaning the original definition will not be blown away by a file reload.
If the value of the symbol is unset (zero) or set to false, it will be defined.
Otherwise, no effect, other than to inform the type system of the symbol type."
`(begin
(define-extern ,name ,type)
(define-once ,name ,value)
)
)
(defmacro continue (&key (from #f))
"Skips the remainder of the current loop iteration. Optionally continue from a labeled loop."
`(return-from ,(string->symbol (if from (string-append (symbol->string from) "-continue") "continue")) #f)
)
(defmacro break (&key (from #f))
"Exits the current loop immediately. Optionally break from a labeled loop."
`(return-from ,(string->symbol (if from (string-append (symbol->string from) "-break") "break")) #f)
)
(defmacro while (test &key (label #f) &rest body)
"While loop. The test is evaluated before body."
(with-gensyms (reloop test-exit)
(let ((break-label (string->symbol (if label (string-append (symbol->string label) "-break") "break")))
(continue-label (string->symbol (if label (string-append (symbol->string label) "-continue") "continue"))))
`(block ,break-label
(goto ,test-exit)
(label ,reloop)
(block ,continue-label
,@(if (null? body) (list `(return-from ,continue-label #f)) body)
)
(label ,test-exit)
(when-goto ,test ,reloop)
#f
)
)
)
)
(defmacro until (test &key (label #f) &rest body)
"Until loop. The body is evaluated before the test."
(with-gensyms (reloop)
(let ((break-label (string->symbol (if label (string-append (symbol->string label) "-break") "break")))
(continue-label (string->symbol (if label (string-append (symbol->string label) "-continue") "continue"))))
`(block ,break-label
(label ,reloop)
(block ,continue-label
,@(if (null? body) (list `(return-from ,continue-label #f)) body)
)
(when-goto (not ,test) ,reloop)
)
)
)
)
(defmacro dotimes (var &key (label #f) &key (unroll 1) &rest body)
"Loop like for (int i = 0; i < end, i++)
var is a list made up of a variable to bind the amount to (second item), and the remaining forms are evaluated after the loop is finished
Supports optional loop unrolling with :unroll factor
Supports (_ x) syntax to iterate without binding and incrementing a variable when unrolled, and unrolls by default
Warning: Does not validate the :unroll factor ratio and doesn't support break/continue when unrolling"
(let ((index-var (first var))
(limit (second var))
(continue-label (string->symbol (if label (string-append (symbol->string label) "-continue") "continue")))
(unrolled-body '())
)
(when (and (integer? limit)
(> unroll limit)
)
(error ":unroll factor must be <= the loop limit.")
)
(when (< unroll 1)
(error ":unroll factor must be >= 1.")
)
;; gensym to support iterating when needed if a binding var wasn't provided
(with-gensyms (temp-index-var)
;; Append each expression from the body followed by the increment if needed, :unroll times
(dotimes (x unroll)
(let ((body-copy body))
(while (> (length body-copy) 0)
(set! unrolled-body (cons (car body-copy) unrolled-body))
(set! body-copy (cdr body-copy))
)
)
(unless (eq? index-var '_)
(set! unrolled-body (cons `(1+! ,index-var) unrolled-body))
)
;; Only append the temporary index increment if we aren't fully unrolling
;; or if the limit could be dynamic
(when (and (eq? index-var '_)
(or (not (integer? limit))
(not (eq? limit unroll))
)
)
(set! unrolled-body (cons `(1+! ,temp-index-var) unrolled-body))
)
)
;; Reverse the unrolled body to maintain order
(let ((reversed-body '()))
(while (> (length unrolled-body) 0)
(set! reversed-body (cons (car unrolled-body) reversed-body))
(set! unrolled-body (cdr unrolled-body))
)
(set! unrolled-body reversed-body)
)
;; Build the loop construct or the unrolled body
(if (eq? index-var '_)
(if (eq? unroll limit)
;; Don't need any loop constructs or index variables when fully unrolling
`(begin ,@unrolled-body)
`(let ((,temp-index-var 0))
(while (< ,temp-index-var ,limit) :label ,label
(block ,continue-label
,@unrolled-body
)
)
,@(cddr var)
)
)
`(let ((,index-var 0))
,@(if (eq? unroll limit)
;; Don't need any loop constructs when fully unrolling
unrolled-body
(list `(while (< ,index-var ,limit) :label ,label
(block ,continue-label
,@unrolled-body)
)
)
)
,@(cddr var)
)
)
)
)
)
(defmacro countdown (var &key (label #f) &rest body)
"Loop like for (int i = end; i-- > 0)"
`(let ((,(first var) ,(second var)))
(while (!= ,(first var) 0) :label ,label
(set! ,(first var) (- ,(first var) 1))
,@body
)
)
)
(defmacro loop (&key (label #f) &rest body)
"Loop this code forever."
`(while #t :label ,label
,@body)
)
(defmacro doarray (bindings &key (label #f) &rest body)
"iterate over an array. usage: (doarray (<array entry name> <array>) <code>)"
(with-gensyms (len i)
(let ((val (first bindings))
(arr (second bindings))
(continue-label (string->symbol (if label (string-append (symbol->string label) "-continue") "continue"))))
`(let* ((,len (-> ,arr length))
(,i 0)
(,val (-> ,arr ,i)))
(while (< ,i ,len) :label ,label
(block ,continue-label
,@(if (null? body) (list `(return-from ,continue-label #f)) body)
)
(1+! ,i)
(set! ,val (-> ,arr ,i))
)
)
)
)
)
;; Backup some values, and restore after executing body.
;; Non-dynamic (nonlocal jumps out of body will skip restore)
;; NOTE : GOAL protected defs in a FIFO manner (this is FILO/LIFO), this should be fixed at some point
(defmacro protect (defs &rest body)
(if (null? defs)
;; nothing to backup, just insert body (base case)
`(begin ,@body)
;; a unique name for the thing we are backing up
(with-gensyms (backup)
;; store the original value of the first def in backup
`(let ((,backup ,(first defs)))
;; backup any other things which need backing up
(protect ,(cdr defs)
;; execute the body
,@body
)
;; restore the first thing
(set! ,(first defs) ,backup)
)
)
)
)
(defmacro if (condition true-case &rest others)
(if (> (length others) 1)
(error "got too many arguments to if")
#f
)
(if (null? others)
`(cond (,condition ,true-case))
`(cond (,condition ,true-case)
(else ,(first others))
)
)
)
(defmacro when (condition &rest body)
`(if ,condition
(begin ,@body)
)
)
(defmacro unless (condition &rest body)
`(if (not ,condition)
(begin ,@body)
)
)
(defmacro aif (condition true &rest false)
"Anaphoric if, similar to Common Lisp"
`(let ((it ,condition))
(if it
,true
,@false
)
)
)
(defmacro awhen (condition &rest body)
"Anaphoric when"
`(aif ,condition (begin ,@body) #f)
)
(defmacro return (val)
`(return-from #f ,val)
)
(defmacro empty ()
"The decompiler may use (empty) as the body of a loop with nothing in it."
`(none)
)
(defmacro case (switch &key (comp =) &rest cases)
"A switch-case construct. switch is saved onto a local variable and compared against each case, sequentially.
else can be used like the 'default' case, but it must be the last one.
comp is the function to use when evaluating the clauses. It can be any valid head of a form (operator or call)."
(with-gensyms (sw)
;; save the switch to a variable (only evaluated once)
`(let ((,sw ,switch))
;; build the cond construct with each case
(cond ,@(apply
(lambda (x) `(
;; each case is of format ((cond cond cond...) body)
,@(let ((conditions (first x)) ;; list of conditions, OR just else
(body (rest x))) ;; the body
(cond
;; if the condition is just 'else'
( (eq? conditions 'else)
`(else ,@body)
)
;; if the list is made up of a single condition
( (= (length conditions) 1)
`((,comp ,sw ,(first conditions)) ,@body)
)
;; otherwise it is made up of multiple conditions, or them together!
(#t
`((or ,@(apply (lambda (c) `(,comp ,sw ,c)) conditions)) ,@body)
)
)
)
)
)
cases)
)
)
)
)
(defmacro case-str (switch &rest cases)
"Same as case, but for string comparisons instead."
`(case ,switch ,@cases :comp string=)
)
(defmacro dolist (bindings &rest body)
`(let ((,(car bindings) ,(cadr bindings)))
(while (not (null? ,(car bindings)))
,@body
(set! ,(car bindings) (cdr ,(car bindings)))
)
)
)
(defmacro the-as-safe (type obj)
(with-gensyms (temp)
`(let ((,temp ,obj))
(if (type? ,temp ,type)
(the-as ,type ,temp))
)
)
)
(defmacro symbol-member? (sym lst)
"is sym present in lst? uses or, so it short circuits."
(with-gensyms (sym-var)
`(let ((,sym-var ,sym))
;; TODO fix this bug
(or ,@(apply (lambda (x) `(= ,sym-var (quote ,x))) (second lst))))
)
)
;;;;;;;;;;;;;;;;;;;
;; Math Macros
;;;;;;;;;;;;;;;;;;;
(defmacro 1+ (var)
`(+ ,var 1)
)
(defmacro inc (val)
"Increments a value"
`(1+ ,val))
(defmacro +! (place amount)
`(set! ,place (+ ,place ,amount))
)
(defmacro 1+! (place)
`(+! ,place 1)
)
(defmacro inc! (place)
`(+! ,place 1)
)
(defmacro 1- (var)
`(+ ,var -1)
)
(defmacro dec (val)
"Decrements a value"
`(1- ,val))
(defmacro -! (place amount)
`(set! ,place (- ,place ,amount))
)
(defmacro 1-! (place)
`(-! ,place 1)
)
(defmacro dec! (place)
`(-! ,place 1)
)
(defmacro *! (place amount)
`(set! ,place (* ,place ,amount))
)
(defmacro /! (place amount)
`(set! ,place (/ ,place ,amount))
)
(defmacro zero? (thing)
`(eq? ,thing 0)
)
(defmacro nonzero? (thing)
`(neq? ,thing 0)
)
(defmacro or! (place &rest args)
`(set! ,place (or ,place ,@args))
)
(defmacro not! (var)
`(set! ,var (not ,var)))
(defmacro true! (var)
`(set! ,var #t))
(defmacro false! (var)
`(set! ,var #f))
(defmacro max! (val maxval)
`(set! ,val (max ,val ,maxval)))
(defmacro min! (val minval)
`(set! ,val (min ,val ,minval)))
(defmacro minmax (val minval maxval)
`(max (min ,val ,maxval) ,minval)
)
(defmacro fminmax (val minval maxval)
`(fmax (fmin ,val ,maxval) ,minval)
)
(defmacro minmax! (val minval maxval)
`(set! ,val (max (min ,val ,maxval) ,minval))
)
(defmacro fminmax! (val minval maxval)
`(set! ,val (fmax (fmin ,val ,maxval) ,minval))
)
(defmacro maxmin (val minval maxval)
`(min (max ,val ,maxval) ,minval)
)
(defmacro fmaxmin (val minval maxval)
`(fmin (fmax ,val ,maxval) ,minval)
)
(defmacro &+! (val amount)
`(set! ,val (&+ ,val ,amount))
)
(defmacro &- (a b)
`(- (the-as int ,a) (the-as int ,b))
)
(defmacro &-> (&rest args)
`(& (-> ,@args))
)
(defmacro xor (a b)
"xor for #t and #f"
(with-gensyms (a-temp b-temp)
`(let ((,a-temp ,a) (,b-temp ,b))
(or (and ,a-temp (not ,b-temp)) (and (not ,a-temp) ,b-temp))))
)
(defmacro logior! (place amount)
`(set! ,place (logior ,place ,amount))
)
(defmacro logxor! (place amount)
`(set! ,place (logxor ,place ,amount))
)
(defmacro logand! (place amount)
`(set! ,place (logand ,place ,amount))
)
(defmacro logclear (a b)
"Returns the result of setting the bits in b to zero in a"
;; put a first so the return type matches a.
`(logand ,a (lognot ,b))
)
(defmacro logclear! (a b)
"Sets the bits in b to zero in a, in place"
`(set! ,a (logand ,a (lognot ,b)))
)
(defmacro logtest? (a b)
"does a have any of the bits in b?"
`(nonzero? (logand ,a ,b))
)
(defmacro logtesta? (a b)
"does a have ALL of the bits in b?"
`(= (logand ,b ,a) ,b)
)
(defmacro deref (t addr &rest fields)
`(-> (the-as (pointer ,t) ,addr) ,@fields)
)
(defmacro &deref (t addr &rest fields)
`(&-> (the-as (pointer ,t) ,addr) ,@fields)
)
(defmacro sext32 (in)
`(sar (shl ,in 32) 32)
)
(defmacro shift-arith-right-32 (result in sa)
`(set! ,result (sext32 (sar (logand #xffffffff (the-as int ,in)) ,sa)))
)
(defmacro /-0-guard (a b)
"same as divide but returns -1 when divisor is zero (EE-like, DIVU)."
`(let ((divisor ,b))
(if (zero? divisor)
-1
(/ ,a divisor))
)
)
(defmacro /-signed-0-guard (a b)
"same as divide but handles overflow and divide by zero like the EE's DIV instruction."
`(let ((divisor ,b)
(dividend ,a))
(cond
((and (= dividend -2147483648) (= divisor -1))
;; overflow case
-2147483648)
((zero? divisor)
(if (< dividend 0) 1 -1))
(else
(/ dividend divisor))
)
)
)
(defmacro mod-0-guard (a b)
"same as modulo but returns the dividend when divisor is zero (EE-like, DIVU)."
`(let ((divisor ,b))
(if (zero? divisor)
,a
(mod ,a divisor))
)
)
(defmacro mod-signed-0-guard (a b)
"same as modulo but handles overflow and divide by zero like the EE's DIV instruction."
`(let ((divisor ,b)
(dividend ,a))
(cond
((and (= dividend -2147483648) (= divisor -1))
;; overflow case
0)
((zero? divisor)
dividend)
(else
(mod dividend divisor))
)
)
)
(defmacro float->int (a)
"forcefully casts something as a float to int. be careful."
`(the int (the float ,a))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Bit Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro align-n (val n)
"align val to n-byte boundaries"
`(logand (- ,n) (+ (the-as int ,val) (- ,n 1)))
)
(defmacro align16 (val)
`(align-n ,val 16)
)
(defmacro align64 (val)
`(align-n ,val 64)
)
(defmacro bit-field (type val base size &key (signed #t))
"extract bits from an integer value."
(when (and (integer? base) (integer? size))
(when (> (+ base size) 64)
(error "cannot extract fields across 64-bit boundaries"))
(when (< base 0)
(error "bitfield base cannot be negative"))
(when (< size 0)
(error "bitfield size cannot be negative"))
)
`(,(if signed 'sar 'shr) (shl ,val (- 64 (+ ,size ,base))) (- 64 ,size))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; PAIR STUFF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro cons (a b)
`(new 'global 'pair ,a ,b)
)
(defmacro list (&rest args)
(if (null? args)
(quote '())
`(cons ,(car args) (list ,@(cdr args)))
)
)
(defmacro null? (arg)
;; todo, make this better
`(eq? ,arg '())
)
(defmacro caar (arg)
`(car (car ,arg))
)
(defmacro cadr (arg)
`(car (cdr ,arg))
)
(defmacro cddr (arg)
`(cdr (cdr ,arg))
)
(defmacro caddr (arg)
`(car (cdr (cdr ,arg)))
)
(defmacro cadddr (arg)
`(car (cdr (cdr (cdr ,arg))))
)
(defmacro dcons (a b)
`(new 'debug 'pair ,a ,b)
)
(defmacro cons! (lst val)
`(set! ,lst (cons ,val ,lst))
)
(defmacro swap! (a b)
"macro for swapping 2 variables"
`(let ((temp ,a))
(set! ,a ,b)
(set! ,b temp)
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ARRAYS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro first-arr (coll)
"Returns the first element in an array"
`(-> ,coll 0))
(defmacro last-arr (coll)
"Returns the last element in an array"
`(-> ,coll (dec (length ,coll))))
(defmacro last-idx-arr (coll)
"Returns the index of the last element in an array"
`(dec (length ,coll)))
(defmacro arr-idx-of (coll val def)
"Returns the index of an item in an array, returns <def> if is nothing is found."
`(block find-element
(dotimes (i (length ,coll))
(if (= ,val (-> ,coll i))
(return-from find-element i)))
,def))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; METHOD STUFF
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro object-new (allocation type-to-make &rest sz)
(if (null? sz)
`(the (current-method-type) ((method-of-type object new) ,allocation ,type-to-make (the int (-> ,type-to-make size))))
`(the (current-method-type) ((method-of-type object new) ,allocation ,type-to-make ,@sz))
)