blob: 2553872b16239dbb669b9a38978e0cade2a8eeca [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
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>
29
Damien Georgeb4b10fd2015-01-01 23:30:53 +000030#include "py/mpstate.h"
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/qstr.h"
32#include "py/gc.h"
Damien429d7192013-10-04 19:53:11 +010033
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000034// NOTE: we are using linear arrays to store and search for qstr's (unique strings, interned strings)
35// ultimately we will replace this with a static hash table of some kind
36// also probably need to include the length in the string data, to allow null bytes in the string
37
38#if 0 // print debugging info
Paul Sokolovsky44739e22014-02-16 18:11:42 +020039#define DEBUG_printf DEBUG_printf
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000040#else // don't print debugging info
Damien George1dc76af2014-02-26 16:57:08 +000041#define DEBUG_printf(...) (void)0
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000042#endif
43
Damien George55baff42014-01-21 21:40:13 +000044// A qstr is an index into the qstr pool.
45// The data for a qstr contains (hash, length, data).
46// For now we use very simple encoding, just to get the framework correct:
Damien George6e628c42014-03-25 15:27:15 +000047// - hash is 2 bytes (see function below)
Damien George55baff42014-01-21 21:40:13 +000048// - length is 2 bytes
49// - data follows
50// - \0 terminated (for now, so they can be printed using printf)
51
52#define Q_GET_HASH(q) ((q)[0] | ((q)[1] << 8))
Damien George95836f82015-01-11 22:27:30 +000053#define Q_GET_ALLOC(q) (2 + MICROPY_QSTR_BYTES_IN_LEN + Q_GET_LENGTH(q) + 1)
54#define Q_GET_DATA(q) ((q) + 2 + MICROPY_QSTR_BYTES_IN_LEN)
55#if MICROPY_QSTR_BYTES_IN_LEN == 1
56 #define Q_GET_LENGTH(q) ((q)[2])
57 #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); } while (0)
58#elif MICROPY_QSTR_BYTES_IN_LEN == 2
59 #define Q_GET_LENGTH(q) ((q)[2] | ((q)[3] << 8))
60 #define Q_SET_LENGTH(q, len) do { (q)[2] = (len); (q)[3] = (len) >> 8; } while (0)
61#else
62 #error unimplemented qstr length decoding
63#endif
Damien George55baff42014-01-21 21:40:13 +000064
Damien George6e628c42014-03-25 15:27:15 +000065// this must match the equivalent function in makeqstrdata.py
Damien George39dc1452014-10-03 19:52:22 +010066mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) {
Damien George6e628c42014-03-25 15:27:15 +000067 // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html
Damien George40f3c022014-07-03 13:25:24 +010068 mp_uint_t hash = 5381;
Damien George55baff42014-01-21 21:40:13 +000069 for (const byte *top = data + len; data < top; data++) {
Damien George6e628c42014-03-25 15:27:15 +000070 hash = ((hash << 5) + hash) ^ (*data); // hash * 33 ^ data
Damien George55baff42014-01-21 21:40:13 +000071 }
Paul Sokolovsky59e269c2014-04-14 01:43:01 +030072 hash &= 0xffff;
73 // Make sure that valid hash is never zero, zero means "hash not computed"
74 if (hash == 0) {
75 hash++;
76 }
77 return hash;
Damien George55baff42014-01-21 21:40:13 +000078}
79
Damien George73c98d82014-06-11 19:18:03 +010080STATIC const qstr_pool_t const_pool = {
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000081 NULL, // no previous pool
82 0, // no previous pool
83 10, // set so that the first dynamically allocated pool is twice this size; must be <= the len (just below)
84 MP_QSTR_number_of, // corresponds to number of strings in array just below
85 {
Damien George6942f802015-01-11 17:52:45 +000086#define QDEF(id, str) str,
Damien Georged553be52014-04-17 18:03:27 +010087#include "genhdr/qstrdefs.generated.h"
Damien George6942f802015-01-11 17:52:45 +000088#undef QDEF
Damien Georgeeb7bfcb2014-01-04 15:57:35 +000089 },
90};
91
Damien8b3a7c22013-10-23 20:20:17 +010092void qstr_init(void) {
Damien Georgeb4b10fd2015-01-01 23:30:53 +000093 MP_STATE_VM(last_pool) = (qstr_pool_t*)&const_pool; // we won't modify the const_pool since it has no allocated room left
Damien429d7192013-10-04 19:53:11 +010094}
95
Paul Sokolovsky520e2f52014-02-12 18:31:30 +020096STATIC const byte *find_qstr(qstr q) {
Damien George55baff42014-01-21 21:40:13 +000097 // search pool for this qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +000098 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +000099 if (q >= pool->total_prev_len) {
100 return pool->qstrs[q - pool->total_prev_len];
101 }
102 }
103
104 // not found
105 return 0;
106}
107
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200108STATIC qstr qstr_add(const byte *q_ptr) {
Damien George55baff42014-01-21 21:40:13 +0000109 DEBUG_printf("QSTR: add hash=%d len=%d data=%.*s\n", Q_GET_HASH(q_ptr), Q_GET_LENGTH(q_ptr), Q_GET_LENGTH(q_ptr), Q_GET_DATA(q_ptr));
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000110
111 // make sure we have room in the pool for a new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000112 if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) {
113 qstr_pool_t *pool = m_new_obj_var(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2);
114 pool->prev = MP_STATE_VM(last_pool);
115 pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len;
116 pool->alloc = MP_STATE_VM(last_pool)->alloc * 2;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000117 pool->len = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000118 MP_STATE_VM(last_pool) = pool;
119 DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc);
Damien429d7192013-10-04 19:53:11 +0100120 }
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000121
122 // add the new qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000123 MP_STATE_VM(last_pool)->qstrs[MP_STATE_VM(last_pool)->len++] = q_ptr;
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000124
125 // return id for the newly-added qstr
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000126 return MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len - 1;
Damien429d7192013-10-04 19:53:11 +0100127}
128
Damien George39dc1452014-10-03 19:52:22 +0100129qstr qstr_find_strn(const char *str, mp_uint_t str_len) {
Damien George55baff42014-01-21 21:40:13 +0000130 // work out hash of str
Damien George40f3c022014-07-03 13:25:24 +0100131 mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len);
Damien George55baff42014-01-21 21:40:13 +0000132
133 // search pools for the data
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000134 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL; pool = pool->prev) {
Damien George55baff42014-01-21 21:40:13 +0000135 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George1e708fe2014-01-23 18:27:51 +0000136 if (Q_GET_HASH(*q) == str_hash && Q_GET_LENGTH(*q) == str_len && memcmp(Q_GET_DATA(*q), str, str_len) == 0) {
Damien George55baff42014-01-21 21:40:13 +0000137 return pool->total_prev_len + (q - pool->qstrs);
Damien Georgeeb7bfcb2014-01-04 15:57:35 +0000138 }
Damien429d7192013-10-04 19:53:11 +0100139 }
140 }
Damien George55baff42014-01-21 21:40:13 +0000141
142 // not found; return null qstr
143 return 0;
Damien429d7192013-10-04 19:53:11 +0100144}
145
Damien George55baff42014-01-21 21:40:13 +0000146qstr qstr_from_str(const char *str) {
147 return qstr_from_strn(str, strlen(str));
Damien429d7192013-10-04 19:53:11 +0100148}
149
Damien George39dc1452014-10-03 19:52:22 +0100150qstr qstr_from_strn(const char *str, mp_uint_t len) {
Damien George4c81ba82015-01-13 16:21:23 +0000151 assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
Damien George2617eeb2014-05-25 22:27:57 +0100152 qstr q = qstr_find_strn(str, len);
Damien George55baff42014-01-21 21:40:13 +0000153 if (q == 0) {
Damien George40f3c022014-07-03 13:25:24 +0100154 mp_uint_t hash = qstr_compute_hash((const byte*)str, len);
Damien George95836f82015-01-11 22:27:30 +0000155 byte *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
Damien George55baff42014-01-21 21:40:13 +0000156 q_ptr[0] = hash;
157 q_ptr[1] = hash >> 8;
Damien George95836f82015-01-11 22:27:30 +0000158 Q_SET_LENGTH(q_ptr, len);
159 memcpy(q_ptr + 2 + MICROPY_QSTR_BYTES_IN_LEN, str, len);
160 q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
Damien George55baff42014-01-21 21:40:13 +0000161 q = qstr_add(q_ptr);
Damien429d7192013-10-04 19:53:11 +0100162 }
Damien George55baff42014-01-21 21:40:13 +0000163 return q;
Damien429d7192013-10-04 19:53:11 +0100164}
165
Damien George39dc1452014-10-03 19:52:22 +0100166byte *qstr_build_start(mp_uint_t len, byte **q_ptr) {
Damien George95836f82015-01-11 22:27:30 +0000167 assert(len < (1 << (8 * MICROPY_QSTR_BYTES_IN_LEN)));
168 *q_ptr = m_new(byte, 2 + MICROPY_QSTR_BYTES_IN_LEN + len + 1);
169 Q_SET_LENGTH(*q_ptr, len);
Damien George55baff42014-01-21 21:40:13 +0000170 return Q_GET_DATA(*q_ptr);
171}
172
173qstr qstr_build_end(byte *q_ptr) {
Damien George2617eeb2014-05-25 22:27:57 +0100174 qstr q = qstr_find_strn((const char*)Q_GET_DATA(q_ptr), Q_GET_LENGTH(q_ptr));
Damien George55baff42014-01-21 21:40:13 +0000175 if (q == 0) {
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_uint_t len = Q_GET_LENGTH(q_ptr);
177 mp_uint_t hash = qstr_compute_hash(Q_GET_DATA(q_ptr), len);
Damien George55baff42014-01-21 21:40:13 +0000178 q_ptr[0] = hash;
179 q_ptr[1] = hash >> 8;
Damien George95836f82015-01-11 22:27:30 +0000180 q_ptr[2 + MICROPY_QSTR_BYTES_IN_LEN + len] = '\0';
Damien George55baff42014-01-21 21:40:13 +0000181 q = qstr_add(q_ptr);
182 } else {
183 m_del(byte, q_ptr, Q_GET_ALLOC(q_ptr));
184 }
185 return q;
186}
187
Damien George40f3c022014-07-03 13:25:24 +0100188mp_uint_t qstr_hash(qstr q) {
Damien George55baff42014-01-21 21:40:13 +0000189 return Q_GET_HASH(find_qstr(q));
190}
191
Damien George39dc1452014-10-03 19:52:22 +0100192mp_uint_t qstr_len(qstr q) {
Damien George55baff42014-01-21 21:40:13 +0000193 const byte *qd = find_qstr(q);
194 return Q_GET_LENGTH(qd);
195}
196
197// XXX to remove!
198const char *qstr_str(qstr q) {
199 const byte *qd = find_qstr(q);
200 return (const char*)Q_GET_DATA(qd);
201}
202
Damien George39dc1452014-10-03 19:52:22 +0100203const byte *qstr_data(qstr q, mp_uint_t *len) {
Damien George55baff42014-01-21 21:40:13 +0000204 const byte *qd = find_qstr(q);
205 *len = Q_GET_LENGTH(qd);
206 return Q_GET_DATA(qd);
Damien429d7192013-10-04 19:53:11 +0100207}
Damien George4d5b28c2014-01-29 18:56:46 +0000208
Damien George39dc1452014-10-03 19:52:22 +0100209void qstr_pool_info(mp_uint_t *n_pool, mp_uint_t *n_qstr, mp_uint_t *n_str_data_bytes, mp_uint_t *n_total_bytes) {
Damien George4d5b28c2014-01-29 18:56:46 +0000210 *n_pool = 0;
211 *n_qstr = 0;
212 *n_str_data_bytes = 0;
213 *n_total_bytes = 0;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000214 for (qstr_pool_t *pool = MP_STATE_VM(last_pool); pool != NULL && pool != &const_pool; pool = pool->prev) {
Damien George4d5b28c2014-01-29 18:56:46 +0000215 *n_pool += 1;
216 *n_qstr += pool->len;
217 for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) {
Damien George0b13f3e2014-10-24 23:12:25 +0100218 #if MICROPY_ENABLE_GC
219 *n_str_data_bytes += gc_nbytes(*q); // this counts actual bytes used in heap
220 #else
Damien George4d5b28c2014-01-29 18:56:46 +0000221 *n_str_data_bytes += Q_GET_ALLOC(*q);
Damien George0b13f3e2014-10-24 23:12:25 +0100222 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000223 }
Damien George0b13f3e2014-10-24 23:12:25 +0100224 #if MICROPY_ENABLE_GC
225 *n_total_bytes += gc_nbytes(pool); // this counts actual bytes used in heap
226 #else
Damien George4d5b28c2014-01-29 18:56:46 +0000227 *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc;
Damien George0b13f3e2014-10-24 23:12:25 +0100228 #endif
Damien George4d5b28c2014-01-29 18:56:46 +0000229 }
230 *n_total_bytes += *n_str_data_bytes;
231}