-
Notifications
You must be signed in to change notification settings - Fork 4
/
sig.rkt
146 lines (108 loc) · 4.75 KB
/
sig.rkt
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
#lang racket/base
;; This module exports signatures used to interface all components in
;; the Staapl language.
;; Components in staapl are collections of macros or "op
;; transformers". Macros are Scat functions of type:
;;
;; (stack-of op) -> (stack-of op)
;;
;; The these macros are implemented as scheme functions and thus not
;; to be confused with scheme syntax transformers. In general the
;; compilation stage is shifted one level in Staapl code: target code
;; is always data in the scheme sense.
;;
;; The "op" objects represent the target machine's operations. These
;; objects contain information that lives at macro compile time, they
;; are information embedded in the signature to allow separate
;; compilation of op transformers.
(require racket/unit
"op.rkt"
"ns.rkt")
(provide (all-defined-out))
(define-syntax define-macro-set
(syntax-rules (extends)
((_ name extends sig^ (w ...))
(ns (macro) (define-signature name extends sig^ (w ...))))
((_ name (w ...))
(ns (macro) (define-signature name (w ...))))))
(define-syntax-rule (define-scat-set name (w ...))
(ns (scat) (define-signature name (w ...))))
(define-syntax-rule (define-op-set name (w ...))
(begin
(ns (op) (define-signature op^ (w ...)))
(ns (asm) (define-signature asm^ (w ...)))
(ns (dasm) (define-signature dasm^ (w ...)))
(define-signature name ((open op^) (open asm^) (open dasm^)))))
;; OPCODES
;; These are signatures containing static information about the
;; opcodes. Currently this is only the formal argument list used in
;; arity checking and error reporting.
;; FIXME: because qw is used at the very core of the macro code
;; transformer, scat needs to be re-implemented as a unit before this
;; will work. I'll try to brin the code to a point where some
;; test-xxx.ss modules are operational so the change to scat can be
;; gradually propagated upward.
;; (define-op-signature op-quote-decl^ op-quote-impl^ ;; code/data distinction
;; (cw val)
;; (qw val))
;; (define-op-signature op-jump-decl^ op-quote-impl^ ;; control flow
;; (label l)
;; (jw l)
;; (jw/false l)
;; (exit))
;; MACROS
;; The label^ set defines branch target representation (sym), branch
;; target marking (label:) and branch instructions (cw jw jw/if exit).
;; These behave as RPN assembler macros.
(define-macro-set jump^ (make-label enter: >label cw jw exit reachable))
(define-macro-set cjump^ (jw/false))
(define-macro-set ram^ (allot))
;; Forth style control words on top of the jump representation.
(define-macro-set control^ (m-swap m-dup m> >m word>m if else then begin again
do while repeat until end:))
;; Target code placement.
(define-macro-set org^ (org-begin org-end here))
;; Compiler internal interface.
(define-macro-set cfg^ (menter mleave mexit close-chain
word-org-begin
compile-macro/raw ;; Does not compile 'exit' !
compose-macro
>macro
semi ";" "."))
;; Basic stack manip + ALU
(define-macro-set stack^ (dup swap drop + - * / not))
(define-macro-set code^ (compose i execute compile nop))
(define-macro-set rstack^ (for next >r r> swap>r r- +r rdrop rl rh))
;; Compilation and manipulation of binary lists. Note that `length'
;; and `cons' might need a rename.
(define-macro-set comma-extra^ (dw> for-list for-table sym>bin l:length l:cons))
(define-macro-set comma^ ("," byte-slots))
;; Machine parameters used to parameterize the CFG compiler.
(define-macro-set machine^ (code-size address))
;; PIC18 specific. Move more of these to standard wordsets.
(define-macro-set stack-extra^ (and or xor pow >>> <<< 1+ 1- min max odd?
toggle neg >> << 2/ over nip))
(define-macro-set memory-extra^ (1-! 1+! swap! ! @ high low high? low? +! -!
@! or! and! xor!))
;; COMPILER
(define-macro-set comp-debug^ (.cs))
(define-signature instantiate^
(;; Compiler invokation.
compile-words
;; These functions apply appropriate wrappers before
;; the objects are defined in the namespace.
wrap-variable
wrap-word
wrap-macro
))
;; Code postprocessing state->state function. This function is handed
;; the full compilation state to provide any postprocessing such as
;; optimizations and the elimination of pseudo ops, before the code is
;; handed to the assembler.
(define-signature postproc^ (postproc))
;; Access to the extended compiler state.
(define-signature state-tag^ (state-tag-label state-tag-ref state-tag-set))
;; Shallow Co-routines (SCR)
(define-macro-set scr^ (scr-begin scr-reg scr-yield scr-var))
;; This is the top-level "go" for the compiler.
(define-signature compiler^ (compile!))