blob: 99cd3f3832710fb906ffe386f60ef4484cbafe4f [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
Damien George2326d522014-03-27 23:26:35 +000027// This code glues the code emitters to the runtime.
28
Damien George440f0412014-03-28 18:38:20 +000029#include <stdio.h>
Damien Georged1e443d2014-03-29 11:39:36 +000030#include <string.h>
Damien George2326d522014-03-27 23:26:35 +000031#include <assert.h>
32
Damien George2326d522014-03-27 23:26:35 +000033#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030034#include "misc.h"
Damien George2326d522014-03-27 23:26:35 +000035#include "qstr.h"
36#include "obj.h"
37#include "runtime0.h"
38#include "runtime.h"
39#include "emitglue.h"
Damien George440f0412014-03-28 18:38:20 +000040#include "bc.h"
Damien George2326d522014-03-27 23:26:35 +000041
42#if 0 // print debugging info
43#define DEBUG_PRINT (1)
44#define WRITE_CODE (1)
45#define DEBUG_printf DEBUG_printf
46#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
47#else // don't print debugging info
48#define DEBUG_printf(...) (void)0
49#define DEBUG_OP_printf(...) (void)0
50#endif
51
Damien Georgedf8127a2014-04-13 11:04:33 +010052mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
53 mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
54 rc->kind = MP_CODE_RESERVED;
55 return rc;
Damien George2326d522014-03-27 23:26:35 +000056}
57
Damien George3c658a42014-08-24 16:28:17 +010058void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, mp_uint_t len, mp_uint_t n_pos_args, mp_uint_t n_kwonly_args, qstr *arg_names, mp_uint_t scope_flags) {
Damien Georgeccc85ea2014-05-10 13:40:46 +010059 rc->kind = MP_CODE_BYTECODE;
Damien Georgedf8127a2014-04-13 11:04:33 +010060 rc->scope_flags = scope_flags;
Damien George2827d622014-04-27 15:50:52 +010061 rc->n_pos_args = n_pos_args;
62 rc->n_kwonly_args = n_kwonly_args;
63 rc->arg_names = arg_names;
Damien Georgedf8127a2014-04-13 11:04:33 +010064 rc->u_byte.code = code;
65 rc->u_byte.len = len;
Damien George2326d522014-03-27 23:26:35 +000066
67#ifdef DEBUG_PRINT
Damien George3c658a42014-08-24 16:28:17 +010068 DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " n_kwonly_args=" UINT_FMT " flags=%x\n", code, len, n_pos_args, n_kwonly_args, (uint)scope_flags);
Damien George2827d622014-04-27 15:50:52 +010069 DEBUG_printf(" arg names:");
70 for (int i = 0; i < n_pos_args + n_kwonly_args; i++) {
71 DEBUG_printf(" %s", qstr_str(arg_names[i]));
72 }
73 DEBUG_printf("\n");
Damien George2326d522014-03-27 23:26:35 +000074#endif
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030075#if MICROPY_DEBUG_PRINTERS
76 if (mp_verbose_flag > 0) {
Damien George3c658a42014-08-24 16:28:17 +010077 for (mp_uint_t i = 0; i < len; i++) {
Paul Sokolovskyf7539712014-06-03 01:38:01 +030078 if (i > 0 && i % 16 == 0) {
79 printf("\n");
80 }
81 printf(" %02x", code[i]);
82 }
83 printf("\n");
Paul Sokolovskya4ac5b92014-06-03 01:24:29 +030084 mp_bytecode_print(rc, code, len);
Paul Sokolovsky6b344d72014-05-05 00:50:05 +030085 }
Damien George2326d522014-03-27 23:26:35 +000086#endif
87}
88
Damien George2ac4af62014-08-15 16:45:41 +010089#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
Damien George3c658a42014-08-24 16:28:17 +010090void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, mp_uint_t n_args, mp_uint_t type_sig) {
Damien Georgeccc85ea2014-05-10 13:40:46 +010091 assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
92 rc->kind = kind;
Damien Georgedf8127a2014-04-13 11:04:33 +010093 rc->scope_flags = 0;
Damien George2827d622014-04-27 15:50:52 +010094 rc->n_pos_args = n_args;
Damien George3c658a42014-08-24 16:28:17 +010095 rc->u_native.fun_data = fun_data;
Damien George2ac4af62014-08-15 16:45:41 +010096 rc->u_native.type_sig = type_sig;
Damien George2326d522014-03-27 23:26:35 +000097
98#ifdef DEBUG_PRINT
Damien George3c658a42014-08-24 16:28:17 +010099 DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_args=" UINT_FMT "\n", kind, fun_data, fun_len, n_args);
100 for (mp_uint_t i = 0; i < fun_len; i++) {
Damien George2326d522014-03-27 23:26:35 +0000101 if (i > 0 && i % 16 == 0) {
102 DEBUG_printf("\n");
103 }
Damien George3c658a42014-08-24 16:28:17 +0100104 DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
Damien George2326d522014-03-27 23:26:35 +0000105 }
106 DEBUG_printf("\n");
107
108#ifdef WRITE_CODE
Damien George915197a2014-05-12 23:11:14 +0100109 FILE *fp_write_code = fopen("out-code", "wb");
Damien Georgeb427d6a2014-08-26 23:35:57 +0100110 fwrite(fun_data, fun_len, 1, fp_write_code);
Damien George915197a2014-05-12 23:11:14 +0100111 fclose(fp_write_code);
Damien George2326d522014-03-27 23:26:35 +0000112#endif
113#endif
114}
Damien George2ac4af62014-08-15 16:45:41 +0100115#endif
Damien George2326d522014-03-27 23:26:35 +0000116
Damien Georgedf8127a2014-04-13 11:04:33 +0100117mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
118 DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
119 assert(rc != NULL);
Damien George2326d522014-03-27 23:26:35 +0000120
Damien George69b89d22014-04-11 13:38:30 +0000121 // def_args must be MP_OBJ_NULL or a tuple
122 assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
123
Damien Georgef0778a72014-06-07 22:01:00 +0100124 // def_kw_args must be MP_OBJ_NULL or a dict
125 assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
Damien Georgee337f1e2014-03-31 15:18:37 +0100126
Damien Georgedf8127a2014-04-13 11:04:33 +0100127 // make the function, depending on the raw code kind
Damien George2326d522014-03-27 23:26:35 +0000128 mp_obj_t fun;
Damien Georgedf8127a2014-04-13 11:04:33 +0100129 switch (rc->kind) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100130 case MP_CODE_BYTECODE:
Damien Georgef0778a72014-06-07 22:01:00 +0100131 fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_pos_args, rc->n_kwonly_args, def_args, def_kw_args, rc->u_byte.code);
Damien George2326d522014-03-27 23:26:35 +0000132 break;
Damien George2ac4af62014-08-15 16:45:41 +0100133 #if MICROPY_EMIT_NATIVE
Damien Georgeccc85ea2014-05-10 13:40:46 +0100134 case MP_CODE_NATIVE_PY:
Damien George3c658a42014-08-24 16:28:17 +0100135 fun = mp_obj_new_fun_native(rc->n_pos_args, rc->u_native.fun_data);
Damien George2326d522014-03-27 23:26:35 +0000136 break;
Damien Georgeccc85ea2014-05-10 13:40:46 +0100137 case MP_CODE_NATIVE_VIPER:
Damien George3c658a42014-08-24 16:28:17 +0100138 fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->u_native.fun_data, rc->u_native.type_sig);
Damien George2ac4af62014-08-15 16:45:41 +0100139 break;
140 #endif
141 #if MICROPY_EMIT_INLINE_THUMB
Damien Georgeccc85ea2014-05-10 13:40:46 +0100142 case MP_CODE_NATIVE_ASM:
Damien George3c658a42014-08-24 16:28:17 +0100143 fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_native.fun_data);
Damien George2326d522014-03-27 23:26:35 +0000144 break;
Damien George2ac4af62014-08-15 16:45:41 +0100145 #endif
Damien George2326d522014-03-27 23:26:35 +0000146 default:
Damien Georgedf8127a2014-04-13 11:04:33 +0100147 // raw code was never set (this should not happen)
Damien George2326d522014-03-27 23:26:35 +0000148 assert(0);
Damien Georged1e443d2014-03-29 11:39:36 +0000149 return mp_const_none;
Damien George2326d522014-03-27 23:26:35 +0000150 }
151
152 // check for generator functions and if so wrap in generator object
Damien Georgedf8127a2014-04-13 11:04:33 +0100153 if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
Damien George2326d522014-03-27 23:26:35 +0000154 fun = mp_obj_new_gen_wrap(fun);
155 }
156
157 return fun;
158}
159
Damien George3c658a42014-08-24 16:28:17 +0100160mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
161 DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
Damien George2326d522014-03-27 23:26:35 +0000162 // make function object
Damien George3558f622014-04-20 17:50:40 +0100163 mp_obj_t ffun;
164 if (n_closed_over & 0x100) {
165 // default positional and keyword args given
166 ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
167 } else {
168 // default positional and keyword args not given
169 ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
170 }
Damien George2326d522014-03-27 23:26:35 +0000171 // wrap function in closure object
Damien George3558f622014-04-20 17:50:40 +0100172 return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
Damien George2326d522014-03-27 23:26:35 +0000173}