blob: 8011c0fb0bfc95e8e2ad3c5dc4223a2dbbd7fde8 [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
Ilya Dmitrichenko5630b012014-04-12 16:44:32 +010027#include <string.h>
Damien429d7192013-10-04 19:53:11 +010028#include <stdint.h>
Damien429d7192013-10-04 19:53:11 +010029#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/emit.h"
Damien429d7192013-10-04 19:53:11 +010032
Damien George1dc76af2014-02-26 16:57:08 +000033#define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010034
Damien George7ff996c2014-09-08 23:05:16 +010035void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) {
Damien4b03e772013-10-05 14:17:09 +010036 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010037
Damien George7ff996c2014-09-08 23:05:16 +010038 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010039 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010040
41 // call the emit backend with the correct code
Damien4b03e772013-10-05 14:17:09 +010042 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010043 EMIT(load_name, qst);
Damien4b03e772013-10-05 14:17:09 +010044 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010045 EMIT(load_global, qst);
Damien4b03e772013-10-05 14:17:09 +010046 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George0abb5602015-01-16 12:24:49 +000047 EMIT(load_fast, qst, id->local_num);
Damien4b03e772013-10-05 14:17:09 +010048 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +010049 EMIT(load_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010050 } else {
51 assert(0);
52 }
53}
54
Damien George7ff996c2014-09-08 23:05:16 +010055void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) {
Damien4b03e772013-10-05 14:17:09 +010056 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010057
Damien George7ff996c2014-09-08 23:05:16 +010058 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010059 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010060
61 // call the emit backend with the correct code
62 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010063 EMIT(store_name, qst);
Damien429d7192013-10-04 19:53:11 +010064 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010065 EMIT(store_global, qst);
Damien429d7192013-10-04 19:53:11 +010066 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +010067 EMIT(store_fast, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010068 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +010069 EMIT(store_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010070 } else {
71 assert(0);
72 }
73}
74
Damien George7ff996c2014-09-08 23:05:16 +010075void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) {
Damien4b03e772013-10-05 14:17:09 +010076 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
77
Damien George7ff996c2014-09-08 23:05:16 +010078 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010079 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010080
81 // call the emit backend with the correct code
82 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010083 EMIT(delete_name, qst);
Damien429d7192013-10-04 19:53:11 +010084 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010085 EMIT(delete_global, qst);
Damien429d7192013-10-04 19:53:11 +010086 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +010087 EMIT(delete_fast, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010088 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +010089 EMIT(delete_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010090 } else {
91 assert(0);
92 }
93}