2014-03-27 23:26:35 +00:00
|
|
|
// This code glues the code emitters to the runtime.
|
|
|
|
|
2014-03-28 18:38:20 +00:00
|
|
|
#include <stdio.h>
|
2014-03-29 11:39:36 +00:00
|
|
|
#include <string.h>
|
2014-03-27 23:26:35 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime0.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "emitglue.h"
|
2014-03-28 18:38:20 +00:00
|
|
|
#include "bc.h"
|
2014-03-27 23:26:35 +00: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
|
|
|
|
|
2014-03-28 18:38:20 +00:00
|
|
|
#ifdef WRITE_CODE
|
|
|
|
FILE *fp_write_code = NULL;
|
|
|
|
#endif
|
|
|
|
|
2014-03-27 23:26:35 +00:00
|
|
|
void mp_emit_glue_init(void) {
|
2014-03-28 18:38:20 +00:00
|
|
|
#ifdef WRITE_CODE
|
|
|
|
fp_write_code = fopen("out-code", "wb");
|
|
|
|
#endif
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void mp_emit_glue_deinit(void) {
|
2014-03-28 18:38:20 +00:00
|
|
|
#ifdef WRITE_CODE
|
|
|
|
if (fp_write_code != NULL) {
|
|
|
|
fclose(fp_write_code);
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-13 11:04:33 +01: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 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 15:50:52 +01:00
|
|
|
void mp_emit_glue_assign_byte_code(mp_raw_code_t *rc, byte *code, uint len, uint n_pos_args, uint n_kwonly_args, qstr *arg_names, uint scope_flags) {
|
2014-04-13 11:04:33 +01:00
|
|
|
rc->kind = MP_CODE_BYTE;
|
|
|
|
rc->scope_flags = scope_flags;
|
2014-04-27 15:50:52 +01:00
|
|
|
rc->n_pos_args = n_pos_args;
|
|
|
|
rc->n_kwonly_args = n_kwonly_args;
|
|
|
|
rc->arg_names = arg_names;
|
2014-04-13 11:04:33 +01:00
|
|
|
rc->u_byte.code = code;
|
|
|
|
rc->u_byte.len = len;
|
2014-03-27 23:26:35 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2014-04-27 15:50:52 +01:00
|
|
|
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);
|
|
|
|
DEBUG_printf(" arg names:");
|
|
|
|
for (int i = 0; i < n_pos_args + n_kwonly_args; i++) {
|
|
|
|
DEBUG_printf(" %s", qstr_str(arg_names[i]));
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
2014-03-27 23:26:35 +00:00
|
|
|
for (int i = 0; i < 128 && i < len; i++) {
|
|
|
|
if (i > 0 && i % 16 == 0) {
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
|
|
|
DEBUG_printf(" %02x", code[i]);
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
#if MICROPY_DEBUG_PRINTERS
|
|
|
|
mp_byte_code_print(code, len);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:04:33 +01:00
|
|
|
void mp_emit_glue_assign_native_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
|
|
|
|
rc->kind = MP_CODE_NATIVE;
|
|
|
|
rc->scope_flags = 0;
|
2014-04-27 15:50:52 +01:00
|
|
|
rc->n_pos_args = n_args;
|
2014-04-13 11:04:33 +01:00
|
|
|
rc->u_native.fun = fun;
|
2014-03-27 23:26:35 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2014-04-13 11:04:33 +01:00
|
|
|
DEBUG_printf("assign native code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
|
2014-03-27 23:26:35 +00:00
|
|
|
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
|
|
|
|
for (int i = 0; i < 128 && i < len; i++) {
|
|
|
|
if (i > 0 && i % 16 == 0) {
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
|
|
|
DEBUG_printf(" %02x", fun_data[i]);
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
|
|
|
|
#ifdef WRITE_CODE
|
|
|
|
if (fp_write_code != NULL) {
|
|
|
|
fwrite(fun_data, len, 1, fp_write_code);
|
|
|
|
fflush(fp_write_code);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:04:33 +01:00
|
|
|
void mp_emit_glue_assign_inline_asm_code(mp_raw_code_t *rc, void *fun, uint len, int n_args) {
|
|
|
|
rc->kind = MP_CODE_INLINE_ASM;
|
|
|
|
rc->scope_flags = 0;
|
2014-04-27 15:50:52 +01:00
|
|
|
rc->n_pos_args = n_args;
|
2014-04-13 11:04:33 +01:00
|
|
|
rc->u_inline_asm.fun = fun;
|
2014-03-27 23:26:35 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PRINT
|
2014-04-13 11:04:33 +01:00
|
|
|
DEBUG_printf("assign inline asm code: fun=%p len=%u n_args=%d\n", fun, len, n_args);
|
2014-03-27 23:26:35 +00:00
|
|
|
byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code
|
|
|
|
for (int i = 0; i < 128 && i < len; i++) {
|
|
|
|
if (i > 0 && i % 16 == 0) {
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
}
|
|
|
|
DEBUG_printf(" %02x", fun_data[i]);
|
|
|
|
}
|
|
|
|
DEBUG_printf("\n");
|
|
|
|
|
|
|
|
#ifdef WRITE_CODE
|
|
|
|
if (fp_write_code != NULL) {
|
|
|
|
fwrite(fun_data, len, 1, fp_write_code);
|
2014-04-12 17:54:52 +01:00
|
|
|
fflush(fp_write_code);
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-13 11:04:33 +01: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 23:26:35 +00:00
|
|
|
|
2014-04-11 14:38:30 +01: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-03-31 15:18:37 +01:00
|
|
|
// TODO implement default kw args
|
|
|
|
assert(def_kw_args == MP_OBJ_NULL);
|
|
|
|
|
2014-04-13 11:04:33 +01:00
|
|
|
// make the function, depending on the raw code kind
|
2014-03-27 23:26:35 +00:00
|
|
|
mp_obj_t fun;
|
2014-04-13 11:04:33 +01:00
|
|
|
switch (rc->kind) {
|
2014-03-27 23:26:35 +00:00
|
|
|
case MP_CODE_BYTE:
|
2014-04-27 15:50:52 +01:00
|
|
|
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);
|
2014-03-27 23:26:35 +00:00
|
|
|
break;
|
|
|
|
case MP_CODE_NATIVE:
|
2014-04-27 15:50:52 +01:00
|
|
|
fun = mp_make_function_n(rc->n_pos_args, rc->u_native.fun);
|
2014-03-27 23:26:35 +00:00
|
|
|
break;
|
|
|
|
case MP_CODE_INLINE_ASM:
|
2014-04-27 15:50:52 +01:00
|
|
|
fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->u_inline_asm.fun);
|
2014-03-27 23:26:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
2014-04-13 11:04:33 +01:00
|
|
|
// raw code was never set (this should not happen)
|
2014-03-27 23:26:35 +00:00
|
|
|
assert(0);
|
2014-03-29 11:39:36 +00:00
|
|
|
return mp_const_none;
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check for generator functions and if so wrap in generator object
|
2014-04-13 11:04:33 +01:00
|
|
|
if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
|
2014-03-27 23:26:35 +00:00
|
|
|
fun = mp_obj_new_gen_wrap(fun);
|
|
|
|
}
|
|
|
|
|
2014-04-06 12:58:40 +01:00
|
|
|
return fun;
|
|
|
|
}
|
|
|
|
|
2014-04-20 17:50:40 +01:00
|
|
|
mp_obj_t mp_make_closure_from_raw_code(mp_raw_code_t *rc, uint n_closed_over, const mp_obj_t *args) {
|
2014-04-23 00:31:54 +01:00
|
|
|
DEBUG_OP_printf("make_closure_from_raw_code %p %u %p\n", rc, n_closed_over, args);
|
2014-03-27 23:26:35 +00:00
|
|
|
// make function object
|
2014-04-20 17:50:40 +01: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 23:26:35 +00:00
|
|
|
// wrap function in closure object
|
2014-04-20 17:50:40 +01:00
|
|
|
return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
|
2014-03-27 23:26:35 +00:00
|
|
|
}
|