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 | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
Damien George | d1e443d | 2014-03-29 11:39:36 +0000 | [diff] [blame] | 30 | #include <string.h> |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 31 | #include <assert.h> |
| 32 | |
| 33 | #include "misc.h" |
| 34 | #include "mpconfig.h" |
| 35 | #include "qstr.h" |
| 36 | #include "obj.h" |
| 37 | #include "runtime0.h" |
| 38 | #include "runtime.h" |
| 39 | #include "emitglue.h" |
Damien George | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 40 | #include "bc.h" |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 41 | |
| 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 George | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 52 | #ifdef WRITE_CODE |
| 53 | FILE *fp_write_code = NULL; |
| 54 | #endif |
| 55 | |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 56 | void mp_emit_glue_init(void) { |
Damien George | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 57 | #ifdef WRITE_CODE |
| 58 | fp_write_code = fopen("out-code", "wb"); |
| 59 | #endif |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void mp_emit_glue_deinit(void) { |
Damien George | 440f041 | 2014-03-28 18:38:20 +0000 | [diff] [blame] | 63 | #ifdef WRITE_CODE |
| 64 | if (fp_write_code != NULL) { |
| 65 | fclose(fp_write_code); |
| 66 | } |
| 67 | #endif |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 70 | mp_raw_code_t *mp_emit_glue_new_raw_code(void) { |
| 71 | mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1); |
| 72 | rc->kind = MP_CODE_RESERVED; |
| 73 | return rc; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame^] | 76 | void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags) { |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 77 | rc->kind = MP_CODE_BYTE; |
| 78 | rc->scope_flags = scope_flags; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 79 | rc->n_pos_args = n_pos_args; |
| 80 | rc->n_kwonly_args = n_kwonly_args; |
| 81 | rc->arg_names = arg_names; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 82 | rc->u_byte.code = code; |
| 83 | rc->u_byte.len = len; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 84 | |
| 85 | #ifdef DEBUG_PRINT |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 86 | DEBUG_printf("assign byte code: code=%p len=%u n_pos_args=%d n_kwonly_args=%d flags=%x\n", code, len, n_pos_args, n_kwonly_args, scope_flags); |
| 87 | DEBUG_printf(" arg names:"); |
| 88 | for (int i = 0; i < n_pos_args + n_kwonly_args; i++) { |
| 89 | DEBUG_printf(" %s", qstr_str(arg_names[i])); |
| 90 | } |
| 91 | DEBUG_printf("\n"); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 92 | for (int i = 0; i < 128 && i < len; i++) { |
| 93 | if (i > 0 && i % 16 == 0) { |
| 94 | DEBUG_printf("\n"); |
| 95 | } |
| 96 | DEBUG_printf(" %02x", code[i]); |
| 97 | } |
| 98 | DEBUG_printf("\n"); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 99 | #endif |
Paul Sokolovsky | 6b344d7 | 2014-05-05 00:50:05 +0300 | [diff] [blame] | 100 | #if MICROPY_DEBUG_PRINTERS |
| 101 | if (mp_verbose_flag > 0) { |
Damien George | 3417bc2 | 2014-05-10 10:36:38 +0100 | [diff] [blame^] | 102 | mp_bytecode_print(code, len); |
Paul Sokolovsky | 6b344d7 | 2014-05-05 00:50:05 +0300 | [diff] [blame] | 103 | } |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 104 | #endif |
| 105 | } |
| 106 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 107 | void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) { |
| 108 | rc->kind = MP_CODE_NATIVE; |
| 109 | rc->scope_flags = 0; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 110 | rc->n_pos_args = n_args; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 111 | rc->u_native.fun = fun; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 112 | |
| 113 | #ifdef DEBUG_PRINT |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 114 | DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 115 | byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code |
| 116 | for (int i = 0; i < 128 && i < len; i++) { |
| 117 | if (i > 0 && i % 16 == 0) { |
| 118 | DEBUG_printf("\n"); |
| 119 | } |
| 120 | DEBUG_printf(" %02x", fun_data[i]); |
| 121 | } |
| 122 | DEBUG_printf("\n"); |
| 123 | |
| 124 | #ifdef WRITE_CODE |
| 125 | if (fp_write_code != NULL) { |
| 126 | fwrite(fun_data, len, 1, fp_write_code); |
| 127 | fflush(fp_write_code); |
| 128 | } |
| 129 | #endif |
| 130 | #endif |
| 131 | } |
| 132 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 133 | void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) { |
| 134 | rc->kind = MP_CODE_INLINE_ASM; |
| 135 | rc->scope_flags = 0; |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 136 | rc->n_pos_args = n_args; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 137 | rc->u_inline_asm.fun = fun; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 138 | |
| 139 | #ifdef DEBUG_PRINT |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 140 | DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 141 | byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code |
| 142 | for (int i = 0; i < 128 && i < len; i++) { |
| 143 | if (i > 0 && i % 16 == 0) { |
| 144 | DEBUG_printf("\n"); |
| 145 | } |
| 146 | DEBUG_printf(" %02x", fun_data[i]); |
| 147 | } |
| 148 | DEBUG_printf("\n"); |
| 149 | |
| 150 | #ifdef WRITE_CODE |
| 151 | if (fp_write_code != NULL) { |
| 152 | fwrite(fun_data, len, 1, fp_write_code); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 153 | fflush(fp_write_code); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 154 | } |
| 155 | #endif |
| 156 | #endif |
| 157 | } |
| 158 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 159 | mp_obj_t mp_make_function_from_raw_code(mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) { |
| 160 | DEBUG_OP_printf("make_function_from_raw_code %p\n", rc); |
| 161 | assert(rc != NULL); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 162 | |
Damien George | 69b89d2 | 2014-04-11 13:38:30 +0000 | [diff] [blame] | 163 | // def_args must be MP_OBJ_NULL or a tuple |
| 164 | assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple)); |
| 165 | |
Damien George | e337f1e | 2014-03-31 15:18:37 +0100 | [diff] [blame] | 166 | // TODO implement default kw args |
| 167 | assert(def_kw_args == MP_OBJ_NULL); |
| 168 | |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 169 | // make the function, depending on the raw code kind |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 170 | mp_obj_t fun; |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 171 | switch (rc->kind) { |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 172 | case MP_CODE_BYTE: |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 173 | fun = mp_obj_new_fun_bc(rc->scope_flags, rc->arg_names, rc->n_pos_args, rc->n_kwonly_args, def_args, rc->u_byte.code); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 174 | break; |
| 175 | case MP_CODE_NATIVE: |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 176 | fun = mp_make_function_n(rc->n_pos_args, rc->u_native.fun); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 177 | break; |
| 178 | case MP_CODE_INLINE_ASM: |
Damien George | 2827d62 | 2014-04-27 15:50:52 +0100 | [diff] [blame] | 179 | fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_inline_asm.fun); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 180 | break; |
| 181 | default: |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 182 | // raw code was never set (this should not happen) |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 183 | assert(0); |
Damien George | d1e443d | 2014-03-29 11:39:36 +0000 | [diff] [blame] | 184 | return mp_const_none; |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // check for generator functions and if so wrap in generator object |
Damien George | df8127a | 2014-04-13 11:04:33 +0100 | [diff] [blame] | 188 | if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 189 | fun = mp_obj_new_gen_wrap(fun); |
| 190 | } |
| 191 | |
| 192 | return fun; |
| 193 | } |
| 194 | |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 195 | mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, uint n_closed_over, const mp_obj_t *args) { |
Paul Sokolovsky | 5e3e2d0 | 2014-04-23 02:31:54 +0300 | [diff] [blame] | 196 | DEBUG_OP_printf("make_closure_from_raw_code %p %u %p\n", rc, n_closed_over, args); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 197 | // make function object |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 198 | mp_obj_t ffun; |
| 199 | if (n_closed_over & 0x100) { |
| 200 | // default positional and keyword args given |
| 201 | ffun = mp_make_function_from_raw_code(rc, args[0], args[1]); |
| 202 | } else { |
| 203 | // default positional and keyword args not given |
| 204 | ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); |
| 205 | } |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 206 | // wrap function in closure object |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 207 | 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] | 208 | } |