sage/targets/
mod.rs

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
//! # Targets Module
//!
//! This module contains the code dealing with building the virtual
//! machine code to various targets, such as C source code.
//!
//! ## Current Structure
//!
//! Right now, this module is a bit empty, only implementing C (GCC only)
//! as a compiler target. This is due to the fact that it has been much
//! simpler to build the language on top of the virtual machine when there
//! are fewer implementations to change.
//!
//! ## Future Structure
//!
//! In the future, this module will be *much* more featured.
//! The frontend will use `Put` and `Get` to interact with the
//! virtual machine's I/O device, and this module will provide hooks
//! to add "system calls" to implement actual hardware-specific behaviors
//! using these instructions, **but with portability in mind**.
//!
//! Consider the `fork` system call on Unix systems. A backend might
//! implement a `fork` frontend for this virtual machine by executing
//! `fork` when a correct series of `Put` values are given to the I/O device.
//!
//! Other backends might not implement `fork` though, and so "catch all"
//! code can be implemented to *allow a backend to compile **as if** `fork`
//! is provided*. This might work by providing some frontend code which takes
//! two functions and runs them serially instead of in parallel, but still
//! accomplishing a simulated `fork`'s state.
//!
//! Other hardware specific instructions can be implemented this way very
//! nicely. Consider a VGA device which displays a screen. A hardware
//! specific implementation can be written for each supported target,
//! and a "catch all" implementation can work by trying to draw the screen
//! as ASCII/UNICODE with `PutChar. A hardware specific implementation may
//! also *choose* to fail under unsupported targets to prevent use where
//! not intended.

pub mod c;
pub use c::*;
pub mod sage_lisp;
pub use sage_lisp::*;

// pub mod sage_os;
// pub use sage_os::*;

// pub mod x86;
// pub use x86::*;

use log::info;

use crate::{
    side_effects::{Input, Output},
    vm::{self, *},
};

/// A trait for a target architecture to be compiled to.
pub trait Architecture {
    /// The name of the target architecture.
    fn name(&self) -> &str;
    /// The version of the target architecture.
    fn version(&self) -> &str;

    /// Whether or not the target architecture supports floating point.
    fn supports_floats(&self) -> bool;
    /// Whether or not the target architecture supports the given input (mode + channel).
    fn supports_input(&self, src: &Input) -> bool;
    /// Whether or not the target architecture supports the given output (mode + channel).
    fn supports_output(&self, dst: &Output) -> bool;

    /// Get a value from the given input stream (mode + channel).
    fn get(&mut self, src: &Input) -> Result<String, String>;
    /// Put a value to the given output stream (mode + channel).
    fn put(&mut self, dst: &Output) -> Result<String, String>;
    /// Peek a value from the device connected to the program.
    fn peek(&mut self) -> Result<String, String>;
    /// Poke a value to the device connected to the program.
    fn poke(&mut self) -> Result<String, String>;

    /// The code before the program starts.
    fn prelude(&self, _is_core: bool) -> Option<String> {
        None
    }

    /// The code after each instruction.
    fn postop(&self) -> Option<String> {
        None
    }

    /// The code after the program ends.
    fn postlude(&self, _is_core: bool) -> Option<String> {
        None
    }

    /// The code before the function definitions.
    fn pre_funs(&self, _funs: Vec<i32>) -> Option<String> {
        None
    }

    /// The code after the function definitions.
    fn post_funs(&self, _funs: Vec<i32>) -> Option<String> {
        None
    }

    /// The string used for indentation.
    fn indentation(&self) -> Option<String> {
        Some("\t".to_string())
    }

    /// Compile the declaration of a procedure.
    fn declare_proc(&mut self, label_id: usize) -> String;
    /// Compile an `End` instruction (with the matching `If` or `While` or `Function`)
    fn end(&mut self, matching: &CoreOp, fun: Option<usize>) -> String;
    /// Compile a `CoreOp` instruction.
    fn op(&mut self, op: &vm::CoreOp) -> String;
    /// Compile a `StandardOp` instruction.
    fn std_op(&mut self, op: &vm::StandardOp) -> Result<String, String>;
}

/// Implement a compiler for the given target.
pub trait CompiledTarget: Architecture {
    fn build_op(
        &mut self,
        op: &vm::CoreOp,
        matching_ops: &mut Vec<vm::CoreOp>,
        matching_funs: &mut Vec<usize>,
        current_fun: &mut usize,
        indent: &mut usize,
    ) -> Result<String, String> {
        Ok(match op {
            CoreOp::Function => {
                matching_ops.push(op.clone());

                matching_funs.push(*current_fun);
                let fun_header = self.declare_proc(*current_fun);
                *current_fun += 1;

                *indent += 1;
                fun_header
            }
            CoreOp::While | CoreOp::If => {
                matching_ops.push(op.clone());
                *indent += 1;

                self.op(op)
            }
            CoreOp::Else => {
                if let Some(CoreOp::If) = matching_ops.pop() {
                    matching_ops.push(op.clone());
                    self.op(op)
                } else {
                    return Err("Unexpected else".to_string());
                }
            }
            CoreOp::End => {
                *indent -= 1;
                if let Some(op) = matching_ops.pop() {
                    self.end(
                        &op,
                        if let CoreOp::Function = op {
                            matching_funs.pop()
                        } else {
                            None
                        },
                    )
                } else {
                    return Err("Unexpected end".to_string());
                }
            }

            CoreOp::Get(src) => {
                if !self.supports_input(src) {
                    return Err(format!(
                        "Input {:?} not supported on target {}",
                        src,
                        self.name()
                    ));
                }
                self.get(src)?
            }
            CoreOp::Put(dst) => {
                if !self.supports_output(dst) {
                    return Err(format!(
                        "Output {:?} not supported on target {}",
                        dst,
                        self.name()
                    ));
                }
                self.put(dst)?
            }
            other => self.op(other),
        })
    }

    fn build_std_op(
        &mut self,
        std_op: &vm::StandardOp,
        matching_ops: &mut Vec<vm::CoreOp>,
        matching_funs: &mut Vec<usize>,
        current_fun: &mut usize,
        indent: &mut usize,
    ) -> Result<String, String> {
        match std_op {
            StandardOp::CoreOp(op) => {
                self.build_op(op, matching_ops, matching_funs, current_fun, indent)
            }
            other => self.std_op(other),
        }
    }

    /// Compile the core variant of the machine code (must be implemented for every target).
    fn build_core(&mut self, program: &vm::CoreProgram) -> Result<String, String> {
        info!("Compiling core program for target {}", self.name());
        let (main_ops, function_defs) = program.clone().get_main_and_functions();
        let mut result = self.prelude(true).unwrap_or("".to_string());

        let mut matching_ops = vec![];
        let mut matching_funs = vec![];
        let mut current_fun: usize = 0;

        let tab = self.indentation().unwrap_or("".to_string());

        let mut indent = 0;

        result += &self
            .pre_funs(function_defs.keys().cloned().collect())
            .unwrap_or("".to_string());
        for i in 0..function_defs.len() as i32 {
            let f = &function_defs[&i];
            for op in f {
                result += &tab.repeat(indent);
                result += &self.build_op(
                    op,
                    &mut matching_ops,
                    &mut matching_funs,
                    &mut current_fun,
                    &mut indent,
                )?;
                result += &self.postop().unwrap_or("".to_string());
            }
        }
        result += &self
            .post_funs(function_defs.keys().cloned().collect())
            .unwrap_or("".to_string());
        indent = 1;
        for op in main_ops {
            result += &tab.repeat(indent);
            result += &self.build_op(
                &op,
                &mut matching_ops,
                &mut matching_funs,
                &mut current_fun,
                &mut indent,
            )?;
            result += &self.postop().unwrap_or("".to_string());
        }

        info!("Finished compiling core program for target {}", self.name());
        Ok(result + &tab + self.postlude(true).unwrap_or("".to_string()).as_str())
    }

    /// Compile the standard variant of the machine code (should be implemented for every target possible).
    fn build_std(&mut self, program: &vm::StandardProgram) -> Result<String, String> {
        info!("Compiling standard program for target {}", self.name());
        let (main_ops, function_defs) = program.clone().get_main_and_functions();
        let mut result = self.prelude(false).unwrap_or("".to_string());

        let mut matching_ops = vec![];
        let mut matching_funs = vec![];
        let mut current_fun: usize = 0;

        let tab = self.indentation().unwrap_or("".to_string());

        let mut indent = 0;
        result += &self
            .pre_funs(function_defs.keys().cloned().collect())
            .unwrap_or("".to_string());
        for i in 0..function_defs.len() as i32 {
            let f = &function_defs[&i];
            for op in f {
                result += &tab.repeat(indent);
                result += &self.build_std_op(
                    op,
                    &mut matching_ops,
                    &mut matching_funs,
                    &mut current_fun,
                    &mut indent,
                )?;
                result += &self.postop().unwrap_or("".to_string());
            }
        }
        result += &self
            .post_funs(function_defs.keys().cloned().collect())
            .unwrap_or("".to_string());
        indent = 1;
        for op in main_ops {
            result += &tab.repeat(indent);
            result += &self.build_std_op(
                &op,
                &mut matching_ops,
                &mut matching_funs,
                &mut current_fun,
                &mut indent,
            )?;
            result += &self.postop().unwrap_or("".to_string());
        }

        info!(
            "Finished compiling standard program for target {}",
            self.name()
        );
        Ok(result + &tab + self.postlude(false).unwrap_or("".to_string()).as_str())
    }
}