-
Notifications
You must be signed in to change notification settings - Fork 85
/
exportScript.sml
200 lines (179 loc) · 5.54 KB
/
exportScript.sml
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
(*
Some common helper functions for writing the final byte list ->
string exporter.
*)
open preamble mlstringTheory mlvectorTheory mlintTheory;
val _ = new_theory "export";
Definition split16_def:
(split16 f [] = Nil) /\
(split16 f xs =
let xs1 = TAKE 16 xs in
let xs2 = DROP 16 xs in
SmartAppend (f xs1) (split16 f xs2))
Termination
WF_REL_TAC `measure (LENGTH o SND)`
\\ fs [listTheory.LENGTH_DROP]
End
val preamble_tm =
``(MAP (\n. strlit(n ++ "\n"))
["/* Preprocessor to get around Mac OS, Windows, and Linux differences in naming and calling conventions */";
"";
"#if defined(__APPLE__)";
"# define cdecl(s) _##s";
"#else";
"# define cdecl(s) s";
"#endif";
"";
"#if defined(__APPLE__)";
"# define wcdecl(s) _##s";
"#elif defined(__WIN32)";
"# define wcdecl(s) windows_##s";
"#else";
"# define wcdecl(s) s";
"#endif";
"";
"#if defined(__APPLE__)";
"# define wcml(s) s";
"#elif defined(__WIN32)";
"# define wcml(s) windows_##s";
"#else";
"# define wcml(s) s";
"#endif";
"";
"#if defined(__APPLE__)";
".macro _makesym name, base, len";
".set \\name, cake_main+\\base";
".endm";
"# define makesym(name,base,len) _makesym name, base, len";
"#elif defined(__WIN32)";
".macro _makesym name, base, len";
".set \\name, cake_main+\\base";
".endm";
"# define makesym(name,base,len) _makesym name, base, len";
"#else";
".macro _makesym name, base, len";
".local \\name";
".set \\name, cake_main+\\base";
".size \\name, \\len";
".type \\name, function";
".endm";
"# define makesym(name,base,len) _makesym name, base, len";
"#endif";
"";
"#define DATA_BUFFER_SIZE 65536";
"#define CODE_BUFFER_SIZE 5242880";
"";
" .file \"cake.S\"";
""])`` |> EVAL |> rconc;
Definition preamble_def:
preamble = ^preamble_tm
End
Definition data_section_def:
data_section word_directive ret =
MAP (\n. strlit (n ++ "\n"))
([" .data";
" .p2align 3";
"cdecl(cml_heap): " ++ word_directive ++ " 0";
"cdecl(cml_stack): " ++ word_directive ++ " 0";
"cdecl(cml_stackend): " ++ word_directive ++ " 0"] ++
(if ret then
["ret_base: " ++ word_directive ++ " 0";
"ret_stack: " ++ word_directive ++ " 0";
"ret_stackend: " ++ word_directive ++ " 0";
"can_enter: " ++ word_directive ++ " 0"]
else []) ++
[" .p2align 3";
"cake_bitmaps:"])
End
Definition data_buffer_def:
data_buffer =
MAP (\n. strlit (n ++ "\n"))
[" .globl cdecl(cake_bitmaps_buffer_begin)";
"cdecl(cake_bitmaps_buffer_begin):";
"#if defined(EVAL)";
" .space DATA_BUFFER_SIZE";
"#endif";
" .globl cdecl(cake_bitmaps_buffer_end)";
"cdecl(cake_bitmaps_buffer_end):"]
End
Definition code_buffer_def:
code_buffer =
MAP (\n. strlit (n ++ "\n"))
[" .globl cdecl(cake_codebuffer_begin)";
"cdecl(cake_codebuffer_begin):";
"#if defined(EVAL)";
" .space CODE_BUFFER_SIZE";
"#endif";
" .p2align 12";
" .globl cdecl(cake_codebuffer_end)";
"cdecl(cake_codebuffer_end):";
" .space 4096"]
End
Definition comm_strlit_def:
comm_strlit = strlit ","
End
Definition newl_strlit_def:
newl_strlit = strlit "\n"
End
Definition comma_cat_def:
comma_cat f x =
case x of
| [] => [newl_strlit]
| [x] => [f x; newl_strlit]
| (x::xs) => f x :: comm_strlit :: comma_cat f xs
End
Definition words_line_def:
words_line word_directive to_string ls =
List (word_directive :: comma_cat to_string ls)
End
Definition word_to_string_def:
word_to_string w = toString(w2n w)
End
Definition byte_to_string_def:
byte_to_string (b:word8) =
strlit ("0x" ++ [EL (w2n b DIV 16) "0123456789ABCDEF"]
++ [EL (w2n b MOD 16) "0123456789ABCDEF"])
End
Definition all_bytes_def:
all_bytes = Vector (GENLIST (\n. byte_to_string (n2w n)) 256)
End
Theorem all_bytes_eq =
EVAL ``all_bytes``
Theorem byte_to_string_eq:
!b. byte_to_string b = sub all_bytes (w2n b)
Proof
Cases_on `b` \\ once_rewrite_tac [EQ_SYM_EQ]
\\ rewrite_tac [all_bytes_def,mlvectorTheory.sub_def]
\\ full_simp_tac std_ss [w2n_n2w,EVAL ``dimword (:8)``]
\\ full_simp_tac std_ss [listTheory.EL_GENLIST]
QED
(* gas allows 0-9a-zA-Z_$. in labels as long as the first is _A-Za-z *)
(* we prefix labels and reserve $; . is special *)
Definition escape_sym_char_def:
escape_sym_char ch = let code = ORD ch in
if code >= 0x61 /\ code <= 0x7A \/ code >= 0x41 /\ code <= 0x5A \/
code >= 0x30 /\ code <= 0x39 \/ code = 0x5F then str ch else
«$» ^ toString(code) ^ «_»
End
Definition get_sym_label_def:
get_sym_label (ix,appl) (name,start,len) =
let label =
«cml_» ^ concat (MAP escape_sym_char (explode name)) ^
«_» ^ toString(ix) in
(ix + 1, appl ++ [(name,label,start,len)])
End
Definition get_sym_labels_def:
get_sym_labels syms =
SND $ FOLDL get_sym_label (0, []) syms
End
Definition emit_symbol_def:
emit_symbol appl (name,label,start,len) =
misc$Append appl (misc$List
[« makesym(» ^ label ^ «, » ^ toString(start) ^ «, » ^
toString(len) ^ «)\n»])
End
Definition emit_symbols_def:
emit_symbols lsyms =
FOLDL emit_symbol misc$Nil lsyms
End
val _ = export_theory ();