emitcommon.c 3.9 KB
Newer Older
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
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <string.h>
Damien's avatar
Damien committed
28
29
30
31
#include <stdint.h>
#include <assert.h>

#include "misc.h"
32
#include "mpconfig.h"
33
#include "qstr.h"
Damien's avatar
Damien committed
34
35
#include "lexer.h"
#include "parse.h"
36
#include "runtime0.h"
37
38
39
#include "obj.h"
#include "emitglue.h"
#include "scope.h"
Damien's avatar
Damien committed
40
41
#include "emit.h"

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

44
45
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
46

47
48
    id_info_t *id = scope_find(scope, qstr);
    assert(id != NULL); // TODO can this ever fail?
Damien's avatar
Damien committed
49
50

    // call the emit backend with the correct code
51
    if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien's avatar
Damien committed
52
        EMIT(load_name, qstr);
53
    } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien's avatar
Damien committed
54
        EMIT(load_global, qstr);
55
    } else if (id->kind == ID_INFO_KIND_LOCAL) {
56
        EMIT(load_fast, qstr, id->flags, id->local_num);
57
    } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
58
        EMIT(load_deref, qstr, id->local_num);
Damien's avatar
Damien committed
59
60
61
62
63
    } else {
        assert(0);
    }
}

64
65
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
66

67
68
    id_info_t *id = scope_find(scope, qstr);
    assert(id != NULL); // TODO can this ever fail?
Damien's avatar
Damien committed
69
70
71
72
73
74
75
76
77

    // 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) {
78
        EMIT(store_deref, qstr, id->local_num);
Damien's avatar
Damien committed
79
80
81
82
83
    } else {
        assert(0);
    }
}

84
85
86
87
88
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
89
90
91
92
93
94
95
96
97

    // 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) {
98
        EMIT(delete_deref, qstr, id->local_num);
Damien's avatar
Damien committed
99
100
101
102
    } else {
        assert(0);
    }
}