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 | |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 27 | // This code glues the code emitters to the runtime. |
| 28 | |
Damien George | d8c834c | 2015-11-02 21:55:42 +0000 | [diff] [blame] | 29 | #include <stdint.h> |
Damien George | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
Damien George | d1e443d | 2014-03-29 11:39:36 +0000 | [diff] [blame] | 31 | #include <string.h> |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 32 | #include <assert.h> |
| 33 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 34 | #include "py/emitglue.h" |
| 35 | #include "py/runtime0.h" |
| 36 | #include "py/bc.h" |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 37 | |
| 38 | #if 0 // print debugging info |
| 39 | #define DEBUG_PRINT (1) |
| 40 | #define WRITE_CODE (1) |
| 41 | #define DEBUG_printf DEBUG_printf |
| 42 | #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__) |
| 43 | #else // don't print debugging info |
| 44 | #define DEBUG_printf(...) (void)0 |
| 45 | #define DEBUG_OP_printf(...) (void)0 |
| 46 | #endif |
| 47 | |
Paul Sokolovsky | 295ea12 | 2015-11-21 16:34:57 +0200 | [diff] [blame] | 48 | #if MICROPY_DEBUG_PRINTERS |
| 49 | mp_uint_t mp_verbose_flag = 0; |
| 50 | #endif |
| 51 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 52 | mp_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 George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Damien George | d8c834c | 2015-11-02 21:55:42 +0000 | [diff] [blame] | 58 | void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code, mp_uint_t len, |
| 59 | const mp_uint_t *const_table, |
| 60 | #if MICROPY_PERSISTENT_CODE_SAVE |
| 61 | uint16_t n_obj, uint16_t n_raw_code, |
| 62 | #endif |
| 63 | mp_uint_t scope_flags) { |
| 64 | |
Damien George | ccc85ea | 2014-05-10 13:40:46 +0100 | [diff] [blame] | 65 | rc->kind = MP_CODE_BYTECODE; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 66 | rc->scope_flags = scope_flags; |
Damien George | d8c834c | 2015-11-02 21:55:42 +0000 | [diff] [blame] | 67 | rc->data.u_byte.bytecode = code; |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 68 | rc->data.u_byte.const_table = const_table; |
Damien George | d8c834c | 2015-11-02 21:55:42 +0000 | [diff] [blame] | 69 | #if MICROPY_PERSISTENT_CODE_SAVE |
| 70 | rc->data.u_byte.bc_len = len; |
| 71 | rc->data.u_byte.n_obj = n_obj; |
| 72 | rc->data.u_byte.n_raw_code = n_raw_code; |
| 73 | #endif |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 74 | |
| 75 | #ifdef DEBUG_PRINT |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 76 | DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 77 | #endif |
Paul Sokolovsky | 6b344d7 | 2014-05-05 00:50:05 +0300 | [diff] [blame] | 78 | #if MICROPY_DEBUG_PRINTERS |
Paul Sokolovsky | 429e3f0 | 2014-10-26 23:20:22 +0200 | [diff] [blame] | 79 | if (mp_verbose_flag >= 2) { |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 80 | mp_bytecode_print(rc, code, len, const_table); |
Paul Sokolovsky | 6b344d7 | 2014-05-05 00:50:05 +0300 | [diff] [blame] | 81 | } |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 82 | #endif |
| 83 | } |
| 84 | |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 85 | #if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_ASM |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 86 | void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table, mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) { |
Damien George | ccc85ea | 2014-05-10 13:40:46 +0100 | [diff] [blame] | 87 | assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM); |
| 88 | rc->kind = kind; |
Damien George | 9988618 | 2015-04-06 22:38:53 +0100 | [diff] [blame] | 89 | rc->scope_flags = scope_flags; |
| 90 | rc->n_pos_args = n_pos_args; |
Damien George | 32444b7 | 2015-01-24 23:14:12 +0000 | [diff] [blame] | 91 | rc->data.u_native.fun_data = fun_data; |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 92 | rc->data.u_native.const_table = const_table; |
Damien George | 32444b7 | 2015-01-24 23:14:12 +0000 | [diff] [blame] | 93 | rc->data.u_native.type_sig = type_sig; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 94 | |
| 95 | #ifdef DEBUG_PRINT |
Damien George | 3a3db4d | 2015-10-22 23:45:37 +0100 | [diff] [blame] | 96 | DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags); |
Damien George | 3c658a4 | 2014-08-24 16:28:17 +0100 | [diff] [blame] | 97 | for (mp_uint_t i = 0; i < fun_len; i++) { |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 98 | if (i > 0 && i % 16 == 0) { |
| 99 | DEBUG_printf("\n"); |
| 100 | } |
Damien George | 3c658a4 | 2014-08-24 16:28:17 +0100 | [diff] [blame] | 101 | DEBUG_printf(" %02x", ((byte*)fun_data)[i]); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 102 | } |
| 103 | DEBUG_printf("\n"); |
| 104 | |
| 105 | #ifdef WRITE_CODE |
Damien George | 915197a | 2014-05-12 23:11:14 +0100 | [diff] [blame] | 106 | FILE *fp_write_code = fopen("out-code", "wb"); |
Damien George | b427d6a | 2014-08-26 23:35:57 +0100 | [diff] [blame] | 107 | fwrite(fun_data, fun_len, 1, fp_write_code); |
Damien George | 915197a | 2014-05-12 23:11:14 +0100 | [diff] [blame] | 108 | fclose(fp_write_code); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 109 | #endif |
Damien George | ff8dd3f | 2015-01-20 12:47:20 +0000 | [diff] [blame] | 110 | #else |
| 111 | (void)fun_len; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 112 | #endif |
| 113 | } |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 114 | #endif |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 115 | |
Damien George | ed0c112 | 2016-01-31 22:19:42 +0000 | [diff] [blame] | 116 | mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) { |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 117 | DEBUG_OP_printf("make_function_from_raw_code %p\n", rc); |
| 118 | assert(rc != NULL); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 119 | |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 120 | // def_args must be MP_OBJ_NULL or a tuple |
| 121 | assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple)); |
| 122 | |
Damien George | f0778a7 | 2014-06-07 22:01:00 +0100 | [diff] [blame] | 123 | // def_kw_args must be MP_OBJ_NULL or a dict |
| 124 | assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict)); |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 125 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 126 | // make the function, depending on the raw code kind |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 127 | mp_obj_t fun; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 128 | switch (rc->kind) { |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 129 | #if MICROPY_EMIT_NATIVE |
Damien George | ccc85ea | 2014-05-10 13:40:46 +0100 | [diff] [blame] | 130 | case MP_CODE_NATIVE_PY: |
Damien George | 713ea18 | 2015-10-23 01:23:11 +0100 | [diff] [blame] | 131 | fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->data.u_native.fun_data, rc->data.u_native.const_table); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 132 | break; |
Damien George | ccc85ea | 2014-05-10 13:40:46 +0100 | [diff] [blame] | 133 | case MP_CODE_NATIVE_VIPER: |
Damien George | 32444b7 | 2015-01-24 23:14:12 +0000 | [diff] [blame] | 134 | fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig); |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 135 | break; |
| 136 | #endif |
Damien George | ad297a1 | 2016-12-09 13:17:49 +1100 | [diff] [blame] | 137 | #if MICROPY_EMIT_INLINE_ASM |
Damien George | ccc85ea | 2014-05-10 13:40:46 +0100 | [diff] [blame] | 138 | case MP_CODE_NATIVE_ASM: |
Damien George | 8f54c08 | 2016-01-15 15:20:43 +0000 | [diff] [blame] | 139 | fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 140 | break; |
Damien George | 2ac4af6 | 2014-08-15 16:45:41 +0100 | [diff] [blame] | 141 | #endif |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 142 | default: |
Damien George | 46a6592 | 2016-12-21 11:52:05 +1100 | [diff] [blame] | 143 | // rc->kind should always be set and BYTECODE is the only remaining case |
| 144 | assert(rc->kind == MP_CODE_BYTECODE); |
| 145 | fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->data.u_byte.bytecode, rc->data.u_byte.const_table); |
| 146 | break; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // check for generator functions and if so wrap in generator object |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 150 | if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 151 | fun = mp_obj_new_gen_wrap(fun); |
| 152 | } |
| 153 | |
| 154 | return fun; |
| 155 | } |
| 156 | |
Damien George | ed0c112 | 2016-01-31 22:19:42 +0000 | [diff] [blame] | 157 | mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) { |
Damien George | 3c658a4 | 2014-08-24 16:28:17 +0100 | [diff] [blame] | 158 | DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 159 | // make function object |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 160 | mp_obj_t ffun; |
| 161 | if (n_closed_over & 0x100) { |
| 162 | // default positional and keyword args given |
| 163 | ffun = mp_make_function_from_raw_code(rc, args[0], args[1]); |
| 164 | } else { |
| 165 | // default positional and keyword args not given |
| 166 | ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); |
| 167 | } |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 168 | // wrap function in closure object |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 169 | return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2)); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 170 | } |