2014-05-03 23:27:38 +01:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 23:27:38 +01: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 22:14:58 +01:00
|
|
|
#ifndef MICROPY_INCLUDED_PY_MISC_H
|
|
|
|
#define MICROPY_INCLUDED_PY_MISC_H
|
2014-05-03 23:27:38 +01:00
|
|
|
|
2013-10-04 19:53:11 +01:00
|
|
|
// a mini library of useful types and functions
|
|
|
|
|
|
|
|
/** types *******************************************************/
|
|
|
|
|
2014-01-06 21:51:53 +00:00
|
|
|
#include <stdbool.h>
|
2015-12-07 18:08:07 +00:00
|
|
|
#include <stdint.h>
|
2014-09-28 08:54:35 +01:00
|
|
|
#include <stddef.h>
|
2013-10-04 19:53:11 +01:00
|
|
|
|
|
|
|
typedef unsigned char byte;
|
|
|
|
typedef unsigned int uint;
|
|
|
|
|
2014-02-04 22:44:55 +00:00
|
|
|
/** generic ops *************************************************/
|
|
|
|
|
2014-02-05 23:57:48 +00:00
|
|
|
#ifndef MIN
|
2014-02-04 22:44:55 +00:00
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
2014-02-05 23:57:48 +00:00
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
2014-02-04 22:44:55 +00:00
|
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
2014-02-05 23:57:48 +00:00
|
|
|
#endif
|
2014-02-04 22:44:55 +00:00
|
|
|
|
2016-12-26 22:00:12 +00:00
|
|
|
// Classical double-indirection stringification of preprocessor macro's value
|
2019-05-09 08:11:33 +01:00
|
|
|
#define MP_STRINGIFY_HELPER(x) #x
|
|
|
|
#define MP_STRINGIFY(x) MP_STRINGIFY_HELPER(x)
|
2016-12-26 22:00:12 +00:00
|
|
|
|
2018-05-18 04:10:31 +01:00
|
|
|
// Static assertion macro
|
|
|
|
#define MP_STATIC_ASSERT(cond) ((void)sizeof(char[1 - 2 * !(cond)]))
|
2023-11-13 15:04:40 +00:00
|
|
|
// In C++ things like comparing extern const pointers are not constant-expressions so cannot be used
|
|
|
|
// in MP_STATIC_ASSERT. Note that not all possible compiler versions will reject this. Some gcc versions
|
|
|
|
// do, others only with -Werror=vla, msvc always does.
|
|
|
|
// The (void) is needed to avoid "left operand of comma operator has no effect [-Werror=unused-value]"
|
|
|
|
// when using this macro on the left-hand side of a comma.
|
|
|
|
#if defined(_MSC_VER) || defined(__cplusplus)
|
|
|
|
#define MP_STATIC_ASSERT_NONCONSTEXPR(cond) ((void)1)
|
2022-07-16 14:47:41 +01:00
|
|
|
#else
|
2023-11-13 15:04:40 +00:00
|
|
|
#define MP_STATIC_ASSERT_NONCONSTEXPR(cond) MP_STATIC_ASSERT(cond)
|
2022-07-16 14:47:41 +01:00
|
|
|
#endif
|
2018-05-18 04:10:31 +01:00
|
|
|
|
2020-10-15 14:15:16 +01:00
|
|
|
// Round-up integer division
|
|
|
|
#define MP_CEIL_DIVIDE(a, b) (((a) + (b) - 1) / (b))
|
2021-08-09 16:09:31 +01:00
|
|
|
#define MP_ROUND_DIVIDE(a, b) (((a) + (b) / 2) / (b))
|
2020-10-15 14:15:16 +01:00
|
|
|
|
2016-12-26 21:55:42 +00:00
|
|
|
/** memory allocation ******************************************/
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2013-12-29 19:33:23 +00:00
|
|
|
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
|
|
|
|
|
2013-10-04 19:53:11 +01:00
|
|
|
#define m_new(type, num) ((type *)(m_malloc(sizeof(type) * (num))))
|
2014-05-10 17:36:41 +01:00
|
|
|
#define m_new_maybe(type, num) ((type *)(m_malloc_maybe(sizeof(type) * (num))))
|
2013-10-04 19:53:11 +01:00
|
|
|
#define m_new0(type, num) ((type *)(m_malloc0(sizeof(type) * (num))))
|
2013-12-21 18:17:45 +00:00
|
|
|
#define m_new_obj(type) (m_new(type, 1))
|
2014-10-09 16:53:37 +01:00
|
|
|
#define m_new_obj_maybe(type) (m_new_maybe(type, 1))
|
2023-11-03 03:19:55 +00:00
|
|
|
#define m_new_obj_var(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
|
|
|
|
#define m_new_obj_var0(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc0(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
|
|
|
|
#define m_new_obj_var_maybe(obj_type, var_field, var_type, var_num) ((obj_type *)m_malloc_maybe(offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
|
2014-04-05 20:35:48 +01:00
|
|
|
#if MICROPY_ENABLE_FINALISER
|
|
|
|
#define m_new_obj_with_finaliser(type) ((type *)(m_malloc_with_finaliser(sizeof(type))))
|
2023-11-03 03:19:55 +00:00
|
|
|
#define m_new_obj_var_with_finaliser(type, var_field, var_type, var_num) ((type *)m_malloc_with_finaliser(offsetof(type, var_field) + sizeof(var_type) * (var_num)))
|
2014-04-05 20:35:48 +01:00
|
|
|
#else
|
|
|
|
#define m_new_obj_with_finaliser(type) m_new_obj(type)
|
2023-11-03 03:19:55 +00:00
|
|
|
#define m_new_obj_var_with_finaliser(type, var_field, var_type, var_num) m_new_obj_var(type, var_field, var_type, var_num)
|
2014-04-05 20:35:48 +01:00
|
|
|
#endif
|
2015-02-27 09:54:12 +00:00
|
|
|
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
|
2013-12-29 19:33:23 +00:00
|
|
|
#define m_renew(type, ptr, old_num, new_num) ((type *)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
|
2015-06-13 21:53:22 +01:00
|
|
|
#define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type *)(m_realloc_maybe((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num), (allow_move))))
|
2013-12-29 19:33:23 +00:00
|
|
|
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
|
2023-11-03 03:19:55 +00:00
|
|
|
#define m_del_var(obj_type, var_field, var_type, var_num, ptr) (m_free(ptr, offsetof(obj_type, var_field) + sizeof(var_type) * (var_num)))
|
2015-02-27 09:54:12 +00:00
|
|
|
#else
|
|
|
|
#define m_renew(type, ptr, old_num, new_num) ((type *)(m_realloc((ptr), sizeof(type) * (new_num))))
|
2015-06-13 21:53:22 +01:00
|
|
|
#define m_renew_maybe(type, ptr, old_num, new_num, allow_move) ((type *)(m_realloc_maybe((ptr), sizeof(type) * (new_num), (allow_move))))
|
2015-02-27 09:54:12 +00:00
|
|
|
#define m_del(type, ptr, num) ((void)(num), m_free(ptr))
|
2023-11-03 03:19:55 +00:00
|
|
|
#define m_del_var(obj_type, var_field, var_type, var_num, ptr) ((void)(var_num), m_free(ptr))
|
2015-02-27 09:54:12 +00:00
|
|
|
#endif
|
|
|
|
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2014-09-23 18:10:17 +01:00
|
|
|
void *m_malloc(size_t num_bytes);
|
|
|
|
void *m_malloc_maybe(size_t num_bytes);
|
|
|
|
void *m_malloc_with_finaliser(size_t num_bytes);
|
|
|
|
void *m_malloc0(size_t num_bytes);
|
2015-02-27 09:54:12 +00:00
|
|
|
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
|
2014-09-23 18:10:17 +01:00
|
|
|
void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes);
|
2015-06-13 21:53:22 +01:00
|
|
|
void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move);
|
2014-09-23 18:10:17 +01:00
|
|
|
void m_free(void *ptr, size_t num_bytes);
|
2015-02-27 09:54:12 +00:00
|
|
|
#else
|
|
|
|
void *m_realloc(void *ptr, size_t new_num_bytes);
|
2015-06-13 21:53:22 +01:00
|
|
|
void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move);
|
2015-02-27 09:54:12 +00:00
|
|
|
void m_free(void *ptr);
|
|
|
|
#endif
|
2017-08-31 08:00:14 +01:00
|
|
|
NORETURN void m_malloc_fail(size_t num_bytes);
|
2014-09-23 18:10:17 +01:00
|
|
|
|
2022-05-04 03:12:11 +01:00
|
|
|
#if MICROPY_TRACKED_ALLOC
|
|
|
|
// These alloc/free functions track the pointers in a linked list so the GC does not reclaim
|
|
|
|
// them. They can be used by code that requires traditional C malloc/free semantics.
|
|
|
|
void *m_tracked_calloc(size_t nmemb, size_t size);
|
|
|
|
void m_tracked_free(void *ptr_in);
|
|
|
|
#endif
|
|
|
|
|
2014-09-23 18:10:17 +01:00
|
|
|
#if MICROPY_MEM_STATS
|
|
|
|
size_t m_get_total_bytes_allocated(void);
|
|
|
|
size_t m_get_current_bytes_allocated(void);
|
|
|
|
size_t m_get_peak_bytes_allocated(void);
|
|
|
|
#endif
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2014-04-26 10:47:29 +01:00
|
|
|
/** array helpers ***********************************************/
|
|
|
|
|
|
|
|
// get the number of elements in a fixed-size array
|
2014-06-19 17:54:34 +01:00
|
|
|
#define MP_ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
|
2014-04-26 10:47:29 +01:00
|
|
|
|
2014-07-12 13:55:47 +01:00
|
|
|
// align ptr to the nearest multiple of "alignment"
|
2015-11-27 17:01:44 +00:00
|
|
|
#define MP_ALIGN(ptr, alignment) (void *)(((uintptr_t)(ptr) + ((alignment) - 1)) & ~((alignment) - 1))
|
2014-07-12 13:55:47 +01:00
|
|
|
|
2013-10-04 19:53:11 +01:00
|
|
|
/** unichar / UTF-8 *********************************************/
|
|
|
|
|
2015-01-28 14:07:11 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
|
|
|
// with unicode enabled we need a type which can fit chars up to 0x10ffff
|
|
|
|
typedef uint32_t unichar;
|
|
|
|
#else
|
|
|
|
// without unicode enabled we can only need to fit chars up to 0xff
|
|
|
|
// (on 16-bit archs uint is 16-bits and more efficient than uint32_t)
|
|
|
|
typedef uint unichar;
|
|
|
|
#endif
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2018-02-14 07:19:22 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
2014-06-14 04:18:34 +01:00
|
|
|
unichar utf8_get_char(const byte *s);
|
|
|
|
const byte *utf8_next_char(const byte *s);
|
2018-02-14 07:19:22 +00:00
|
|
|
size_t utf8_charlen(const byte *str, size_t len);
|
|
|
|
#else
|
|
|
|
static inline unichar utf8_get_char(const byte *s) {
|
|
|
|
return *s;
|
|
|
|
}
|
|
|
|
static inline const byte *utf8_next_char(const byte *s) {
|
|
|
|
return s + 1;
|
|
|
|
}
|
|
|
|
static inline size_t utf8_charlen(const byte *str, size_t len) {
|
|
|
|
(void)str;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2014-01-06 21:51:53 +00:00
|
|
|
bool unichar_isspace(unichar c);
|
|
|
|
bool unichar_isalpha(unichar c);
|
|
|
|
bool unichar_isprint(unichar c);
|
|
|
|
bool unichar_isdigit(unichar c);
|
2014-01-22 20:40:02 +00:00
|
|
|
bool unichar_isxdigit(unichar c);
|
2016-02-16 13:36:18 +00:00
|
|
|
bool unichar_isident(unichar c);
|
2019-12-14 12:38:15 +00:00
|
|
|
bool unichar_isalnum(unichar c);
|
2014-05-31 07:30:03 +01:00
|
|
|
bool unichar_isupper(unichar c);
|
|
|
|
bool unichar_islower(unichar c);
|
2014-05-10 17:47:41 +01:00
|
|
|
unichar unichar_tolower(unichar c);
|
|
|
|
unichar unichar_toupper(unichar c);
|
2015-05-18 22:41:25 +01:00
|
|
|
mp_uint_t unichar_xdigit_value(unichar c);
|
2014-06-03 20:28:12 +01:00
|
|
|
#define UTF8_IS_NONASCII(ch) ((ch) & 0x80)
|
|
|
|
#define UTF8_IS_CONT(ch) (((ch) & 0xC0) == 0x80)
|
2013-10-04 19:53:11 +01:00
|
|
|
|
|
|
|
/** variable string *********************************************/
|
|
|
|
|
2013-10-20 14:39:58 +01:00
|
|
|
typedef struct _vstr_t {
|
2014-09-23 18:10:17 +01:00
|
|
|
size_t alloc;
|
|
|
|
size_t len;
|
2013-10-20 14:39:58 +01:00
|
|
|
char *buf;
|
2022-10-08 23:32:22 +01:00
|
|
|
bool fixed_buf;
|
2013-10-20 14:39:58 +01:00
|
|
|
} vstr_t;
|
|
|
|
|
2014-02-06 21:11:19 +00:00
|
|
|
// convenience macro to declare a vstr with a fixed size buffer on the stack
|
|
|
|
#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
|
|
|
|
|
2014-09-23 18:10:17 +01:00
|
|
|
void vstr_init(vstr_t *vstr, size_t alloc);
|
2015-01-21 22:48:37 +00:00
|
|
|
void vstr_init_len(vstr_t *vstr, size_t len);
|
2014-09-23 18:10:17 +01:00
|
|
|
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf);
|
2015-04-09 23:56:15 +01:00
|
|
|
struct _mp_print_t;
|
|
|
|
void vstr_init_print(vstr_t *vstr, size_t alloc, struct _mp_print_t *print);
|
2013-10-20 14:39:58 +01:00
|
|
|
void vstr_clear(vstr_t *vstr);
|
2016-10-14 06:46:34 +01:00
|
|
|
vstr_t *vstr_new(size_t alloc);
|
2013-10-04 19:53:11 +01:00
|
|
|
void vstr_free(vstr_t *vstr);
|
py/vstr: Remove vstr.had_error flag and inline basic vstr functions.
The vstr.had_error flag was a relic from the very early days which assumed
that the malloc functions (eg m_new, m_renew) returned NULL if they failed
to allocate. But that's no longer the case: these functions will raise an
exception if they fail.
Since it was impossible for had_error to be set, this patch introduces no
change in behaviour.
An alternative option would be to change the malloc calls to the _maybe
variants, which return NULL instead of raising, but then a lot of code
will need to explicitly check if the vstr had an error and raise if it
did.
The code-size savings for this patch are, in bytes: bare-arm:188,
minimal:456, unix(NDEBUG,x86-64):368, stmhal:228, esp8266:360.
2016-09-19 02:17:02 +01:00
|
|
|
static inline void vstr_reset(vstr_t *vstr) {
|
|
|
|
vstr->len = 0;
|
|
|
|
}
|
|
|
|
static inline char *vstr_str(vstr_t *vstr) {
|
|
|
|
return vstr->buf;
|
|
|
|
}
|
|
|
|
static inline size_t vstr_len(vstr_t *vstr) {
|
|
|
|
return vstr->len;
|
|
|
|
}
|
2014-09-23 18:10:17 +01:00
|
|
|
void vstr_hint_size(vstr_t *vstr, size_t size);
|
|
|
|
char *vstr_extend(vstr_t *vstr, size_t size);
|
|
|
|
char *vstr_add_len(vstr_t *vstr, size_t len);
|
2015-01-29 13:57:23 +00:00
|
|
|
char *vstr_null_terminated_str(vstr_t *vstr);
|
2013-10-20 14:39:58 +01:00
|
|
|
void vstr_add_byte(vstr_t *vstr, byte v);
|
|
|
|
void vstr_add_char(vstr_t *vstr, unichar chr);
|
2013-10-04 19:53:11 +01:00
|
|
|
void vstr_add_str(vstr_t *vstr, const char *str);
|
2014-09-23 18:10:17 +01:00
|
|
|
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len);
|
|
|
|
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b);
|
|
|
|
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr);
|
|
|
|
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut);
|
|
|
|
void vstr_cut_tail_bytes(vstr_t *vstr, size_t bytes_to_cut);
|
|
|
|
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut);
|
2013-11-03 18:20:56 +00:00
|
|
|
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
|
2013-11-03 18:30:10 +00:00
|
|
|
|
2014-02-04 22:47:06 +00:00
|
|
|
/** non-dynamic size-bounded variable buffer/string *************/
|
|
|
|
|
2014-09-23 18:10:17 +01:00
|
|
|
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; size_t buf##_len = max_size; char *buf##_p = buf;
|
2014-02-05 23:57:48 +00:00
|
|
|
#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
|
2014-02-04 22:47:06 +00:00
|
|
|
#define CHECKBUF_APPEND(buf, src, src_len) \
|
2014-09-23 18:10:17 +01:00
|
|
|
{ size_t l = MIN(src_len, buf##_len); \
|
2014-02-04 22:47:06 +00:00
|
|
|
memcpy(buf##_p, src, l); \
|
|
|
|
buf##_len -= l; \
|
|
|
|
buf##_p += l; }
|
|
|
|
#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
|
|
|
|
#define CHECKBUF_LEN(buf) (buf##_p - buf)
|
|
|
|
|
2013-11-03 18:30:10 +00:00
|
|
|
#ifdef va_start
|
2013-11-03 18:20:56 +00:00
|
|
|
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
|
2013-11-03 18:30:10 +00:00
|
|
|
#endif
|
2013-10-04 19:53:11 +01:00
|
|
|
|
2014-02-16 16:11:42 +00:00
|
|
|
// Debugging helpers
|
|
|
|
int DEBUG_printf(const char *fmt, ...);
|
|
|
|
|
2014-09-23 18:10:17 +01:00
|
|
|
extern mp_uint_t mp_verbose_flag;
|
2014-05-04 22:50:05 +01:00
|
|
|
|
2015-01-13 15:19:37 +00:00
|
|
|
/** float internals *************/
|
|
|
|
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2020-02-14 01:26:46 +00:00
|
|
|
|
2015-01-13 15:19:37 +00:00
|
|
|
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
|
|
|
|
#define MP_FLOAT_EXP_BITS (11)
|
2022-07-28 15:49:18 +01:00
|
|
|
#define MP_FLOAT_EXP_OFFSET (1023)
|
2015-01-13 15:19:37 +00:00
|
|
|
#define MP_FLOAT_FRAC_BITS (52)
|
2020-02-14 01:26:46 +00:00
|
|
|
typedef uint64_t mp_float_uint_t;
|
2015-01-13 15:19:37 +00:00
|
|
|
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
|
|
|
|
#define MP_FLOAT_EXP_BITS (8)
|
2022-07-28 15:49:18 +01:00
|
|
|
#define MP_FLOAT_EXP_OFFSET (127)
|
2015-01-13 15:19:37 +00:00
|
|
|
#define MP_FLOAT_FRAC_BITS (23)
|
2020-02-14 01:26:46 +00:00
|
|
|
typedef uint32_t mp_float_uint_t;
|
2015-01-13 15:19:37 +00:00
|
|
|
#endif
|
2020-02-14 01:26:46 +00:00
|
|
|
|
2015-01-13 15:19:37 +00:00
|
|
|
#define MP_FLOAT_EXP_BIAS ((1 << (MP_FLOAT_EXP_BITS - 1)) - 1)
|
2020-02-14 01:26:46 +00:00
|
|
|
|
|
|
|
typedef union _mp_float_union_t {
|
|
|
|
mp_float_t f;
|
|
|
|
#if MP_ENDIANNESS_LITTLE
|
|
|
|
struct {
|
|
|
|
mp_float_uint_t frc : MP_FLOAT_FRAC_BITS;
|
|
|
|
mp_float_uint_t exp : MP_FLOAT_EXP_BITS;
|
|
|
|
mp_float_uint_t sgn : 1;
|
|
|
|
} p;
|
|
|
|
#else
|
|
|
|
struct {
|
2020-06-18 01:58:51 +01:00
|
|
|
mp_float_uint_t sgn : 1;
|
2020-02-14 01:26:46 +00:00
|
|
|
mp_float_uint_t exp : MP_FLOAT_EXP_BITS;
|
|
|
|
mp_float_uint_t frc : MP_FLOAT_FRAC_BITS;
|
|
|
|
} p;
|
|
|
|
#endif
|
|
|
|
mp_float_uint_t i;
|
|
|
|
} mp_float_union_t;
|
|
|
|
|
2015-01-13 15:19:37 +00:00
|
|
|
#endif // MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
/** ROM string compression *************/
|
|
|
|
|
2020-04-13 05:52:10 +01:00
|
|
|
#if MICROPY_ROM_TEXT_COMPRESSION
|
|
|
|
|
2021-04-22 03:13:58 +01:00
|
|
|
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
|
|
|
|
#error "MICROPY_ERROR_REPORTING_NONE requires MICROPY_ROM_TEXT_COMPRESSION disabled"
|
|
|
|
#endif
|
|
|
|
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
#ifdef NO_QSTR
|
|
|
|
|
2020-04-13 05:52:10 +01:00
|
|
|
// Compression enabled but doing QSTR extraction.
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
// So leave MP_COMPRESSED_ROM_TEXT in place for makeqstrdefs.py / makecompresseddata.py to find them.
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-04-13 05:52:10 +01:00
|
|
|
// Compression enabled and doing a regular build.
|
|
|
|
// Map MP_COMPRESSED_ROM_TEXT to the compressed strings.
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
|
|
|
|
// Force usage of the MP_ERROR_TEXT macro by requiring an opaque type.
|
2020-10-14 14:42:18 +01:00
|
|
|
typedef struct {
|
2022-06-23 15:02:59 +01:00
|
|
|
#if defined(__clang__) || defined(_MSC_VER)
|
|
|
|
// Fix "error: empty struct has size 0 in C, size 1 in C++", and the msvc counterpart
|
|
|
|
// "C requires that a struct or union have at least one member"
|
2020-10-14 14:42:18 +01:00
|
|
|
char dummy;
|
|
|
|
#endif
|
|
|
|
} *mp_rom_error_text_t;
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-06-23 15:02:59 +01:00
|
|
|
inline MP_ALWAYSINLINE const char *MP_COMPRESSED_ROM_TEXT(const char *msg) {
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
// "genhdr/compressed.data.h" contains an invocation of the MP_MATCH_COMPRESSED macro for each compressed string.
|
|
|
|
// The giant if(strcmp) tree is optimized by the compiler, which turns this into a direct return of the compressed data.
|
|
|
|
#define MP_MATCH_COMPRESSED(a, b) if (strcmp(msg, a) == 0) { return b; } else
|
|
|
|
|
|
|
|
// It also contains a single invocation of the MP_COMPRESSED_DATA macro, we don't need that here.
|
|
|
|
#define MP_COMPRESSED_DATA(x)
|
|
|
|
|
|
|
|
#include "genhdr/compressed.data.h"
|
|
|
|
|
|
|
|
#undef MP_COMPRESSED_DATA
|
|
|
|
#undef MP_MATCH_COMPRESSED
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2020-04-13 05:52:10 +01:00
|
|
|
#endif
|
|
|
|
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
// Compression not enabled, just make it a no-op.
|
2020-04-13 05:52:10 +01:00
|
|
|
|
py: Implement "common word" compression scheme for error messages.
The idea here is that there's a moderate amount of ROM used up by exception
text. Obviously we try to keep the messages short, and the code can enable
terse errors, but it still adds up. Listed below is the total string data
size for various ports:
bare-arm 2860
minimal 2876
stm32 8926 (PYBV11)
cc3200 3751
esp32 5721
This commit implements compression of these strings. It takes advantage of
the fact that these strings are all 7-bit ascii and extracts the top 128
frequently used words from the messages and stores them packed (dropping
their null-terminator), then uses (0x80 | index) inside strings to refer to
these common words. Spaces are automatically added around words, saving
more bytes. This happens transparently in the build process, mirroring the
steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT
macro wraps any literal string that should compressed, and it's
automatically decompressed in mp_decompress_rom_string.
There are many schemes that could be used for the compression, and some are
included in py/makecompresseddata.py for reference (space, Huffman, ngram,
common word). Results showed that the common-word compression gets better
results. This is before counting the increased cost of the Huffman
decoder. This might be slightly counter-intuitive, but this data is
extremely repetitive at a word-level, and the byte-level entropy coder
can't quite exploit that as efficiently. Ideally one would combine both
approaches, but for now the common-word approach is the one that is used.
For additional comparison, the size of the raw data compressed with gzip
and zlib is calculated, as a sort of proxy for a lower entropy bound. With
this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use
x% more bytes than the data compressed with gzip -- not counting the code
overhead of a decoder, and how this would be hypothetically implemented).
The feature is disabled by default and can be enabled by setting
MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2019-09-26 13:19:29 +01:00
|
|
|
typedef const char *mp_rom_error_text_t;
|
|
|
|
#define MP_COMPRESSED_ROM_TEXT(x) x
|
|
|
|
|
|
|
|
#endif // MICROPY_ROM_TEXT_COMPRESSION
|
|
|
|
|
|
|
|
// Might add more types of compressed text in the future.
|
|
|
|
// For now, forward directly to MP_COMPRESSED_ROM_TEXT.
|
|
|
|
#define MP_ERROR_TEXT(x) (mp_rom_error_text_t)MP_COMPRESSED_ROM_TEXT(x)
|
|
|
|
|
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 22:14:58 +01:00
|
|
|
#endif // MICROPY_INCLUDED_PY_MISC_H
|