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
201
202
203
204
205
206
use memory::unit::Unit;
use value::Value;
use ::{read, read_or_err, vector};
pub mod structure;
pub mod compile;
pub mod assemble;
pub mod var;
pub mod func;
use handle::Handle;
#[link(wasm_import_module = "env")]
extern {
fn wasm_compile_init(byte_address: u32, byte_count: u32, mem_base: u32, tab_base: u32);
fn post_output(byte_address: u32, byte_count: u32);
fn post_error(byte_address: u32, byte_count: u32);
}
pub struct Statics {
pub sym_do: Value,
pub sym_if: Value,
pub sym_fn: Value,
pub sym_def: Value,
pub sym_let: Value,
pub sym_loop: Value,
pub sym_recur: Value,
pub local_use: Value,
pub forms_using: Value,
pub refers_to: Value,
pub resolved_from: Value,
pub arity_bitmap: Value,
pub arity_to_idx: Value,
pub vararg: Value,
pub captures: Value,
pub sym_value: Value,
pub sym_value_ref: Value,
pub key_name: Value,
pub key_args: Value,
pub key_ret: Value,
pub key_mapped: Value,
pub key_alias: Value,
pub sym_fress: Value,
pub sym_amp: Value,
}
use std::cell::Cell;
thread_local! {
pub static STATICS: Cell<usize> = Cell::new(0);
}
pub fn load_statics() {
let x = Box::new(Statics {
sym_do: read("do"),
sym_if: read("if"),
sym_fn: read("fn"),
sym_def: read("def"),
sym_let: read("let"),
sym_loop: read("loop"),
sym_recur: read("recur"),
local_use: read(":local-use"),
forms_using: read(":forms-using"),
refers_to: read(":refers-to"),
resolved_from: read(":resolved-from"),
arity_bitmap: read(":arity-bitmap"),
arity_to_idx: read(":arity-to-idx"),
vararg: read(":vararg"),
captures: read(":captures"),
sym_value: read("value"),
sym_value_ref: read("&value"),
key_name: read(":name"),
key_args: read(":args"),
key_ret: read(":ret"),
key_mapped: read(":mapped"),
key_alias: read(":alias"),
sym_fress: read("fress"),
sym_amp: read("&"),
});
let y = Box::into_raw(x) as usize;
STATICS.with(|c| c.set(y));
}
pub fn get_statics() -> &'static Statics {
let u = STATICS.with(|c| c.get());
unsafe { &*(u as *const Statics) }
}
#[no_mangle]
pub extern fn initialize_global_state() {
use std::panic;
panic::set_hook(Box::new(|msg| {
let s = format!("{}", msg);
::trace::panic_error(&s);
}));
init();
}
pub fn init() {
load_statics();
compile::init_primitives();
var::init();
}
#[no_mangle]
pub extern fn read_eval_print(byte_address: u32, byte_count: u32) {
group!("Read-eval-print routine!");
let m = _read_structure_compile_assemble(byte_address, byte_count);
unsafe {
wasm_compile_init(m.as_ptr() as usize as u32, m.len() as u32,
0, 0)
}
group_end!();
}
pub fn _read_structure_compile_assemble(byte_address: u32, byte_count: u32) -> Vec<u8> {
group!("Reading");
let v = {
use std::slice;
let bytes = unsafe {
slice::from_raw_parts(byte_address as usize as *const u8,
byte_count as usize)
};
use std::str;
let s = str::from_utf8(bytes).unwrap();
let res = read_or_err(s);
match res {
Ok(v) => v,
Err(msg) => {
unsafe {
post_error(msg.as_ptr() as u32, msg.len() as u32)
}
error!("{}", msg);
unimplemented!();
}
}
};
group_end!();
group!("Structuring");
let structured = {
let res = structure::structure(&v);
match res {
Ok(r) => r,
Err(msg) => {
unsafe {
post_error(msg.as_ptr() as u32, msg.len() as u32)
}
error!("{}", msg);
unimplemented!();
}
}
};
group_end!();
group!("Compiling");
let ctx = compile::compile_top_level(&structured);
group_end!();
compile::show_context(&ctx);
group!("Assembling");
let module = assemble::wasm_module(&ctx);
group_end!();
module
}