2021-02-09 20:59:14 -05:00
|
|
|
;;-*-Lisp-*-
|
|
|
|
|
|
|
|
|
2020-09-13 10:40:21 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; OTHER STUFF
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
;; get list of all goal files
|
|
|
|
(asm-file "goal_src/build/all_files.gc")
|
|
|
|
|
|
|
|
;; tell compiler about stuff defined/implemented in the runtime.
|
|
|
|
(asm-file "goal_src/kernel-defs.gc")
|
|
|
|
|
2020-09-12 20:41:12 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; BUILD SYSTEM
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-06 17:42:20 -04:00
|
|
|
(defmacro m (file)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Make: compile a file fully and save the result"
|
2020-09-06 17:42:20 -04:00
|
|
|
`(asm-file ,file :color :write)
|
|
|
|
)
|
|
|
|
|
2021-06-01 16:07:45 -04:00
|
|
|
(defmacro md (file &rest path)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Make Debug: make + print disassembly for a file"
|
2021-06-12 12:55:38 -04:00
|
|
|
(if (null? path)
|
|
|
|
`(asm-file ,file :color :write :disassemble)
|
|
|
|
`(asm-file ,file :color :write :disassemble ,(first path))
|
|
|
|
)
|
2020-12-01 21:39:46 -05:00
|
|
|
)
|
|
|
|
|
2020-09-06 17:42:20 -04:00
|
|
|
(defmacro ml (file)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Make Load: make and load the file through the listener"
|
2020-09-06 17:42:20 -04:00
|
|
|
`(asm-file ,file :color :load :write)
|
2020-09-07 13:28:16 -04:00
|
|
|
)
|
|
|
|
|
2020-09-12 20:41:12 -04:00
|
|
|
(desfun make-build-command (file)
|
|
|
|
`(asm-file ,file :color :write)
|
|
|
|
)
|
|
|
|
|
2020-12-08 21:41:36 -05:00
|
|
|
|
|
|
|
(defmacro build-kernel ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Build kernel and create the KERNEL CGO"
|
2020-12-08 21:41:36 -05:00
|
|
|
`(begin
|
|
|
|
,@(apply make-build-command all-kernel-goal-files)
|
2021-05-09 17:03:58 -04:00
|
|
|
(build-dgos "goal_src/build/kernel_dgos.gc")
|
2020-12-08 21:41:36 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-12 20:41:12 -04:00
|
|
|
(defmacro build-game ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Build all game code and all game CGOs"
|
2020-09-12 20:41:12 -04:00
|
|
|
`(begin
|
2020-12-08 21:41:36 -05:00
|
|
|
(build-kernel)
|
2020-09-12 20:41:12 -04:00
|
|
|
,@(apply make-build-command all-goal-files)
|
2021-05-09 17:03:58 -04:00
|
|
|
(build-dgos "goal_src/build/game_dgos.gc")
|
2020-09-12 20:41:12 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-11-19 21:22:16 -05:00
|
|
|
(defmacro build-data ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Build all game data"
|
2020-11-19 21:22:16 -05:00
|
|
|
`(begin
|
|
|
|
(asm-data-file game-text "assets/game_text.txt")
|
2020-11-24 20:48:38 -05:00
|
|
|
(asm-data-file game-count "assets/game_count.txt")
|
2021-07-02 14:50:58 -04:00
|
|
|
(asm-data-file dir-tpages "assets/tpage-dir.txt")
|
2020-11-19 21:22:16 -05:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-13 17:34:02 -04:00
|
|
|
(defmacro blg ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Build engine and kernel CGOs (code only, no data for now) and load them in the listener
|
|
|
|
Uses the blocking dgo-load."
|
2020-09-13 17:34:02 -04:00
|
|
|
`(begin
|
|
|
|
(build-game)
|
|
|
|
(dgo-load "kernel" global #xf #x200000)
|
2021-06-30 19:20:31 -04:00
|
|
|
(load-package "game" global)
|
2020-09-13 17:34:02 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-06-27 22:23:27 -04:00
|
|
|
(defmacro lg ()
|
|
|
|
"Load an already built game."
|
2021-06-30 19:20:31 -04:00
|
|
|
`(load-package "game" global)
|
2021-06-27 22:23:27 -04:00
|
|
|
)
|
|
|
|
|
2020-10-24 22:51:40 -04:00
|
|
|
(defmacro tc ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Typecheck against the all-types file"
|
2020-10-24 22:51:40 -04:00
|
|
|
`(m "decompiler/config/all-types.gc")
|
|
|
|
)
|
|
|
|
|
2020-09-07 13:28:16 -04:00
|
|
|
(defmacro e ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Exit the compiler"
|
2020-09-07 13:28:16 -04:00
|
|
|
`(:exit)
|
|
|
|
)
|
|
|
|
|
2020-12-08 21:41:36 -05:00
|
|
|
(defmacro dbc ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Put the compiler in debug mode"
|
2020-09-14 20:24:05 -04:00
|
|
|
`(begin
|
|
|
|
(set-config! print-ir #t)
|
|
|
|
(set-config! print-regalloc #t)
|
|
|
|
)
|
|
|
|
)
|
2020-09-13 10:40:21 -04:00
|
|
|
|
2020-09-07 13:28:16 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; CONDITIONAL COMPILATION
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro #when (clause &rest body)
|
|
|
|
`(#cond (,clause ,@body))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro #unless (clause &rest body)
|
|
|
|
`(#cond ((not ,clause) ,@body))
|
|
|
|
)
|
|
|
|
|
2021-11-24 00:44:04 -05:00
|
|
|
(defmacro #if (clause true false)
|
|
|
|
`(#cond (,clause ,true) (#t ,false))
|
|
|
|
)
|
|
|
|
|
2020-09-07 13:28:16 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; TARGET CONTROL
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro lt (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Listen to target. Opens a connection and flushes buffers"
|
2020-09-07 13:28:16 -04:00
|
|
|
`(begin
|
|
|
|
(listen-to-target ,@args)
|
|
|
|
(:status)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro r (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Reset the target state and reconnect."
|
2020-09-07 13:28:16 -04:00
|
|
|
`(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)
|
|
|
|
)
|
2020-09-12 13:11:42 -04:00
|
|
|
)
|
|
|
|
|
2020-09-24 17:19:23 -04:00
|
|
|
(defmacro shutdown-target ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Make the target exit. The runtime itself will exit and not restart automatically."
|
2020-09-24 17:19:23 -04:00
|
|
|
`(begin
|
|
|
|
(reset-target :shutdown)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-11-06 13:59:39 -05:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; DEBUGGER MACROS
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro db (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Print bytes."
|
2020-11-06 13:59:39 -05:00
|
|
|
`(:pm 1 ,@args)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro dh (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Print halfwords (16-bits)"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(:pm 2 ,@args)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro dw (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Print words (32-bits)"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(:pm 4 ,@args)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro dd (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Print doublewords (64-bits)"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(:pm 8 ,@args)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro df (&rest args)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Print floats (32-bit)"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(:pm 4 ,@args :print-mode float)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro segfault ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Dereference the GOAL 0 pointer, which should be a segfault"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(-> (the (pointer int) 0))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro fpe ()
|
2021-03-01 11:15:24 -05:00
|
|
|
"Trigger a SIGFPE by doing integer division by zero"
|
2020-11-06 13:59:39 -05:00
|
|
|
`(/ 0 0)
|
|
|
|
)
|
2020-09-12 13:11:42 -04:00
|
|
|
|
2021-03-14 16:11:42 -04:00
|
|
|
(defmacro break! ()
|
|
|
|
"A breakpoint. Todo - should we use int3 instead?"
|
|
|
|
`(/ 0 0)
|
|
|
|
)
|
|
|
|
|
2021-04-11 16:07:01 -04:00
|
|
|
(defmacro crash! ()
|
|
|
|
"Cause a crash by attempting to deference 0x0"
|
|
|
|
`(-> (the (pointer uint8) 0))
|
|
|
|
)
|
|
|
|
|
2020-09-12 13:11:42 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; GOAL Syntax
|
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Bind vars in body
|
|
|
|
(defmacro let (bindings &rest body)
|
|
|
|
`((lambda :inline-only #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 :inline-only #t (,(caar bindings))
|
|
|
|
(let* ,(cdr bindings) ,@body))
|
|
|
|
,(car (cdar bindings))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
;; Define a new function
|
|
|
|
(defmacro defun (name bindings &rest body)
|
|
|
|
(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 ,bindings ,@(cdr body)))
|
|
|
|
;; otherwise don't ignore it.
|
|
|
|
`(define ,name (lambda :name ,name ,bindings ,@body))
|
|
|
|
)
|
2020-09-13 17:34:02 -04:00
|
|
|
)
|
|
|
|
|
2021-06-25 17:55:50 -04:00
|
|
|
;; 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)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-02-20 11:42:46 -05:00
|
|
|
(defmacro defun-extern (function-name &rest type-info)
|
|
|
|
`(define-extern ,function-name (function ,@type-info))
|
|
|
|
)
|
|
|
|
|
2020-12-05 17:09:46 -05:00
|
|
|
(defmacro defun-debug (name bindings &rest body)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Define a function which is only present in debug mode. Otherwise the function becomes nothing"
|
2020-12-05 17:09:46 -05:00
|
|
|
`(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))
|
|
|
|
)
|
2021-02-09 20:24:33 -05:00
|
|
|
|
2020-12-05 17:09:46 -05:00
|
|
|
;; function not loaded, set function to the nothing function.
|
|
|
|
;; we don't typecheck this.
|
|
|
|
(define :no-typecheck #t ,name nothing)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-03-01 11:15:24 -05:00
|
|
|
(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)
|
|
|
|
(if (or (not ,name) (zero? ,name))
|
|
|
|
(set! ,name ,value)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-13 17:34:02 -04:00
|
|
|
(defmacro while (test &rest body)
|
2021-03-01 11:15:24 -05:00
|
|
|
"While loop. The test is evaluated before body."
|
2020-09-13 17:34:02 -04:00
|
|
|
(with-gensyms (reloop test-exit)
|
|
|
|
`(begin
|
|
|
|
(goto ,test-exit)
|
|
|
|
(label ,reloop)
|
|
|
|
,@body
|
|
|
|
(label ,test-exit)
|
|
|
|
(when-goto ,test ,reloop)
|
|
|
|
#f
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-19 13:22:14 -04:00
|
|
|
(defmacro until (test &rest body)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Until loop. The body is evaluated before the test."
|
2020-09-19 13:22:14 -04:00
|
|
|
(with-gensyms (reloop)
|
|
|
|
`(begin
|
|
|
|
(label ,reloop)
|
|
|
|
,@body
|
|
|
|
(when-goto (not ,test) ,reloop)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro dotimes (var &rest body)
|
2021-07-23 18:30:49 -04:00
|
|
|
"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."
|
2020-09-19 13:22:14 -04:00
|
|
|
`(let (( ,(first var) 0))
|
|
|
|
(while (< ,(first var) ,(second var))
|
|
|
|
,@body
|
2021-04-22 18:31:54 -04:00
|
|
|
(1+! ,(first var))
|
2020-09-19 13:22:14 -04:00
|
|
|
)
|
|
|
|
,@(cddr var)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-05 17:09:46 -05:00
|
|
|
(defmacro countdown (var &rest body)
|
2021-03-01 11:15:24 -05:00
|
|
|
"Loop like for (int i = end; i-- > 0)"
|
2020-12-05 17:09:46 -05:00
|
|
|
`(let ((,(first var) ,(second var)))
|
|
|
|
(while (!= ,(first var) 0)
|
|
|
|
(set! ,(first var) (- ,(first var) 1))
|
|
|
|
,@body
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-07-25 00:23:30 -04:00
|
|
|
(defmacro loop (&rest body)
|
|
|
|
"Loop this code forever."
|
|
|
|
`(while #t
|
|
|
|
,@body)
|
|
|
|
)
|
|
|
|
|
2021-11-30 19:14:43 -05:00
|
|
|
(defmacro doarray (bindings &rest body)
|
|
|
|
"iterate over an array. usage: (doarray (<array entry name> <array>) <code>)"
|
|
|
|
|
|
|
|
(with-gensyms (len i)
|
|
|
|
(let ((val (first bindings))
|
|
|
|
(arr (second bindings)))
|
|
|
|
|
|
|
|
`(let* ((,len (-> ,arr length))
|
|
|
|
(,i 0)
|
|
|
|
(,val (-> ,arr ,i)))
|
|
|
|
(while (< ,i ,len)
|
|
|
|
,@body
|
|
|
|
(1+! ,i)
|
|
|
|
(set! ,val (-> ,arr ,i))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-13 17:34:02 -04:00
|
|
|
;; Backup some values, and restore after executing body.
|
|
|
|
;; Non-dynamic (nonlocal jumps out of body will skip restore)
|
2021-06-29 20:30:52 -04:00
|
|
|
;; NOTE : GOAL protected defs in a FIFO manner (this is FILO/LIFO), this should be fixed at some point
|
2020-09-13 17:34:02 -04:00
|
|
|
(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)
|
2020-09-25 21:11:27 -04:00
|
|
|
(if (> (length others) 1)
|
|
|
|
(error "got too many arguments to if")
|
|
|
|
#f
|
|
|
|
)
|
2020-09-13 17:34:02 -04:00
|
|
|
(if (null? others)
|
|
|
|
`(cond (,condition ,true-case))
|
|
|
|
`(cond (,condition ,true-case)
|
|
|
|
(else ,(first others))
|
|
|
|
)
|
|
|
|
)
|
2020-09-18 22:02:27 -04:00
|
|
|
)
|
|
|
|
|
2020-09-19 16:50:42 -04:00
|
|
|
(defmacro when (condition &rest body)
|
|
|
|
`(if ,condition
|
|
|
|
(begin ,@body)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro unless (condition &rest body)
|
|
|
|
`(if (not ,condition)
|
|
|
|
(begin ,@body)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-06-12 10:48:38 -04:00
|
|
|
(defmacro swhen (condition &rest body)
|
|
|
|
"Same as when, but saves the branch condition onto a variable named bc"
|
|
|
|
|
|
|
|
`(let ((bc ,condition))
|
|
|
|
(if bc
|
|
|
|
(begin ,@body)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-02-07 18:21:00 -05:00
|
|
|
(defmacro return (val)
|
|
|
|
`(return-from #f ,val)
|
|
|
|
)
|
2020-09-19 13:22:14 -04:00
|
|
|
|
|
|
|
|
2021-03-17 19:26:35 -04:00
|
|
|
(defmacro empty ()
|
|
|
|
"The decompiler may use (empty) as the body of a loop with nothing in it."
|
|
|
|
`(none)
|
|
|
|
)
|
|
|
|
|
2021-04-16 21:50:38 -04:00
|
|
|
(defmacro case (switch &rest cases)
|
|
|
|
"A switch-case construct. switch is saved onto a local variable and compared against each case, sequentially.
|
2021-06-04 13:22:50 -04:00
|
|
|
else can be used like the 'default' case, but it must be the last one."
|
2021-04-16 21:50:38 -04:00
|
|
|
|
|
|
|
(with-gensyms (sw)
|
2021-05-25 16:36:36 -04:00
|
|
|
;; save the switch to a variable (only evaluated once)
|
2021-04-16 21:50:38 -04:00
|
|
|
`(let ((sw ,switch))
|
2021-05-25 16:36:36 -04:00
|
|
|
;; build the cond construct with each case
|
2021-04-16 21:50:38 -04:00
|
|
|
(cond ,@(apply
|
2021-05-25 16:36:36 -04:00
|
|
|
(lambda (x) `(
|
|
|
|
;; each case is of format ((cond cond cond...) body)
|
2021-06-01 16:07:45 -04:00
|
|
|
,@(let ((conditions (first x)) ;; list of conditions, OR just else
|
2021-05-25 16:36:36 -04:00
|
|
|
(body (rest x))) ;; the body
|
|
|
|
|
|
|
|
(cond
|
2021-06-01 16:07:45 -04:00
|
|
|
;; if the condition is just 'else'
|
|
|
|
( (eq? conditions 'else)
|
2021-05-25 16:36:36 -04:00
|
|
|
`(else ,@body)
|
|
|
|
)
|
2021-06-01 16:07:45 -04:00
|
|
|
;; if the list is made up of a single condition
|
|
|
|
( (= (length conditions) 1)
|
|
|
|
`((= sw ,(first conditions)) ,@body)
|
2021-05-25 16:36:36 -04:00
|
|
|
)
|
2021-06-01 16:07:45 -04:00
|
|
|
;; otherwise it is made up of multiple conditions, or them together!
|
2021-05-25 16:36:36 -04:00
|
|
|
(#t
|
2021-06-01 16:07:45 -04:00
|
|
|
`((or ,@(apply (lambda (c) `(= sw ,c)) conditions)) ,@body)
|
2021-05-25 16:36:36 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2021-04-16 21:50:38 -04:00
|
|
|
cases)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-09-18 22:02:27 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Math Macros
|
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2021-04-22 18:31:54 -04:00
|
|
|
(defmacro 1+ (var)
|
2020-09-18 22:02:27 -04:00
|
|
|
`(+ ,var 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro +! (place amount)
|
|
|
|
`(set! ,place (+ ,place ,amount))
|
|
|
|
)
|
|
|
|
|
2021-04-22 18:31:54 -04:00
|
|
|
(defmacro 1+! (place)
|
2020-09-18 22:02:27 -04:00
|
|
|
`(set! ,place (+ 1 ,place))
|
|
|
|
)
|
|
|
|
|
2021-06-04 13:22:50 -04:00
|
|
|
(defmacro 1- (var)
|
|
|
|
`(- ,var 1)
|
|
|
|
)
|
|
|
|
|
2020-09-18 22:02:27 -04:00
|
|
|
(defmacro -! (place amount)
|
|
|
|
`(set! ,place (- ,place ,amount))
|
|
|
|
)
|
|
|
|
|
2021-06-04 13:22:50 -04:00
|
|
|
(defmacro 1-! (place)
|
|
|
|
`(set! ,place (- 1 ,place))
|
2020-09-18 22:02:27 -04:00
|
|
|
)
|
|
|
|
|
2021-06-04 13:22:50 -04:00
|
|
|
(defmacro *! (place amount)
|
|
|
|
`(set! ,place (* ,place ,amount))
|
2020-09-19 13:22:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro zero? (thing)
|
|
|
|
`(eq? ,thing 0)
|
|
|
|
)
|
|
|
|
|
2020-12-05 17:09:46 -05:00
|
|
|
(defmacro nonzero? (thing)
|
|
|
|
`(neq? ,thing 0)
|
|
|
|
)
|
|
|
|
|
2021-09-06 20:35:03 -04:00
|
|
|
(defmacro not! (var)
|
|
|
|
`(set! ,var (not ,var)))
|
|
|
|
(defmacro true! (var)
|
|
|
|
`(set! ,var #t))
|
|
|
|
(defmacro false! (var)
|
|
|
|
`(set! ,var #f))
|
|
|
|
|
2021-08-01 17:11:32 -04:00
|
|
|
(defmacro minmax (val minval maxval)
|
|
|
|
`(max (min ,val ,maxval) ,minval)
|
|
|
|
)
|
|
|
|
(defmacro fminmax (val minval maxval)
|
|
|
|
`(fmax (fmin ,val ,maxval) ,minval)
|
|
|
|
)
|
|
|
|
|
2020-09-25 21:11:27 -04:00
|
|
|
(defmacro &+! (val amount)
|
|
|
|
`(set! ,val (&+ ,val ,amount))
|
2020-09-19 13:22:14 -04:00
|
|
|
)
|
|
|
|
|
2020-09-25 21:11:27 -04:00
|
|
|
(defmacro &- (a b)
|
2021-03-14 16:11:42 -04:00
|
|
|
`(- (the-as int ,a) (the-as int ,b))
|
2020-09-19 16:50:42 -04:00
|
|
|
)
|
|
|
|
|
2020-09-25 21:11:27 -04:00
|
|
|
(defmacro &-> (&rest args)
|
|
|
|
`(& (-> ,@args))
|
2020-09-19 16:50:42 -04:00
|
|
|
)
|
|
|
|
|
2021-02-09 20:24:33 -05:00
|
|
|
(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))
|
|
|
|
)
|
|
|
|
|
2021-07-23 18:30:49 -04:00
|
|
|
(defmacro logclear (a b)
|
|
|
|
"Returns the result of setting the bits in b to zero in a"
|
2021-07-23 20:51:26 -04:00
|
|
|
;; put a first so the return type matches a.
|
|
|
|
`(logand ,a (lognot ,b))
|
2021-07-23 18:30:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro logclear! (a b)
|
|
|
|
"Sets the bits in b to zero in a, in place"
|
2021-07-23 20:51:26 -04:00
|
|
|
`(set! ,a (logand ,a (lognot ,b)))
|
2021-07-23 18:30:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
(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?"
|
2021-08-09 19:18:53 -04:00
|
|
|
`(= (logand ,b ,a) ,b)
|
2021-07-23 18:30:49 -04:00
|
|
|
)
|
|
|
|
|
2021-05-25 16:36:36 -04:00
|
|
|
(defmacro deref (t addr &rest fields)
|
|
|
|
`(-> (the-as (pointer ,t) ,addr) ,@fields)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro &deref (t addr &rest fields)
|
|
|
|
`(&-> (the-as (pointer ,t) ,addr) ,@fields)
|
|
|
|
)
|
|
|
|
|
2020-09-25 21:11:27 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Bit Macros
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2021-08-01 17:11:32 -04:00
|
|
|
(defmacro align-n (val n)
|
|
|
|
"align val to n-byte boundaries"
|
|
|
|
`(logand ,(- n) (+ (the-as int ,val) ,(- n 1)))
|
2020-09-19 16:50:42 -04:00
|
|
|
)
|
|
|
|
|
2021-08-01 17:11:32 -04:00
|
|
|
(defmacro align16 (val)
|
|
|
|
`(align-n ,val 16)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro align64 (val)
|
|
|
|
`(align-n ,val 64)
|
2020-11-22 12:59:55 -05:00
|
|
|
)
|
2020-09-19 16:50:42 -04:00
|
|
|
|
2020-09-19 13:22:14 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; TYPE STUFF
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro basic? (obj)
|
|
|
|
;; todo, make this more efficient
|
|
|
|
`(= 4 (logand (the integer ,obj) #b111))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro pair? (obj)
|
|
|
|
;; todo, make this more efficient
|
2021-02-11 09:49:15 -05:00
|
|
|
;`(= 2 (logand (the integer ,obj) #b111))
|
|
|
|
`(< (shl (the-as int ,obj) 62) 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro not-pair? (obj)
|
|
|
|
`(>= (shl (the-as int ,obj) 62) 0)
|
2020-09-19 13:22:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro binteger? (obj)
|
|
|
|
`(zero? (logand (the integer ,obj) #b111))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro rtype-of (obj)
|
|
|
|
`(cond ((binteger? ,obj) binteger)
|
|
|
|
((pair? ,obj) pair)
|
|
|
|
(else (-> (the basic ,obj) type))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; 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)))
|
|
|
|
)
|
2020-09-19 16:50:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro null? (arg)
|
|
|
|
;; todo, make this better
|
|
|
|
`(eq? ,arg '())
|
|
|
|
)
|
2021-03-01 11:15:24 -05:00
|
|
|
|
2020-09-19 16:50:42 -04:00
|
|
|
(defmacro caar (arg)
|
|
|
|
`(car (car ,arg))
|
|
|
|
)
|
|
|
|
|
2021-08-01 17:11:32 -04:00
|
|
|
(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))))
|
|
|
|
)
|
|
|
|
|
2021-12-09 18:39:40 -05:00
|
|
|
(defmacro dcons (a b)
|
|
|
|
`(new 'debug 'pair ,a ,b)
|
|
|
|
)
|
|
|
|
|
2020-09-19 16:50:42 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; METHOD STUFF
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
|
2021-02-09 20:59:14 -05:00
|
|
|
(defmacro object-new (allocation type-to-make &rest sz)
|
2020-09-19 16:50:42 -04:00
|
|
|
(if (null? sz)
|
2021-02-09 20:59:14 -05:00
|
|
|
`(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))
|
2020-09-19 16:50:42 -04:00
|
|
|
)
|
|
|
|
)
|
2020-09-24 17:19:23 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; TEST STUFF
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro expect-eq (a b &key (name "unknown"))
|
|
|
|
`(if (!= ,a ,b)
|
|
|
|
(format #t "Test Failed On Test ~D: ~A~%" *test-count* ,name)
|
|
|
|
(+! *test-count* 1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro expect-true (a)
|
|
|
|
`(expect-eq ,a #t)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro expect-false (a)
|
|
|
|
`(expect-eq ,a #f)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro start-test (test-name)
|
|
|
|
`(begin
|
|
|
|
(define *test-name* ,test-name)
|
|
|
|
(define *test-count* 0)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro finish-test ()
|
|
|
|
`(format #t "Test ~A: ~D Passes~%" *test-name* *test-count*)
|
2020-12-04 12:57:10 -05:00
|
|
|
)
|
2021-02-01 20:41:37 -05:00
|
|
|
|
|
|
|
|
2021-02-22 01:02:12 -05:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; (Fake) MIPS Macros
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2021-04-16 21:50:38 -04:00
|
|
|
;; these are macros for MIPS instructions which we may want to keep in the source code for
|
2021-02-22 01:02:12 -05:00
|
|
|
;; readibility/curiosity/documentation, but will not translate into any actual instructions at all
|
|
|
|
|
|
|
|
;; A macro that generates a macro for the specified instruction
|
|
|
|
(defmacro fake-asm (asm-name &rest args)
|
|
|
|
`(defmacro ,asm-name (,@args) `(none))
|
|
|
|
)
|
2021-07-15 21:37:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Build System
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro makeo (object-name &rest flags)
|
|
|
|
"Make the given object. Can use a string, or not."
|
|
|
|
`(make ,(string-append
|
|
|
|
"out/obj/"
|
|
|
|
(if (string? object-name) object-name (symbol->string object-name))
|
|
|
|
".o"
|
|
|
|
)
|
|
|
|
,@flags)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro make-cgo (file)
|
|
|
|
`(make ,(string-append "out/iso/" file ".CGO"))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro make-dgo (file)
|
|
|
|
`(make ,(string-append "out/iso/" file ".DGO"))
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro make-group (name &key (verbose #f) &key (force #f))
|
|
|
|
`(make ,(string-append "GROUP:" name) :verbose ,verbose :force ,force)
|
|
|
|
)
|
|
|
|
|
2021-08-04 21:30:08 -04:00
|
|
|
(defmacro rl ()
|
|
|
|
`(begin
|
|
|
|
(make-group "iso")
|
|
|
|
(lg)
|
|
|
|
(dbg)
|
|
|
|
)
|
|
|
|
)
|
2021-08-31 11:05:03 -04:00
|
|
|
|
|
|
|
(defmacro mi ()
|
|
|
|
"Make ISO"
|
|
|
|
`(make-group "iso")
|
|
|
|
)
|
2021-11-23 18:25:57 -05:00
|
|
|
|
|
|
|
(defmacro mh1 ()
|
|
|
|
"Make hub1"
|
|
|
|
`(make-group "hub1")
|
|
|
|
)
|
2021-08-09 19:18:53 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; enum stuff
|
|
|
|
;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
(defmacro enum->string (enum input)
|
|
|
|
"return the name of an enum value"
|
|
|
|
|
|
|
|
`(case ,input
|
|
|
|
,@(apply (lambda (x) `(((,enum ,(car x) )) ,(symbol->string (car x) ) )) (reverse (get-enum-vals enum)))
|
|
|
|
(else "*unknown*")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
(defmacro bit-enum->string (enum input stream)
|
|
|
|
"print the enum bits in input to stream"
|
|
|
|
|
|
|
|
(with-gensyms (val str)
|
|
|
|
`(let ((,val ,input)
|
|
|
|
(,str ,stream))
|
2021-11-30 19:14:43 -05:00
|
|
|
|
2021-08-09 19:18:53 -04:00
|
|
|
,@(apply (lambda (x)
|
|
|
|
`(if (logtesta? ,val (,enum ,(car x)))
|
|
|
|
(format ,str ,(fmt #f "{} " (car x)))
|
|
|
|
)
|
2021-11-30 19:14:43 -05:00
|
|
|
|
2021-08-09 19:18:53 -04:00
|
|
|
) (reverse (get-enum-vals enum)))
|
2021-11-30 19:14:43 -05:00
|
|
|
|
2021-08-09 19:18:53 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-08-04 21:30:08 -04:00
|
|
|
|
2021-07-15 21:37:15 -04:00
|
|
|
;; load the default project
|
|
|
|
(load-project "goal_src/game.gp")
|