2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_RUNTIME_H
|
|
|
|
#define MICROPY_INCLUDED_PY_RUNTIME_H
|
2014-12-28 20:02:19 -05:00
|
|
|
|
2015-01-01 18:30:53 -05:00
|
|
|
#include "py/mpstate.h"
|
py: Introduce a Python stack for scoped allocation.
This patch introduces the MICROPY_ENABLE_PYSTACK option (disabled by
default) which enables a "Python stack" that allows to allocate and free
memory in a scoped, or Last-In-First-Out (LIFO) way, similar to alloca().
A new memory allocation API is introduced along with this Py-stack. It
includes both "local" and "nonlocal" LIFO allocation. Local allocation is
intended to be equivalent to using alloca(), whereby the same function must
free the memory. Nonlocal allocation is where another function may free
the memory, so long as it's still LIFO.
Follow-up patches will convert all uses of alloca() and VLA to the new
scoped allocation API. The old behaviour (using alloca()) will still be
available, but when MICROPY_ENABLE_PYSTACK is enabled then alloca() is no
longer required or used.
The benefits of enabling this option are (or will be once subsequent
patches are made to convert alloca()/VLA):
- Toolchains without alloca() can use this feature to obtain correct and
efficient scoped memory allocation (compared to using the heap instead
of alloca(), which is slower).
- Even if alloca() is available, enabling the Py-stack gives slightly more
efficient use of stack space when calling nested Python functions, due to
the way that compilers implement alloca().
- Enabling the Py-stack with the stackless mode allows for even more
efficient stack usage, as well as retaining high performance (because the
heap is no longer used to build and destroy stackless code states).
- With Py-stack and stackless enabled, Python-calling-Python is no longer
recursive in the C mp_execute_bytecode function.
The micropython.pystack_use() function is included to measure usage of the
Python stack.
2017-11-26 07:28:40 -05:00
|
|
|
#include "py/pystack.h"
|
2014-05-03 18:27:38 -04:00
|
|
|
|
2014-03-30 16:14:55 -04:00
|
|
|
typedef enum {
|
|
|
|
MP_VM_RETURN_NORMAL,
|
|
|
|
MP_VM_RETURN_YIELD,
|
|
|
|
MP_VM_RETURN_EXCEPTION,
|
|
|
|
} mp_vm_return_kind_t;
|
|
|
|
|
2014-04-19 19:13:22 -04:00
|
|
|
typedef enum {
|
2014-04-26 06:19:17 -04:00
|
|
|
MP_ARG_BOOL = 0x001,
|
|
|
|
MP_ARG_INT = 0x002,
|
|
|
|
MP_ARG_OBJ = 0x003,
|
|
|
|
MP_ARG_KIND_MASK = 0x0ff,
|
|
|
|
MP_ARG_REQUIRED = 0x100,
|
|
|
|
MP_ARG_KW_ONLY = 0x200,
|
|
|
|
} mp_arg_flag_t;
|
|
|
|
|
|
|
|
typedef union _mp_arg_val_t {
|
2014-04-19 19:13:22 -04:00
|
|
|
bool u_bool;
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t u_int;
|
2014-04-19 19:13:22 -04:00
|
|
|
mp_obj_t u_obj;
|
2015-11-27 08:38:15 -05:00
|
|
|
mp_rom_obj_t u_rom_obj;
|
2014-04-26 06:19:17 -04:00
|
|
|
} mp_arg_val_t;
|
2014-04-19 19:13:22 -04:00
|
|
|
|
2014-04-26 06:19:17 -04:00
|
|
|
typedef struct _mp_arg_t {
|
2016-09-22 22:13:51 -04:00
|
|
|
uint16_t qst;
|
|
|
|
uint16_t flags;
|
2014-04-26 06:19:17 -04:00
|
|
|
mp_arg_val_t defval;
|
|
|
|
} mp_arg_t;
|
2014-04-19 19:13:22 -04:00
|
|
|
|
2017-10-03 20:31:05 -04:00
|
|
|
// Tables mapping operator enums to qstrs, defined in objtype.c
|
2017-10-21 04:06:32 -04:00
|
|
|
extern const byte mp_unary_op_method_name[];
|
|
|
|
extern const byte mp_binary_op_method_name[];
|
2015-01-27 13:02:25 -05:00
|
|
|
|
2014-03-30 08:35:08 -04:00
|
|
|
void mp_init(void);
|
|
|
|
void mp_deinit(void);
|
|
|
|
|
2020-02-06 04:25:54 -05:00
|
|
|
void mp_keyboard_interrupt(void);
|
2020-02-05 09:05:47 -05:00
|
|
|
void mp_handle_pending(bool raise_exc);
|
2017-02-16 02:05:06 -05:00
|
|
|
void mp_handle_pending_tail(mp_uint_t atomic_state);
|
|
|
|
|
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
void mp_sched_lock(void);
|
|
|
|
void mp_sched_unlock(void);
|
2020-05-02 03:49:15 -04:00
|
|
|
#define mp_sched_num_pending() (MP_STATE_VM(sched_len))
|
2017-02-16 02:05:06 -05:00
|
|
|
bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
|
|
|
|
#endif
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
// extra printing method specifically for mp_obj_t's which are integral type
|
|
|
|
int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec);
|
|
|
|
|
2018-09-13 23:39:17 -04:00
|
|
|
void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig);
|
|
|
|
static inline void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) {
|
2019-01-25 00:03:05 -05:00
|
|
|
mp_arg_check_num_sig(n_args, n_kw, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw));
|
2018-09-13 23:39:17 -04:00
|
|
|
}
|
2016-03-14 18:35:48 -04:00
|
|
|
void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
|
|
|
void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals);
|
2015-01-01 10:33:50 -05:00
|
|
|
NORETURN void mp_arg_error_terse_mismatch(void);
|
2014-05-06 12:52:35 -04:00
|
|
|
NORETURN void mp_arg_error_unimpl_kw(void);
|
2014-03-30 08:35:08 -04:00
|
|
|
|
2017-03-24 03:43:28 -04:00
|
|
|
static inline mp_obj_dict_t *mp_locals_get(void) {
|
|
|
|
return MP_STATE_THREAD(dict_locals);
|
|
|
|
}
|
|
|
|
static inline void mp_locals_set(mp_obj_dict_t *d) {
|
|
|
|
MP_STATE_THREAD(dict_locals) = d;
|
|
|
|
}
|
|
|
|
static inline mp_obj_dict_t *mp_globals_get(void) {
|
|
|
|
return MP_STATE_THREAD(dict_globals);
|
|
|
|
}
|
|
|
|
static inline void mp_globals_set(mp_obj_dict_t *d) {
|
|
|
|
MP_STATE_THREAD(dict_globals) = d;
|
|
|
|
}
|
2014-03-30 08:35:08 -04:00
|
|
|
|
2015-01-20 06:55:10 -05:00
|
|
|
mp_obj_t mp_load_name(qstr qst);
|
|
|
|
mp_obj_t mp_load_global(qstr qst);
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t mp_load_build_class(void);
|
2015-01-20 06:55:10 -05:00
|
|
|
void mp_store_name(qstr qst, mp_obj_t obj);
|
|
|
|
void mp_store_global(qstr qst, mp_obj_t obj);
|
|
|
|
void mp_delete_name(qstr qst);
|
|
|
|
void mp_delete_global(qstr qst);
|
2014-03-30 08:35:08 -04:00
|
|
|
|
2017-08-28 23:04:01 -04:00
|
|
|
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg);
|
|
|
|
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
|
2014-03-30 08:35:08 -04:00
|
|
|
|
|
|
|
mp_obj_t mp_call_function_0(mp_obj_t fun);
|
|
|
|
mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
|
|
|
|
mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
|
2017-02-15 23:32:34 -05:00
|
|
|
mp_obj_t mp_call_function_n_kw(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
|
|
|
mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
|
|
|
|
mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
|
2016-11-21 17:33:55 -05:00
|
|
|
mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
2016-04-25 12:28:12 -04:00
|
|
|
// Call function and catch/dump exception - for Python callbacks from C code
|
2017-12-04 17:38:41 -05:00
|
|
|
// (return MP_OBJ_NULL in case of exception).
|
|
|
|
mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
|
|
|
|
mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
|
2014-03-30 08:35:08 -04:00
|
|
|
|
2015-04-01 18:31:30 -04:00
|
|
|
typedef struct _mp_call_args_t {
|
2015-03-27 19:14:45 -04:00
|
|
|
mp_obj_t fun;
|
2017-02-15 23:32:34 -05:00
|
|
|
size_t n_args, n_kw, n_alloc;
|
2015-03-27 19:14:45 -04:00
|
|
|
mp_obj_t *args;
|
2015-04-01 18:31:30 -04:00
|
|
|
} mp_call_args_t;
|
2015-03-27 19:14:45 -04:00
|
|
|
|
2015-04-01 18:31:30 -04:00
|
|
|
#if MICROPY_STACKLESS
|
2015-03-27 19:14:45 -04:00
|
|
|
// Takes arguments which are the most general mix of Python arg types, and
|
|
|
|
// prepares argument array suitable for passing to ->call() method of a
|
|
|
|
// function object (and mp_call_function_n_kw()).
|
2015-04-01 18:31:30 -04:00
|
|
|
// (Only needed in stackless mode.)
|
2017-02-15 23:32:34 -05:00
|
|
|
void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args);
|
2015-04-01 18:31:30 -04:00
|
|
|
#endif
|
2015-03-27 19:14:45 -04:00
|
|
|
|
2017-02-15 23:32:34 -05:00
|
|
|
void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
|
|
|
|
void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
|
2014-03-30 08:35:08 -04:00
|
|
|
mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
|
|
|
|
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
|
2015-03-21 10:21:54 -04:00
|
|
|
void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
|
2014-03-30 08:35:08 -04:00
|
|
|
void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
|
2014-03-31 17:57:56 -04:00
|
|
|
void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
|
2018-05-10 09:00:04 -04:00
|
|
|
void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc);
|
2017-04-18 19:45:59 -04:00
|
|
|
void mp_load_super_method(qstr attr, mp_obj_t *dest);
|
2014-03-30 08:35:08 -04:00
|
|
|
void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
|
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf);
|
2014-04-17 18:19:36 -04:00
|
|
|
mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
|
|
|
|
mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
|
2014-03-30 16:14:55 -04:00
|
|
|
mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val);
|
2014-03-30 08:35:08 -04:00
|
|
|
|
|
|
|
mp_obj_t mp_make_raise_obj(mp_obj_t o);
|
|
|
|
|
|
|
|
mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
|
|
|
|
mp_obj_t mp_import_from(mp_obj_t module, qstr name);
|
|
|
|
void mp_import_all(mp_obj_t module);
|
2014-04-12 23:43:18 -04:00
|
|
|
|
2021-04-21 22:13:58 -04:00
|
|
|
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
|
|
|
|
NORETURN void mp_raise_type(const mp_obj_type_t *exc_type);
|
|
|
|
NORETURN void mp_raise_ValueError_no_msg(void);
|
|
|
|
NORETURN void mp_raise_TypeError_no_msg(void);
|
|
|
|
NORETURN void mp_raise_NotImplementedError_no_msg(void);
|
|
|
|
#define mp_raise_msg(exc_type, msg) mp_raise_type(exc_type)
|
|
|
|
#define mp_raise_msg_varg(exc_type, ...) mp_raise_type(exc_type)
|
|
|
|
#define mp_raise_ValueError(msg) mp_raise_ValueError_no_msg()
|
|
|
|
#define mp_raise_TypeError(msg) mp_raise_TypeError_no_msg()
|
|
|
|
#define mp_raise_NotImplementedError(msg) mp_raise_NotImplementedError_no_msg()
|
|
|
|
#else
|
2020-02-10 21:17:41 -05:00
|
|
|
#define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL)
|
2020-01-28 22:27:33 -05:00
|
|
|
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
|
|
|
|
NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...);
|
|
|
|
NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg);
|
|
|
|
NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg);
|
|
|
|
NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg);
|
2021-04-21 22:13:58 -04:00
|
|
|
#endif
|
|
|
|
|
2016-10-06 22:31:59 -04:00
|
|
|
NORETURN void mp_raise_OSError(int errno_);
|
2017-11-20 01:29:58 -05:00
|
|
|
NORETURN void mp_raise_recursion_depth(void);
|
2014-06-07 16:22:41 -04:00
|
|
|
|
2016-08-12 14:58:56 -04:00
|
|
|
#if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
|
|
|
|
#undef mp_check_self
|
|
|
|
#define mp_check_self(pred)
|
|
|
|
#else
|
|
|
|
// A port may define to raise TypeError for example
|
|
|
|
#ifndef mp_check_self
|
|
|
|
#define mp_check_self(pred) assert(pred)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-08-16 17:06:11 -04:00
|
|
|
// helper functions for native/viper code
|
2019-03-08 18:16:09 -05:00
|
|
|
int mp_native_type_from_qstr(qstr qst);
|
2019-03-08 18:22:09 -05:00
|
|
|
mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type);
|
|
|
|
mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type);
|
2014-08-15 18:47:59 -04:00
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
#define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj)))
|
|
|
|
#define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
|
2014-12-28 20:02:19 -05:00
|
|
|
|
2015-01-01 02:29:28 -05:00
|
|
|
#if MICROPY_WARNINGS
|
2018-12-21 06:20:55 -05:00
|
|
|
#ifndef mp_warning
|
|
|
|
void mp_warning(const char *category, const char *msg, ...);
|
|
|
|
#endif
|
2015-01-01 02:29:28 -05:00
|
|
|
#else
|
2017-09-13 06:33:55 -04:00
|
|
|
#define mp_warning(...)
|
2015-01-01 02:29:28 -05:00
|
|
|
#endif
|
|
|
|
|
all: Unify header guard usage.
The code conventions suggest using header guards, but do not define how
those should look like and instead point to existing files. However, not
all existing files follow the same scheme, sometimes omitting header guards
altogether, sometimes using non-standard names, making it easy to
accidentally pick a "wrong" example.
This commit ensures that all header files of the MicroPython project (that
were not simply copied from somewhere else) follow the same pattern, that
was already present in the majority of files, especially in the py folder.
The rules are as follows.
Naming convention:
* start with the words MICROPY_INCLUDED
* contain the full path to the file
* replace special characters with _
In addition, there are no empty lines before #ifndef, between #ifndef and
one empty line before #endif. #endif is followed by a comment containing
the name of the guard macro.
py/grammar.h cannot use header guards by design, since it has to be
included multiple times in a single C file. Several other files also do not
need header guards as they are only used internally and guaranteed to be
included only once:
* MICROPY_MPHALPORT_H
* mpconfigboard.h
* mpconfigport.h
* mpthreadport.h
* pin_defs_*.h
* qstrdefs*.h
2017-06-29 17:14:58 -04:00
|
|
|
#endif // MICROPY_INCLUDED_PY_RUNTIME_H
|