blob: ea651836235b2458e7b4759beba3bbad6ed20343 [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
31#include "misc.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +000033#include "qstr.h"
Damien429d7192013-10-04 19:53:11 +010034#include "lexer.h"
Damien429d7192013-10-04 19:53:11 +010035#include "parse.h"
Damiend99b0522013-12-21 18:17:45 +000036#include "runtime0.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010037#include "obj.h"
38#include "emitglue.h"
39#include "scope.h"
Damien429d7192013-10-04 19:53:11 +010040#include "emit.h"
41
Damien George1dc76af2014-02-26 16:57:08 +000042#define EMIT(fun, ...) (emit_method_table->fun(emit, __VA_ARGS__))
Damien429d7192013-10-04 19:53:11 +010043
Damien4b03e772013-10-05 14:17:09 +010044void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
45 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010046
Damien4b03e772013-10-05 14:17:09 +010047 id_info_t *id = scope_find(scope, qstr);
48 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010049
50 // call the emit backend with the correct code
Damien4b03e772013-10-05 14:17:09 +010051 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
Damien429d7192013-10-04 19:53:11 +010052 EMIT(load_name, qstr);
Damien4b03e772013-10-05 14:17:09 +010053 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien429d7192013-10-04 19:53:11 +010054 EMIT(load_global, qstr);
Damien4b03e772013-10-05 14:17:09 +010055 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George2bf7c092014-04-09 15:26:46 +010056 EMIT(load_fast, qstr, id->flags, id->local_num);
Damien4b03e772013-10-05 14:17:09 +010057 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien27fb45e2013-10-20 15:07:49 +010058 EMIT(load_deref, qstr, id->local_num);
Damien429d7192013-10-04 19:53:11 +010059 } else {
60 assert(0);
61 }
62}
63
Damien4b03e772013-10-05 14:17:09 +010064void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
65 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010066
Damien4b03e772013-10-05 14:17:09 +010067 id_info_t *id = scope_find(scope, qstr);
68 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010069
70 // call the emit backend with the correct code
71 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
72 EMIT(store_name, qstr);
73 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
74 EMIT(store_global, qstr);
75 } else if (id->kind == ID_INFO_KIND_LOCAL) {
76 EMIT(store_fast, qstr, id->local_num);
77 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien27fb45e2013-10-20 15:07:49 +010078 EMIT(store_deref, qstr, id->local_num);
Damien429d7192013-10-04 19:53:11 +010079 } else {
80 assert(0);
81 }
82}
83
Damien4b03e772013-10-05 14:17:09 +010084void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr) {
85 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
86
87 id_info_t *id = scope_find(scope, qstr);
88 assert(id != NULL); // TODO can this ever fail?
Damien429d7192013-10-04 19:53:11 +010089
90 // call the emit backend with the correct code
91 if (id == NULL || id->kind == ID_INFO_KIND_GLOBAL_IMPLICIT) {
92 EMIT(delete_name, qstr);
93 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
94 EMIT(delete_global, qstr);
95 } else if (id->kind == ID_INFO_KIND_LOCAL) {
96 EMIT(delete_fast, qstr, id->local_num);
97 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien27fb45e2013-10-20 15:07:49 +010098 EMIT(delete_deref, qstr, id->local_num);
Damien429d7192013-10-04 19:53:11 +010099 } else {
100 assert(0);
101 }
102}