|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 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 | + |
| 27 | +#include<stdio.h> |
| 28 | +#include<string.h> |
| 29 | +#include<assert.h> |
| 30 | + |
| 31 | +#include"mpconfig.h" |
| 32 | +#include"nlr.h" |
| 33 | +#include"misc.h" |
| 34 | +#include"qstr.h" |
| 35 | +#include"obj.h" |
| 36 | +#include"runtime0.h" |
| 37 | +#include"runtime.h" |
| 38 | +#include"emitglue.h" |
| 39 | + |
| 40 | +#ifMICROPY_EMIT_NATIVE |
| 41 | + |
| 42 | +#if0// print debugging info |
| 43 | +#defineDEBUG_printf DEBUG_printf |
| 44 | +#else// don't print debugging info |
| 45 | +#defineDEBUG_printf(...) (void)0 |
| 46 | +#endif |
| 47 | + |
| 48 | +// convert a Micro Python object to a valid native value based on type |
| 49 | +mp_uint_tmp_convert_obj_to_native(mp_obj_tobj,mp_uint_ttype) { |
| 50 | +DEBUG_printf("mp_convert_obj_to_native(%p, "UINT_FMT")\n",obj,type); |
| 51 | +switch (type&3) { |
| 52 | +caseMP_NATIVE_TYPE_OBJ:return (mp_uint_t)obj; |
| 53 | +caseMP_NATIVE_TYPE_BOOL: |
| 54 | +caseMP_NATIVE_TYPE_INT: |
| 55 | +caseMP_NATIVE_TYPE_UINT:returnmp_obj_get_int(obj); |
| 56 | +default:assert(0);return0; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// convert a native value to a Micro Python object based on type |
| 61 | +mp_obj_tmp_convert_native_to_obj(mp_uint_tval,mp_uint_ttype) { |
| 62 | +DEBUG_printf("mp_convert_native_to_obj("UINT_FMT", "UINT_FMT")\n",val,type); |
| 63 | +switch (type&3) { |
| 64 | +caseMP_NATIVE_TYPE_OBJ:return (mp_obj_t)val; |
| 65 | +caseMP_NATIVE_TYPE_BOOL:returnMP_BOOL(val); |
| 66 | +caseMP_NATIVE_TYPE_INT:returnmp_obj_new_int(val); |
| 67 | +caseMP_NATIVE_TYPE_UINT:returnmp_obj_new_int_from_uint(val); |
| 68 | +default:assert(0);returnmp_const_none; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// wrapper that accepts n_args and n_kw in one argument |
| 73 | +// (native emitter can only pass at most 3 arguments to a function) |
| 74 | +mp_obj_tmp_native_call_function_n_kw(mp_obj_tfun_in,uintn_args_kw,constmp_obj_t*args) { |
| 75 | +returnmp_call_function_n_kw(fun_in,n_args_kw&0xff, (n_args_kw >>8)&0xff,args); |
| 76 | +} |
| 77 | + |
| 78 | +// wrapper that makes raise obj and raises it |
| 79 | +NORETURNvoidmp_native_raise(mp_obj_to) { |
| 80 | +nlr_raise(mp_make_raise_obj(o)); |
| 81 | +} |
| 82 | + |
| 83 | +// these must correspond to the respective enum in runtime0.h |
| 84 | +void*constmp_fun_table[MP_F_NUMBER_OF]= { |
| 85 | +mp_convert_obj_to_native, |
| 86 | +mp_convert_native_to_obj, |
| 87 | +mp_load_const_int, |
| 88 | +mp_load_const_dec, |
| 89 | +mp_load_const_str, |
| 90 | +mp_load_const_bytes, |
| 91 | +mp_load_name, |
| 92 | +mp_load_global, |
| 93 | +mp_load_build_class, |
| 94 | +mp_load_attr, |
| 95 | +mp_load_method, |
| 96 | +mp_store_name, |
| 97 | +mp_store_global, |
| 98 | +mp_store_attr, |
| 99 | +mp_obj_subscr, |
| 100 | +mp_obj_is_true, |
| 101 | +mp_unary_op, |
| 102 | +mp_binary_op, |
| 103 | +mp_obj_new_tuple, |
| 104 | +mp_obj_new_list, |
| 105 | +mp_obj_list_append, |
| 106 | +mp_obj_new_dict, |
| 107 | +mp_obj_dict_store, |
| 108 | +#ifMICROPY_PY_BUILTINS_SET |
| 109 | +mp_obj_new_set, |
| 110 | +mp_obj_set_store, |
| 111 | +#endif |
| 112 | +mp_make_function_from_raw_code, |
| 113 | +mp_native_call_function_n_kw, |
| 114 | +mp_call_method_n_kw, |
| 115 | +mp_getiter, |
| 116 | +mp_iternext, |
| 117 | +nlr_push, |
| 118 | +nlr_pop, |
| 119 | +mp_native_raise, |
| 120 | +mp_import_name, |
| 121 | +mp_import_from, |
| 122 | +mp_import_all, |
| 123 | +#ifMICROPY_PY_BUILTINS_SLICE |
| 124 | +mp_obj_new_slice, |
| 125 | +#endif |
| 126 | +mp_unpack_sequence, |
| 127 | +mp_unpack_ex, |
| 128 | +}; |
| 129 | + |
| 130 | +/* |
| 131 | +void mp_f_vector(mp_fun_kind_t fun_kind) { |
| 132 | + (mp_f_table[fun_kind])(); |
| 133 | +} |
| 134 | +*/ |
| 135 | + |
| 136 | +#endif// MICROPY_EMIT_NATIVE |