blob: e199bc6e9c099cccdaa35b88d3a5af139bf1358d [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
Damiend99b0522013-12-21 18:17:45 +000031#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030032#include "misc.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
Damien George7ff996c2014-09-08 23:05:16 +010044void 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 +010045 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010046
Damien George7ff996c2014-09-08 23:05:16 +010047 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010048 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) {
Damien George7ff996c2014-09-08 23:05:16 +010052 EMIT(load_name, qst);
Damien4b03e772013-10-05 14:17:09 +010053 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010054 EMIT(load_global, qst);
Damien4b03e772013-10-05 14:17:09 +010055 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +010056 EMIT(load_fast, qst, 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) {
Damien George7ff996c2014-09-08 23:05:16 +010058 EMIT(load_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010059 } else {
60 assert(0);
61 }
62}
63
Damien George7ff996c2014-09-08 23:05:16 +010064void 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 +010065 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
Damien429d7192013-10-04 19:53:11 +010066
Damien George7ff996c2014-09-08 23:05:16 +010067 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010068 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) {
Damien George7ff996c2014-09-08 23:05:16 +010072 EMIT(store_name, qst);
Damien429d7192013-10-04 19:53:11 +010073 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010074 EMIT(store_global, qst);
Damien429d7192013-10-04 19:53:11 +010075 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +010076 EMIT(store_fast, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010077 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +010078 EMIT(store_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010079 } else {
80 assert(0);
81 }
82}
83
Damien George7ff996c2014-09-08 23:05:16 +010084void 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 +010085 // assumes pass is greater than 1, ie that all identifiers are defined in the scope
86
Damien George7ff996c2014-09-08 23:05:16 +010087 id_info_t *id = scope_find(scope, qst);
Damien4b03e772013-10-05 14:17:09 +010088 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) {
Damien George7ff996c2014-09-08 23:05:16 +010092 EMIT(delete_name, qst);
Damien429d7192013-10-04 19:53:11 +010093 } else if (id->kind == ID_INFO_KIND_GLOBAL_EXPLICIT) {
Damien George7ff996c2014-09-08 23:05:16 +010094 EMIT(delete_global, qst);
Damien429d7192013-10-04 19:53:11 +010095 } else if (id->kind == ID_INFO_KIND_LOCAL) {
Damien George7ff996c2014-09-08 23:05:16 +010096 EMIT(delete_fast, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010097 } else if (id->kind == ID_INFO_KIND_CELL || id->kind == ID_INFO_KIND_FREE) {
Damien George7ff996c2014-09-08 23:05:16 +010098 EMIT(delete_deref, qst, id->local_num);
Damien429d7192013-10-04 19:53:11 +010099 } else {
100 assert(0);
101 }
102}