blob: 941f34dafbcbbfe4d2f9cc3b00d990d3f898f831 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien39cf3282013-10-05 23:18:38 +010027#include <assert.h>
28
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/emit.h"
Damien39cf3282013-10-05 23:18:38 +010030
31struct _emit_t {
Damien39cf3282013-10-05 23:18:38 +010032 scope_t *scope;
33};
34
Damien George35e2a4e2014-02-05 00:51:47 +000035emit_t *emit_pass1_new(void) {
Damien39cf3282013-10-05 23:18:38 +010036 emit_t *emit = m_new(emit_t, 1);
Damien39cf3282013-10-05 23:18:38 +010037 return emit;
38}
39
40void emit_pass1_free(emit_t *emit) {
Damien732407f2013-12-29 19:33:23 +000041 m_del_obj(emit_t, emit);
Damien39cf3282013-10-05 23:18:38 +010042}
43
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020044STATIC void emit_pass1_dummy(emit_t *emit) {
Damien39cf3282013-10-05 23:18:38 +010045}
46
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020047STATIC void emit_pass1_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien George36db6bc2014-05-07 17:24:22 +010048 assert(pass == MP_PASS_SCOPE);
Damien39cf3282013-10-05 23:18:38 +010049 emit->scope = scope;
50}
51
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020052STATIC void emit_pass1_end_pass(emit_t *emit) {
Damien39cf3282013-10-05 23:18:38 +010053}
54
Damien Georgec90717a2014-04-10 15:40:38 +000055STATIC bool emit_pass1_last_emit_was_return_value(emit_t *emit) {
56 return false;
57}
58
Damien George7ff996c2014-09-08 23:05:16 +010059STATIC void emit_pass1_load_id(emit_t *emit, qstr qst) {
Damien39cf3282013-10-05 23:18:38 +010060 // name adding/lookup
61 bool added;
Damien George7ff996c2014-09-08 23:05:16 +010062 id_info_t *id = scope_find_or_add_id(emit->scope, qst, &added);
Damien39cf3282013-10-05 23:18:38 +010063 if (added) {
Damien George35e2a4e2014-02-05 00:51:47 +000064#if MICROPY_EMIT_CPYTHON
Damien George7ff996c2014-09-08 23:05:16 +010065 if (qst == MP_QSTR_super && emit->scope->kind == SCOPE_FUNCTION) {
Damien39cf3282013-10-05 23:18:38 +010066 // special case, super is a global, and also counts as use of __class__
67 id->kind = ID_INFO_KIND_GLOBAL_EXPLICIT;
Damien George35e2a4e2014-02-05 00:51:47 +000068 id_info_t *id2 = scope_find_local_in_parent(emit->scope, MP_QSTR___class__);
Damien39cf3282013-10-05 23:18:38 +010069 if (id2 != NULL) {
Damien George35e2a4e2014-02-05 00:51:47 +000070 id2 = scope_find_or_add_id(emit->scope, MP_QSTR___class__, &added);
Damien39cf3282013-10-05 23:18:38 +010071 if (added) {
72 id2->kind = ID_INFO_KIND_FREE;
Damien George35e2a4e2014-02-05 00:51:47 +000073 scope_close_over_in_parents(emit->scope, MP_QSTR___class__);
Damien39cf3282013-10-05 23:18:38 +010074 }
75 }
Damien George35e2a4e2014-02-05 00:51:47 +000076 } else
77#endif
78 {
Damien George7ff996c2014-09-08 23:05:16 +010079 id_info_t *id2 = scope_find_local_in_parent(emit->scope, qst);
Damien39cf3282013-10-05 23:18:38 +010080 if (id2 != NULL && (id2->kind == ID_INFO_KIND_LOCAL || id2->kind == ID_INFO_KIND_CELL || id2->kind == ID_INFO_KIND_FREE)) {
81 id->kind = ID_INFO_KIND_FREE;
Damien George7ff996c2014-09-08 23:05:16 +010082 scope_close_over_in_parents(emit->scope, qst);
Damien39cf3282013-10-05 23:18:38 +010083 } else {
84 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
85 }
86 }
87 }
88}
89
Damien George7ff996c2014-09-08 23:05:16 +010090STATIC id_info_t *get_id_for_modification(scope_t *scope, qstr qst) {
Damien39cf3282013-10-05 23:18:38 +010091 // name adding/lookup
92 bool added;
Damien George7ff996c2014-09-08 23:05:16 +010093 id_info_t *id = scope_find_or_add_id(scope, qst, &added);
Damien39cf3282013-10-05 23:18:38 +010094 if (added) {
95 if (scope->kind == SCOPE_MODULE || scope->kind == SCOPE_CLASS) {
96 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
97 } else {
98 id->kind = ID_INFO_KIND_LOCAL;
99 }
100 } else if (scope->kind >= SCOPE_FUNCTION && scope->kind <= SCOPE_GEN_EXPR && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
101 // rebind as a local variable
102 id->kind = ID_INFO_KIND_LOCAL;
103 }
104
105 assert(id != NULL); // TODO can this ever fail?
106
107 return id;
108}
109
Damien George7ff996c2014-09-08 23:05:16 +0100110STATIC void emit_pass1_store_id(emit_t *emit, qstr qst) {
111 get_id_for_modification(emit->scope, qst);
Damien39cf3282013-10-05 23:18:38 +0100112}
113
Damien George7ff996c2014-09-08 23:05:16 +0100114STATIC void emit_pass1_delete_id(emit_t *emit, qstr qst) {
115 id_info_t *id = get_id_for_modification(emit->scope, qst);
Damien George6ce42772014-04-12 18:20:40 +0100116 // this flag is unused
117 //id->flags |= ID_FLAG_IS_DELETED;
118 (void)id; // suppress compiler warning
Damien39cf3282013-10-05 23:18:38 +0100119}
120
121const emit_method_table_t emit_pass1_method_table = {
122 (void*)emit_pass1_dummy,
123 emit_pass1_start_pass,
124 emit_pass1_end_pass,
Damien Georgec90717a2014-04-10 15:40:38 +0000125 emit_pass1_last_emit_was_return_value,
Damien39cf3282013-10-05 23:18:38 +0100126 (void*)emit_pass1_dummy,
Damien George08335002014-01-18 23:24:36 +0000127 (void*)emit_pass1_dummy,
Damien39cf3282013-10-05 23:18:38 +0100128
129 emit_pass1_load_id,
130 emit_pass1_store_id,
131 emit_pass1_delete_id,
132
133 (void*)emit_pass1_dummy,
134 (void*)emit_pass1_dummy,
135 (void*)emit_pass1_dummy,
136 (void*)emit_pass1_dummy,
137 (void*)emit_pass1_dummy,
138 (void*)emit_pass1_dummy,
139 (void*)emit_pass1_dummy,
140 (void*)emit_pass1_dummy,
141 (void*)emit_pass1_dummy,
142 (void*)emit_pass1_dummy,
143 (void*)emit_pass1_dummy,
144 (void*)emit_pass1_dummy,
145 (void*)emit_pass1_dummy,
146 (void*)emit_pass1_dummy,
147 (void*)emit_pass1_dummy,
148 (void*)emit_pass1_dummy,
149 (void*)emit_pass1_dummy,
150 (void*)emit_pass1_dummy,
151 (void*)emit_pass1_dummy,
152 (void*)emit_pass1_dummy,
153 (void*)emit_pass1_dummy,
154 (void*)emit_pass1_dummy,
155 (void*)emit_pass1_dummy,
156 (void*)emit_pass1_dummy,
157 (void*)emit_pass1_dummy,
158 (void*)emit_pass1_dummy,
159 (void*)emit_pass1_dummy,
160 (void*)emit_pass1_dummy,
161 (void*)emit_pass1_dummy,
162 (void*)emit_pass1_dummy,
163 (void*)emit_pass1_dummy,
164 (void*)emit_pass1_dummy,
165 (void*)emit_pass1_dummy,
166 (void*)emit_pass1_dummy,
167 (void*)emit_pass1_dummy,
168 (void*)emit_pass1_dummy,
169 (void*)emit_pass1_dummy,
170 (void*)emit_pass1_dummy,
171 (void*)emit_pass1_dummy,
172 (void*)emit_pass1_dummy,
173 (void*)emit_pass1_dummy,
174 (void*)emit_pass1_dummy,
175 (void*)emit_pass1_dummy,
176 (void*)emit_pass1_dummy,
177 (void*)emit_pass1_dummy,
178 (void*)emit_pass1_dummy,
179 (void*)emit_pass1_dummy,
180 (void*)emit_pass1_dummy,
181 (void*)emit_pass1_dummy,
182 (void*)emit_pass1_dummy,
183 (void*)emit_pass1_dummy,
184 (void*)emit_pass1_dummy,
185 (void*)emit_pass1_dummy,
186 (void*)emit_pass1_dummy,
187 (void*)emit_pass1_dummy,
188 (void*)emit_pass1_dummy,
189 (void*)emit_pass1_dummy,
190 (void*)emit_pass1_dummy,
191 (void*)emit_pass1_dummy,
192 (void*)emit_pass1_dummy,
Damien Georgedab13852015-01-13 15:55:54 +0000193 (void*)emit_pass1_dummy,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000194 #if MICROPY_PY_BUILTINS_SET
Damien39cf3282013-10-05 23:18:38 +0100195 (void*)emit_pass1_dummy,
196 (void*)emit_pass1_dummy,
Damien Georgee37dcaa2014-12-27 17:07:16 +0000197 #endif
Damien George83204f32014-12-27 17:20:41 +0000198 #if MICROPY_PY_BUILTINS_SLICE
Damien39cf3282013-10-05 23:18:38 +0100199 (void*)emit_pass1_dummy,
Damien George83204f32014-12-27 17:20:41 +0000200 #endif
Damien39cf3282013-10-05 23:18:38 +0100201 (void*)emit_pass1_dummy,
202 (void*)emit_pass1_dummy,
203 (void*)emit_pass1_dummy,
204 (void*)emit_pass1_dummy,
205 (void*)emit_pass1_dummy,
206 (void*)emit_pass1_dummy,
207 (void*)emit_pass1_dummy,
208 (void*)emit_pass1_dummy,
209 (void*)emit_pass1_dummy,
210 (void*)emit_pass1_dummy,
Damien Georgeb601d952014-06-30 05:17:25 +0100211
212 (void*)emit_pass1_dummy,
213 (void*)emit_pass1_dummy,
214
Damien George5f6a25f2014-04-20 18:02:27 +0100215#if MICROPY_EMIT_CPYTHON
Damien39cf3282013-10-05 23:18:38 +0100216 (void*)emit_pass1_dummy,
Damien George729f7b42014-04-17 22:10:53 +0100217 (void*)emit_pass1_dummy,
Damien George3558f622014-04-20 17:50:40 +0100218 (void*)emit_pass1_dummy,
Damien George5f6a25f2014-04-20 18:02:27 +0100219#endif
Damien39cf3282013-10-05 23:18:38 +0100220};