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
|
/##
Copyright 2021 Kyle Gunger
This file is licensed under the CDDL 1.0 (the License)
and may only be used in accordance with the License.
You should have received a copy of the License with this
software/source code. If you did not, a copy can be found
at the following URL:
https://opensource.org/licenses/CDDL-1.0
THIS SOFTWARE/SOURCE CODE IS PROVIDED "AS IS" WITH NO
WARRANTY, GUARANTEE, OR CLAIM OF FITNESS FOR ANY PURPOSE
EXPRESS OR IMPLIED
#/
/; module compile
# :include "compile/arch/arch.tnsl"
:include "compile/isa_x86.tnsl"
# :include "compile/comp.tnsl"
;/
/; compile_block (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec)
;cur`++
/; loop (cur` < len data`) [cur`++]
/; if (string_equate(data`{cur`}.data`, ";/") || string_equate(data`{cur`}.data`, ";;"))
;break
;; else if (string_equate(data`{cur`}.data`, "/;"))
;bool ch = true
/; loop (ch)
;compile_block(cur, data, hsec, csec, dsec)
/; if (cur` !< len data`)
;break
;/
;ch = string_equate(data`{cur`}.data`, ";;")
;/
;; else if (string_equate(data`{cur`}.data`, ";"))
;compile_statement(cur, data, hsec, csec, dsec)
;/
;/
;/
/; compile_statement (~int cur, ~{}Token data, ~{}charp hsec, csec, dsec)
;cur`++
/; if (cur` < len data`)
/; if (string_equate(data`{cur`}.data`, "asm"))
;{}charp raw_asm = unquote_string(data`{cur` + 1}.data`)
;add_strings(csec, ~raw_asm)
;/
;/
;/
/; do_compile ({}charp file, ~{}Token data)
;{}charp hsec = ".global _start\n"
;{}charp csec = ".text\n"
;{}charp dsec = ".data\n"
;int j = len data`
/; loop (int i = 0; i < j) [i++]
/; if (string_equate(data`{i}.data`, "/;"))
;compile_block(~i, data, ~hsec, ~csec, ~dsec)
;; else if (string_equate(data`{i}.data`, ";"))
;compile_statement(~i, data, ~hsec, ~csec, ~dsec)
;; else
;break
;/
;/
;tnsl.io.File out = tnsl.io.writeFile(file)
/; loop (int i = 0; i < len hsec) [i++]
;out.write(hsec{i})
;/
;out.write('\n')
/; loop (int i = 0; i < len csec) [i++]
;out.write(csec{i})
;/
;out.write('\n')
/; loop (int i = 0; i < len dsec) [i++]
;out.write(dsec{i})
;/
;out.write('\n')
;/
;struct Symtab {
{}{}charp
sym_names,
{}uint
sym_sizes
}
|