blob: e914431d325d761d263f1aa478e4f111874b2b2c [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
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28
Damien George51dfcb42015-01-01 20:27:54 +000029#include "py/emit.h"
Damien429d7192013-10-04 19:53:11 +010030
Damien Georgedd5353a2015-12-18 12:35:44 +000031#if MICROPY_ENABLE_COMPILER
32
Damien George542bd6b2015-03-26 14:42:40 +000033void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst) {
34 // name adding/lookup
35 bool added;
36 id_info_t *id = scope_find_or_add_id(scope, qst, &added);
37 if (added) {
Damien George0d105172016-09-30 13:53:00 +100038 scope_find_local_and_close_over(scope, id, qst);
Damien429d7192013-10-04 19:53:11 +010039 }
40}
41
Damien George542bd6b2015-03-26 14:42:40 +000042void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
43 // name adding/lookup
44 bool added;
45 id_info_t *id = scope_find_or_add_id(scope, qst, &added);
46 if (added) {
Damien George3dea8c92016-09-30 12:34:05 +100047 if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
Damien George542bd6b2015-03-26 14:42:40 +000048 id->kind = ID_INFO_KIND_LOCAL;
Damien George3dea8c92016-09-30 12:34:05 +100049 } else {
50 id->kind = ID_INFO_KIND_GLOBAL_IMPLICIT;
Damien George542bd6b2015-03-26 14:42:40 +000051 }
Damien George3dea8c92016-09-30 12:34:05 +100052 } else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien George542bd6b2015-03-26 14:42:40 +000053 // rebind as a local variable
54 id->kind = ID_INFO_KIND_LOCAL;
Damien429d7192013-10-04 19:53:11 +010055 }
56}
57
Damien George542bd6b2015-03-26 14:42:40 +000058void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst) {
Damien4b03e772013-10-05 14:17:09 +010059 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
60
Damien George7ff996c2014-09-08 23:05:16 +010061 id_info_t *id = scope_find(scope, qst);
Damien George542bd6b2015-03-26 14:42:40 +000062 assert(id != NULL);
Damien429d7192013-10-04 19:53:11 +010063
64 // call the emit backend with the correct code
Damien George542bd6b2015-03-26 14:42:40 +000065 if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
66 emit_method_table->name(emit, qst);
Damien429d7192013-10-04 19:53:11 +010067 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George542bd6b2015-03-26 14:42:40 +000068 emit_method_table->global(emit, qst);
Damien429d7192013-10-04 19:53:11 +010069 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George542bd6b2015-03-26 14:42:40 +000070 emit_method_table->fast(emit, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010071 } else {
Damien George542bd6b2015-03-26 14:42:40 +000072 assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE);
73 emit_method_table->deref(emit, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010074 }
75}
Damien Georgedd5353a2015-12-18 12:35:44 +000076
77#endif // MICROPY_ENABLE_COMPILER