py/nativeglue: Add new header file with native function table typedef.

pull/1/head
Damien George 2019-10-07 11:56:24 +11:00
parent b47e155bd0
commit 360d972c16
7 changed files with 150 additions and 65 deletions

View File

@ -47,7 +47,7 @@
#include <assert.h>
#include "py/emit.h"
#include "py/bc.h"
#include "py/nativeglue.h"
#include "py/objstr.h"
#if MICROPY_DEBUG_VERBOSE // print debugging info
@ -687,7 +687,7 @@ STATIC void emit_native_end_pass(emit_t *emit) {
#if !MICROPY_DYNAMIC_COMPILER
// Store mp_fun_table pointer just after qstrs
// (but in dynamic-compiler mode eliminate dependency on mp_fun_table)
emit->const_table[nqstr] = (mp_uint_t)(uintptr_t)mp_fun_table;
emit->const_table[nqstr] = (mp_uint_t)(uintptr_t)&mp_fun_table;
#endif
#if MICROPY_PERSISTENT_CODE_SAVE

View File

@ -1,7 +1,7 @@
// x86 specific stuff
#include "py/mpconfig.h"
#include "py/runtime0.h"
#include "py/nativeglue.h"
#if MICROPY_EMIT_X86

View File

@ -30,8 +30,7 @@
#include "py/runtime.h"
#include "py/smallint.h"
#include "py/emitglue.h"
#include "py/bc.h"
#include "py/nativeglue.h"
#if MICROPY_DEBUG_VERBOSE // print debugging info
#define DEBUG_printf DEBUG_printf
@ -211,7 +210,7 @@ STATIC bool mp_native_yield_from(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *re
}
// these must correspond to the respective enum in runtime0.h
const void *const mp_fun_table[MP_F_NUMBER_OF] = {
const mp_fun_table_t mp_fun_table = {
&mp_const_none_obj,
&mp_const_false_obj,
&mp_const_true_obj,

141
py/nativeglue.h 100644
View File

@ -0,0 +1,141 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2019 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.
*/
#ifndef MICROPY_INCLUDED_PY_NATIVEGLUE_H
#define MICROPY_INCLUDED_PY_NATIVEGLUE_H
#include "py/obj.h"
#include "py/persistentcode.h"
typedef enum {
MP_F_CONST_NONE_OBJ = 0,
MP_F_CONST_FALSE_OBJ,
MP_F_CONST_TRUE_OBJ,
MP_F_CONVERT_OBJ_TO_NATIVE,
MP_F_CONVERT_NATIVE_TO_OBJ,
MP_F_NATIVE_SWAP_GLOBALS,
MP_F_LOAD_NAME,
MP_F_LOAD_GLOBAL,
MP_F_LOAD_BUILD_CLASS,
MP_F_LOAD_ATTR,
MP_F_LOAD_METHOD,
MP_F_LOAD_SUPER_METHOD,
MP_F_STORE_NAME,
MP_F_STORE_GLOBAL,
MP_F_STORE_ATTR,
MP_F_OBJ_SUBSCR,
MP_F_OBJ_IS_TRUE,
MP_F_UNARY_OP,
MP_F_BINARY_OP,
MP_F_BUILD_TUPLE,
MP_F_BUILD_LIST,
MP_F_BUILD_MAP,
MP_F_BUILD_SET,
MP_F_STORE_SET,
MP_F_LIST_APPEND,
MP_F_STORE_MAP,
MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
MP_F_NATIVE_CALL_FUNCTION_N_KW,
MP_F_CALL_METHOD_N_KW,
MP_F_CALL_METHOD_N_KW_VAR,
MP_F_NATIVE_GETITER,
MP_F_NATIVE_ITERNEXT,
MP_F_NLR_PUSH,
MP_F_NLR_POP,
MP_F_NATIVE_RAISE,
MP_F_IMPORT_NAME,
MP_F_IMPORT_FROM,
MP_F_IMPORT_ALL,
MP_F_NEW_SLICE,
MP_F_UNPACK_SEQUENCE,
MP_F_UNPACK_EX,
MP_F_DELETE_NAME,
MP_F_DELETE_GLOBAL,
MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
MP_F_ARG_CHECK_NUM_SIG,
MP_F_SETUP_CODE_STATE,
MP_F_SMALL_INT_FLOOR_DIVIDE,
MP_F_SMALL_INT_MODULO,
MP_F_NATIVE_YIELD_FROM,
MP_F_SETJMP,
MP_F_NUMBER_OF,
} mp_fun_kind_t;
typedef struct _mp_fun_table_t {
mp_const_obj_t const_none;
mp_const_obj_t const_false;
mp_const_obj_t const_true;
mp_uint_t (*native_from_obj)(mp_obj_t obj, mp_uint_t type);
mp_obj_t (*native_to_obj)(mp_uint_t val, mp_uint_t type);
mp_obj_dict_t *(*swap_globals)(mp_obj_dict_t *new_globals);
mp_obj_t (*load_name)(qstr qst);
mp_obj_t (*load_global)(qstr qst);
mp_obj_t (*load_build_class)(void);
mp_obj_t (*load_attr)(mp_obj_t base, qstr attr);
void (*load_method)(mp_obj_t base, qstr attr, mp_obj_t *dest);
void (*load_super_method)(qstr attr, mp_obj_t *dest);
void (*store_name)(qstr qst, mp_obj_t obj);
void (*store_global)(qstr qst, mp_obj_t obj);
void (*store_attr)(mp_obj_t base, qstr attr, mp_obj_t val);
mp_obj_t (*obj_subscr)(mp_obj_t base, mp_obj_t index, mp_obj_t val);
bool (*obj_is_true)(mp_obj_t arg);
mp_obj_t (*unary_op)(mp_unary_op_t op, mp_obj_t arg);
mp_obj_t (*binary_op)(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
mp_obj_t (*new_tuple)(size_t n, const mp_obj_t *items);
mp_obj_t (*new_list)(size_t n, mp_obj_t *items);
mp_obj_t (*new_dict)(size_t n_args);
mp_obj_t (*new_set)(size_t n_args, mp_obj_t *items);
void (*set_store)(mp_obj_t self_in, mp_obj_t item);
mp_obj_t (*list_append)(mp_obj_t self_in, mp_obj_t arg);
mp_obj_t (*dict_store)(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
mp_obj_t (*make_function_from_raw_code)(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args);
mp_obj_t (*call_function_n_kw)(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args);
mp_obj_t (*call_method_n_kw)(size_t n_args, size_t n_kw, const mp_obj_t *args);
mp_obj_t (*call_method_n_kw_var)(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
mp_obj_t (*getiter)(mp_obj_t obj, mp_obj_iter_buf_t *iter);
mp_obj_t (*iternext)(mp_obj_iter_buf_t *iter);
unsigned int (*nlr_push)(nlr_buf_t *);
void (*nlr_pop)(void);
void (*raise)(mp_obj_t o);
mp_obj_t (*import_name)(qstr name, mp_obj_t fromlist, mp_obj_t level);
mp_obj_t (*import_from)(mp_obj_t module, qstr name);
void (*import_all)(mp_obj_t module);
mp_obj_t (*new_slice)(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
void (*unpack_sequence)(mp_obj_t seq, size_t num, mp_obj_t *items);
void (*unpack_ex)(mp_obj_t seq, size_t num, mp_obj_t *items);
void (*delete_name)(qstr qst);
void (*delete_global)(qstr qst);
mp_obj_t (*make_closure_from_raw_code)(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args);
void (*arg_check_num_sig)(size_t n_args, size_t n_kw, uint32_t sig);
void (*setup_code_state)(mp_code_state_t *code_state, size_t n_args, size_t n_kw, const mp_obj_t *args);
mp_int_t (*small_int_floor_divide)(mp_int_t num, mp_int_t denom);
mp_int_t (*small_int_modulo)(mp_int_t dividend, mp_int_t divisor);
bool (*yield_from)(mp_obj_t gen, mp_obj_t send_value, mp_obj_t *ret_value);
void *setjmp;
} mp_fun_table_t;
extern const mp_fun_table_t mp_fun_table;
#endif // MICROPY_INCLUDED_PY_NATIVEGLUE_H

View File

@ -30,7 +30,7 @@
#include <assert.h>
#include "py/reader.h"
#include "py/emitglue.h"
#include "py/nativeglue.h"
#include "py/persistentcode.h"
#include "py/bc0.h"
#include "py/objstr.h"
@ -453,7 +453,7 @@ STATIC mp_raw_code_t *load_raw_code(mp_reader_t *reader, qstr_window_t *qw) {
#if MICROPY_EMIT_MACHINE_CODE
if (kind != MP_CODE_BYTECODE) {
// Populate mp_fun_table entry
*ct++ = (mp_uint_t)(uintptr_t)mp_fun_table;
*ct++ = (mp_uint_t)(uintptr_t)&mp_fun_table;
}
#endif

View File

@ -156,60 +156,4 @@ typedef enum {
MP_BINARY_OP_IS_NOT,
} mp_binary_op_t;
typedef enum {
MP_F_CONST_NONE_OBJ = 0,
MP_F_CONST_FALSE_OBJ,
MP_F_CONST_TRUE_OBJ,
MP_F_CONVERT_OBJ_TO_NATIVE,
MP_F_CONVERT_NATIVE_TO_OBJ,
MP_F_NATIVE_SWAP_GLOBALS,
MP_F_LOAD_NAME,
MP_F_LOAD_GLOBAL,
MP_F_LOAD_BUILD_CLASS,
MP_F_LOAD_ATTR,
MP_F_LOAD_METHOD,
MP_F_LOAD_SUPER_METHOD,
MP_F_STORE_NAME,
MP_F_STORE_GLOBAL,
MP_F_STORE_ATTR,
MP_F_OBJ_SUBSCR,
MP_F_OBJ_IS_TRUE,
MP_F_UNARY_OP,
MP_F_BINARY_OP,
MP_F_BUILD_TUPLE,
MP_F_BUILD_LIST,
MP_F_BUILD_MAP,
MP_F_BUILD_SET,
MP_F_STORE_SET,
MP_F_LIST_APPEND,
MP_F_STORE_MAP,
MP_F_MAKE_FUNCTION_FROM_RAW_CODE,
MP_F_NATIVE_CALL_FUNCTION_N_KW,
MP_F_CALL_METHOD_N_KW,
MP_F_CALL_METHOD_N_KW_VAR,
MP_F_NATIVE_GETITER,
MP_F_NATIVE_ITERNEXT,
MP_F_NLR_PUSH,
MP_F_NLR_POP,
MP_F_NATIVE_RAISE,
MP_F_IMPORT_NAME,
MP_F_IMPORT_FROM,
MP_F_IMPORT_ALL,
MP_F_NEW_SLICE,
MP_F_UNPACK_SEQUENCE,
MP_F_UNPACK_EX,
MP_F_DELETE_NAME,
MP_F_DELETE_GLOBAL,
MP_F_MAKE_CLOSURE_FROM_RAW_CODE,
MP_F_ARG_CHECK_NUM_SIG,
MP_F_SETUP_CODE_STATE,
MP_F_SMALL_INT_FLOOR_DIVIDE,
MP_F_SMALL_INT_MODULO,
MP_F_NATIVE_YIELD_FROM,
MP_F_SETJMP,
MP_F_NUMBER_OF,
} mp_fun_kind_t;
extern const void *const mp_fun_table[MP_F_NUMBER_OF];
#endif // MICROPY_INCLUDED_PY_RUNTIME0_H

View File

@ -314,7 +314,7 @@ class RawCode(object):
print(' MP_ROM_QSTR(%s),' % global_qstrs[qst].qstr_id)
for i in range(len(self.objs)):
if self.objs[i] is MPFunTable:
print(' mp_fun_table,')
print(' &mp_fun_table,')
elif type(self.objs[i]) is float:
print('#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A || MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B')
print(' MP_ROM_PTR(&const_obj_%s_%u),' % (self.escaped_name, i))
@ -711,6 +711,7 @@ def freeze_mpy(base_qstrs, raw_codes):
print('#include "py/objint.h"')
print('#include "py/objstr.h"')
print('#include "py/emitglue.h"')
print('#include "py/nativeglue.h"')
print()
print('#if MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE != %u' % config.MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE)