1 | /* |
2 | * This file is part of the MicroPython 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 | |
27 | #include <assert.h> |
28 | |
29 | #include "py/emit.h" |
30 | |
31 | #if MICROPY_ENABLE_COMPILER |
32 | |
33 | void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) { |
34 | // name adding/lookup |
35 | id_info_t *id = scope_find_or_add_id(scope, qst, ID_INFO_KIND_GLOBAL_IMPLICIT); |
36 | if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
37 | // rebind as a local variable |
38 | id->kind = ID_INFO_KIND_LOCAL; |
39 | } |
40 | } |
41 | |
42 | void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst) { |
43 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
44 | |
45 | id_info_t *id = scope_find(scope, qst); |
46 | assert(id != NULL); |
47 | |
48 | // call the emit backend with the correct code |
49 | if (id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
50 | emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_NAME); |
51 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
52 | emit_method_table->global(emit, qst, MP_EMIT_IDOP_GLOBAL_GLOBAL); |
53 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
54 | emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_FAST); |
55 | } else { |
56 | assert(id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE); |
57 | emit_method_table->local(emit, qst, id->local_num, MP_EMIT_IDOP_LOCAL_DEREF); |
58 | } |
59 | } |
60 | |
61 | #endif // MICROPY_ENABLE_COMPILER |
62 | |