-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.fs
123 lines (105 loc) · 3.75 KB
/
compiler.fs
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
\ Generic Target Compiler.
0 [if]
Copyright (C) 2009-2015 by Charles Shattuck.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For LGPL information: http://www.gnu.org/copyleft/lesser.txt
[then]
only forth also definitions
vocabulary targ
nowarn
: target only forth also targ also definitions ; immediate
: ] postpone target ; immediate
: host only targ also forth also definitions ; immediate
: [ postpone host ; immediate
host
warn
: :m postpone target : ;
: m; postpone ; ; immediate
:m words words m;
\ as far as gforth is concerned, the target image is byte addressed
\ even though the AVR memory is word addressed. Branch addresses
\ need to be word addressed, so begin = here 2/ for example
create target-image target-size allot
target-image target-size $ff fill \ ROM erased.
: there ( a1 - a2) target-image + ;
: !-t ( n a - ) there over 8 rshift over 1 + c! c! ;
\ : @-t ( a - n) there count swap c@ 8 lshift + ; \ ??
variable tdp \ Rom pointer.
:m HERE ( - a) tdp @ m;
:m ORG ( a - ) tdp ! m;
:m ALLOT ( n - ) tdp +! m;
:m , ( n - ) HERE !-t 2 ALLOT m;
: ,-t ( n - ) target , m;
: report cr ." HERE=" target HERE host u. cr ;
variable trp \ Ram pointer.
: ramHERE ( - a) trp @ ;
: ramORG ( a - ) trp ! ; 0 ramORG
: ramALLOT ( n - ) trp +! ;
\ ----- Optimization ----- /
variable 'edge
: hide target-size 1 - 'edge ! ; hide
: hint target here host 'edge ! ;
: edge 'edge @ ;
\ ----- Labels ----- /
nowarn
variable labels 0 labels !
warn
: label ( - )
[ labels @ here labels ! , ] HERE host , BL word count here
over char+ allot place align ;
: show ( a - ) 2 cells + count type ;
: label? ( a - 0|a)
>r labels begin @ dup while dup cell+ @ r@ = if
r> drop exit then repeat r> drop ;
nowarn
: (words words ;
: .words labels begin @ dup while dup cell+ @ 2/
base @ >r hex u. r> base ! dup show 2
spaces repeat drop ;
: target-words
labels begin @ dup while dup show space repeat drop ;
warn
create _space 1 c, 32 c,
\ create _crlf 2 c, 13 c, 10 c,
create _crlf 1 c, 10 c,
create _comma 1 c, char , c, \ 32 c, \ modified
\ create _commaf 3 c, 32 c, char , c, 32 c, \ canonical
: (.) ( n - a n) 0 <# #s #> ;
0 value save-fid
: spit ( a n) save-fid write-file abort" write error" ;
: crlf _crlf count spit ;
: spaced _space count spit ;
: save ( - )
0 to save-fid s" ./memory.h" delete-file drop
s" ./memory.h" r/w create-file abort" Error creating memory.h" to save-fid
s" // memory.h" save-fid write-file abort" write error" crlf
crlf
s" const uint16_t memory[] = {" spit crlf
spaced spaced spaced
target-image target-size 2/ 0 do
spaced
dup w@ (.) spit _comma count spit 2 +
i 7 and 0= if crlf spaced spaced spaced then
loop drop
crlf s" };" spit crlf crlf
s" // END." spit crlf
save-fid close-file abort" Error closing memory.h" ;
\ ----- Headers on the target ----- /
variable thp
target-image target-size + 4 - thp !
0 thp @ !
nowarn
: header ( - )
thp @ >r labels @ cell+ dup cell+ dup c@ 3 + dup 1 and + negate thp +!
thp @ over c@ 1 + dup 1 and + move @ 2/ dup 8 rshift r@ 1 - c! r> 2 - c! ;
warn