Skip to content

Commit 3a8b160

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 635543c + 978607a commit 3a8b160

13 files changed

Lines changed: 198 additions & 5 deletions

File tree

py/binary.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,52 @@ mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
7777
return MP_OBJ_NEW_SMALL_INT(val);
7878
}
7979

80+
mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr) {
81+
machine_int_t val = 0;
82+
byte *p = *ptr;
83+
switch (typecode) {
84+
case 'b':
85+
val = (int8_t)*p++;
86+
break;
87+
case BYTEARRAY_TYPECODE:
88+
case 'B':
89+
val = *p++;
90+
break;
91+
case 'h':
92+
val = (int16_t)((p[1] << 8) | p[0]);
93+
break;
94+
case 'H':
95+
val = (p[1] << 8) | p[0];
96+
break;
97+
case 'i':
98+
case 'l':
99+
val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
100+
*ptr = p + 4;
101+
return mp_obj_new_int(val);
102+
case 'I':
103+
case 'L':
104+
val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
105+
*ptr = p + 4;
106+
return mp_obj_new_int_from_uint(val);
107+
#if 0 //TODO
108+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
109+
case 'q':
110+
case 'Q':
111+
// TODO: Explode API more to cover signedness
112+
return mp_obj_new_int_from_ll(((long long*)p)[index]);
113+
#endif
114+
#if MICROPY_ENABLE_FLOAT
115+
case 'f':
116+
return mp_obj_new_float(((float*)p)[index]);
117+
case 'd':
118+
return mp_obj_new_float(((double*)p)[index]);
119+
#endif
120+
#endif
121+
}
122+
*ptr = p;
123+
return MP_OBJ_NEW_SMALL_INT(val);
124+
}
125+
80126
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
81127
machine_int_t val = 0;
82128
if (MP_OBJ_IS_INT(val_in)) {

py/binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
int mp_binary_get_size(char typecode);
66
mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
7+
mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr);
78
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ extern const mp_obj_module_t mp_module_collections;
4141
extern const mp_obj_module_t mp_module_io;
4242
extern const mp_obj_module_t mp_module_math;
4343
extern const mp_obj_module_t mp_module_micropython;
44+
extern const mp_obj_module_t mp_module_struct;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
127127
{ MP_OBJ_NEW_QSTR(MP_QSTR_io), (mp_obj_t)&mp_module_io },
128128
#endif
129129
{ MP_OBJ_NEW_QSTR(MP_QSTR_collections), (mp_obj_t)&mp_module_collections },
130+
#if MICROPY_ENABLE_MOD_STRUCT
131+
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_struct },
132+
#endif
130133

131134
#if MICROPY_ENABLE_FLOAT
132135
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },

py/makeqstrdata.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def do_work(infiles):
2929
for infile in infiles:
3030
with open(infile, 'rt') as f:
3131
line_number = 0
32+
conditional = None
3233
for line in f:
3334
line_number += 1
3435
line = line.strip()
@@ -37,6 +38,18 @@ def do_work(infiles):
3738
if len(line) == 0 or line.startswith('//'):
3839
continue
3940

41+
if line[0] == '#':
42+
if conditional == "<endif>":
43+
assert line == "#endif"
44+
conditional = None
45+
else:
46+
assert conditional is None
47+
conditional = line
48+
continue
49+
50+
if conditional == "<endif>":
51+
assert False, "#endif expected before '%s'" % line
52+
4053
# verify line is of the correct form
4154
match = re.match(r'Q\((.+)\)$', line)
4255
if not match:
@@ -52,15 +65,21 @@ def do_work(infiles):
5265
continue
5366

5467
# add the qstr to the list, with order number to retain original order in file
55-
qstrs[ident] = (len(qstrs), ident, qstr)
68+
qstrs[ident] = (len(qstrs), ident, qstr, conditional)
69+
if conditional is not None:
70+
conditional = "<endif>"
5671

5772
# process the qstrs, printing out the generated C header file
5873
print('// This file was automatically generated by makeqstrdata.py')
5974
print('')
60-
for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]):
75+
for order, ident, qstr, conditional in sorted(qstrs.values(), key=lambda x: x[0]):
6176
qhash = compute_hash(qstr)
6277
qlen = len(qstr)
78+
if conditional:
79+
print(conditional)
6380
print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr))
81+
if conditional:
82+
print('#endif')
6483

6584
return True
6685

py/modstruct.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "obj.h"
7+
#include "builtin.h"
8+
#include "objtuple.h"
9+
#include "binary.h"
10+
11+
#if MICROPY_ENABLE_MOD_STRUCT
12+
13+
STATIC char get_fmt_type(const char **fmt) {
14+
char t = **fmt;
15+
switch (t) {
16+
case '!':
17+
t = '>';
18+
break;
19+
case '@':
20+
case '=':
21+
case '<':
22+
case '>':
23+
break;
24+
default:
25+
return '@';
26+
}
27+
// Skip type char
28+
(*fmt)++;
29+
return t;
30+
}
31+
32+
STATIC uint calcsize_items(const char *fmt) {
33+
// TODO
34+
return strlen(fmt);
35+
}
36+
37+
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
38+
const char *fmt = mp_obj_str_get_str(fmt_in);
39+
char fmt_type = get_fmt_type(&fmt);
40+
assert(fmt_type == '<'); (void)fmt_type;
41+
uint size;
42+
for (size = 0; *fmt; fmt++) {
43+
int sz = mp_binary_get_size(*fmt);
44+
// TODO
45+
assert(sz != -1);
46+
size += sz;
47+
}
48+
return MP_OBJ_NEW_SMALL_INT(size);
49+
}
50+
MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
51+
52+
STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
53+
// TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
54+
const char *fmt = mp_obj_str_get_str(fmt_in);
55+
char fmt_type = get_fmt_type(&fmt);
56+
assert(fmt_type == '<'); (void)fmt_type;
57+
uint size = calcsize_items(fmt);
58+
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
59+
buffer_info_t bufinfo;
60+
mp_get_buffer_raise(data_in, &bufinfo);
61+
byte *p = bufinfo.buf;
62+
63+
for (uint i = 0; i < size; i++) {
64+
mp_obj_t item = mp_binary_get_val_unaligned_le(*fmt++, &p);
65+
res->items[i] = item;
66+
}
67+
return res;
68+
}
69+
MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
70+
71+
STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
73+
{ MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
74+
{ MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
75+
};
76+
77+
STATIC const mp_obj_dict_t mp_module_struct_globals = {
78+
.base = {&mp_type_dict},
79+
.map = {
80+
.all_keys_are_qstrs = 1,
81+
.table_is_fixed_array = 1,
82+
.used = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
83+
.alloc = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
84+
.table = (mp_map_elem_t*)mp_module_struct_globals_table,
85+
},
86+
};
87+
88+
const mp_obj_module_t mp_module_struct = {
89+
.base = { &mp_type_module },
90+
.name = MP_QSTR_struct,
91+
.globals = (mp_obj_dict_t*)&mp_module_struct_globals,
92+
};
93+
94+
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ typedef double mp_float_t;
115115
#define MICROPY_ENABLE_MOD_IO (1)
116116
#endif
117117

118+
// Whether to provide "struct" module
119+
#ifndef MICROPY_ENABLE_MOD_STRUCT
120+
#define MICROPY_ENABLE_MOD_STRUCT (1)
121+
#endif
122+
118123
// Whether to support slice object and correspondingly
119124
// slice subscript operators
120125
#ifndef MICROPY_ENABLE_SLICE

py/objfun.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,13 @@ bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, co
211211
}
212212

213213
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
214-
DEBUG_printf("Input: ");
214+
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
215+
DEBUG_printf("Input pos args: ");
215216
dump_args(args, n_args);
217+
DEBUG_printf("Input kw args: ");
218+
dump_args(args + n_args, n_kw * 2);
216219
mp_obj_fun_bc_t *self = self_in;
220+
DEBUG_printf("Func n_def_args: %d\n", self->n_def_args);
217221

218222
const mp_obj_t *kwargs = args + n_args;
219223
mp_obj_t *extra_args = self->extra_args + self->n_def_args;
@@ -295,9 +299,9 @@ continue2:;
295299
// Now fill in defaults
296300
mp_obj_t *d = &flat_args[self->n_args - 1];
297301
mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
298-
for (int i = self->n_def_args; i > 0; i--) {
302+
for (int i = self->n_def_args; i > 0; i--, d--, s--) {
299303
if (*d == MP_OBJ_NULL) {
300-
*d-- = *s--;
304+
*d = *s;
301305
}
302306
}
303307
DEBUG_printf("Args after filling defaults: ");

py/objstr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) {
330330
} else {
331331
if (!MP_OBJ_IS_TYPE(arg, &mp_type_list)) {
332332
// arg is not a list, try to convert it to one
333+
// TODO: Try to optimize?
333334
arg = mp_type_list.make_new((mp_obj_t)&mp_type_list, 1, 0, &arg);
334335
}
335336
mp_obj_list_get(arg, &seq_len, &seq_items);

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ PY_O_BASENAME = \
8181
modio.o \
8282
modmath.o \
8383
modmicropython.o \
84+
modstruct.o \
8485
vm.o \
8586
showbc.o \
8687
repl.o \

0 commit comments

Comments
 (0)