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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// STARK, a system for computer augmented design.
// Copyright (C) 2021 Matthew Rothlisberger

// STARK is licensed under the terms of the GNU Affero General Public
// License. See the top level LICENSE file for the license text.

// Find full copyright information in the top level COPYRIGHT file.

// <>

// src/sail/stdenv.rs

// Items, particularly native procedures, which are part of the
// standard Sail environment and should be automatically loaded.

// <>

use super::{core::*, memmgt};

/// Generates a slice of native Sail function pointers along with
/// names and argument counts
///
/// This may be a constant or may be local to a Rust scope. The syntax
/// used is quite similar to that of regular functions but eases
/// access to arguments in the body. All native functions must return
/// a valid Sail object.
///
/// TODO: type checks and variable length arglists for native functions
/// TODO: generate these functions somehow else if macros won't cut it
#[macro_export]
macro_rules! sail_fn {
    ( const $array:ident; $reg:ident $tbl:ident $env:ident;
      $( $name:literal $argct:literal [ $($args:ident),* ] $body:block )+
    ) => {
        pub const $array: &[(&str, crate::sail::core::NativeFn, u16)] =
            &[$(($name, |
                _reg: *mut crate::sail::memmgt::Region,
                _tbl: *mut crate::sail::SlHead,
                _env: *mut crate::sail::SlHead,
                _args: &[*mut crate::sail::SlHead]
                | {
                    let $reg = _reg;
                    let $tbl = _tbl;
                    let $env = _env;

                    let mut _ind = 0;
                    $(
                        let $args = _args[_ind];
                        _ind += 1;
                    )*

                        $body
                },
                $argct)),+];
    };

    ( let $array:ident; $reg:ident $tbl:ident $env:ident;
      $( $name:literal $argct:literal [ $($args:ident),* ] $body:block )+
    ) => {
        let $array: &[(&str, crate::sail::core::NativeFn, u16)] =
            &[$(($name, |
                _reg: *mut crate::sail::memmgt::Region,
                _tbl: *mut crate::sail::SlHead,
                _env: *mut crate::sail::SlHead,
                _args: &[*mut crate::sail::SlHead]
                | {
                    let $reg = _reg;
                    let $tbl = _tbl;
                    let $env = _env;

                    let mut _ind = 0;
                    $(
                        let $args = _args[_ind];
                        _ind += 1;
                    )*

                        $body
                },
                $argct)),+];
    };
}

// TODO: native functions MUST be fully safe to use
// TODO: sensible type checking & operator overloading
sail_fn! {
    const ENVFNS;
    _reg _tbl _env;

    // TODO: use fixed point at times to avoid floating point errors?

    "+" 2 [fst, snd] {
        let typ = core_type(fst);
        assert_eq!(typ, core_type(snd));

        match typ.expect("type invalid") {
            CoreType::I64 => {
                let out = i64_make(_reg);
                let result = i64_get(fst) + i64_get(snd);
                i64_set(out, result);
                return out;
            }
            CoreType::F32 => {
                let out = f32_make(_reg);
                let result = f32_get(fst) + f32_get(snd);
                f32_set(out, result);
                return out;
            }
            _ => panic!("type invalid for add"),
        }
    }

    "-" 2 [fst, snd] {
        let typ = core_type(fst);
        assert_eq!(typ, core_type(snd));

        match typ.expect("type invalid") {
            CoreType::I64 => {
                let out = i64_make(_reg);
                let result = i64_get(fst) - i64_get(snd);
                i64_set(out, result);
                return out;
            }
            CoreType::F32 => {
                let out = f32_make(_reg);
                let result = f32_get(fst) - f32_get(snd);
                f32_set(out, result);
                return out;
            }
            _ => panic!("type invalid for sub"),
        }
    }

    "*" 2 [fst, snd] {
        let typ = core_type(fst);
        assert_eq!(typ, core_type(snd));

        match typ.expect("type invalid") {
            CoreType::I64 => {
                let out = i64_make(_reg);
                let result = i64_get(fst) * i64_get(snd);
                i64_set(out, result);
                return out;
            }
            CoreType::F32 => {
                let out = f32_make(_reg);
                let result = f32_get(fst) * f32_get(snd);
                f32_set(out, result);
                return out;
            }
            _ => panic!("type invalid for mul"),
        }
    }

    "/" 2 [fst, snd] {
        let typ = core_type(fst);
        assert_eq!(typ, core_type(snd));

        match typ.expect("type invalid") {
            CoreType::I64 => {
                let out = i64_make(_reg);
                let result = i64_get(fst) / i64_get(snd);
                i64_set(out, result);
                return out;
            }
            CoreType::F32 => {
                let out = f32_make(_reg);
                let result = f32_get(fst) / f32_get(snd);
                f32_set(out, result);
                return out;
            }
            _ => panic!("type invalid for div"),
        }
    }

    "mod" 2 [fst, snd] {
        let out = i64_make(_reg);
        let result = i64_get(fst) % i64_get(snd);
        i64_set(out, result);
        return out;
    }

    "neg" 1 [val] {
        match core_type(val).expect("type invalid") {
            CoreType::I64 => {
                return i64_init(_reg, -i64_get(val));
            }
            CoreType::F32 => {
                return f32_init(_reg, -f32_get(val));
            }
            _ => panic!("type invalid for div"),
        }
    }

    "=" 2 [fst, snd] {
        // let out = init_bool(reg);
        let result = i64_get(fst) == i64_get(snd);
        if result {
            env_lookup_by_id(_env, super::S_T_INTERN.0)
        } else {
            nil()
        }
        // bool_set(out, result);
        // return out;
    }

    "eq" 2 [fst, snd] {
        // let out = init_bool(reg);
        let result = core_eq(fst, snd);
        if result {
            env_lookup_by_id(_env, super::S_T_INTERN.0)
        } else {
            nil()
        }
        // bool_set(out, result);
        // return out;
    }

    "not" 1 [val] {
        // let out = init_bool(reg);
        if !truthy(val) {
            // bool_set(out, false)
            env_lookup_by_id(_env, super::S_T_INTERN.0)
        } else {
            // bool_set(out, true)
            nil()
        }
        // return out;
    }

    "qtx" 2 [sender, item] {
        super::queue::queue_tx(sender, item);

        // let out = init_bool(reg);
        // bool_set(out, true);
        // return out;

        return nil();
    }

    "qrx" 2 [receiver] {
        return super::queue::queue_rx(receiver);
    }

    "as-f32" 1 [val] {
        return f32_init(_reg, f64_get(val) as f32);
    }

    "arr-vec-make" 3 [typ, len, init] {
        coretypck!(typ ; Symbol);
        coretypck!(len ; I64);

        let typ = sym_get_id(typ);
        let len = i64_get(len) as u32;

        assert!(temp_base_sized_p(typ));
        assert_eq!(typ, super::get_self_type(init));

        unsafe {
            let size = vec_size(8, temp_get_size(typ), len as usize);
            let ptr = memmgt::alloc(_reg, size, Cfg::VecArr as u8);

            write_field_unchecked::<u32>(ptr, 0, typ);
            write_field_unchecked::<u32>(ptr, 4, len);

            for i in 0..len {
                std::ptr::copy_nonoverlapping(
                    value_ptr(init),
                    value_ptr(ptr).add(8 + (temp_get_size(typ) * i as usize)),
                    temp_get_size(typ),
                )
            }

            return ptr;
        }

    }

    "arr-vec-get" 2 [target, idx] {
        coretypck!(target ; VecArr);
        coretypck!(idx ; I64);
        let typ = super::arrvec_get_typ(target);
        assert!(temp_base_sized_p(typ));

        let idx = i64_get(idx) as u32;
        assert!(idx < super::arrvec_get_len(target));

        return temp_init_from(_reg, typ, unsafe {
            value_ptr(target).add(8 + (temp_get_size(typ) * idx as usize))
        });
    }

    "arr-vec-set" 3 [target, idx, val] {
        coretypck!(target ; VecArr);
        coretypck!(idx ; I64);
        let typ = super::arrvec_get_typ(target);
        assert!(temp_base_sized_p(typ));
        assert_eq!(typ, super::get_self_type(val));

        let idx = i64_get(idx) as u32;
        assert!(idx < super::arrvec_get_len(target));

        unsafe {
            std::ptr::copy_nonoverlapping(
                value_ptr(val),
                value_ptr(target).add(8 + (temp_get_size(typ) * idx as usize)),
                temp_get_size(typ),
            )
        }

        return target;
    }

    "print" 1 [arg] {
        println!("{}", super::context(_tbl, arg).to_string());
        return nil();
    }

    "dbg" 1 [arg] {
        println!("{}", super::context(_tbl, arg).to_string());
        return arg;
    }

    "printenv" 0 [] {
        println!("{}", super::context(_tbl, _env).to_string());
        return nil();
    }

    "parse" 1 [strin] {
        coretypck!(strin ; VecStr);
        let strsl = string_get(strin);

        return match super::parser::parse(_reg, _tbl, strsl) {
            Ok(head) => head,
            Err(err) => super::errcode_init(_reg, err),
        };
    }
}