blob: 9eab01c4ebac9df6585972abb907d0f38f86b530 [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
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020027#include <stdint.h>
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030028#include <stdlib.h>
Paul Sokolovsky62798832014-06-02 16:04:26 +030029#include <string.h>
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020030#include <assert.h>
31
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020032#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030033#include "misc.h"
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020034#include "qstr.h"
35#include "obj.h"
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020036#include "binary.h"
37
38// Helpers to work with binary-encoded data
39
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030040int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
41 int size = 0;
42 int align = 1;
43 switch (struct_type) {
44 case '<': case '>':
45 switch (val_type) {
46 case 'b': case 'B':
47 size = 1; break;
48 case 'h': case 'H':
49 size = 2; break;
50 case 'i': case 'I':
51 size = 4; break;
52 case 'l': case 'L':
53 size = 4; break;
54 case 'q': case 'Q':
55 size = 8; break;
56 }
57 break;
58 case '@': {
59 // TODO:
60 // The simplest heuristic for alignment is to align by value
61 // size, but that doesn't work for "bigger than int" types,
62 // for example, long long may very well have long alignment
63 // So, we introduce separate alignment handling, but having
64 // formal support for that is different from actually supporting
65 // particular (or any) ABI.
66 switch (val_type) {
67 case BYTEARRAY_TYPECODE:
68 case 'b': case 'B':
69 align = size = 1; break;
70 case 'h': case 'H':
71 align = size = sizeof(short); break;
72 case 'i': case 'I':
73 align = size = sizeof(int); break;
74 case 'l': case 'L':
75 align = size = sizeof(long); break;
76 case 'q': case 'Q':
77 // TODO: This is for x86
78 align = sizeof(int); size = sizeof(long long); break;
Paul Sokolovsky62798832014-06-02 16:04:26 +030079 case 'P': case 'O': case 'S':
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +030080 align = size = sizeof(void*); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030081 }
82 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +020083 }
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030084 if (palign != NULL) {
85 *palign = align;
86 }
87 return size;
Paul Sokolovskyc2033242014-02-14 20:21:50 +020088}
89
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030090mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
Paul Sokolovsky76f8ced2014-02-15 00:28:41 +020091 machine_int_t val = 0;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020092 switch (typecode) {
93 case 'b':
94 val = ((int8_t*)p)[index];
95 break;
96 case BYTEARRAY_TYPECODE:
97 case 'B':
98 val = ((uint8_t*)p)[index];
99 break;
100 case 'h':
101 val = ((int16_t*)p)[index];
102 break;
103 case 'H':
104 val = ((uint16_t*)p)[index];
105 break;
106 case 'i':
107 case 'l':
108 return mp_obj_new_int(((int32_t*)p)[index]);
109 case 'I':
110 case 'L':
111 return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
Damien George96056a62014-02-15 23:02:00 +0000112#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200113 case 'q':
114 case 'Q':
115 // TODO: Explode API more to cover signedness
116 return mp_obj_new_int_from_ll(((long long*)p)[index]);
117#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100118#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200119 case 'f':
120 return mp_obj_new_float(((float*)p)[index]);
121 case 'd':
122 return mp_obj_new_float(((double*)p)[index]);
123#endif
124 }
125 return MP_OBJ_NEW_SMALL_INT(val);
126}
127
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300128#define is_signed(typecode) (typecode > 'Z')
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300129mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300130 byte *p = *ptr;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300131 uint align;
132
133 int size = mp_binary_get_size(struct_type, val_type, &align);
134 if (struct_type == '@') {
135 // Make pointer aligned
136 p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
137 #if MP_ENDIANNESS_LITTLE
138 struct_type = '<';
139 #else
140 struct_type = '>';
141 #endif
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300142 }
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300143
144 int delta;
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300145 if (struct_type == '<') {
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300146 delta = -1;
147 p += size - 1;
148 } else {
149 delta = 1;
150 }
151
152 machine_int_t val = 0;
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300153 if (is_signed(val_type) && *p & 0x80) {
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300154 val = -1;
155 }
156 for (uint i = 0; i < size; i++) {
157 val <<= 8;
158 val |= *p;
159 p += delta;
160 }
161
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300162 *ptr += size;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300163 if (val_type == 'O') {
164 return (mp_obj_t)val;
Paul Sokolovsky62798832014-06-02 16:04:26 +0300165 } else if (val_type == 'S') {
166 return mp_obj_new_str((char*)val, strlen((char*)val), false);
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300167 } else if (is_signed(val_type)) {
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300168 return mp_obj_new_int(val);
169 } else {
170 return mp_obj_new_int_from_uint(val);
171 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300172}
173
Paul Sokolovsky62044602014-04-19 03:13:15 +0300174void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
175 byte *p = *ptr;
176 uint align;
177
178 int size = mp_binary_get_size(struct_type, val_type, &align);
179 if (struct_type == '@') {
180 // Make pointer aligned
181 p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
182 #if MP_ENDIANNESS_LITTLE
183 struct_type = '<';
184 #else
185 struct_type = '>';
186 #endif
187 }
188
189#if MP_ENDIANNESS_BIG
190#error Not implemented
191#endif
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300192 machine_int_t val;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300193 byte *in = (byte*)&val;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300194 switch (val_type) {
195 case 'O':
196 in = (byte*)&val_in;
197 break;
198 default:
199 val = mp_obj_get_int(val_in);
200 }
201
Paul Sokolovsky62044602014-04-19 03:13:15 +0300202 int in_delta, out_delta;
203 uint val_sz = MIN(size, sizeof(val));
204 if (struct_type == '>') {
205 in_delta = -1;
206 out_delta = 1;
207 in += val_sz - 1;
208 } else {
209 in_delta = out_delta = 1;
210 }
211
212 for (uint i = val_sz; i > 0; i--) {
213 *p = *in;
214 p += out_delta;
215 in += in_delta;
216 }
217
218 *ptr += size;
219}
220
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300221void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
Damien George71e9bfa2014-04-18 23:28:12 +0100222 switch (typecode) {
Damien Georgefb510b32014-06-01 13:32:54 +0100223#if MICROPY_PY_BUILTINS_FLOAT
Damien George71e9bfa2014-04-18 23:28:12 +0100224 case 'f':
225 ((float*)p)[index] = mp_obj_float_get(val_in);
226 break;
227 case 'd':
228 ((double*)p)[index] = mp_obj_float_get(val_in);
229 break;
230#endif
231 default:
232 mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200233 }
Damien George71e9bfa2014-04-18 23:28:12 +0100234}
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200235
Damien George71e9bfa2014-04-18 23:28:12 +0100236void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val) {
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200237 switch (typecode) {
238 case 'b':
239 ((int8_t*)p)[index] = val;
240 break;
241 case BYTEARRAY_TYPECODE:
242 case 'B':
243 val = ((uint8_t*)p)[index] = val;
244 break;
245 case 'h':
246 val = ((int16_t*)p)[index] = val;
247 break;
248 case 'H':
249 val = ((uint16_t*)p)[index] = val;
250 break;
251 case 'i':
252 case 'l':
253 ((int32_t*)p)[index] = val;
254 break;
255 case 'I':
256 case 'L':
257 ((uint32_t*)p)[index] = val;
258 break;
259#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
260 case 'q':
261 case 'Q':
262 assert(0);
263 ((long long*)p)[index] = val;
264 break;
265#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100266#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200267 case 'f':
Damien George71e9bfa2014-04-18 23:28:12 +0100268 ((float*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200269 break;
270 case 'd':
Damien George71e9bfa2014-04-18 23:28:12 +0100271 ((double*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200272 break;
273#endif
274 }
275}