Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1 | // in principle, rt_xxx functions are called only by vm/native/viper and make assumptions about args |
| 2 | // py_xxx functions are safer and can be called by anyone |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 3 | // note that rt_assign_xxx are called only from emit*, and maybe we can rename them to reflect this |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 4 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <assert.h> |
| 10 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 11 | #include "nlr.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 12 | #include "misc.h" |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 13 | #include "mpyconfig.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 14 | #include "runtime.h" |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 15 | #include "bc.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 16 | |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 17 | #if 0 // print debugging info |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 18 | #define DEBUG_PRINT (1) |
| 19 | #define WRITE_NATIVE (1) |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 20 | #define DEBUG_printf(args...) printf(args) |
| 21 | #define DEBUG_OP_printf(args...) printf(args) |
| 22 | #else // don't print debugging info |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 23 | #define DEBUG_printf(args...) (void)0 |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 24 | #define DEBUG_OP_printf(args...) (void)0 |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 25 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 26 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | typedef machine_int_t py_small_int_t; |
| 28 | |
| 29 | #define IS_O(o, k) (((((py_small_int_t)(o)) & 1) == 0) && (((py_obj_base_t*)(o))->kind == (k))) |
| 30 | #define IS_SMALL_INT(o) (((py_small_int_t)(o)) & 1) |
| 31 | #define FROM_SMALL_INT(o) (((py_small_int_t)(o)) >> 1) |
| 32 | #define TO_SMALL_INT(o) ((py_obj_t)(((o) << 1) | 1)) |
| 33 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 34 | #if MICROPY_ENABLE_FLOAT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | typedef machine_float_t float_t; |
| 36 | #endif |
| 37 | |
| 38 | typedef enum { |
| 39 | O_CONST, |
| 40 | O_STR, |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 41 | #if MICROPY_ENABLE_FLOAT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | O_FLOAT, |
| 43 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 44 | O_EXCEPTION_0, |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 45 | O_EXCEPTION_N, |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 46 | O_RANGE, |
| 47 | O_RANGE_IT, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | O_FUN_0, |
| 49 | O_FUN_1, |
| 50 | O_FUN_2, |
| 51 | O_FUN_N, |
| 52 | O_FUN_BC, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 53 | O_FUN_ASM, |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 54 | O_GEN_WRAP, |
| 55 | O_GEN_INSTANCE, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 56 | O_BOUND_METH, |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 57 | O_TUPLE, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 58 | O_LIST, |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 59 | O_TUPLE_IT, |
| 60 | O_LIST_IT, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 61 | O_SET, |
| 62 | O_MAP, |
| 63 | O_CLASS, |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 64 | O_OBJ, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 65 | } py_obj_kind_t; |
| 66 | |
| 67 | typedef enum { |
| 68 | MAP_QSTR, |
| 69 | MAP_PY_OBJ, |
| 70 | } py_map_kind_t; |
| 71 | |
| 72 | typedef struct _py_map_elem_t { |
| 73 | py_obj_t key; |
| 74 | py_obj_t value; |
| 75 | } py_map_elem_t; |
| 76 | |
| 77 | typedef struct _py_map_t { |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 78 | struct { |
| 79 | py_map_kind_t kind : 1; |
| 80 | machine_uint_t used : (8 * BYTES_PER_WORD - 1); |
| 81 | }; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 82 | machine_uint_t alloc; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 83 | py_map_elem_t *table; |
| 84 | } py_map_t; |
| 85 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 86 | typedef struct _py_obj_base_t py_obj_base_t; |
| 87 | |
| 88 | struct _py_obj_base_t { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 89 | py_obj_kind_t kind; |
| 90 | union { |
| 91 | const char *id; |
| 92 | qstr u_str; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 93 | #if MICROPY_ENABLE_FLOAT |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 94 | float_t u_flt; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 95 | #endif |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 96 | struct { // for O_EXCEPTION_0 |
| 97 | qstr id; |
| 98 | } u_exc0; |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 99 | struct { // for O_EXCEPTION_N |
| 100 | // TODO make generic object or something |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 101 | qstr id; |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 102 | int n_args; |
| 103 | const void **args; |
| 104 | } u_exc_n; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 105 | struct { // for O_RANGE |
| 106 | // TODO make generic object or something |
| 107 | machine_int_t start; |
| 108 | machine_int_t stop; |
| 109 | machine_int_t step; |
| 110 | } u_range; |
| 111 | struct { // for O_RANGE_IT |
| 112 | // TODO make generic object or something |
| 113 | machine_int_t cur; |
| 114 | machine_int_t stop; |
| 115 | machine_int_t step; |
| 116 | } u_range_it; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 117 | struct { // for O_FUN_[012N] |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 118 | int n_args; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 119 | void *fun; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 120 | } u_fun; |
| 121 | struct { // for O_FUN_BC |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 122 | int n_args; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 123 | byte *code; |
| 124 | uint len; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 125 | } u_fun_bc; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 126 | struct { // for O_FUN_ASM |
| 127 | int n_args; |
| 128 | void *fun; |
| 129 | } u_fun_asm; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 130 | struct { // for O_GEN_WRAP |
| 131 | int n_state; |
| 132 | py_obj_base_t *fun; |
| 133 | } u_gen_wrap; |
| 134 | struct { // for O_GEN_INSTANCE |
| 135 | py_obj_t *state; |
| 136 | const byte *ip; |
| 137 | py_obj_t *sp; |
| 138 | } u_gen_instance; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 139 | struct { // for O_BOUND_METH |
| 140 | py_obj_t meth; |
| 141 | py_obj_t self; |
| 142 | } u_bound_meth; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 143 | struct { // for O_TUPLE, O_LIST |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 144 | machine_uint_t alloc; |
| 145 | machine_uint_t len; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 146 | py_obj_t *items; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 147 | } u_tuple_list; |
| 148 | struct { // for O_TUPLE_IT, O_LIST_IT |
| 149 | py_obj_base_t *obj; |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 150 | machine_uint_t cur; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 151 | } u_tuple_list_it; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 152 | struct { // for O_SET |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 153 | machine_uint_t alloc; |
| 154 | machine_uint_t used; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 155 | py_obj_t *table; |
| 156 | } u_set; |
| 157 | py_map_t u_map; // for O_MAP |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 158 | struct { // for O_CLASS |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 159 | py_map_t *locals; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 160 | } u_class; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 161 | struct { // for O_OBJ |
| 162 | py_obj_base_t *class; // points to a O_CLASS object |
| 163 | py_map_t *members; |
| 164 | } u_obj; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 165 | }; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 166 | }; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 167 | |
| 168 | py_obj_t py_const_none; |
| 169 | py_obj_t py_const_false; |
| 170 | py_obj_t py_const_true; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 171 | py_obj_t py_const_stop_iteration; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 172 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 173 | // locals and globals need to be pointers because they can be the same in outer module scope |
| 174 | py_map_t *map_locals; |
| 175 | py_map_t *map_globals; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 176 | py_map_t map_builtins; |
| 177 | |
| 178 | // approximatelly doubling primes; made with Mathematica command: Table[Prime[Floor[(1.7)^n]], {n, 3, 24}] |
| 179 | static int doubling_primes[] = {7, 19, 43, 89, 179, 347, 647, 1229, 2297, 4243, 7829, 14347, 26017, 47149, 84947, 152443, 273253, 488399, 869927, 1547173, 2745121, 4861607}; |
| 180 | |
| 181 | int get_doubling_prime_greater_or_equal_to(int x) { |
| 182 | for (int i = 0; i < sizeof(doubling_primes) / sizeof(int); i++) { |
| 183 | if (doubling_primes[i] >= x) { |
| 184 | return doubling_primes[i]; |
| 185 | } |
| 186 | } |
| 187 | // ran out of primes in the table! |
| 188 | // return something sensible, at least make it odd |
| 189 | return x | 1; |
| 190 | } |
| 191 | |
| 192 | void py_map_init(py_map_t *map, py_map_kind_t kind, int n) { |
| 193 | map->kind = kind; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 194 | map->used = 0; |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 195 | map->alloc = get_doubling_prime_greater_or_equal_to(n + 1); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 196 | map->table = m_new0(py_map_elem_t, map->alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | py_map_t *py_map_new(py_map_kind_t kind, int n) { |
| 200 | py_map_t *map = m_new(py_map_t, 1); |
| 201 | py_map_init(map, kind, n); |
| 202 | return map; |
| 203 | } |
| 204 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 205 | machine_int_t py_obj_hash(py_obj_t o_in) { |
| 206 | if (o_in == py_const_false) { |
| 207 | return 0; // needs to hash to same as the integer 0, since False==0 |
| 208 | } else if (o_in == py_const_true) { |
| 209 | return 1; // needs to hash to same as the integer 1, since True==1 |
| 210 | } else if (IS_SMALL_INT(o_in)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 211 | return FROM_SMALL_INT(o_in); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 212 | } else if (IS_O(o_in, O_CONST)) { |
| 213 | return (machine_int_t)o_in; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 214 | } else if (IS_O(o_in, O_STR)) { |
| 215 | return ((py_obj_base_t*)o_in)->u_str; |
| 216 | } else { |
| 217 | assert(0); |
| 218 | return 0; |
| 219 | } |
| 220 | } |
| 221 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 222 | // this function implements the '==' operator (and so the inverse of '!=') |
| 223 | // from the python language reference: |
| 224 | // "The objects need not have the same type. If both are numbers, they are converted |
| 225 | // to a common type. Otherwise, the == and != operators always consider objects of |
| 226 | // different types to be unequal." |
| 227 | // note also that False==0 and True==1 are true expressions |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 228 | bool py_obj_equal(py_obj_t o1, py_obj_t o2) { |
| 229 | if (o1 == o2) { |
| 230 | return true; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 231 | } else if (IS_SMALL_INT(o1) || IS_SMALL_INT(o2)) { |
| 232 | if (IS_SMALL_INT(o1) && IS_SMALL_INT(o2)) { |
| 233 | return false; |
| 234 | } else { |
| 235 | if (IS_SMALL_INT(o2)) { |
| 236 | py_obj_t temp = o1; o1 = o2; o2 = temp; |
| 237 | } |
| 238 | // o1 is the SMALL_INT, o2 is not |
| 239 | py_small_int_t val = FROM_SMALL_INT(o1); |
| 240 | if (o2 == py_const_false) { |
| 241 | return val == 0; |
| 242 | } else if (o2 == py_const_true) { |
| 243 | return val == 1; |
| 244 | } else { |
| 245 | return false; |
| 246 | } |
| 247 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 248 | } else if (IS_O(o1, O_STR) && IS_O(o2, O_STR)) { |
| 249 | return ((py_obj_base_t*)o1)->u_str == ((py_obj_base_t*)o2)->u_str; |
| 250 | } else { |
| 251 | assert(0); |
| 252 | return false; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | py_map_elem_t* py_map_lookup_helper(py_map_t *map, py_obj_t index, bool add_if_not_found) { |
| 257 | bool is_map_py_obj = (map->kind == MAP_PY_OBJ); |
| 258 | machine_uint_t hash; |
| 259 | if (is_map_py_obj) { |
| 260 | hash = py_obj_hash(index); |
| 261 | } else { |
| 262 | hash = (machine_uint_t)index; |
| 263 | } |
| 264 | uint pos = hash % map->alloc; |
| 265 | for (;;) { |
| 266 | py_map_elem_t *elem = &map->table[pos]; |
| 267 | if (elem->key == NULL) { |
| 268 | // not in table |
| 269 | if (add_if_not_found) { |
| 270 | if (map->used + 1 >= map->alloc) { |
| 271 | // not enough room in table, rehash it |
| 272 | int old_alloc = map->alloc; |
| 273 | py_map_elem_t *old_table = map->table; |
| 274 | map->alloc = get_doubling_prime_greater_or_equal_to(map->alloc + 1); |
| 275 | map->used = 0; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 276 | map->table = m_new0(py_map_elem_t, map->alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 277 | for (int i = 0; i < old_alloc; i++) { |
| 278 | if (old_table[i].key != NULL) { |
| 279 | py_map_lookup_helper(map, old_table[i].key, true)->value = old_table[i].value; |
| 280 | } |
| 281 | } |
| 282 | m_free(old_table); |
| 283 | // restart the search for the new element |
| 284 | pos = hash % map->alloc; |
| 285 | } else { |
| 286 | map->used += 1; |
| 287 | elem->key = index; |
| 288 | return elem; |
| 289 | } |
| 290 | } else { |
| 291 | return NULL; |
| 292 | } |
| 293 | } else if (elem->key == index || (is_map_py_obj && py_obj_equal(elem->key, index))) { |
| 294 | // found it |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 295 | /* it seems CPython does not replace the index; try x={True:'true'};x[1]='one';x |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 296 | if (add_if_not_found) { |
| 297 | elem->key = index; |
| 298 | } |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 299 | */ |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 300 | return elem; |
| 301 | } else { |
| 302 | // not yet found, keep searching in this table |
| 303 | pos = (pos + 1) % map->alloc; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | py_map_elem_t* py_qstr_map_lookup(py_map_t *map, qstr index, bool add_if_not_found) { |
| 309 | py_obj_t o = (py_obj_t)(machine_uint_t)index; |
| 310 | return py_map_lookup_helper(map, o, add_if_not_found); |
| 311 | } |
| 312 | |
| 313 | py_map_elem_t* py_map_lookup(py_obj_t o, py_obj_t index, bool add_if_not_found) { |
| 314 | assert(IS_O(o, O_MAP)); |
| 315 | return py_map_lookup_helper(&((py_obj_base_t *)o)->u_map, index, add_if_not_found); |
| 316 | } |
| 317 | |
| 318 | static bool fit_small_int(py_small_int_t o) { |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | py_obj_t py_obj_new_const(const char *id) { |
| 323 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 324 | o->kind = O_CONST; |
| 325 | o->id = id; |
| 326 | return (py_obj_t)o; |
| 327 | } |
| 328 | |
| 329 | py_obj_t py_obj_new_str(qstr qstr) { |
| 330 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 331 | o->kind = O_STR; |
| 332 | o->u_str = qstr; |
| 333 | return (py_obj_t)o; |
| 334 | } |
| 335 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 336 | #if MICROPY_ENABLE_FLOAT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 337 | py_obj_t py_obj_new_float(float_t val) { |
| 338 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 339 | o->kind = O_FLOAT; |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 340 | o->u_flt = val; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 341 | return (py_obj_t)o; |
| 342 | } |
| 343 | #endif |
| 344 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 345 | py_obj_t py_obj_new_exception_0(qstr id) { |
| 346 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 347 | o->kind = O_EXCEPTION_0; |
| 348 | o->u_exc0.id = id; |
| 349 | return (py_obj_t)o; |
| 350 | } |
| 351 | |
| 352 | py_obj_t py_obj_new_exception_2(qstr id, const char *fmt, const char *s1, const char *s2) { |
| 353 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 354 | o->kind = O_EXCEPTION_N; |
| 355 | o->u_exc_n.id = id; |
| 356 | o->u_exc_n.n_args = 3; |
| 357 | o->u_exc_n.args = m_new(const void*, 3); |
| 358 | o->u_exc_n.args[0] = fmt; |
| 359 | o->u_exc_n.args[1] = s1; |
| 360 | o->u_exc_n.args[2] = s2; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 361 | return (py_obj_t)o; |
| 362 | } |
| 363 | |
| 364 | // range is a class and instances are immutable sequence objects |
| 365 | py_obj_t py_obj_new_range(int start, int stop, int step) { |
| 366 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 367 | o->kind = O_RANGE; |
| 368 | o->u_range.start = start; |
| 369 | o->u_range.stop = stop; |
| 370 | o->u_range.step = step; |
| 371 | return o; |
| 372 | } |
| 373 | |
| 374 | py_obj_t py_obj_new_range_iterator(int cur, int stop, int step) { |
| 375 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 376 | o->kind = O_RANGE_IT; |
| 377 | o->u_range_it.cur = cur; |
| 378 | o->u_range_it.stop = stop; |
| 379 | o->u_range_it.step = step; |
| 380 | return o; |
| 381 | } |
| 382 | |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 383 | py_obj_t py_obj_new_tuple_iterator(py_obj_base_t *tuple, int cur) { |
| 384 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 385 | o->kind = O_TUPLE_IT; |
| 386 | o->u_tuple_list_it.obj = tuple; |
| 387 | o->u_tuple_list_it.cur = cur; |
| 388 | return o; |
| 389 | } |
| 390 | |
| 391 | py_obj_t py_obj_new_list_iterator(py_obj_base_t *list, int cur) { |
| 392 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 393 | o->kind = O_LIST_IT; |
| 394 | o->u_tuple_list_it.obj = list; |
| 395 | o->u_tuple_list_it.cur = cur; |
| 396 | return o; |
| 397 | } |
| 398 | |
| 399 | py_obj_t rt_list_append(py_obj_t self_in, py_obj_t arg) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 400 | assert(IS_O(self_in, O_LIST)); |
| 401 | py_obj_base_t *self = self_in; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 402 | if (self->u_tuple_list.len >= self->u_tuple_list.alloc) { |
| 403 | self->u_tuple_list.alloc *= 2; |
| 404 | self->u_tuple_list.items = m_renew(py_obj_t, self->u_tuple_list.items, self->u_tuple_list.alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 405 | } |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 406 | self->u_tuple_list.items[self->u_tuple_list.len++] = arg; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 407 | return arg; |
| 408 | } |
| 409 | |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 410 | py_obj_t rt_gen_instance_next(py_obj_t self_in) { |
| 411 | py_obj_t ret = rt_iternext(self_in); |
| 412 | if (ret == py_const_stop_iteration) { |
| 413 | nlr_jump(py_obj_new_exception_0(qstr_from_str_static("StopIteration"))); |
| 414 | } else { |
| 415 | return ret; |
| 416 | } |
| 417 | } |
| 418 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 419 | static qstr q_append; |
| 420 | static qstr q_print; |
| 421 | static qstr q_len; |
| 422 | static qstr q___build_class__; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 423 | static qstr q___next__; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 424 | static qstr q_AttributeError; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 425 | static qstr q_IndexError; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 426 | static qstr q_KeyError; |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 427 | static qstr q_NameError; |
| 428 | static qstr q_TypeError; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 429 | |
| 430 | typedef enum { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 431 | PY_CODE_NONE, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 432 | PY_CODE_BYTE, |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 433 | PY_CODE_NATIVE, |
| 434 | PY_CODE_INLINE_ASM, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 435 | } py_code_kind_t; |
| 436 | |
| 437 | typedef struct _py_code_t { |
| 438 | py_code_kind_t kind; |
| 439 | int n_args; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 440 | int n_locals; |
| 441 | int n_stack; |
| 442 | bool is_generator; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 443 | union { |
| 444 | struct { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 445 | byte *code; |
| 446 | uint len; |
| 447 | } u_byte; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 448 | struct { |
| 449 | py_fun_t fun; |
| 450 | } u_native; |
| 451 | struct { |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 452 | void *fun; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 453 | } u_inline_asm; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 454 | }; |
| 455 | } py_code_t; |
| 456 | |
| 457 | static int next_unique_code_id; |
| 458 | static py_code_t *unique_codes; |
| 459 | |
| 460 | py_obj_t fun_list_append; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 461 | py_obj_t fun_gen_instance_next; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 462 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 463 | py_obj_t py_builtin___repl_print__(py_obj_t o) { |
| 464 | if (o != py_const_none) { |
| 465 | py_obj_print(o); |
| 466 | printf("\n"); |
| 467 | } |
| 468 | return py_const_none; |
| 469 | } |
| 470 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 471 | py_obj_t py_builtin_print(py_obj_t o) { |
| 472 | if (IS_O(o, O_STR)) { |
| 473 | // special case, print string raw |
| 474 | printf("%s\n", qstr_str(((py_obj_base_t*)o)->u_str)); |
| 475 | } else { |
| 476 | // print the object Python style |
| 477 | py_obj_print(o); |
| 478 | printf("\n"); |
| 479 | } |
| 480 | return py_const_none; |
| 481 | } |
| 482 | |
| 483 | py_obj_t py_builtin_len(py_obj_t o_in) { |
| 484 | py_small_int_t len = 0; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 485 | if (IS_O(o_in, O_TUPLE) || IS_O(o_in, O_LIST)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 486 | py_obj_base_t *o = o_in; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 487 | len = o->u_tuple_list.len; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 488 | } else if (IS_O(o_in, O_MAP)) { |
| 489 | py_obj_base_t *o = o_in; |
| 490 | len = o->u_map.used; |
| 491 | } else { |
| 492 | assert(0); |
| 493 | } |
| 494 | return TO_SMALL_INT(len); |
| 495 | } |
| 496 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 497 | py_obj_t py_builtin___build_class__(py_obj_t o_class_fun, py_obj_t o_class_name) { |
| 498 | // we differ from CPython: we set the new __locals__ object here |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 499 | py_map_t *old_locals = map_locals; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 500 | py_map_t *class_locals = py_map_new(MAP_QSTR, 0); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 501 | map_locals = class_locals; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 502 | |
| 503 | // call the class code |
| 504 | rt_call_function_1(o_class_fun, (py_obj_t)0xdeadbeef); |
| 505 | |
| 506 | // restore old __locals__ object |
| 507 | map_locals = old_locals; |
| 508 | |
| 509 | // create and return the new class |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 510 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 511 | o->kind = O_CLASS; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 512 | o->u_class.locals = class_locals; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 513 | return o; |
| 514 | } |
| 515 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 516 | py_obj_t py_builtin_range(py_obj_t o_arg) { |
Damien | 9fc7933 | 2013-10-23 00:01:10 +0100 | [diff] [blame] | 517 | return py_obj_new_range(0, py_get_int(o_arg), 1); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 520 | #ifdef WRITE_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 521 | FILE *fp_native = NULL; |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 522 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 523 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 524 | void rt_init(void) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 525 | q_append = qstr_from_str_static("append"); |
| 526 | q_print = qstr_from_str_static("print"); |
| 527 | q_len = qstr_from_str_static("len"); |
| 528 | q___build_class__ = qstr_from_str_static("__build_class__"); |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 529 | q___next__ = qstr_from_str_static("__next__"); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 530 | q_AttributeError = qstr_from_str_static("AttributeError"); |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 531 | q_IndexError = qstr_from_str_static("IndexError"); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 532 | q_KeyError = qstr_from_str_static("KeyError"); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 533 | q_NameError = qstr_from_str_static("NameError"); |
| 534 | q_TypeError = qstr_from_str_static("TypeError"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 535 | |
| 536 | py_const_none = py_obj_new_const("None"); |
| 537 | py_const_false = py_obj_new_const("False"); |
| 538 | py_const_true = py_obj_new_const("True"); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 539 | py_const_stop_iteration = py_obj_new_const("StopIteration"); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 540 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 541 | // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New()) |
| 542 | map_locals = map_globals = py_map_new(MAP_QSTR, 1); |
| 543 | py_qstr_map_lookup(map_globals, qstr_from_str_static("__name__"), true)->value = py_obj_new_str(qstr_from_str_static("__main__")); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 544 | |
| 545 | py_map_init(&map_builtins, MAP_QSTR, 3); |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 546 | py_qstr_map_lookup(&map_builtins, qstr_from_str_static("__repl_print__"), true)->value = rt_make_function_1(py_builtin___repl_print__); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 547 | py_qstr_map_lookup(&map_builtins, q_print, true)->value = rt_make_function_1(py_builtin_print); |
| 548 | py_qstr_map_lookup(&map_builtins, q_len, true)->value = rt_make_function_1(py_builtin_len); |
| 549 | py_qstr_map_lookup(&map_builtins, q___build_class__, true)->value = rt_make_function_2(py_builtin___build_class__); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 550 | py_qstr_map_lookup(&map_builtins, qstr_from_str_static("range"), true)->value = rt_make_function_1(py_builtin_range); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 551 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 552 | next_unique_code_id = 2; // 1 is reserved for the __main__ module scope |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 553 | unique_codes = NULL; |
| 554 | |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 555 | fun_list_append = rt_make_function_2(rt_list_append); |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 556 | fun_gen_instance_next = rt_make_function_1(rt_gen_instance_next); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 557 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 558 | #ifdef WRITE_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 559 | fp_native = fopen("out-native", "wb"); |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 560 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 563 | void rt_deinit(void) { |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 564 | #ifdef WRITE_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 565 | if (fp_native != NULL) { |
| 566 | fclose(fp_native); |
| 567 | } |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 568 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 569 | } |
| 570 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 571 | int rt_get_unique_code_id(bool is_main_module) { |
| 572 | if (is_main_module) { |
| 573 | return 1; |
| 574 | } else { |
| 575 | return next_unique_code_id++; |
| 576 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 579 | static void alloc_unique_codes(void) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 580 | if (unique_codes == NULL) { |
| 581 | unique_codes = m_new(py_code_t, next_unique_code_id); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 582 | for (int i = 0; i < next_unique_code_id; i++) { |
| 583 | unique_codes[i].kind = PY_CODE_NONE; |
| 584 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 585 | } |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 588 | void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 589 | alloc_unique_codes(); |
| 590 | |
| 591 | assert(unique_code_id < next_unique_code_id); |
| 592 | unique_codes[unique_code_id].kind = PY_CODE_BYTE; |
| 593 | unique_codes[unique_code_id].n_args = n_args; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 594 | unique_codes[unique_code_id].n_locals = n_locals; |
| 595 | unique_codes[unique_code_id].n_stack = n_stack; |
| 596 | unique_codes[unique_code_id].is_generator = is_generator; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 597 | unique_codes[unique_code_id].u_byte.code = code; |
| 598 | unique_codes[unique_code_id].u_byte.len = len; |
| 599 | |
| 600 | DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d\n", unique_code_id, code, len, n_args); |
| 601 | } |
| 602 | |
| 603 | void rt_assign_native_code(int unique_code_id, py_fun_t fun, uint len, int n_args) { |
| 604 | alloc_unique_codes(); |
| 605 | |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 606 | assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 607 | unique_codes[unique_code_id].kind = PY_CODE_NATIVE; |
| 608 | unique_codes[unique_code_id].n_args = n_args; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 609 | unique_codes[unique_code_id].n_locals = 0; |
| 610 | unique_codes[unique_code_id].n_stack = 0; |
| 611 | unique_codes[unique_code_id].is_generator = false; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 612 | unique_codes[unique_code_id].u_native.fun = fun; |
| 613 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 614 | #ifdef DEBUG_PRINT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 615 | DEBUG_printf("assign native code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args); |
| 616 | byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code |
| 617 | for (int i = 0; i < 128 && i < len; i++) { |
| 618 | if (i > 0 && i % 16 == 0) { |
| 619 | DEBUG_printf("\n"); |
| 620 | } |
| 621 | DEBUG_printf(" %02x", fun_data[i]); |
| 622 | } |
| 623 | DEBUG_printf("\n"); |
| 624 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 625 | #ifdef WRITE_NATIVE |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 626 | if (fp_native != NULL) { |
| 627 | fwrite(fun_data, len, 1, fp_native); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 628 | fflush(fp_native); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 629 | } |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 630 | #endif |
| 631 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 634 | void rt_assign_inline_asm_code(int unique_code_id, py_fun_t fun, uint len, int n_args) { |
| 635 | alloc_unique_codes(); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 636 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 637 | assert(1 <= unique_code_id && unique_code_id < next_unique_code_id); |
| 638 | unique_codes[unique_code_id].kind = PY_CODE_INLINE_ASM; |
| 639 | unique_codes[unique_code_id].n_args = n_args; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 640 | unique_codes[unique_code_id].n_locals = 0; |
| 641 | unique_codes[unique_code_id].n_stack = 0; |
| 642 | unique_codes[unique_code_id].is_generator = false; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 643 | unique_codes[unique_code_id].u_inline_asm.fun = fun; |
| 644 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 645 | #ifdef DEBUG_PRINT |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 646 | DEBUG_printf("assign inline asm code: id=%d fun=%p len=%u n_args=%d\n", unique_code_id, fun, len, n_args); |
| 647 | byte *fun_data = (byte*)(((machine_uint_t)fun) & (~1)); // need to clear lower bit in case it's thumb code |
| 648 | for (int i = 0; i < 128 && i < len; i++) { |
| 649 | if (i > 0 && i % 16 == 0) { |
| 650 | DEBUG_printf("\n"); |
| 651 | } |
| 652 | DEBUG_printf(" %02x", fun_data[i]); |
| 653 | } |
| 654 | DEBUG_printf("\n"); |
| 655 | |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 656 | #ifdef WRITE_NATIVE |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 657 | if (fp_native != NULL) { |
| 658 | fwrite(fun_data, len, 1, fp_native); |
| 659 | } |
Damien | a1ddfcc | 2013-10-10 23:25:50 +0100 | [diff] [blame] | 660 | #endif |
| 661 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 662 | } |
| 663 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 664 | bool py_obj_is_callable(py_obj_t o_in) { |
| 665 | if (IS_SMALL_INT(o_in)) { |
| 666 | return false; |
| 667 | } else { |
| 668 | py_obj_base_t *o = o_in; |
| 669 | switch (o->kind) { |
| 670 | case O_FUN_0: |
| 671 | case O_FUN_1: |
| 672 | case O_FUN_2: |
| 673 | case O_FUN_N: |
| 674 | case O_FUN_BC: |
| 675 | case O_FUN_ASM: |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 676 | // what about O_CLASS, and an O_OBJ that has a __call__ method? |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 677 | return true; |
| 678 | default: |
| 679 | return false; |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 684 | const char *py_obj_get_type_str(py_obj_t o_in) { |
| 685 | if (IS_SMALL_INT(o_in)) { |
| 686 | return "int"; |
| 687 | } else { |
| 688 | py_obj_base_t *o = o_in; |
| 689 | switch (o->kind) { |
| 690 | case O_CONST: |
| 691 | if (o == py_const_none) { |
| 692 | return "NoneType"; |
| 693 | } else { |
| 694 | return "bool"; |
| 695 | } |
| 696 | case O_STR: |
| 697 | return "str"; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 698 | #if MICROPY_ENABLE_FLOAT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 699 | case O_FLOAT: |
| 700 | return "float"; |
| 701 | #endif |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 702 | case O_FUN_0: |
| 703 | case O_FUN_1: |
| 704 | case O_FUN_2: |
| 705 | case O_FUN_N: |
| 706 | case O_FUN_BC: |
| 707 | return "function"; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 708 | case O_GEN_INSTANCE: |
| 709 | return "generator"; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 710 | case O_TUPLE: |
| 711 | return "tuple"; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 712 | case O_LIST: |
| 713 | return "list"; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 714 | case O_TUPLE_IT: |
| 715 | return "tuple_iterator"; |
| 716 | case O_LIST_IT: |
| 717 | return "list_iterator"; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 718 | case O_SET: |
| 719 | return "set"; |
| 720 | case O_MAP: |
| 721 | return "dict"; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 722 | case O_OBJ: |
| 723 | { |
| 724 | py_map_elem_t *qn = py_qstr_map_lookup(o->u_obj.class->u_class.locals, qstr_from_str_static("__qualname__"), false); |
| 725 | assert(qn != NULL); |
| 726 | assert(IS_O(qn->value, O_STR)); |
| 727 | return qstr_str(((py_obj_base_t*)qn->value)->u_str); |
| 728 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 729 | default: |
| 730 | assert(0); |
| 731 | return "UnknownType"; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | void py_obj_print(py_obj_t o_in) { |
| 737 | if (IS_SMALL_INT(o_in)) { |
| 738 | printf("%d", (int)FROM_SMALL_INT(o_in)); |
| 739 | } else { |
| 740 | py_obj_base_t *o = o_in; |
| 741 | switch (o->kind) { |
| 742 | case O_CONST: |
| 743 | printf("%s", o->id); |
| 744 | break; |
| 745 | case O_STR: |
| 746 | // TODO need to escape chars etc |
| 747 | printf("'%s'", qstr_str(o->u_str)); |
| 748 | break; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 749 | #if MICROPY_ENABLE_FLOAT |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 750 | case O_FLOAT: |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 751 | printf("%f", o->u_flt); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 752 | break; |
| 753 | #endif |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 754 | case O_EXCEPTION_0: |
| 755 | printf("%s", qstr_str(o->u_exc0.id)); |
| 756 | break; |
Damien | f086ecf | 2013-10-22 16:05:11 +0100 | [diff] [blame] | 757 | case O_EXCEPTION_N: |
| 758 | printf("%s: ", qstr_str(o->u_exc_n.id)); |
| 759 | printf(o->u_exc_n.args[0], o->u_exc_n.args[1], o->u_exc_n.args[2]); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 760 | break; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 761 | case O_GEN_INSTANCE: |
| 762 | printf("<generator object 'fun-name' at %p>", o); |
| 763 | break; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 764 | case O_TUPLE: |
| 765 | printf("("); |
| 766 | for (int i = 0; i < o->u_tuple_list.len; i++) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 767 | if (i > 0) { |
| 768 | printf(", "); |
| 769 | } |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 770 | py_obj_print(o->u_tuple_list.items[i]); |
| 771 | } |
| 772 | if (o->u_tuple_list.len == 1) { |
| 773 | printf(","); |
| 774 | } |
| 775 | printf(")"); |
| 776 | break; |
| 777 | case O_LIST: |
| 778 | printf("["); |
| 779 | for (int i = 0; i < o->u_tuple_list.len; i++) { |
| 780 | if (i > 0) { |
| 781 | printf(", "); |
| 782 | } |
| 783 | py_obj_print(o->u_tuple_list.items[i]); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 784 | } |
| 785 | printf("]"); |
| 786 | break; |
| 787 | case O_SET: |
| 788 | { |
| 789 | bool first = true; |
| 790 | printf("{"); |
| 791 | for (int i = 0; i < o->u_set.alloc; i++) { |
| 792 | if (o->u_set.table[i] != NULL) { |
| 793 | if (!first) { |
| 794 | printf(", "); |
| 795 | } |
| 796 | first = false; |
| 797 | py_obj_print(o->u_set.table[i]); |
| 798 | } |
| 799 | } |
| 800 | printf("}"); |
| 801 | break; |
| 802 | } |
| 803 | case O_MAP: |
| 804 | { |
| 805 | bool first = true; |
| 806 | printf("{"); |
| 807 | for (int i = 0; i < o->u_map.alloc; i++) { |
| 808 | if (o->u_map.table[i].key != NULL) { |
| 809 | if (!first) { |
| 810 | printf(", "); |
| 811 | } |
| 812 | first = false; |
| 813 | py_obj_print(o->u_map.table[i].key); |
| 814 | printf(": "); |
| 815 | py_obj_print(o->u_map.table[i].value); |
| 816 | } |
| 817 | } |
| 818 | printf("}"); |
| 819 | break; |
| 820 | } |
| 821 | default: |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 822 | printf("<? %d>", o->kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 823 | assert(0); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | int rt_is_true(py_obj_t arg) { |
| 829 | DEBUG_OP_printf("is true %p\n", arg); |
| 830 | if (IS_SMALL_INT(arg)) { |
| 831 | if (FROM_SMALL_INT(arg) == 0) { |
| 832 | return 0; |
| 833 | } else { |
| 834 | return 1; |
| 835 | } |
| 836 | } else if (arg == py_const_none) { |
| 837 | return 0; |
| 838 | } else if (arg == py_const_false) { |
| 839 | return 0; |
| 840 | } else if (arg == py_const_true) { |
| 841 | return 1; |
| 842 | } else { |
| 843 | assert(0); |
| 844 | return 0; |
| 845 | } |
| 846 | } |
| 847 | |
Damien | 9fc7933 | 2013-10-23 00:01:10 +0100 | [diff] [blame] | 848 | int py_get_int(py_obj_t arg) { |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 849 | if (arg == py_const_false) { |
| 850 | return 0; |
| 851 | } else if (arg == py_const_true) { |
| 852 | return 1; |
| 853 | } else if (IS_SMALL_INT(arg)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 854 | return FROM_SMALL_INT(arg); |
| 855 | } else { |
| 856 | assert(0); |
| 857 | return 0; |
| 858 | } |
| 859 | } |
| 860 | |
Damien | 9fc7933 | 2013-10-23 00:01:10 +0100 | [diff] [blame] | 861 | qstr py_get_qstr(py_obj_t arg) { |
| 862 | if (IS_O(arg, O_STR)) { |
| 863 | return ((py_obj_base_t*)arg)->u_str; |
| 864 | } else { |
| 865 | assert(0); |
| 866 | return 0; |
| 867 | } |
| 868 | } |
| 869 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 870 | py_obj_t rt_load_const_str(qstr qstr) { |
| 871 | DEBUG_OP_printf("load '%s'\n", qstr_str(qstr)); |
| 872 | return py_obj_new_str(qstr); |
| 873 | } |
| 874 | |
| 875 | py_obj_t rt_load_name(qstr qstr) { |
| 876 | // logic: search locals, globals, builtins |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 877 | DEBUG_OP_printf("load name %s\n", qstr_str(qstr)); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 878 | py_map_elem_t *elem = py_qstr_map_lookup(map_locals, qstr, false); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 879 | if (elem == NULL) { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 880 | elem = py_qstr_map_lookup(map_globals, qstr, false); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 881 | if (elem == NULL) { |
| 882 | elem = py_qstr_map_lookup(&map_builtins, qstr, false); |
| 883 | if (elem == NULL) { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 884 | nlr_jump(py_obj_new_exception_2(q_NameError, "name '%s' is not defined", qstr_str(qstr), NULL)); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 885 | } |
| 886 | } |
| 887 | } |
| 888 | return elem->value; |
| 889 | } |
| 890 | |
| 891 | py_obj_t rt_load_global(qstr qstr) { |
| 892 | // logic: search globals, builtins |
| 893 | DEBUG_OP_printf("load global %s\n", qstr_str(qstr)); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 894 | py_map_elem_t *elem = py_qstr_map_lookup(map_globals, qstr, false); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 895 | if (elem == NULL) { |
| 896 | elem = py_qstr_map_lookup(&map_builtins, qstr, false); |
| 897 | if (elem == NULL) { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 898 | nlr_jump(py_obj_new_exception_2(q_NameError, "name '%s' is not defined", qstr_str(qstr), NULL)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | return elem->value; |
| 902 | } |
| 903 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 904 | py_obj_t rt_load_build_class(void) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 905 | DEBUG_OP_printf("load_build_class\n"); |
| 906 | py_map_elem_t *elem = py_qstr_map_lookup(&map_builtins, q___build_class__, false); |
| 907 | if (elem == NULL) { |
| 908 | printf("name doesn't exist: __build_class__\n"); |
| 909 | assert(0); |
| 910 | } |
| 911 | return elem->value; |
| 912 | } |
| 913 | |
| 914 | void rt_store_name(qstr qstr, py_obj_t obj) { |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 915 | DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 916 | py_qstr_map_lookup(map_locals, qstr, true)->value = obj; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | void rt_store_global(qstr qstr, py_obj_t obj) { |
| 920 | DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 921 | py_qstr_map_lookup(map_globals, qstr, true)->value = obj; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | py_obj_t rt_unary_op(int op, py_obj_t arg) { |
| 925 | assert(0); |
| 926 | return py_const_none; |
| 927 | } |
| 928 | |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 929 | uint get_index(py_obj_base_t *base, py_obj_t index) { |
| 930 | // assumes base is O_TUPLE or O_LIST |
| 931 | // TODO False and True are considered 0 and 1 for indexing purposes |
| 932 | int len = base->u_tuple_list.len; |
| 933 | if (IS_SMALL_INT(index)) { |
| 934 | int i = FROM_SMALL_INT(index); |
| 935 | if (i < 0) { |
| 936 | i += len; |
| 937 | } |
| 938 | if (i < 0 || i >= len) { |
| 939 | nlr_jump(py_obj_new_exception_2(q_IndexError, "%s index out of range", py_obj_get_type_str(base), NULL)); |
| 940 | } |
| 941 | return i; |
| 942 | } else { |
| 943 | nlr_jump(py_obj_new_exception_2(q_TypeError, "%s indices must be integers, not %s", py_obj_get_type_str(base), py_obj_get_type_str(index))); |
| 944 | } |
| 945 | } |
| 946 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 947 | py_obj_t rt_binary_op(int op, py_obj_t lhs, py_obj_t rhs) { |
| 948 | DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs); |
| 949 | if (op == RT_BINARY_OP_SUBSCR) { |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 950 | if ((IS_O(lhs, O_TUPLE) || IS_O(lhs, O_LIST))) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 951 | // tuple/list load |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 952 | uint index = get_index(lhs, rhs); |
| 953 | return ((py_obj_base_t*)lhs)->u_tuple_list.items[index]; |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 954 | } else if (IS_O(lhs, O_MAP)) { |
| 955 | // map load |
| 956 | py_map_elem_t *elem = py_map_lookup(lhs, rhs, false); |
| 957 | if (elem == NULL) { |
| 958 | nlr_jump(py_obj_new_exception_2(q_KeyError, "<value>", NULL, NULL)); |
| 959 | } else { |
| 960 | return elem->value; |
| 961 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 962 | } else { |
| 963 | assert(0); |
| 964 | } |
| 965 | } else if (IS_SMALL_INT(lhs) && IS_SMALL_INT(rhs)) { |
| 966 | py_small_int_t val; |
| 967 | switch (op) { |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 968 | case RT_BINARY_OP_OR: |
| 969 | case RT_BINARY_OP_INPLACE_OR: val = FROM_SMALL_INT(lhs) | FROM_SMALL_INT(rhs); break; |
| 970 | case RT_BINARY_OP_XOR: |
| 971 | case RT_BINARY_OP_INPLACE_XOR: val = FROM_SMALL_INT(lhs) ^ FROM_SMALL_INT(rhs); break; |
| 972 | case RT_BINARY_OP_AND: |
| 973 | case RT_BINARY_OP_INPLACE_AND: val = FROM_SMALL_INT(lhs) & FROM_SMALL_INT(rhs); break; |
| 974 | case RT_BINARY_OP_LSHIFT: |
| 975 | case RT_BINARY_OP_INPLACE_LSHIFT: val = FROM_SMALL_INT(lhs) << FROM_SMALL_INT(rhs); break; |
| 976 | case RT_BINARY_OP_RSHIFT: |
| 977 | case RT_BINARY_OP_INPLACE_RSHIFT: val = FROM_SMALL_INT(lhs) >> FROM_SMALL_INT(rhs); break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 978 | case RT_BINARY_OP_ADD: |
| 979 | case RT_BINARY_OP_INPLACE_ADD: val = FROM_SMALL_INT(lhs) + FROM_SMALL_INT(rhs); break; |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 980 | case RT_BINARY_OP_SUBTRACT: |
| 981 | case RT_BINARY_OP_INPLACE_SUBTRACT: val = FROM_SMALL_INT(lhs) - FROM_SMALL_INT(rhs); break; |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 982 | case RT_BINARY_OP_MULTIPLY: |
| 983 | case RT_BINARY_OP_INPLACE_MULTIPLY: val = FROM_SMALL_INT(lhs) * FROM_SMALL_INT(rhs); break; |
| 984 | case RT_BINARY_OP_FLOOR_DIVIDE: |
| 985 | case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = FROM_SMALL_INT(lhs) / FROM_SMALL_INT(rhs); break; |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 986 | #if MICROPY_ENABLE_FLOAT |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 987 | case RT_BINARY_OP_TRUE_DIVIDE: |
| 988 | case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return py_obj_new_float((float_t)FROM_SMALL_INT(lhs) / (float_t)FROM_SMALL_INT(rhs)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 989 | #endif |
| 990 | default: printf("%d\n", op); assert(0); val = 0; |
| 991 | } |
| 992 | if (fit_small_int(val)) { |
| 993 | return TO_SMALL_INT(val); |
| 994 | } |
| 995 | } else if (IS_O(lhs, O_STR) && IS_O(rhs, O_STR)) { |
| 996 | const char *lhs_str = qstr_str(((py_obj_base_t*)lhs)->u_str); |
| 997 | const char *rhs_str = qstr_str(((py_obj_base_t*)rhs)->u_str); |
| 998 | char *val; |
| 999 | switch (op) { |
| 1000 | case RT_BINARY_OP_ADD: |
| 1001 | case RT_BINARY_OP_INPLACE_ADD: val = m_new(char, strlen(lhs_str) + strlen(rhs_str) + 1); strcpy(val, lhs_str); strcat(val, rhs_str); break; |
| 1002 | default: printf("%d\n", op); assert(0); val = NULL; |
| 1003 | } |
| 1004 | return py_obj_new_str(qstr_from_str_take(val)); |
| 1005 | } |
| 1006 | assert(0); |
| 1007 | return py_const_none; |
| 1008 | } |
| 1009 | |
| 1010 | py_obj_t rt_compare_op(int op, py_obj_t lhs, py_obj_t rhs) { |
| 1011 | DEBUG_OP_printf("compare %d %p %p\n", op, lhs, rhs); |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 1012 | |
| 1013 | // deal with == and != |
| 1014 | if (op == RT_COMPARE_OP_EQUAL || op == RT_COMPARE_OP_NOT_EQUAL) { |
| 1015 | if (py_obj_equal(lhs, rhs)) { |
| 1016 | if (op == RT_COMPARE_OP_EQUAL) { |
| 1017 | return py_const_true; |
| 1018 | } else { |
| 1019 | return py_const_false; |
| 1020 | } |
| 1021 | } else { |
| 1022 | if (op == RT_COMPARE_OP_EQUAL) { |
| 1023 | return py_const_false; |
| 1024 | } else { |
| 1025 | return py_const_true; |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | // deal with small ints |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1031 | if (IS_SMALL_INT(lhs) && IS_SMALL_INT(rhs)) { |
| 1032 | int cmp; |
| 1033 | switch (op) { |
| 1034 | case RT_COMPARE_OP_LESS: cmp = FROM_SMALL_INT(lhs) < FROM_SMALL_INT(rhs); break; |
| 1035 | case RT_COMPARE_OP_MORE: cmp = FROM_SMALL_INT(lhs) > FROM_SMALL_INT(rhs); break; |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 1036 | case RT_COMPARE_OP_LESS_EQUAL: cmp = FROM_SMALL_INT(lhs) <= FROM_SMALL_INT(rhs); break; |
| 1037 | case RT_COMPARE_OP_MORE_EQUAL: cmp = FROM_SMALL_INT(lhs) >= FROM_SMALL_INT(rhs); break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1038 | default: assert(0); cmp = 0; |
| 1039 | } |
| 1040 | if (cmp) { |
| 1041 | return py_const_true; |
| 1042 | } else { |
| 1043 | return py_const_false; |
| 1044 | } |
| 1045 | } |
Damien | 7b2d3f3 | 2013-10-22 16:53:02 +0100 | [diff] [blame] | 1046 | |
| 1047 | // not implemented |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1048 | assert(0); |
| 1049 | return py_const_none; |
| 1050 | } |
| 1051 | |
| 1052 | py_obj_t rt_make_function_from_id(int unique_code_id) { |
Damien | b05d707 | 2013-10-05 13:37:10 +0100 | [diff] [blame] | 1053 | DEBUG_OP_printf("make_function_from_id %d\n", unique_code_id); |
| 1054 | if (unique_code_id < 1 || unique_code_id >= next_unique_code_id) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1055 | // illegal code id |
| 1056 | return py_const_none; |
| 1057 | } |
| 1058 | py_code_t *c = &unique_codes[unique_code_id]; |
| 1059 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1060 | switch (c->kind) { |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 1061 | case PY_CODE_BYTE: |
| 1062 | o->kind = O_FUN_BC; |
| 1063 | o->u_fun_bc.n_args = c->n_args; |
| 1064 | o->u_fun_bc.code = c->u_byte.code; |
| 1065 | o->u_fun_bc.len = c->u_byte.len; |
| 1066 | break; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1067 | case PY_CODE_NATIVE: |
| 1068 | switch (c->n_args) { |
| 1069 | case 0: o->kind = O_FUN_0; break; |
| 1070 | case 1: o->kind = O_FUN_1; break; |
| 1071 | case 2: o->kind = O_FUN_2; break; |
| 1072 | default: assert(0); |
| 1073 | } |
| 1074 | o->u_fun.fun = c->u_native.fun; |
| 1075 | break; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 1076 | case PY_CODE_INLINE_ASM: |
| 1077 | o->kind = O_FUN_ASM; |
| 1078 | o->u_fun_asm.n_args = c->n_args; |
| 1079 | o->u_fun_asm.fun = c->u_inline_asm.fun; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1080 | break; |
| 1081 | default: |
| 1082 | assert(0); |
| 1083 | } |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1084 | |
| 1085 | // check for generator functions and if so wrap in generator object |
| 1086 | if (c->is_generator) { |
| 1087 | py_obj_base_t *o2 = m_new(py_obj_base_t, 1); |
| 1088 | o2->kind = O_GEN_WRAP; |
| 1089 | // we have at least 3 locals so the bc can write back fast[0,1,2] safely; should improve how this is done |
| 1090 | o2->u_gen_wrap.n_state = (c->n_locals < 3 ? 3 : c->n_locals) + c->n_stack; |
| 1091 | o2->u_gen_wrap.fun = o; |
| 1092 | o = o2; |
| 1093 | } |
| 1094 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1095 | return o; |
| 1096 | } |
| 1097 | |
| 1098 | py_obj_t rt_make_function_0(py_fun_0_t fun) { |
| 1099 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1100 | o->kind = O_FUN_0; |
| 1101 | o->u_fun.fun = fun; |
| 1102 | return o; |
| 1103 | } |
| 1104 | |
| 1105 | py_obj_t rt_make_function_1(py_fun_1_t fun) { |
| 1106 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1107 | o->kind = O_FUN_1; |
| 1108 | o->u_fun.fun = fun; |
| 1109 | return o; |
| 1110 | } |
| 1111 | |
| 1112 | py_obj_t rt_make_function_2(py_fun_2_t fun) { |
| 1113 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1114 | o->kind = O_FUN_2; |
| 1115 | o->u_fun.fun = fun; |
| 1116 | return o; |
| 1117 | } |
| 1118 | |
| 1119 | py_obj_t rt_make_function(int n_args, py_fun_t code) { |
| 1120 | // assumes code is a pointer to a py_fun_t (i think this is safe...) |
| 1121 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1122 | o->kind = O_FUN_N; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1123 | o->u_fun.n_args = n_args; |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 1124 | o->u_fun.fun = code; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1125 | return o; |
| 1126 | } |
| 1127 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1128 | py_obj_t rt_call_function_0(py_obj_t fun) { |
| 1129 | return rt_call_function_n(fun, 0, NULL); |
| 1130 | } |
| 1131 | |
| 1132 | py_obj_t rt_call_function_1(py_obj_t fun, py_obj_t arg) { |
| 1133 | return rt_call_function_n(fun, 1, &arg); |
| 1134 | } |
| 1135 | |
| 1136 | py_obj_t rt_call_function_2(py_obj_t fun, py_obj_t arg1, py_obj_t arg2) { |
| 1137 | py_obj_t args[2]; |
| 1138 | args[1] = arg1; |
| 1139 | args[0] = arg2; |
| 1140 | return rt_call_function_n(fun, 2, args); |
| 1141 | } |
| 1142 | |
| 1143 | typedef machine_uint_t (*inline_asm_fun_0_t)(); |
| 1144 | typedef machine_uint_t (*inline_asm_fun_1_t)(machine_uint_t); |
| 1145 | typedef machine_uint_t (*inline_asm_fun_2_t)(machine_uint_t, machine_uint_t); |
| 1146 | typedef machine_uint_t (*inline_asm_fun_3_t)(machine_uint_t, machine_uint_t, machine_uint_t); |
| 1147 | |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1148 | // convert a Python object to a sensible value for inline asm |
| 1149 | machine_uint_t rt_convert_obj_for_inline_asm(py_obj_t obj) { |
| 1150 | // TODO for byte_array, pass pointer to the array |
| 1151 | if (IS_SMALL_INT(obj)) { |
| 1152 | return FROM_SMALL_INT(obj); |
| 1153 | } else if (obj == py_const_none) { |
| 1154 | return 0; |
| 1155 | } else if (obj == py_const_false) { |
| 1156 | return 0; |
| 1157 | } else if (obj == py_const_true) { |
| 1158 | return 1; |
| 1159 | } else { |
| 1160 | py_obj_base_t *o = obj; |
| 1161 | switch (o->kind) { |
| 1162 | case O_STR: |
| 1163 | // pointer to the string (it's probably constant though!) |
| 1164 | return (machine_uint_t)qstr_str(o->u_str); |
| 1165 | |
Damien | 3ef4abb | 2013-10-12 16:53:13 +0100 | [diff] [blame] | 1166 | #if MICROPY_ENABLE_FLOAT |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1167 | case O_FLOAT: |
| 1168 | // convert float to int (could also pass in float registers) |
| 1169 | return (machine_int_t)o->u_flt; |
Damien | c025ebb | 2013-10-12 14:30:21 +0100 | [diff] [blame] | 1170 | #endif |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1171 | |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1172 | case O_TUPLE: |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1173 | case O_LIST: |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1174 | // pointer to start of tuple/list (could pass length, but then could use len(x) for that) |
| 1175 | return (machine_uint_t)o->u_tuple_list.items; |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1176 | |
| 1177 | default: |
| 1178 | // just pass along a pointer to the object |
| 1179 | return (machine_uint_t)obj; |
| 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | // convert a return value from inline asm to a sensible Python object |
| 1185 | py_obj_t rt_convert_val_from_inline_asm(machine_uint_t val) { |
| 1186 | return TO_SMALL_INT(val); |
| 1187 | } |
| 1188 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1189 | // args are in reverse order in the array |
| 1190 | py_obj_t rt_call_function_n(py_obj_t fun, int n_args, const py_obj_t *args) { |
| 1191 | int n_args_fun = 0; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1192 | if (IS_O(fun, O_FUN_0)) { |
| 1193 | py_obj_base_t *o = fun; |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1194 | if (n_args != 0) { |
| 1195 | n_args_fun = 0; |
| 1196 | goto bad_n_args; |
| 1197 | } |
| 1198 | DEBUG_OP_printf("calling native %p()\n", o->u_fun.fun); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1199 | return ((py_fun_0_t)o->u_fun.fun)(); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1200 | |
| 1201 | } else if (IS_O(fun, O_FUN_1)) { |
| 1202 | py_obj_base_t *o = fun; |
| 1203 | if (n_args != 1) { |
| 1204 | n_args_fun = 1; |
| 1205 | goto bad_n_args; |
| 1206 | } |
| 1207 | DEBUG_OP_printf("calling native %p(%p)\n", o->u_fun.fun, args[0]); |
| 1208 | return ((py_fun_1_t)o->u_fun.fun)(args[0]); |
| 1209 | |
| 1210 | } else if (IS_O(fun, O_FUN_2)) { |
| 1211 | py_obj_base_t *o = fun; |
| 1212 | if (n_args != 2) { |
| 1213 | n_args_fun = 2; |
| 1214 | goto bad_n_args; |
| 1215 | } |
| 1216 | DEBUG_OP_printf("calling native %p(%p, %p)\n", o->u_fun.fun, args[1], args[0]); |
| 1217 | return ((py_fun_2_t)o->u_fun.fun)(args[1], args[0]); |
| 1218 | |
| 1219 | // TODO O_FUN_N |
| 1220 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1221 | } else if (IS_O(fun, O_FUN_BC)) { |
| 1222 | py_obj_base_t *o = fun; |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1223 | if (n_args != o->u_fun_bc.n_args) { |
| 1224 | n_args_fun = o->u_fun_bc.n_args; |
| 1225 | goto bad_n_args; |
| 1226 | } |
| 1227 | DEBUG_OP_printf("calling byte code %p(n_args=%d)\n", o->u_fun_bc.code, n_args); |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1228 | return py_execute_byte_code(o->u_fun_bc.code, args, n_args); |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1229 | |
Damien | e4af64f | 2013-10-06 12:04:13 +0100 | [diff] [blame] | 1230 | } else if (IS_O(fun, O_FUN_ASM)) { |
| 1231 | py_obj_base_t *o = fun; |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1232 | if (n_args != o->u_fun_asm.n_args) { |
| 1233 | n_args_fun = o->u_fun_asm.n_args; |
| 1234 | goto bad_n_args; |
| 1235 | } |
| 1236 | DEBUG_OP_printf("calling inline asm %p(n_args=%d)\n", o->u_fun_asm.fun, n_args); |
| 1237 | machine_uint_t ret; |
| 1238 | if (n_args == 0) { |
| 1239 | ret = ((inline_asm_fun_0_t)o->u_fun_asm.fun)(); |
| 1240 | } else if (n_args == 1) { |
| 1241 | ret = ((inline_asm_fun_1_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[0])); |
| 1242 | } else if (n_args == 2) { |
| 1243 | ret = ((inline_asm_fun_2_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[1]), rt_convert_obj_for_inline_asm(args[0])); |
| 1244 | } else if (n_args == 3) { |
| 1245 | ret = ((inline_asm_fun_3_t)o->u_fun_asm.fun)(rt_convert_obj_for_inline_asm(args[2]), rt_convert_obj_for_inline_asm(args[1]), rt_convert_obj_for_inline_asm(args[0])); |
| 1246 | } else { |
| 1247 | assert(0); |
| 1248 | ret = 0; |
| 1249 | } |
| 1250 | return rt_convert_val_from_inline_asm(ret); |
| 1251 | |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1252 | } else if (IS_O(fun, O_GEN_WRAP)) { |
| 1253 | py_obj_base_t *o = fun; |
| 1254 | py_obj_base_t *o_fun = o->u_gen_wrap.fun; |
| 1255 | assert(o_fun->kind == O_FUN_BC); // TODO |
| 1256 | if (n_args != o_fun->u_fun_bc.n_args) { |
| 1257 | n_args_fun = o_fun->u_fun_bc.n_args; |
| 1258 | goto bad_n_args; |
| 1259 | } |
| 1260 | py_obj_t *state = m_new(py_obj_t, 1 + o->u_gen_wrap.n_state); |
| 1261 | // put function object at first slot in state (to keep u_gen_instance small) |
| 1262 | state[0] = o_fun; |
| 1263 | // init args |
| 1264 | for (int i = 0; i < n_args; i++) { |
| 1265 | state[1 + i] = args[n_args - 1 - i]; |
| 1266 | } |
| 1267 | py_obj_base_t *o2 = m_new(py_obj_base_t, 1); |
| 1268 | o2->kind = O_GEN_INSTANCE; |
| 1269 | o2->u_gen_instance.state = state; |
| 1270 | o2->u_gen_instance.ip = o_fun->u_fun_bc.code; |
| 1271 | o2->u_gen_instance.sp = state + o->u_gen_wrap.n_state; |
| 1272 | return o2; |
| 1273 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1274 | } else if (IS_O(fun, O_BOUND_METH)) { |
| 1275 | py_obj_base_t *o = fun; |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1276 | DEBUG_OP_printf("calling bound method %p(self=%p, n_args=%d)\n", o->u_bound_meth.meth, o->u_bound_meth.self, n_args); |
| 1277 | if (n_args == 0) { |
| 1278 | return rt_call_function_n(o->u_bound_meth.meth, 1, &o->u_bound_meth.self); |
| 1279 | } else if (n_args == 1) { |
| 1280 | py_obj_t args2[2]; |
| 1281 | args2[1] = o->u_bound_meth.self; |
| 1282 | args2[0] = args[0]; |
| 1283 | return rt_call_function_n(o->u_bound_meth.meth, 2, args2); |
| 1284 | } else { |
| 1285 | // TODO not implemented |
| 1286 | assert(0); |
| 1287 | return py_const_none; |
| 1288 | //return rt_call_function_2(o->u_bound_meth.meth, n_args + 1, o->u_bound_meth.self + args); |
| 1289 | } |
| 1290 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1291 | } else if (IS_O(fun, O_CLASS)) { |
| 1292 | // instantiate an instance of a class |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1293 | if (n_args != 0) { |
| 1294 | n_args_fun = 0; |
| 1295 | goto bad_n_args; |
| 1296 | } |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1297 | DEBUG_OP_printf("instantiate object of class %p with no args\n", fun); |
| 1298 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1299 | o->kind = O_OBJ; |
| 1300 | o->u_obj.class = fun; |
| 1301 | o->u_obj.members = py_map_new(MAP_QSTR, 0); |
| 1302 | return o; |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1303 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1304 | } else { |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1305 | printf("fun %p %d\n", fun, ((py_obj_base_t*)fun)->kind); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1306 | assert(0); |
| 1307 | return py_const_none; |
| 1308 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1309 | |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1310 | bad_n_args: |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1311 | nlr_jump(py_obj_new_exception_2(q_TypeError, "function takes %d positional arguments but %d were given", (const char*)(machine_int_t)n_args_fun, (const char*)(machine_int_t)n_args)); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1314 | // args contains: arg(n_args-1) arg(n_args-2) ... arg(0) self/NULL fun |
| 1315 | // if n_args==0 then there are only self/NULL and fun |
| 1316 | py_obj_t rt_call_method_n(int n_args, const py_obj_t *args) { |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 1317 | DEBUG_OP_printf("call method %p(self=%p, n_args=%d)\n", args[n_args + 1], args[n_args], n_args); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1318 | return rt_call_function_n(args[n_args + 1], n_args + ((args[n_args] == NULL) ? 0 : 1), args); |
| 1319 | } |
| 1320 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1321 | // items are in reverse order |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1322 | py_obj_t rt_build_tuple(int n_args, py_obj_t *items) { |
| 1323 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1324 | o->kind = O_TUPLE; |
| 1325 | o->u_tuple_list.alloc = n_args < 4 ? 4 : n_args; |
| 1326 | o->u_tuple_list.len = n_args; |
| 1327 | o->u_tuple_list.items = m_new(py_obj_t, o->u_tuple_list.alloc); |
| 1328 | for (int i = 0; i < n_args; i++) { |
| 1329 | o->u_tuple_list.items[i] = items[n_args - i - 1]; |
| 1330 | } |
| 1331 | return o; |
| 1332 | } |
| 1333 | |
| 1334 | // items are in reverse order |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1335 | py_obj_t rt_build_list(int n_args, py_obj_t *items) { |
| 1336 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1337 | o->kind = O_LIST; |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1338 | o->u_tuple_list.alloc = n_args < 4 ? 4 : n_args; |
| 1339 | o->u_tuple_list.len = n_args; |
| 1340 | o->u_tuple_list.items = m_new(py_obj_t, o->u_tuple_list.alloc); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1341 | for (int i = 0; i < n_args; i++) { |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1342 | o->u_tuple_list.items[i] = items[n_args - i - 1]; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1343 | } |
| 1344 | return o; |
| 1345 | } |
| 1346 | |
| 1347 | py_obj_t py_set_lookup(py_obj_t o_in, py_obj_t index, bool add_if_not_found) { |
| 1348 | assert(IS_O(o_in, O_SET)); |
| 1349 | py_obj_base_t *o = o_in; |
| 1350 | int hash = py_obj_hash(index); |
| 1351 | int pos = hash % o->u_set.alloc; |
| 1352 | for (;;) { |
| 1353 | py_obj_t elem = o->u_set.table[pos]; |
| 1354 | if (elem == NULL) { |
| 1355 | // not in table |
| 1356 | if (add_if_not_found) { |
| 1357 | if (o->u_set.used + 1 >= o->u_set.alloc) { |
| 1358 | // not enough room in table, rehash it |
| 1359 | int old_alloc = o->u_set.alloc; |
| 1360 | py_obj_t *old_table = o->u_set.table; |
| 1361 | o->u_set.alloc = get_doubling_prime_greater_or_equal_to(o->u_set.alloc + 1); |
| 1362 | o->u_set.used = 0; |
| 1363 | o->u_set.table = m_new(py_obj_t, o->u_set.alloc); |
| 1364 | for (int i = 0; i < old_alloc; i++) { |
| 1365 | if (old_table[i] != NULL) { |
| 1366 | py_set_lookup(o, old_table[i], true); |
| 1367 | } |
| 1368 | } |
| 1369 | m_free(old_table); |
| 1370 | // restart the search for the new element |
| 1371 | pos = hash % o->u_set.alloc; |
| 1372 | } else { |
| 1373 | o->u_set.used += 1; |
| 1374 | o->u_set.table[pos] = index; |
| 1375 | return index; |
| 1376 | } |
| 1377 | } else { |
| 1378 | return NULL; |
| 1379 | } |
| 1380 | } else if (py_obj_equal(elem, index)) { |
| 1381 | // found it |
| 1382 | return elem; |
| 1383 | } else { |
| 1384 | // not yet found, keep searching in this table |
| 1385 | pos = (pos + 1) % o->u_set.alloc; |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | py_obj_t rt_build_set(int n_args, py_obj_t *items) { |
| 1391 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1392 | o->kind = O_SET; |
| 1393 | o->u_set.alloc = get_doubling_prime_greater_or_equal_to(n_args + 1); |
| 1394 | o->u_set.used = 0; |
| 1395 | o->u_set.table = m_new(py_obj_t, o->u_set.alloc); |
| 1396 | for (int i = 0; i < o->u_set.alloc; i++) { |
| 1397 | o->u_set.table[i] = NULL; |
| 1398 | } |
| 1399 | for (int i = 0; i < n_args; i++) { |
| 1400 | py_set_lookup(o, items[i], true); |
| 1401 | } |
| 1402 | return o; |
| 1403 | } |
| 1404 | |
Damien | c12aa46 | 2013-10-16 20:57:49 +0100 | [diff] [blame] | 1405 | py_obj_t rt_store_set(py_obj_t set, py_obj_t item) { |
| 1406 | py_set_lookup(set, item, true); |
| 1407 | return set; |
| 1408 | } |
| 1409 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1410 | py_obj_t rt_build_map(int n_args) { |
| 1411 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1412 | o->kind = O_MAP; |
| 1413 | py_map_init(&o->u_map, MAP_PY_OBJ, n_args); |
| 1414 | return o; |
| 1415 | } |
| 1416 | |
| 1417 | py_obj_t rt_store_map(py_obj_t map, py_obj_t key, py_obj_t value) { |
| 1418 | assert(IS_O(map, O_MAP)); // should always be |
| 1419 | py_map_lookup(map, key, true)->value = value; |
| 1420 | return map; |
| 1421 | } |
| 1422 | |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1423 | py_obj_t build_bound_method(py_obj_t self, py_obj_t meth) { |
| 1424 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1425 | o->kind = O_BOUND_METH; |
| 1426 | o->u_bound_meth.meth = meth; |
| 1427 | o->u_bound_meth.self = self; |
| 1428 | return o; |
| 1429 | } |
| 1430 | |
| 1431 | py_obj_t rt_load_attr(py_obj_t base, qstr attr) { |
| 1432 | DEBUG_OP_printf("load attr %s\n", qstr_str(attr)); |
| 1433 | if (IS_O(base, O_LIST) && attr == q_append) { |
| 1434 | return build_bound_method(base, fun_list_append); |
| 1435 | } else if (IS_O(base, O_CLASS)) { |
| 1436 | py_obj_base_t *o = base; |
| 1437 | py_map_elem_t *elem = py_qstr_map_lookup(o->u_class.locals, attr, false); |
| 1438 | if (elem == NULL) { |
| 1439 | goto no_attr; |
| 1440 | } |
| 1441 | return elem->value; |
| 1442 | } else if (IS_O(base, O_OBJ)) { |
| 1443 | // logic: look in obj members then class locals (TODO check this against CPython) |
| 1444 | py_obj_base_t *o = base; |
| 1445 | py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.members, attr, false); |
| 1446 | if (elem != NULL) { |
| 1447 | // object member, always treated as a value |
| 1448 | return elem->value; |
| 1449 | } |
| 1450 | elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false); |
| 1451 | if (elem != NULL) { |
| 1452 | if (py_obj_is_callable(elem->value)) { |
| 1453 | // class member is callable so build a bound method |
| 1454 | return build_bound_method(base, elem->value); |
| 1455 | } else { |
| 1456 | // class member is a value, so just return that value |
| 1457 | return elem->value; |
| 1458 | } |
| 1459 | } |
| 1460 | goto no_attr; |
| 1461 | } |
| 1462 | |
| 1463 | no_attr: |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1464 | nlr_jump(py_obj_new_exception_2(q_AttributeError, "'%s' object has no attribute '%s'", py_obj_get_type_str(base), qstr_str(attr))); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | void rt_load_method(py_obj_t base, qstr attr, py_obj_t *dest) { |
| 1468 | DEBUG_OP_printf("load method %s\n", qstr_str(attr)); |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1469 | if (IS_O(base, O_GEN_INSTANCE) && attr == q___next__) { |
| 1470 | dest[1] = fun_gen_instance_next; |
| 1471 | dest[0] = base; |
| 1472 | return; |
| 1473 | } else if (IS_O(base, O_LIST) && attr == q_append) { |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1474 | dest[1] = fun_list_append; |
| 1475 | dest[0] = base; |
| 1476 | return; |
| 1477 | } else if (IS_O(base, O_OBJ)) { |
| 1478 | // logic: look in obj members then class locals (TODO check this against CPython) |
| 1479 | py_obj_base_t *o = base; |
| 1480 | py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.members, attr, false); |
| 1481 | if (elem != NULL) { |
| 1482 | // object member, always treated as a value |
| 1483 | dest[1] = elem->value; |
| 1484 | dest[0] = NULL; |
| 1485 | return; |
| 1486 | } |
| 1487 | elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false); |
| 1488 | if (elem != NULL) { |
| 1489 | if (py_obj_is_callable(elem->value)) { |
| 1490 | // class member is callable so build a bound method |
| 1491 | dest[1] = elem->value; |
| 1492 | dest[0] = base; |
| 1493 | return; |
| 1494 | } else { |
| 1495 | // class member is a value, so just return that value |
| 1496 | dest[1] = elem->value; |
| 1497 | dest[0] = NULL; |
| 1498 | return; |
| 1499 | } |
| 1500 | } |
| 1501 | goto no_attr; |
| 1502 | } |
| 1503 | |
| 1504 | no_attr: |
| 1505 | dest[1] = rt_load_attr(base, attr); |
| 1506 | dest[0] = NULL; |
| 1507 | } |
| 1508 | |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1509 | void rt_store_attr(py_obj_t base, qstr attr, py_obj_t value) { |
| 1510 | DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value); |
Damien | ec63cce | 2013-10-22 22:58:17 +0100 | [diff] [blame] | 1511 | if (IS_O(base, O_CLASS)) { |
| 1512 | // TODO CPython allows STORE_ATTR to a class, but is this the correct implementation? |
| 1513 | py_obj_base_t *o = base; |
| 1514 | py_qstr_map_lookup(o->u_class.locals, attr, true)->value = value; |
| 1515 | } else if (IS_O(base, O_OBJ)) { |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1516 | // logic: look in class locals (no add) then obj members (add) (TODO check this against CPython) |
| 1517 | py_obj_base_t *o = base; |
| 1518 | py_map_elem_t *elem = py_qstr_map_lookup(o->u_obj.class->u_class.locals, attr, false); |
| 1519 | if (elem != NULL) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1520 | elem->value = value; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1521 | } else { |
Damien | ec63cce | 2013-10-22 22:58:17 +0100 | [diff] [blame] | 1522 | py_qstr_map_lookup(o->u_obj.members, attr, true)->value = value; |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 1523 | } |
| 1524 | } else { |
| 1525 | printf("?AttributeError: '%s' object has no attribute '%s'\n", py_obj_get_type_str(base), qstr_str(attr)); |
| 1526 | assert(0); |
| 1527 | } |
| 1528 | } |
| 1529 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1530 | void rt_store_subscr(py_obj_t base, py_obj_t index, py_obj_t value) { |
Damien | 5ac1b2e | 2013-10-18 19:58:12 +0100 | [diff] [blame] | 1531 | DEBUG_OP_printf("store subscr %p[%p] <- %p\n", base, index, value); |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1532 | if (IS_O(base, O_LIST)) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1533 | // list store |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1534 | uint i = get_index(base, index); |
| 1535 | ((py_obj_base_t*)base)->u_tuple_list.items[i] = value; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1536 | } else if (IS_O(base, O_MAP)) { |
| 1537 | // map store |
| 1538 | py_map_lookup(base, index, true)->value = value; |
| 1539 | } else { |
| 1540 | assert(0); |
| 1541 | } |
| 1542 | } |
| 1543 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1544 | py_obj_t rt_getiter(py_obj_t o_in) { |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1545 | if (IS_O(o_in, O_GEN_INSTANCE)) { |
| 1546 | return o_in; |
| 1547 | } else if (IS_O(o_in, O_RANGE)) { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1548 | py_obj_base_t *o = o_in; |
| 1549 | return py_obj_new_range_iterator(o->u_range.start, o->u_range.stop, o->u_range.step); |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1550 | } else if (IS_O(o_in, O_TUPLE)) { |
| 1551 | return py_obj_new_tuple_iterator(o_in, 0); |
| 1552 | } else if (IS_O(o_in, O_LIST)) { |
| 1553 | return py_obj_new_list_iterator(o_in, 0); |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1554 | } else { |
| 1555 | nlr_jump(py_obj_new_exception_2(q_TypeError, "'%s' object is not iterable", py_obj_get_type_str(o_in), NULL)); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | py_obj_t rt_iternext(py_obj_t o_in) { |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1560 | if (IS_O(o_in, O_GEN_INSTANCE)) { |
| 1561 | py_obj_base_t *self = o_in; |
| 1562 | py_obj_base_t *fun = self->u_gen_instance.state[0]; |
| 1563 | assert(fun->kind == O_FUN_BC); |
| 1564 | bool yield = py_execute_byte_code_2(fun->u_fun_bc.code, &self->u_gen_instance.ip, &self->u_gen_instance.state[1], &self->u_gen_instance.sp); |
| 1565 | if (yield) { |
| 1566 | return *self->u_gen_instance.sp; |
| 1567 | } else { |
| 1568 | if (*self->u_gen_instance.sp == py_const_none) { |
| 1569 | return py_const_stop_iteration; |
| 1570 | } else { |
| 1571 | // TODO return StopIteration with value *self->u_gen_instance.sp |
| 1572 | return py_const_stop_iteration; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | } else if (IS_O(o_in, O_RANGE_IT)) { |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1577 | py_obj_base_t *o = o_in; |
| 1578 | if ((o->u_range_it.step > 0 && o->u_range_it.cur < o->u_range_it.stop) || (o->u_range_it.step < 0 && o->u_range_it.cur > o->u_range_it.stop)) { |
| 1579 | py_obj_t o_out = TO_SMALL_INT(o->u_range_it.cur); |
| 1580 | o->u_range_it.cur += o->u_range_it.step; |
| 1581 | return o_out; |
| 1582 | } else { |
| 1583 | return py_const_stop_iteration; |
| 1584 | } |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1585 | |
Damien | c226dca | 2013-10-16 16:12:52 +0100 | [diff] [blame] | 1586 | } else if (IS_O(o_in, O_TUPLE_IT) || IS_O(o_in, O_LIST_IT)) { |
| 1587 | py_obj_base_t *o = o_in; |
| 1588 | if (o->u_tuple_list_it.cur < o->u_tuple_list_it.obj->u_tuple_list.len) { |
| 1589 | py_obj_t o_out = o->u_tuple_list_it.obj->u_tuple_list.items[o->u_tuple_list_it.cur]; |
| 1590 | o->u_tuple_list_it.cur += 1; |
| 1591 | return o_out; |
| 1592 | } else { |
| 1593 | return py_const_stop_iteration; |
| 1594 | } |
Damien | bd25445 | 2013-10-16 20:39:12 +0100 | [diff] [blame] | 1595 | |
Damien | ce89a21 | 2013-10-15 22:25:17 +0100 | [diff] [blame] | 1596 | } else { |
| 1597 | nlr_jump(py_obj_new_exception_2(q_TypeError, "? '%s' object is not iterable", py_obj_get_type_str(o_in), NULL)); |
| 1598 | } |
| 1599 | } |
| 1600 | |
Damien | df4b4f3 | 2013-10-19 18:28:01 +0100 | [diff] [blame] | 1601 | void *const rt_fun_table[RT_F_NUMBER_OF] = { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1602 | rt_load_const_str, |
| 1603 | rt_load_name, |
| 1604 | rt_load_global, |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 1605 | rt_load_build_class, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1606 | rt_load_attr, |
| 1607 | rt_load_method, |
| 1608 | rt_store_name, |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 1609 | rt_store_attr, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1610 | rt_store_subscr, |
| 1611 | rt_is_true, |
| 1612 | rt_unary_op, |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1613 | rt_build_tuple, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1614 | rt_build_list, |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1615 | rt_list_append, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1616 | rt_build_map, |
| 1617 | rt_store_map, |
| 1618 | rt_build_set, |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1619 | rt_store_set, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1620 | rt_make_function_from_id, |
Damien | eb19efb | 2013-10-10 22:06:54 +0100 | [diff] [blame] | 1621 | rt_call_function_n, |
Damien | 7f5dacf | 2013-10-10 11:24:39 +0100 | [diff] [blame] | 1622 | rt_call_method_n, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1623 | rt_binary_op, |
| 1624 | rt_compare_op, |
Damien | d2755ec | 2013-10-16 23:58:48 +0100 | [diff] [blame] | 1625 | rt_getiter, |
| 1626 | rt_iternext, |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1627 | }; |
| 1628 | |
| 1629 | /* |
| 1630 | void rt_f_vector(rt_fun_kind_t fun_kind) { |
| 1631 | (rt_f_table[fun_kind])(); |
| 1632 | } |
| 1633 | */ |
Damien | ec63cce | 2013-10-22 22:58:17 +0100 | [diff] [blame] | 1634 | |
| 1635 | // temporary way of making C modules |
| 1636 | // hack: use class to mimic a module |
| 1637 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 1638 | py_obj_t py_module_new(void) { |
Damien | ec63cce | 2013-10-22 22:58:17 +0100 | [diff] [blame] | 1639 | py_obj_base_t *o = m_new(py_obj_base_t, 1); |
| 1640 | o->kind = O_CLASS; |
| 1641 | o->u_class.locals = py_map_new(MAP_QSTR, 0); |
| 1642 | return o; |
| 1643 | } |