emitcommon.c 2.66 KB
Newer Older
1
#include <string.h>
Damien's avatar
Damien committed
2
3
4
5
#include <stdint.h>
#include <assert.h>

#include "misc.h"
6
#include "mpconfig.h"
7
#include "qstr.h"
Damien's avatar
Damien committed
8
9
10
#include "lexer.h"
#include "parse.h"
#include "scope.h"
11
#include "runtime0.h"
Damien's avatar
Damien committed
12
13
#include "emit.h"

14
#define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__))
Damien's avatar
Damien committed
15

16
17
void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
    // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien's avatar
Damien committed
18

19
20
    id_info_t *id = scope_find(scope, qstr);
    assert(id != NULL); // TODO can this ever fail?
Damien's avatar
Damien committed
21
22

    // call the emit backend with the correct code
23
    if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien's avatar
Damien committed
24
        EMIT(load_name, qstr);
25
    } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien's avatar
Damien committed
26
        EMIT(load_global, qstr);
27
    } else if (id->kind == ID_INFO_KIND_LOCAL) {
28
        EMIT(load_fast, qstr, id->flags, id->local_num);
29
    } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
30
        EMIT(load_deref, qstr, id->local_num);
Damien's avatar
Damien committed
31
32
33
34
35
    } else {
        assert(0);
    }
}

36
37
void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
    // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien's avatar
Damien committed
38

39
40
    id_info_t *id = scope_find(scope, qstr);
    assert(id != NULL); // TODO can this ever fail?
Damien's avatar
Damien committed
41
42
43
44
45
46
47
48
49

    // call the emit backend with the correct code
    if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
        EMIT(store_name, qstr);
    } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
        EMIT(store_global, qstr);
    } else if (id->kind == ID_INFO_KIND_LOCAL) {
        EMIT(store_fast, qstr, id->local_num);
    } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
50
        EMIT(store_deref, qstr, id->local_num);
Damien's avatar
Damien committed
51
52
53
54
55
    } else {
        assert(0);
    }
}

56
57
58
59
60
void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
    // assumes pass is greater than 1, ie that all identifiers are defined in the scope

    id_info_t *id = scope_find(scope, qstr);
    assert(id != NULL); // TODO can this ever fail?
Damien's avatar
Damien committed
61
62
63
64
65
66
67
68
69

    // call the emit backend with the correct code
    if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
        EMIT(delete_name, qstr);
    } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
        EMIT(delete_global, qstr);
    } else if (id->kind == ID_INFO_KIND_LOCAL) {
        EMIT(delete_fast, qstr, id->local_num);
    } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
70
        EMIT(delete_deref, qstr, id->local_num);
Damien's avatar
Damien committed
71
72
73
74
    } else {
        assert(0);
    }
}