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 | |
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 | |
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 |
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 | rc->kind = kind; |
112 | rc->scope_flags = scope_flags; |
113 | rc->n_pos_args = n_pos_args; |
114 | rc->fun_data = fun_data; |
115 | rc->const_table = const_table; |
116 | rc->type_sig = type_sig; |
117 | |
118 | #if MICROPY_PERSISTENT_CODE_SAVE |
119 | rc->fun_data_len = fun_len; |
120 | rc->prelude_offset = prelude_offset; |
121 | rc->n_obj = n_obj; |
122 | rc->n_raw_code = n_raw_code; |
123 | rc->n_qstr = n_qstr; |
124 | rc->qstr_link = qstr_link; |
125 | #endif |
126 | |
127 | #ifdef DEBUG_PRINT |
128 | 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); |
129 | for (mp_uint_t i = 0; i < fun_len; i++) { |
130 | if (i > 0 && i % 16 == 0) { |
131 | DEBUG_printf("\n" ); |
132 | } |
133 | DEBUG_printf(" %02x" , ((byte *)fun_data)[i]); |
134 | } |
135 | DEBUG_printf("\n" ); |
136 | |
137 | #ifdef WRITE_CODE |
138 | FILE *fp_write_code = fopen("out-code" , "wb" ); |
139 | fwrite(fun_data, fun_len, 1, fp_write_code); |
140 | fclose(fp_write_code); |
141 | #endif |
142 | #else |
143 | (void)fun_len; |
144 | #endif |
145 | } |
146 | #endif |
147 | |
148 | 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) { |
149 | DEBUG_OP_printf("make_function_from_raw_code %p\n" , rc); |
150 | assert(rc != NULL); |
151 | |
152 | // def_args must be MP_OBJ_NULL or a tuple |
153 | assert(def_args == MP_OBJ_NULL || mp_obj_is_type(def_args, &mp_type_tuple)); |
154 | |
155 | // def_kw_args must be MP_OBJ_NULL or a dict |
156 | assert(def_kw_args == MP_OBJ_NULL || mp_obj_is_type(def_kw_args, &mp_type_dict)); |
157 | |
158 | // make the function, depending on the raw code kind |
159 | mp_obj_t fun; |
160 | switch (rc->kind) { |
161 | #if MICROPY_EMIT_NATIVE |
162 | case MP_CODE_NATIVE_PY: |
163 | case MP_CODE_NATIVE_VIPER: |
164 | fun = mp_obj_new_fun_native(def_args, def_kw_args, rc->fun_data, rc->const_table); |
165 | // Check for a generator function, and if so change the type of the object |
166 | if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { |
167 | ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_native_gen_wrap; |
168 | } |
169 | break; |
170 | #endif |
171 | #if MICROPY_EMIT_INLINE_ASM |
172 | case MP_CODE_NATIVE_ASM: |
173 | fun = mp_obj_new_fun_asm(rc->n_pos_args, rc->fun_data, rc->type_sig); |
174 | break; |
175 | #endif |
176 | default: |
177 | // rc->kind should always be set and BYTECODE is the only remaining case |
178 | assert(rc->kind == MP_CODE_BYTECODE); |
179 | fun = mp_obj_new_fun_bc(def_args, def_kw_args, rc->fun_data, rc->const_table); |
180 | // check for generator functions and if so change the type of the object |
181 | if ((rc->scope_flags & MP_SCOPE_FLAG_GENERATOR) != 0) { |
182 | ((mp_obj_base_t *)MP_OBJ_TO_PTR(fun))->type = &mp_type_gen_wrap; |
183 | } |
184 | |
185 | #if MICROPY_PY_SYS_SETTRACE |
186 | mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t *)MP_OBJ_TO_PTR(fun); |
187 | self_fun->rc = rc; |
188 | #endif |
189 | |
190 | break; |
191 | } |
192 | |
193 | return fun; |
194 | } |
195 | |
196 | 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) { |
197 | DEBUG_OP_printf("make_closure_from_raw_code %p " UINT_FMT " %p\n" , rc, n_closed_over, args); |
198 | // make function object |
199 | mp_obj_t ffun; |
200 | if (n_closed_over & 0x100) { |
201 | // default positional and keyword args given |
202 | ffun = mp_make_function_from_raw_code(rc, args[0], args[1]); |
203 | } else { |
204 | // default positional and keyword args not given |
205 | ffun = mp_make_function_from_raw_code(rc, MP_OBJ_NULL, MP_OBJ_NULL); |
206 | } |
207 | // wrap function in closure object |
208 | return mp_obj_new_closure(ffun, n_closed_over & 0xff, args + ((n_closed_over >> 7) & 2)); |
209 | } |
210 | |