emitpass1.c 6.71 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
28
29
30
31
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>

#include "misc.h"
32
#include "mpconfig.h"
33
#include "qstr.h"
34
35
#include "lexer.h"
#include "parse.h"
36
37
#include "obj.h"
#include "emitglue.h"
38
#include "scope.h"
39
#include "runtime0.h"
40
41
42
43
44
45
#include "emit.h"

struct _emit_t {
    scope_t *scope;
};

Damien George's avatar
Damien George committed
46
emit_t *emit_pass1_new(void) {
47
48
49
50
51
    emit_t *emit = m_new(emit_t, 1);
    return emit;
}

void emit_pass1_free(emit_t *emit) {
52
    m_del_obj(emit_t, emit);
53
54
}

55
STATIC void emit_pass1_dummy(emit_t *emit) {
56
57
}

58
STATIC void emit_pass1_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
59
60
61
62
    assert(pass == PASS_1);
    emit->scope = scope;
}

63
STATIC void emit_pass1_end_pass(emit_t *emit) {
64
65
}

66
67
68
69
STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) {
    return false;
}

70
STATIC void emit_pass1_load_id(emit_t *emit, qstr qstr) {
71
72
73
74
    // name adding/lookup
    bool added;
    id_info_t *id = scope_find_or_add_id(emit->scope, qstr, &added);
    if (added) {
Damien George's avatar
Damien George committed
75
76
#if MICROPY_EMIT_CPYTHON
        if (qstr == MP_QSTR_super && emit->scope->kind == SCOPE_FUNCTION) {
77
78
            // special case, super is a global, and also counts as use of __class__
            id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
Damien George's avatar
Damien George committed
79
            id_info_t *id2 = scope_find_local_in_parent(emit->scope, MP_QSTR___class__);
80
            if (id2 != NULL) {
Damien George's avatar
Damien George committed
81
                id2 = scope_find_or_add_id(emit->scope, MP_QSTR___class__, &added);
82
83
                if (added) {
                    id2->kind = ID_INFO_KIND_FREE;
Damien George's avatar
Damien George committed
84
                    scope_close_over_in_parents(emit->scope, MP_QSTR___class__);
85
86
                }
            }
Damien George's avatar
Damien George committed
87
88
89
        } else
#endif
        {
90
91
92
93
94
95
96
97
98
99
100
            id_info_t *id2 = scope_find_local_in_parent(emit->scope, qstr);
            if (id2 != NULL && (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE)) {
                id->kind = ID_INFO_KIND_FREE;
                scope_close_over_in_parents(emit->scope, qstr);
            } else {
                id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
            }
        }
    }
}

101
STATIC id_info_t *get_id_for_modification(scope_t *scope, qstr qstr) {
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    // name adding/lookup
    bool added;
    id_info_t *id = scope_find_or_add_id(scope, qstr, &added);
    if (added) {
        if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
            id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
        } else {
            id->kind = ID_INFO_KIND_LOCAL;
        }
    } else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
        // rebind as a local variable
        id->kind = ID_INFO_KIND_LOCAL;
    }

    assert(id != NULL); // TODO can this ever fail?

    return id;
}

121
STATIC void emit_pass1_store_id(emit_t *emit, qstr qstr) {
122
123
124
    get_id_for_modification(emit->scope, qstr);
}

125
STATIC void emit_pass1_delete_id(emit_t *emit, qstr qstr) {
126
    id_info_t *id = get_id_for_modification(emit->scope, qstr);
127
128
129
    // this flag is unused
    //id->flags |= ID_FLAG_IS_DELETED;
    (void)id; // suppress compiler warning
130
131
132
133
134
135
}

const emit_method_table_t emit_pass1_method_table = {
    (void*)emit_pass1_dummy,
    emit_pass1_start_pass,
    emit_pass1_end_pass,
136
    emit_pass1_last_emit_was_return_value,
137
    (void*)emit_pass1_dummy,
138
    (void*)emit_pass1_dummy,
139
140
141
142
143
144
145
146
147
148

    emit_pass1_load_id,
    emit_pass1_store_id,
    emit_pass1_delete_id,

    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
149
    (void*)emit_pass1_dummy,
150
    (void*)emit_pass1_dummy,
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
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
217
#if MICROPY_EMIT_CPYTHON
218
219
220
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
    (void*)emit_pass1_dummy,
221
#endif
222
};