Numworks Epsilon  1.4.1
Graphing Calculator Operating System
emitcommon.c
Go to the documentation of this file.
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 
34  // name adding/lookup
35  bool added;
36  id_info_t *id = scope_find_or_add_id(scope, qst, &added);
37  if (added) {
38  scope_find_local_and_close_over(scope, id, qst);
39  }
40 }
41 
43  // name adding/lookup
44  bool added;
45  id_info_t *id = scope_find_or_add_id(scope, qst, &added);
46  if (added) {
47  if (SCOPE_IS_FUNC_LIKE(scope->kind)) {
48  id->kind = ID_INFO_KIND_LOCAL;
49  } else {
51  }
52  } else if (SCOPE_IS_FUNC_LIKE(scope->kind) && id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
53  // rebind as a local variable
54  id->kind = ID_INFO_KIND_LOCAL;
55  }
56 }
57 
58 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) {
59  // assumes pass is greater than 1, ie that all identifiers are defined in the scope
60 
61  id_info_t *id = scope_find(scope, qst);
62  assert(id != NULL);
63 
64  // call the emit backend with the correct code
66  emit_method_table->name(emit, qst);
67  } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
68  emit_method_table->global(emit, qst);
69  } else if (id->kind == ID_INFO_KIND_LOCAL) {
70  emit_method_table->fast(emit, qst, id->local_num);
71  } else {
73  emit_method_table->deref(emit, qst, id->local_num);
74  }
75 }
76 
77 #endif // MICROPY_ENABLE_COMPILER
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)
#define assert(e)
Definition: assert.h:9
scope_kind_t kind
Definition: scope.h:70
void(* deref)(emit_t *emit, qstr qst, mp_uint_t local_num)
Definition: emit.h:62
id_info_t * scope_find(scope_t *scope, qstr qstr)
void(* name)(emit_t *emit, qstr qst)
Definition: emit.h:63
#define NULL
Definition: stddef.h:4
void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst)
size_t qstr
Definition: qstr.h:48
id_info_t * scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added)
void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst)
Definition: scope.h:69
uint8_t kind
Definition: scope.h:47
#define SCOPE_IS_FUNC_LIKE(s)
Definition: scope.h:55
void scope_find_local_and_close_over(scope_t *scope, id_info_t *id, qstr qst)
uint16_t local_num
Definition: scope.h:51
struct _emit_t emit_t
Definition: emit.h:58
void(* fast)(emit_t *emit, qstr qst, mp_uint_t local_num)
Definition: emit.h:61
void(* global)(emit_t *emit, qstr qst)
Definition: emit.h:64