1 /*
2  * This file is part of the MicroPython 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 
27 // This code glues the code emitters to the runtime.
28 
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 
34 #include "py/emitglue.h"
35 #include "py/runtime0.h"
36 #include "py/bc.h"
37 #include "py/profile.h"
38 
39 #if MICROPY_DEBUG_VERBOSE // print debugging info
40 #define DEBUG_PRINT (1)
41 #define WRITE_CODE (1)
42 #define DEBUG_printf DEBUG_printf
43 #define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
44 #else // don't print debugging info
45 #define DEBUG_printf(...) (void)0
46 #define DEBUG_OP_printf(...) (void)0
47 #endif
48 
49 #if MICROPY_DEBUG_PRINTERS
50 mp_uint_t mp_verbose_flag = 0;
51 #endif
52 
mp_emit_glue_new_raw_code(void)53 mp_raw_code_t *mp_emit_glue_new_raw_code(void) {
54     mp_raw_code_t *rc = m_new0(mp_raw_code_t, 1);
55     rc->kind = MP_CODE_RESERVED;
56     #if MICROPY_PY_SYS_SETTRACE
57     rc->line_of_definition = 0;
58     #endif
59     return rc;
60 }
61 
mp_emit_glue_assign_bytecode(mp_raw_code_t * rc,const byte * code,size_t len,const mp_uint_t * const_table,uint16_t n_obj,uint16_t n_raw_code,mp_uint_t scope_flags)62 void mp_emit_glue_assign_bytecode(mp_raw_code_t *rc, const byte *code,
63     #if MICROPY_PERSISTENT_CODE_SAVE || MICROPY_DEBUG_PRINTERS
64     size_t len,
65     #endif
66     const mp_uint_t *const_table,
67     #if MICROPY_PERSISTENT_CODE_SAVE
68     uint16_t n_obj, uint16_t n_raw_code,
69     #endif
70     mp_uint_t scope_flags) {
71 
72     rc->kind = MP_CODE_BYTECODE;
73     rc->scope_flags = scope_flags;
74     rc->fun_data = code;
75     rc->const_table = const_table;
76     #if MICROPY_PERSISTENT_CODE_SAVE
77     rc->fun_data_len = len;
78     rc->n_obj = n_obj;
79     rc->n_raw_code = n_raw_code;
80     #endif
81 
82     #if MICROPY_PY_SYS_SETTRACE
83     mp_bytecode_prelude_t *prelude = &rc->prelude;
84     mp_prof_extract_prelude(code, prelude);
85     #endif
86 
87     #ifdef DEBUG_PRINT
88     #if !MICROPY_DEBUG_PRINTERS
89     const size_t len = 0;
90     #endif
91     DEBUG_printf("assign byte code: code=%p len=" UINT_FMT " flags=%x\n", code, len, (uint)scope_flags);
92     #endif
93     #if MICROPY_DEBUG_PRINTERS
94     if (mp_verbose_flag >= 2) {
95         mp_bytecode_print(&mp_plat_print, rc, code, len, const_table);
96     }
97     #endif
98 }
99 
100 #if MICROPY_EMIT_MACHINE_CODE
mp_emit_glue_assign_native(mp_raw_code_t * rc,mp_raw_code_kind_t kind,void * fun_data,mp_uint_t fun_len,const mp_uint_t * const_table,uint16_t prelude_offset,uint16_t n_obj,uint16_t n_raw_code,uint16_t n_qstr,mp_qstr_link_entry_t * qstr_link,mp_uint_t n_pos_args,mp_uint_t scope_flags,mp_uint_t type_sig)101 void mp_emit_glue_assign_native(mp_raw_code_t *rc, mp_raw_code_kind_t kind, void *fun_data, mp_uint_t fun_len, const mp_uint_t *const_table,
102     #if MICROPY_PERSISTENT_CODE_SAVE
103     uint16_t prelude_offset,
104     uint16_t n_obj, uint16_t n_raw_code,
105     uint16_t n_qstr, mp_qstr_link_entry_t *qstr_link,
106     #endif
107     mp_uint_t n_pos_args, mp_uint_t scope_flags, mp_uint_t type_sig) {
108 
109     assert(kind == MP_CODE_NATIVE_PY || kind == MP_CODE_NATIVE_VIPER || kind == MP_CODE_NATIVE_ASM);
110 
111     // Some architectures require flushing/invalidation of the I/D caches,
112     // so that the generated native code which was created in data RAM will
113     // be available for execution from instruction RAM.
114     #if MICROPY_EMIT_THUMB || MICROPY_EMIT_INLINE_THUMB
115     #if __ICACHE_PRESENT == 1
116     // Flush D-cache, so the code emitted is stored in RAM.
117     MP_HAL_CLEAN_DCACHE(fun_data, fun_len);
118     // Invalidate I-cache, so the newly-created code is reloaded from RAM.
119     SCB_InvalidateICache();
120     #endif
121     #elif MICROPY_EMIT_ARM
122     #if (defined(__linux__) && defined(__GNUC__)) || __ARM_ARCH == 7
123     __builtin___clear_cache(fun_data, (uint8_t *)fun_data + fun_len);
124     #elif defined(__arm__)
125     // Flush I-cache and D-cache.
126     asm volatile (
127         "0:"
128         "mrc p15, 0, r15, c7, c10, 3\n" // test and clean D-cache
129         "bne 0b\n"
130         "mov r0, #0\n"
131         "mcr p15, 0, r0, c7, c7, 0\n" // invalidate I-cache and D-cache
132         : : : "r0", "cc");
133     #endif
134     #endif
135 
136     rc->kind = kind;
137     rc->scope_flags = scope_flags;
138     rc->n_pos_args = n_pos_args;
139     rc->fun_data = fun_data;
140     rc->const_table = const_table;
141     rc->type_sig = type_sig;
142 
143     #if MICROPY_PERSISTENT_CODE_SAVE
144     rc->fun_data_len = fun_len;
145     rc->prelude_offset = prelude_offset;
146     rc->n_obj = n_obj;
147     rc->n_raw_code = n_raw_code;
148     rc->n_qstr = n_qstr;
149     rc->qstr_link = qstr_link;
150     #endif
151 
152     #ifdef DEBUG_PRINT
153     DEBUG_printf("assign native: kind=%d fun=%p len=" UINT_FMT " n_pos_args=" UINT_FMT " flags=%x\n", kind, fun_data, fun_len, n_pos_args, (uint)scope_flags);
154     for (mp_uint_t i = 0; i < fun_len; i++) {
155         if (i > 0 && i % 16 == 0) {
156             DEBUG_printf("\n");
157         }
158         DEBUG_printf(" %02x", ((byte *)fun_data)[i]);
159     }
160     DEBUG_printf("\n");
161 
162     #ifdef WRITE_CODE
163     FILE *fp_write_code = fopen("out-code", "wb");
164     fwrite(fun_data, fun_len, 1, fp_write_code);
165     fclose(fp_write_code);
166     #endif
167     #else
168     (void)fun_len;
169     #endif
170 }
171 #endif
172 
mp_make_function_from_raw_code(const mp_raw_code_t * rc,mp_obj_t def_args,mp_obj_t def_kw_args)173 mp_obj_t mp_make_function_from_raw_code(const mp_raw_code_t *rc, mp_obj_t def_args, mp_obj_t def_kw_args) {
174     DEBUG_OP_printf("make_function_from_raw_code %p\n", rc);
175     assert(rc != NULL);
176 
177     // def_args must be MP_OBJ_NULL or a tuple
178     assert(def_args == MP_OBJ_NULL || mp_obj_is_type(def_args, &mp_type_tuple));
179 
180     // def_kw_args must be MP_OBJ_NULL or a dict
181     assert(def_kw_args == MP_OBJ_NULL || mp_obj_is_type(def_kw_args, &mp_type_dict));
182 
183     // make the function, depending on the raw code kind
184     mp_obj_t fun;
185     switch (rc->kind) {
186         #if MICROPY_EMIT_NATIVE
187         case MP_CODE_NATIVE_PY:
188         case MP_CODE_NATIVE_VIPER:
189             fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->fun_data, rc->const_table);
190             // Check for a generator function, and if so change the type of the object
191             if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
192                 ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap;
193             }
194             break;
195         #endif
196         #if MICROPY_EMIT_INLINE_ASM
197         case MP_CODE_NATIVE_ASM:
198             fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig);
199             break;
200         #endif
201         default:
202             // rc->kind should always be set and BYTECODE is the only remaining case
203             assert(rc->kind == MP_CODE_BYTECODE);
204             fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->fun_data, rc->const_table);
205             // check for generator functions and if so change the type of the object
206             if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) {
207                 ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap;
208             }
209 
210             #if MICROPY_PY_SYS_SETTRACE
211             mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun);
212             self_fun->rc = rc;
213             #endif
214 
215             break;
216     }
217 
218     return fun;
219 }
220 
mp_make_closure_from_raw_code(const mp_raw_code_t * rc,mp_uint_t n_closed_over,const mp_obj_t * args)221 mp_obj_t mp_make_closure_from_raw_code(const mp_raw_code_t *rc, mp_uint_t n_closed_over, const mp_obj_t *args) {
222     DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n", rc, n_closed_over, args);
223     // make function object
224     mp_obj_t ffun;
225     if (n_closed_over & 0x100) {
226         // default positional and keyword args given
227         ffun = mp_make_function_from_raw_code(rc, args[0], args[1]);
228     } else {
229         // default positional and keyword args not given
230         ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL);
231     }
232     // wrap function in closure object
233     return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2));
234 }
235