Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 Dmitrichenko | 5630b01 | 2014-04-12 16:44:32 +0100 | [diff] [blame] | 27 | #include <string.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 28 | #include <stdint.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 29 | #include <assert.h> |
| 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/emit.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 32 | |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 33 | #define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__)) |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 34 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 35 | void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 36 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 37 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 38 | id_info_t *id = scope_find(scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 39 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 40 | |
| 41 | // call the emit backend with the correct code |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 42 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 43 | EMIT(load_name, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 44 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 45 | EMIT(load_global, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 46 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
Damien George | 0abb560 | 2015-01-16 12:24:49 +0000 | [diff] [blame] | 47 | EMIT(load_fast, qst, id->local_num); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 48 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 49 | EMIT(load_deref, qst, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 50 | } else { |
| 51 | assert(0); |
| 52 | } |
| 53 | } |
| 54 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 55 | void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 56 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 57 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 58 | id_info_t *id = scope_find(scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 59 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 60 | |
| 61 | // call the emit backend with the correct code |
| 62 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 63 | EMIT(store_name, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 64 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 65 | EMIT(store_global, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 66 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 67 | EMIT(store_fast, qst, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 68 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 69 | EMIT(store_deref, qst, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 70 | } else { |
| 71 | assert(0); |
| 72 | } |
| 73 | } |
| 74 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 75 | void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qst) { |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 76 | // assumes pass is greater than 1, ie that all identifiers are defined in the scope |
| 77 | |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 78 | id_info_t *id = scope_find(scope, qst); |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 79 | assert(id != NULL); // TODO can this ever fail? |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 80 | |
| 81 | // call the emit backend with the correct code |
| 82 | if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 83 | EMIT(delete_name, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 84 | } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 85 | EMIT(delete_global, qst); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 86 | } else if (id->kind == ID_INFO_KIND_LOCAL) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 87 | EMIT(delete_fast, qst, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 88 | } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) { |
Damien George | 7ff996c | 2014-09-08 23:05:16 +0100 | [diff] [blame] | 89 | EMIT(delete_deref, qst, id->local_num); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 90 | } else { |
| 91 | assert(0); |
| 92 | } |
| 93 | } |