blob: f9ca10683749046ce748726568eb9c48c2e3be93 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <assert.h>
28#include <string.h>
Damien Georgeea0461d2015-02-10 11:02:28 +000029#include <stdio.h>
Damien429d7192013-10-04 19:53:11 +010030
Damien Georgeb4b10fd2015-01-01 23:30:53 +000031#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/qstr.h"
33#include "py/gc.h"
Léa Saviotbc129f12019-11-22 10:07:08 +010034#include "py/runtime.h"
Damien429d7192013-10-04 19:53:11 +010035
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000036// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
37// ultimately we will replace this with a static hash table of some kind
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000038
Stefan Naumannace9fb52017-07-24 18:55:14 +020039#if MICROPY_DEBUG_VERBOSE // print debugging info
Paul Sokolovsky44739e22014-02-16 18:11:42 +020040#define DEBUG_printf DEBUG_printf
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000041#else // don't print debugging info
Damien George1dc76af2014-02-26 16:57:08 +000042#define DEBUG_printf(...) (void)0
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000043#endif
44
Damien George55baff42014-01-21 21:40:13 +000045// A qstr is an index into the qstr pool.
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040046// The data for a qstr is \0 terminated (so they can be printed using printf)
Damien George55baff42014-01-21 21:40:13 +000047
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040048#define Q_HASH_MASK ((1 << (8 * MICROPY_QSTR_BYTES_IN_HASH)) - 1)
Damien George55baff42014-01-21 21:40:13 +000049
Damien Georgea1c93a62016-05-26 10:53:34 +000050#if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +000051#define QSTR_ENTER() mp_thread_mutex_lock(&MP_STATE_VM(qstr_mutex), 1)
52#define QSTR_EXIT() mp_thread_mutex_unlock(&MP_STATE_VM(qstr_mutex))
53#else
54#define QSTR_ENTER()
55#define QSTR_EXIT()
56#endif
57
Damien George0d165fe2018-12-13 17:08:26 +110058// Initial number of entries for qstr pool, set so that the first dynamically
59// allocated pool is twice this size. The value here must be <= MP_QSTRnumber_of.
60#define MICROPY_ALLOC_QSTR_ENTRIES_INIT (10)
61
Damien George6e628c42014-03-25 15:27:15 +000062// this must match the equivalent function in makeqstrdata.py
Damien Georgec3f64d92015-11-27 12:23:18 +000063mp_uint_t qstr_compute_hash(const byte *data, size_t len) {
Damien George6e628c42014-03-25 15:27:15 +000064 // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
Damien George40f3c022014-07-03 13:25:24 +010065 mp_uint_t hash = 5381;
Damien George55baff42014-01-21 21:40:13 +000066 for (const byte *top = data + len; data < top; data++) {
Damien George6e628c42014-03-25 15:27:15 +000067 hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
Damien George55baff42014-01-21 21:40:13 +000068 }
Damien Georgec3bd9412015-07-20 11:03:13 +000069 hash &= Q_HASH_MASK;
Paul Sokolovsky59e269c2014-04-14 01:43:01 +030070 // Make sure that valid hash is never zero, zero means "hash not computed"
71 if (hash == 0) {
72 hash++;
73 }
74 return hash;
Damien George55baff42014-01-21 21:40:13 +000075}
76
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040077const qstr_hash_t mp_qstr_const_hashes[] = {
78 #ifndef NO_QSTR
79#define QDEF(id, hash, len, str) hash,
80 #include "genhdr/qstrdefs.generated.h"
81#undef QDEF
82 #endif
83};
84
85const qstr_len_t mp_qstr_const_lengths[] = {
86 #ifndef NO_QSTR
87#define QDEF(id, hash, len, str) len,
88 #include "genhdr/qstrdefs.generated.h"
89#undef QDEF
90 #endif
91};
92
Damien George0a2e9652016-01-31 22:24:16 +000093const qstr_pool_t mp_qstr_const_pool = {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000094 NULL, // no previous pool
95 0, // no previous pool
Damien George0d165fe2018-12-13 17:08:26 +110096 MICROPY_ALLOC_QSTR_ENTRIES_INIT,
Damien George0a2e9652016-01-31 22:24:16 +000097 MP_QSTRnumber_of, // corresponds to number of strings in array just below
Artyom Skrobov18b1ba02021-05-03 14:17:36 -040098 (qstr_hash_t *)mp_qstr_const_hashes,
99 (qstr_len_t *)mp_qstr_const_lengths,
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000100 {
Damien George69661f32020-02-27 15:36:53 +1100101 #ifndef NO_QSTR
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400102#define QDEF(id, hash, len, str) str,
Damien George69661f32020-02-27 15:36:53 +1100103 #include "genhdr/qstrdefs.generated.h"
Damien George6942f802015-01-11 17:52:45 +0000104#undef QDEF
Damien George69661f32020-02-27 15:36:53 +1100105 #endif
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000106 },
107};
108
Damien George0a2e9652016-01-31 22:24:16 +0000109#ifdef MICROPY_QSTR_EXTRA_POOL
110extern const qstr_pool_t MICROPY_QSTR_EXTRA_POOL;
111#define CONST_POOL MICROPY_QSTR_EXTRA_POOL
112#else
113#define CONST_POOL mp_qstr_const_pool
114#endif
115
Damien8b3a7c22013-10-23 20:20:17 +0100116void qstr_init(void) {
Damien George69661f32020-02-27 15:36:53 +1100117 MP_STATE_VM(last_pool) = (qstr_pool_t *)&CONST_POOL; // we won't modify the const_pool since it has no allocated room left
Damien Georgeade9a052015-06-13 21:53:22 +0100118 MP_STATE_VM(qstr_last_chunk) = NULL;
Damien George1f54ad22016-05-26 09:06:46 +0000119
David Lechneredbb73a2020-01-22 11:19:37 -0600120 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +0000121 mp_thread_mutex_init(&MP_STATE_VM(qstr_mutex));
122 #endif
Damien429d7192013-10-04 19:53:11 +0100123}
124
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400125STATIC const qstr_pool_t *find_qstr(qstr *q) {
Damien George55baff42014-01-21 21:40:13 +0000126 // search pool for this qstr
Damien George8e323b82017-11-29 16:58:27 +1100127 // total_prev_len==0 in the final pool, so the loop will always terminate
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400128 const qstr_pool_t *pool = MP_STATE_VM(last_pool);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400129 while (*q < pool->total_prev_len) {
Damien George8e323b82017-11-29 16:58:27 +1100130 pool = pool->prev;
Damien George55baff42014-01-21 21:40:13 +0000131 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400132 *q -= pool->total_prev_len;
133 assert(*q < pool->len);
134 return pool;
Damien George55baff42014-01-21 21:40:13 +0000135}
136
Damien George1f54ad22016-05-26 09:06:46 +0000137// qstr_mutex must be taken while in this function
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400138STATIC qstr qstr_add(mp_uint_t hash, mp_uint_t len, const char *q_ptr) {
139 DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", hash, len, len, q_ptr);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000140
141 // make sure we have room in the pool for a new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000142 if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
Damien George0d165fe2018-12-13 17:08:26 +1100143 size_t new_alloc = MP_STATE_VM(last_pool)->alloc * 2;
144 #ifdef MICROPY_QSTR_EXTRA_POOL
145 // Put a lower bound on the allocation size in case the extra qstr pool has few entries
146 new_alloc = MAX(MICROPY_ALLOC_QSTR_ENTRIES_INIT, new_alloc);
147 #endif
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400148 mp_uint_t pool_size = sizeof(qstr_pool_t)
149 + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * new_alloc;
150 qstr_pool_t *pool = (qstr_pool_t *)m_malloc_maybe(pool_size);
Damien George1f54ad22016-05-26 09:06:46 +0000151 if (pool == NULL) {
Emilie Feral58b56b92021-12-27 15:39:34 +0100152 // Keep qstr_last_chunk consistent with qstr_pool_t: qstr_last_chunk is not scanned
153 // at garbage collection since it's reachable from a qstr_pool_t. And the caller of
154 // this function expects q_ptr to be stored in a qstr_pool_t so it can be reached
155 // by the collector. If qstr_pool_t allocation failed, qstr_last_chunk needs to be
156 // NULL'd. Otherwise it may become a dangling pointer at the next garbage collection.
157 MP_STATE_VM(qstr_last_chunk) = NULL;
Damien George1f54ad22016-05-26 09:06:46 +0000158 QSTR_EXIT();
Damien George0d165fe2018-12-13 17:08:26 +1100159 m_malloc_fail(new_alloc);
Damien George1f54ad22016-05-26 09:06:46 +0000160 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400161 pool->hashes = (qstr_hash_t *)(pool->qstrs + new_alloc);
162 pool->lengths = (qstr_len_t *)(pool->hashes + new_alloc);
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000163 pool->prev = MP_STATE_VM(last_pool);
164 pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
Damien George0d165fe2018-12-13 17:08:26 +1100165 pool->alloc = new_alloc;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000166 pool->len = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000167 MP_STATE_VM(last_pool) = pool;
168 DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
Damien429d7192013-10-04 19:53:11 +0100169 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000170
171 // add the new qstr
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400172 mp_uint_t at = MP_STATE_VM(last_pool)->len;
173 MP_STATE_VM(last_pool)->hashes[at] = hash;
174 MP_STATE_VM(last_pool)->lengths[at] = len;
175 MP_STATE_VM(last_pool)->qstrs[at] = q_ptr;
176 MP_STATE_VM(last_pool)->len++;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000177
178 // return id for the newly-added qstr
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400179 return MP_STATE_VM(last_pool)->total_prev_len + at;
Damien429d7192013-10-04 19:53:11 +0100180}
181
Damien Georgec3f64d92015-11-27 12:23:18 +0000182qstr qstr_find_strn(const char *str, size_t str_len) {
Damien George55baff42014-01-21 21:40:13 +0000183 // work out hash of str
Damien George69661f32020-02-27 15:36:53 +1100184 mp_uint_t str_hash = qstr_compute_hash((const byte *)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000185
186 // search pools for the data
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400187 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400188 for (mp_uint_t at = 0, top = pool->len; at < top; at++) {
189 if (pool->hashes[at] == str_hash && pool->lengths[at] == str_len
190 && memcmp(pool->qstrs[at], str, str_len) == 0) {
191 return pool->total_prev_len + at;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000192 }
Damien429d7192013-10-04 19:53:11 +0100193 }
194 }
Damien George55baff42014-01-21 21:40:13 +0000195
196 // not found; return null qstr
197 return 0;
Damien429d7192013-10-04 19:53:11 +0100198}
199
Damien George55baff42014-01-21 21:40:13 +0000200qstr qstr_from_str(const char *str) {
201 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100202}
203
Damien Georgec3f64d92015-11-27 12:23:18 +0000204qstr qstr_from_strn(const char *str, size_t len) {
Damien George1f54ad22016-05-26 09:06:46 +0000205 QSTR_ENTER();
Damien George2617eeb2014-05-25 22:27:57 +0100206 qstr q = qstr_find_strn(str, len);
Damien George55baff42014-01-21 21:40:13 +0000207 if (q == 0) {
Damien Georgeade9a052015-06-13 21:53:22 +0100208 // qstr does not exist in interned pool so need to add it
209
Léa Saviotbc129f12019-11-22 10:07:08 +0100210 // check that len is not too big
211 if (len >= (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN))) {
212 QSTR_EXIT();
Jim Mussareddef76fe2020-03-02 22:35:22 +1100213 mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("name too long"));
Léa Saviotbc129f12019-11-22 10:07:08 +0100214 }
215
Damien Georgeade9a052015-06-13 21:53:22 +0100216 // compute number of bytes needed to intern this string
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400217 size_t n_bytes = len + 1;
Damien Georgeade9a052015-06-13 21:53:22 +0100218
219 if (MP_STATE_VM(qstr_last_chunk) != NULL && MP_STATE_VM(qstr_last_used) + n_bytes > MP_STATE_VM(qstr_last_alloc)) {
220 // not enough room at end of previously interned string so try to grow
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400221 char *new_p = m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_alloc) + n_bytes, false);
Damien Georgeade9a052015-06-13 21:53:22 +0100222 if (new_p == NULL) {
223 // could not grow existing memory; shrink it to fit previous
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400224 (void)m_renew_maybe(char, MP_STATE_VM(qstr_last_chunk), MP_STATE_VM(qstr_last_alloc), MP_STATE_VM(qstr_last_used), false);
Damien Georgeade9a052015-06-13 21:53:22 +0100225 MP_STATE_VM(qstr_last_chunk) = NULL;
226 } else {
227 // could grow existing memory
228 MP_STATE_VM(qstr_last_alloc) += n_bytes;
229 }
230 }
231
232 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
233 // no existing memory for the interned string so allocate a new chunk
Damien Georgec3f64d92015-11-27 12:23:18 +0000234 size_t al = n_bytes;
Damien Georgeade9a052015-06-13 21:53:22 +0100235 if (al < MICROPY_ALLOC_QSTR_CHUNK_INIT) {
236 al = MICROPY_ALLOC_QSTR_CHUNK_INIT;
237 }
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400238 MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, al);
Damien Georgeade9a052015-06-13 21:53:22 +0100239 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
240 // failed to allocate a large chunk so try with exact size
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400241 MP_STATE_VM(qstr_last_chunk) = m_new_maybe(char, n_bytes);
Damien George1f54ad22016-05-26 09:06:46 +0000242 if (MP_STATE_VM(qstr_last_chunk) == NULL) {
243 QSTR_EXIT();
244 m_malloc_fail(n_bytes);
245 }
Damien Georgeade9a052015-06-13 21:53:22 +0100246 al = n_bytes;
247 }
248 MP_STATE_VM(qstr_last_alloc) = al;
249 MP_STATE_VM(qstr_last_used) = 0;
250 }
251
252 // allocate memory from the chunk for this new interned string's data
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400253 char *q_ptr = MP_STATE_VM(qstr_last_chunk) + MP_STATE_VM(qstr_last_used);
Damien Georgeade9a052015-06-13 21:53:22 +0100254 MP_STATE_VM(qstr_last_used) += n_bytes;
255
256 // store the interned strings' data
Damien George69661f32020-02-27 15:36:53 +1100257 mp_uint_t hash = qstr_compute_hash((const byte *)str, len);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400258 memcpy(q_ptr, str, len);
259 q_ptr[len] = '\0';
260 q = qstr_add(hash, len, q_ptr);
Damien429d7192013-10-04 19:53:11 +0100261 }
Damien George1f54ad22016-05-26 09:06:46 +0000262 QSTR_EXIT();
Damien George55baff42014-01-21 21:40:13 +0000263 return q;
Damien429d7192013-10-04 19:53:11 +0100264}
265
Damien George40f3c022014-07-03 13:25:24 +0100266mp_uint_t qstr_hash(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400267 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400268 return pool->hashes[q];
Damien George55baff42014-01-21 21:40:13 +0000269}
270
Damien Georgec3f64d92015-11-27 12:23:18 +0000271size_t qstr_len(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400272 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400273 return pool->lengths[q];
Damien George55baff42014-01-21 21:40:13 +0000274}
275
Damien George55baff42014-01-21 21:40:13 +0000276const char *qstr_str(qstr q) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400277 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400278 return pool->qstrs[q];
Damien George55baff42014-01-21 21:40:13 +0000279}
280
Damien Georgec3f64d92015-11-27 12:23:18 +0000281const byte *qstr_data(qstr q, size_t *len) {
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400282 const qstr_pool_t *pool = find_qstr(&q);
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400283 *len = pool->lengths[q];
284 return (byte *)pool->qstrs[q];
Damien429d7192013-10-04 19:53:11 +0100285}
Damien George4d5b28c2014-01-29 18:56:46 +0000286
Damien George25784852015-12-17 12:41:40 +0000287void qstr_pool_info(size_t *n_pool, size_t *n_qstr, size_t *n_str_data_bytes, size_t *n_total_bytes) {
Damien George1f54ad22016-05-26 09:06:46 +0000288 QSTR_ENTER();
Damien George4d5b28c2014-01-29 18:56:46 +0000289 *n_pool = 0;
290 *n_qstr = 0;
291 *n_str_data_bytes = 0;
292 *n_total_bytes = 0;
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400293 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
Damien George4d5b28c2014-01-29 18:56:46 +0000294 *n_pool += 1;
295 *n_qstr += pool->len;
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400296 for (qstr_len_t *l = pool->lengths, *l_top = pool->lengths + pool->len; l < l_top; l++) {
297 *n_str_data_bytes += *l + 1;
Damien George4d5b28c2014-01-29 18:56:46 +0000298 }
Damien George0b13f3e2014-10-24 23:12:25 +0100299 #if MICROPY_ENABLE_GC
300 *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
301 #else
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400302 *n_total_bytes += sizeof(qstr_pool_t)
303 + (sizeof(const char *) + sizeof(qstr_hash_t) + sizeof(qstr_len_t)) * pool->alloc;
Damien George0b13f3e2014-10-24 23:12:25 +0100304 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000305 }
306 *n_total_bytes += *n_str_data_bytes;
Damien George1f54ad22016-05-26 09:06:46 +0000307 QSTR_EXIT();
Damien George4d5b28c2014-01-29 18:56:46 +0000308}
Damien Georgeea0461d2015-02-10 11:02:28 +0000309
310#if MICROPY_PY_MICROPYTHON_MEM_INFO
311void qstr_dump_data(void) {
Damien George1f54ad22016-05-26 09:06:46 +0000312 QSTR_ENTER();
Artyom Skrobovf46a7142021-05-04 03:35:45 -0400313 for (const qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &CONST_POOL; pool = pool->prev) {
314 for (const char *const *q = pool->qstrs, *const *q_top = pool->qstrs + pool->len; q < q_top; q++) {
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400315 mp_printf(&mp_plat_print, "Q(%s)\n", *q);
Damien Georgeea0461d2015-02-10 11:02:28 +0000316 }
317 }
Damien George1f54ad22016-05-26 09:06:46 +0000318 QSTR_EXIT();
Damien Georgeea0461d2015-02-10 11:02:28 +0000319}
320#endif
Jim Mussared154b4eb2019-09-26 22:19:29 +1000321
322#if MICROPY_ROM_TEXT_COMPRESSION
323
324#ifdef NO_QSTR
325
326// If NO_QSTR is set, it means we're doing QSTR extraction.
327// So we won't yet have "genhdr/compressed.data.h"
328
329#else
330
331// Emit the compressed_string_data string.
332#define MP_COMPRESSED_DATA(x) STATIC const char *compressed_string_data = x;
333#define MP_MATCH_COMPRESSED(a, b)
334#include "genhdr/compressed.data.h"
335#undef MP_COMPRESSED_DATA
336#undef MP_MATCH_COMPRESSED
337
338#endif // NO_QSTR
339
340// This implements the "common word" compression scheme (see makecompresseddata.py) where the most
341// common 128 words in error messages are replaced by their index into the list of common words.
342
343// The compressed string data is delimited by setting high bit in the final char of each word.
344// e.g. aaaa<0x80|a>bbbbbb<0x80|b>....
345// This method finds the n'th string.
346STATIC const byte *find_uncompressed_string(uint8_t n) {
347 const byte *c = (byte *)compressed_string_data;
348 while (n > 0) {
349 while ((*c & 0x80) == 0) {
350 ++c;
351 }
352 ++c;
353 --n;
354 }
355 return c;
356}
357
358// Given a compressed string in src, decompresses it into dst.
359// dst must be large enough (use MP_MAX_UNCOMPRESSED_TEXT_LEN+1).
360void mp_decompress_rom_string(byte *dst, const mp_rom_error_text_t src_chr) {
361 // Skip past the 0xff marker.
362 const byte *src = (byte *)src_chr + 1;
363 // Need to add spaces around compressed words, except for the first (i.e. transition from 1<->2).
364 // 0 = start, 1 = compressed, 2 = regular.
365 int state = 0;
366 while (*src) {
367 if ((byte) * src >= 128) {
368 if (state != 0) {
369 *dst++ = ' ';
370 }
371 state = 1;
372
373 // High bit set, replace with common word.
374 const byte *word = find_uncompressed_string(*src & 0x7f);
375 // The word is terminated by the final char having its high bit set.
376 while ((*word & 0x80) == 0) {
377 *dst++ = *word++;
378 }
379 *dst++ = (*word & 0x7f);
380 } else {
381 // Otherwise just copy one char.
382 if (state == 1) {
383 *dst++ = ' ';
384 }
385 state = 2;
386
387 *dst++ = *src;
388 }
389 ++src;
390 }
391 // Add null-terminator.
392 *dst = 0;
393}
394
395#endif // MICROPY_ROM_TEXT_COMPRESSION