2014-05-03 18:27:38 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-03-27 19:26:35 -04:00
|
|
|
// This code glues the code emitters to the runtime.
|
|
|
|
|
2014-03-28 14:38:20 -04:00
|
|
|
#include <stdio.h>
|
2014-03-29 07:39:36 -04:00
|
|
|
#include <string.h>
|
2014-03-27 19:26:35 -04:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/emitglue.h"
|
|
|
|
#include "py/runtime0.h"
|
|
|
|
#include "py/bc.h"
|
2014-03-27 19:26:35 -04:00
|
|
|
|
|
|
|
#if 0 // print debugging info
|
|
|
|
#define DEBUG_PRINT (1)
|
|
|
|
#define WRITE_CODE (1)
|
|
|
|
#define DEBUG_printf DEBUG_printf
|
|
|
|
#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#define DEBUG_OP_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2015-01-24 18:14:12 -05:00
|
|
|
struct _mp_raw_code_t {
|
|
|
|
mp_raw_code_kind_t kind : 3;
|
|
|
|
mp_uint_t scope_flags : 7;
|
|
|
|
mp_uint_t n_pos_args : 11;
|
|
|
|
union {
|
|
|
|
struct {
|
2015-10-22 18:45:37 -04:00
|
|
|
const byte *code;
|
2015-01-24 18:14:12 -05:00
|
|
|
} u_byte;
|
|
|
|
struct {
|
|
|
|
void *fun_data;
|
|
|
|
mp_uint_t type_sig; // for viper, compressed as 2-bit types; ret is MSB, then arg0, arg1, etc
|
|
|
|
} u_native;
|
|
|
|
} data;
|
|
|
|
};
|
|
|
|
|
2014-04-13 06:04:33 -04:00
|
|
|
mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
|
|
|
|
mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
|
|
|
|
rc->kind = MP_CODE_RESERVED;
|
|
|
|
return rc;
|
2014-03-27 19:26:35 -04:00
|
|
|
}
|
|
|
|
|
2015-10-22 18:45:37 -04:00
|
|
|
void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, byte *code, mp_uint_t len, mp_uint_t scope_flags) {
|
2014-05-10 08:40:46 -04:00
|
|
|
rc->kind = MP_CODE_BYTECODE;
|
2014-04-13 06:04:33 -04:00
|
|
|
rc->scope_flags = scope_flags;
|
2015-01-24 18:14:12 -05:00
|
|
|
rc->data.u_byte.code = code;
|
2014-03-27 19:26:35 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2015-10-22 18:45:37 -04:00
|
|
|
DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
|
2014-03-27 19:26:35 -04:00
|
|
|
#endif
|
2014-05-04 17:50:05 -04:00
|
|
|
#if MICROPY_DEBUG_PRINTERS
|
2014-10-26 17:20:22 -04:00
|
|
|
if (mp_verbose_flag >= 2) {
|
2015-10-22 18:45:37 -04:00
|
|
|
mp_bytecode_print(rc, code, len);
|
2014-05-04 17:50:05 -04:00
|
|
|
}
|
2014-03-27 19:26:35 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-15 11:45:41 -04:00
|
|
|
#if MICROPY_EMIT_NATIVE || MICROPY_EMIT_INLINE_THUMB
|
2015-10-22 18:45:37 -04:00
|
|
|
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, mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
|
2014-05-10 08:40:46 -04:00
|
|
|
assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
|
|
|
|
rc->kind = kind;
|
2015-04-06 17:38:53 -04:00
|
|
|
rc->scope_flags = scope_flags;
|
|
|
|
rc->n_pos_args = n_pos_args;
|
2015-01-24 18:14:12 -05:00
|
|
|
rc->data.u_native.fun_data = fun_data;
|
|
|
|
rc->data.u_native.type_sig = type_sig;
|
2014-03-27 19:26:35 -04:00
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2015-10-22 18:45:37 -04:00
|
|
|
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);
|
2014-08-24 11:28:17 -04:00
|
|
|
for (mp_uint_t i = 0; i < fun_len; i++) {
|
2014-03-27 19:26:35 -04:00
|
|
|
if (i > 0 && i % 16 == 0) {
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
2014-08-24 11:28:17 -04:00
|
|
|
DEBUG_printf(" %02x", ((byte*)fun_data)[i]);
|
2014-03-27 19:26:35 -04:00
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
|
|
|
|
#ifdef WRITE_CODE
|
2014-05-12 18:11:14 -04:00
|
|
|
FILE *fp_write_code = fopen("out-code", "wb");
|
2014-08-26 18:35:57 -04:00
|
|
|
fwrite(fun_data, fun_len, 1, fp_write_code);
|
2014-05-12 18:11:14 -04:00
|
|
|
fclose(fp_write_code);
|
2014-03-27 19:26:35 -04:00
|
|
|
#endif
|
2015-01-20 07:47:20 -05:00
|
|
|
#else
|
|
|
|
(void)fun_len;
|
2014-03-27 19:26:35 -04:00
|
|
|
#endif
|
|
|
|
}
|
2014-08-15 11:45:41 -04:00
|
|
|
#endif
|
2014-03-27 19:26:35 -04:00
|
|
|
|
2014-04-13 06:04:33 -04:00
|
|
|
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) {
|
|
|
|
DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
|
|
|
|
assert(rc != NULL);
|
2014-03-27 19:26:35 -04:00
|
|
|
|
2014-04-11 09:38:30 -04:00
|
|
|
// def_args must be MP_OBJ_NULL or a tuple
|
|
|
|
assert(def_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_args, &mp_type_tuple));
|
|
|
|
|
2014-06-07 17:01:00 -04:00
|
|
|
// def_kw_args must be MP_OBJ_NULL or a dict
|
|
|
|
assert(def_kw_args == MP_OBJ_NULL || MP_OBJ_IS_TYPE(def_kw_args, &mp_type_dict));
|
2014-03-31 10:18:37 -04:00
|
|
|
|
2014-04-13 06:04:33 -04:00
|
|
|
// make the function, depending on the raw code kind
|
2014-03-27 19:26:35 -04:00
|
|
|
mp_obj_t fun;
|
2014-04-13 06:04:33 -04:00
|
|
|
switch (rc->kind) {
|
2014-05-10 08:40:46 -04:00
|
|
|
case MP_CODE_BYTECODE:
|
2015-01-14 16:32:42 -05:00
|
|
|
no_other_choice:
|
2015-10-22 18:45:37 -04:00
|
|
|
fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->data.u_byte.code);
|
2014-03-27 19:26:35 -04:00
|
|
|
break;
|
2014-08-15 11:45:41 -04:00
|
|
|
#if MICROPY_EMIT_NATIVE
|
2014-05-10 08:40:46 -04:00
|
|
|
case MP_CODE_NATIVE_PY:
|
2015-10-22 18:45:37 -04:00
|
|
|
fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->data.u_native.fun_data);
|
2014-03-27 19:26:35 -04:00
|
|
|
break;
|
2014-05-10 08:40:46 -04:00
|
|
|
case MP_CODE_NATIVE_VIPER:
|
2015-01-24 18:14:12 -05:00
|
|
|
fun = mp_obj_new_fun_viper(rc->n_pos_args, rc->data.u_native.fun_data, rc->data.u_native.type_sig);
|
2014-08-15 11:45:41 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if MICROPY_EMIT_INLINE_THUMB
|
2014-05-10 08:40:46 -04:00
|
|
|
case MP_CODE_NATIVE_ASM:
|
2015-01-24 18:14:12 -05:00
|
|
|
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->data.u_native.fun_data);
|
2014-03-27 19:26:35 -04:00
|
|
|
break;
|
2014-08-15 11:45:41 -04:00
|
|
|
#endif
|
2014-03-27 19:26:35 -04:00
|
|
|
default:
|
2014-04-13 06:04:33 -04:00
|
|
|
// raw code was never set (this should not happen)
|
2014-03-27 19:26:35 -04:00
|
|
|
assert(0);
|
2015-01-14 16:32:42 -05:00
|
|
|
goto no_other_choice; // to help flow control analysis
|
2014-03-27 19:26:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for generator functions and if so wrap in generator object
|
2014-04-13 06:04:33 -04:00
|
|
|
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
|
2014-03-27 19:26:35 -04:00
|
|
|
fun = mp_obj_new_gen_wrap(fun);
|
|
|
|
}
|
|
|
|
|
2014-04-06 07:58:40 -04:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2014-08-24 11:28:17 -04:00
|
|
|
mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
|
|
|
|
DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
|
2014-03-27 19:26:35 -04:00
|
|
|
// make function object
|
2014-04-20 12:50:40 -04:00
|
|
|
mp_obj_t ffun;
|
|
|
|
if (n_closed_over & 0x100) {
|
|
|
|
// default positional and keyword args given
|
|
|
|
ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
|
|
|
|
} else {
|
|
|
|
// default positional and keyword args not given
|
|
|
|
ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
|
|
|
|
}
|
2014-03-27 19:26:35 -04:00
|
|
|
// wrap function in closure object
|
2014-04-20 12:50:40 -04:00
|
|
|
return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
|
2014-03-27 19:26:35 -04:00
|
|
|
}
|