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
//! # target, the module responsible for providing common targets for lasm
//!
//! As stated in the top level documentation of the crate,
//! the purpose of lasm is to be as portable as possible.
//! To maximize portability, the `target` module provides the
//! `Target` trait and a few builtin implementations for 
//! common programming languages.
//! 
//! Using the Instruct enum in the `asm` module, though,
//! Target can be implemented for other programming languages
//! by other crates that use this library. Additionally,
//! uses can write more optimized implementations for languages
//! that already have one.

use crate::Instruct;
use alloc::{string::String, vec::Vec};

/// This trait should be implemented for a struct that represents
/// a target language that lasm assembles to.
pub trait Target {
    /// This function assembles a list of instructions with a given stack size and initial stack pointer.
    ///
    /// The reason the initial stack pointer is necessary is because the output code must know how large
    /// the memory allocated for registers is. Without the initial stack pointer, the output code would
    /// have no clue how much memory registers use.
    fn assemble(&self, initial_stack_ptr: usize, stack_size: usize, code: Vec<Instruct>) -> String;
}

/// C is a target
pub struct C;

impl Target for C {
    fn assemble(&self, initial_stack_ptr: usize, stack_size: usize, code: Vec<Instruct>) -> String {
        let total_mem_size = initial_stack_ptr + stack_size;

        let mut result = format!(
            "#include <stdio.h>
#include <stdbool.h>

const int INIT_STACK_PTR = {reg_size};
const int MEMORY_SIZE = {mem_size};",
            reg_size = initial_stack_ptr,
            mem_size = total_mem_size
        );

        result += r#"
const int ACC = 0;
const int SPR = 1;


void init(double tape[], bool alloc_tape[]) {
    tape[SPR] = INIT_STACK_PTR;
    for (int i=0; i<INIT_STACK_PTR; i++) {
        alloc_tape[i] = true;
    }
}

void push_cell(double tape[], double value) {
    tape[(int)tape[SPR]++] = value;
}

void pop_cell(double tape[], int addr) {
    tape[addr] = tape[(int)--tape[SPR]];
    tape[(int)tape[SPR]] = 0;
}


void deref_load(double tape[]) {
    pop_cell(tape, ACC);
    int addr = tape[ACC];
    push_cell(tape, (int)tape[addr]);
}

void deref_store(double tape[]) {
    pop_cell(tape, ACC);
    int addr = tape[ACC];
    pop_cell(tape, addr);
}


void store(double tape[], int addr, int size) {
    for (int i=0; i<size; i++) {
        pop_cell(tape, addr + size - i - 1);
    }
}

void load(double tape[], int addr, int size) {
    for (int i=0; i<size; i++) {
        push_cell(tape, tape[(int)(addr + i)]);
    }
}


void add(double tape[]) {
    pop_cell(tape, ACC);
    double a = tape[ACC];
    pop_cell(tape, ACC);
    double b = tape[ACC];
    push_cell(tape, a + b);
}

void sub(double tape[]) {
    pop_cell(tape, ACC);
    double a = tape[ACC];
    pop_cell(tape, ACC);
    double b = tape[ACC];
    push_cell(tape, a - b);
}

void div(double tape[]) {
    pop_cell(tape, ACC);
    double a = tape[ACC];
    pop_cell(tape, ACC);
    double b = tape[ACC];
    push_cell(tape, a / b);
}

void mul(double tape[]) {
    pop_cell(tape, ACC);
    double a = tape[ACC];
    pop_cell(tape, ACC);
    double b = tape[ACC];
    push_cell(tape, a * b);
}

void dup(double tape[]) {
    pop_cell(tape, ACC);
    push_cell(tape, tape[ACC]);
    push_cell(tape, tape[ACC]);
}

void cmp(double tape[]) {
    pop_cell(tape, ACC);
    double a = tape[ACC];
    pop_cell(tape, ACC);
    double b = tape[ACC];
    if (a < b) {
        push_cell(tape, -1);
    } else if (a == b) {
        push_cell(tape, 0);
    } else if (a > b) {
        push_cell(tape, 1);
    }
}


void outc(double tape[]) {
    pop_cell(tape, ACC);
    printf("%c", (int)(tape[ACC]) % 256);
}

void outn(double tape[]) {
    pop_cell(tape, ACC);
    printf("%lG", tape[ACC]);
}

void inc(double tape[]) {
    char ch;
    if (scanf("%c", &ch) == 1) {
        push_cell(tape, ch);
    } else {
        push_cell(tape, 0);
    }
}

void inn(double tape[]) {
    double d;
    if (scanf("%lG", &d) == 1) {
        push_cell(tape, d);
    } else {
        push_cell(tape, 0);
    }
}

bool pop_bool(double tape[]) {
    pop_cell(tape, ACC);
    int a = tape[ACC];
    return a;
}


void lasm_alloc(double tape[], bool alloc_tape[], int ptr_addr) {
    pop_cell(tape, ACC);
    int size = tape[ACC];
    int cons_zeroes = 0;
    int i;
    for (i=MEMORY_SIZE-1; i>0; i--) {
        if (!alloc_tape[i]) {
            cons_zeroes++;
        } else {
            cons_zeroes = 0;
        }

        if (cons_zeroes == size) {
            push_cell(tape, i);
            pop_cell(tape, ptr_addr);
            break;
        }
    }

    for (int n=0; n<size; n++) {
        alloc_tape[i+n] = true;
    }
}

void lasm_free(double tape[], bool alloc_tape[], int ptr_addr) {
    pop_cell(tape, ACC);
    int size = tape[ACC];
    int addr = tape[ptr_addr];
    
    for (int n=0; n<size; n++) {
        tape[addr+n] = 0;
        alloc_tape[addr+n] = false;
    }
}

int main() {
    double tape[MEMORY_SIZE];
    bool alloc_tape[MEMORY_SIZE];
    for (int i=0; i<MEMORY_SIZE; i++) tape[i] = 0;
    for (int i=0; i<MEMORY_SIZE; i++) alloc_tape[i] = false;
    init(tape, alloc_tape);
"#;

        for line in &code {
            result += &(String::from("    ")
                + &match line {
                    Instruct::Refer(r) => format!("push_cell(tape, {});", r.get_addr()),
                    Instruct::DerefLoad => String::from("deref_load(tape);"),
                    Instruct::DerefStore => String::from("deref_store(tape);"),
                    Instruct::Alloc(r) => {
                        format!("lasm_alloc(tape, alloc_tape, {});", r.get_addr())
                    }
                    Instruct::Free(r) => format!("lasm_free(tape, alloc_tape, {});", r.get_addr()),
                    Instruct::Load(r) => format!("load(tape, {}, {});", r.get_addr(), r.get_size()),
                    Instruct::Store(r) => {
                        format!("store(tape, {}, {});", r.get_addr(), r.get_size())
                    }
                    Instruct::Push(l) => format!("push_cell(tape, {});", l.get()),
                    Instruct::Pop => String::from("pop_cell(tape, ACC);"),
                    Instruct::Duplicate => String::from("dup(tape);"),
                    Instruct::Add => String::from("add(tape);"),
                    Instruct::Subtract => String::from("sub(tape);"),
                    Instruct::Multiply => String::from("mul(tape);"),
                    Instruct::Divide => String::from("div(tape);"),
                    Instruct::InputChar => String::from("inc(tape);"),
                    Instruct::InputNumber => String::from("inn(tape);"),
                    Instruct::OutputChar => String::from("outc(tape);"),
                    Instruct::OutputNumber => String::from("outn(tape);"),
                    Instruct::Compare => String::from("cmp(tape);"),
                    Instruct::WhileNotZero => String::from("while (pop_bool(tape)) {"),
                    Instruct::EndWhile => String::from("}"),
                }
                + "\n");
        }

        result += r#"
        
    // printf("...\n\n");
    // for (int i=0; i<MEMORY_SIZE; i++) {
    //     if (i == INIT_STACK_PTR) {
    //         printf("STK_BGN ");
    //     }
    //     if (i == (int)tape[SPR]) {
    //         printf("STK_END ");
    //     }
    //     printf("%G ", tape[i]);
    // }
    // printf("\n...\n");
    // for (int i=0; i<MEMORY_SIZE; i++) {
    //     if (i == INIT_STACK_PTR) {
    //         printf("STK_BGN ");
    //     }
    //     if (i == (int)tape[SPR]) {
    //         printf("STK_END ");
    //     }
    //     printf("%d ", alloc_tape[i]);
    // }

    return 0;
}"#;
        result
    }
}