blob: 9121e719f78b51399085a622cdca7bb5ea3478ab [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George51dfcb42015-01-01 20:27:54 +000027#ifndef __MICROPY_INCLUDED_PY_EMIT_H__
28#define __MICROPY_INCLUDED_PY_EMIT_H__
29
30#include "py/lexer.h"
31#include "py/scope.h"
32#include "py/runtime0.h"
33
Damien429d7192013-10-04 19:53:11 +010034/* Notes on passes:
35 * We don't know exactly the opcodes in pass 1 because they depend on the
36 * closing over of variables (LOAD_CLOSURE, BUILD_TUPLE, MAKE_CLOSURE), which
37 * depends on determining the scope of variables in each function, and this
38 * is not known until the end of pass 1.
39 * As a consequence, we don't know the maximum stack size until the end of pass 2.
40 * This is problematic for some emitters (x64) since they need to know the maximum
Paul Sokolovskyfe039b42014-01-06 17:49:21 +020041 * stack size to compile the entry to the function, and this affects code size.
Damien429d7192013-10-04 19:53:11 +010042 */
43
44typedef enum {
Damien George36db6bc2014-05-07 17:24:22 +010045 MP_PASS_SCOPE = 1, // work out id's and their kind, and number of labels
46 MP_PASS_STACK_SIZE = 2, // work out maximum stack size
47 MP_PASS_CODE_SIZE = 3, // work out code size and label offsets
48 MP_PASS_EMIT = 4, // emit code
Damien429d7192013-10-04 19:53:11 +010049} pass_kind_t;
50
Damien George922ddd62014-04-09 12:43:17 +010051#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
52#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
53
Damien George25c84642014-05-30 15:20:41 +010054#define MP_EMIT_BREAK_FROM_FOR (0x8000)
55
Damien George2ac4af62014-08-15 16:45:41 +010056#define MP_EMIT_NATIVE_TYPE_ENABLE (0)
57#define MP_EMIT_NATIVE_TYPE_RETURN (1)
58#define MP_EMIT_NATIVE_TYPE_ARG (2)
59
Damien415eb6f2013-10-05 12:19:06 +010060typedef struct _emit_t emit_t;
Damien429d7192013-10-04 19:53:11 +010061
Damien George542bd6b2015-03-26 14:42:40 +000062typedef struct _mp_emit_method_table_id_ops_t {
63 void (*fast)(emit_t *emit, qstr qst, mp_uint_t local_num);
64 void (*deref)(emit_t *emit, qstr qst, mp_uint_t local_num);
65 void (*name)(emit_t *emit, qstr qst);
66 void (*global)(emit_t *emit, qstr qst);
67} mp_emit_method_table_id_ops_t;
68
Damien415eb6f2013-10-05 12:19:06 +010069typedef struct _emit_method_table_t {
Damien George2ac4af62014-08-15 16:45:41 +010070 void (*set_native_type)(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2);
Damien415eb6f2013-10-05 12:19:06 +010071 void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
72 void (*end_pass)(emit_t *emit);
73 bool (*last_emit_was_return_value)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +010074 void (*adjust_stack_size)(emit_t *emit, mp_int_t delta);
Damien George41125902015-03-26 16:44:14 +000075 void (*set_source_line)(emit_t *emit, mp_uint_t line);
Damien429d7192013-10-04 19:53:11 +010076
Damien George542bd6b2015-03-26 14:42:40 +000077 mp_emit_method_table_id_ops_t load_id;
78 mp_emit_method_table_id_ops_t store_id;
79 mp_emit_method_table_id_ops_t delete_id;
Damien4b03e772013-10-05 14:17:09 +010080
Damien George7ff996c2014-09-08 23:05:16 +010081 void (*label_assign)(emit_t *emit, mp_uint_t l);
82 void (*import_name)(emit_t *emit, qstr qst);
83 void (*import_from)(emit_t *emit, qstr qst);
Damien415eb6f2013-10-05 12:19:06 +010084 void (*import_star)(emit_t *emit);
Damiend99b0522013-12-21 18:17:45 +000085 void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
Damien George40f3c022014-07-03 13:25:24 +010086 void (*load_const_small_int)(emit_t *emit, mp_int_t arg);
Damien George59fba2d2015-06-25 14:42:13 +000087 void (*load_const_str)(emit_t *emit, qstr qst);
Damien George5d66b422015-11-27 12:41:25 +000088 void (*load_const_obj)(emit_t *emit, mp_obj_t obj);
Damien George3558f622014-04-20 17:50:40 +010089 void (*load_null)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +010090 void (*load_attr)(emit_t *emit, qstr qst);
91 void (*load_method)(emit_t *emit, qstr qst);
Damien415eb6f2013-10-05 12:19:06 +010092 void (*load_build_class)(emit_t *emit);
Damien George729f7b42014-04-17 22:10:53 +010093 void (*load_subscr)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +010094 void (*store_attr)(emit_t *emit, qstr qst);
Damien415eb6f2013-10-05 12:19:06 +010095 void (*store_subscr)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +010096 void (*delete_attr)(emit_t *emit, qstr qst);
Damien415eb6f2013-10-05 12:19:06 +010097 void (*delete_subscr)(emit_t *emit);
98 void (*dup_top)(emit_t *emit);
99 void (*dup_top_two)(emit_t *emit);
100 void (*pop_top)(emit_t *emit);
101 void (*rot_two)(emit_t *emit);
102 void (*rot_three)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +0100103 void (*jump)(emit_t *emit, mp_uint_t label);
Damien George63f38322015-02-28 15:04:06 +0000104 void (*pop_jump_if)(emit_t *emit, bool cond, mp_uint_t label);
105 void (*jump_if_or_pop)(emit_t *emit, bool cond, mp_uint_t label);
Damien George7ff996c2014-09-08 23:05:16 +0100106 void (*break_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
107 void (*continue_loop)(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
108 void (*setup_with)(emit_t *emit, mp_uint_t label);
Damien Georgece8b4e82016-04-07 08:50:38 +0100109 void (*with_cleanup)(emit_t *emit, mp_uint_t label);
Damien George7ff996c2014-09-08 23:05:16 +0100110 void (*setup_except)(emit_t *emit, mp_uint_t label);
111 void (*setup_finally)(emit_t *emit, mp_uint_t label);
Damien415eb6f2013-10-05 12:19:06 +0100112 void (*end_finally)(emit_t *emit);
Damien George2326d522014-03-27 23:26:35 +0000113 void (*get_iter)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +0100114 void (*for_iter)(emit_t *emit, mp_uint_t label);
Damien415eb6f2013-10-05 12:19:06 +0100115 void (*for_iter_end)(emit_t *emit);
116 void (*pop_block)(emit_t *emit);
117 void (*pop_except)(emit_t *emit);
Damien Georged17926d2014-03-30 13:35:08 +0100118 void (*unary_op)(emit_t *emit, mp_unary_op_t op);
119 void (*binary_op)(emit_t *emit, mp_binary_op_t op);
Damien George7ff996c2014-09-08 23:05:16 +0100120 void (*build_tuple)(emit_t *emit, mp_uint_t n_args);
121 void (*build_list)(emit_t *emit, mp_uint_t n_args);
122 void (*list_append)(emit_t *emit, mp_uint_t list_stack_index);
123 void (*build_map)(emit_t *emit, mp_uint_t n_args);
Damien415eb6f2013-10-05 12:19:06 +0100124 void (*store_map)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +0100125 void (*map_add)(emit_t *emit, mp_uint_t map_stack_index);
Damien Georgee37dcaa2014-12-27 17:07:16 +0000126 #if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +0100127 void (*build_set)(emit_t *emit, mp_uint_t n_args);
128 void (*set_add)(emit_t *emit, mp_uint_t set_stack_index);
Damien Georgee37dcaa2014-12-27 17:07:16 +0000129 #endif
Damien George83204f32014-12-27 17:20:41 +0000130 #if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +0100131 void (*build_slice)(emit_t *emit, mp_uint_t n_args);
Damien George83204f32014-12-27 17:20:41 +0000132 #endif
Damien George7ff996c2014-09-08 23:05:16 +0100133 void (*unpack_sequence)(emit_t *emit, mp_uint_t n_args);
134 void (*unpack_ex)(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);
135 void (*make_function)(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
136 void (*make_closure)(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
137 void (*call_function)(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
138 void (*call_method)(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
Damien415eb6f2013-10-05 12:19:06 +0100139 void (*return_value)(emit_t *emit);
Damien George7ff996c2014-09-08 23:05:16 +0100140 void (*raise_varargs)(emit_t *emit, mp_uint_t n_args);
Damien415eb6f2013-10-05 12:19:06 +0100141 void (*yield_value)(emit_t *emit);
142 void (*yield_from)(emit_t *emit);
Damien George5f6a25f2014-04-20 18:02:27 +0100143
Damien Georgeb601d952014-06-30 05:17:25 +0100144 // these methods are used to control entry to/exit from an exception handler
145 // they may or may not emit code
146 void (*start_except_handler)(emit_t *emit);
147 void (*end_except_handler)(emit_t *emit);
Damien415eb6f2013-10-05 12:19:06 +0100148} emit_method_table_t;
149
Damien George542bd6b2015-03-26 14:42:40 +0000150void mp_emit_common_get_id_for_load(scope_t *scope, qstr qst);
151void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst);
152void mp_emit_common_id_op(emit_t *emit, const mp_emit_method_table_id_ops_t *emit_method_table, scope_t *scope, qstr qst);
Damien415eb6f2013-10-05 12:19:06 +0100153
Damien6cdd3af2013-10-05 18:08:26 +0100154extern const emit_method_table_t emit_cpython_method_table;
155extern const emit_method_table_t emit_bc_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100156extern const emit_method_table_t emit_native_x64_method_table;
Damien Georgec90f59e2014-09-06 23:06:36 +0100157extern const emit_method_table_t emit_native_x86_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100158extern const emit_method_table_t emit_native_thumb_method_table;
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200159extern const emit_method_table_t emit_native_arm_method_table;
Damien6cdd3af2013-10-05 18:08:26 +0100160
Damien George41125902015-03-26 16:44:14 +0000161extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_load_id_ops;
162extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_store_id_ops;
163extern const mp_emit_method_table_id_ops_t mp_emit_bc_method_table_delete_id_ops;
164
Damien Georgea210c772015-03-26 15:49:53 +0000165emit_t *emit_cpython_new(void);
166emit_t *emit_bc_new(void);
Damien Georgec8b60f02015-04-20 13:29:31 +0000167emit_t *emit_native_x64_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
168emit_t *emit_native_x86_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
169emit_t *emit_native_thumb_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
170emit_t *emit_native_arm_new(mp_obj_t *error_slot, mp_uint_t max_num_labels);
Damien826005c2013-10-05 23:17:28 +0100171
Damien Georgea210c772015-03-26 15:49:53 +0000172void emit_cpython_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels);
173void emit_bc_set_max_num_labels(emit_t* emit, mp_uint_t max_num_labels);
174
175void emit_cpython_free(emit_t *emit);
Damien George41d02b62014-01-24 22:42:28 +0000176void emit_bc_free(emit_t *emit);
177void emit_native_x64_free(emit_t *emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100178void emit_native_x86_free(emit_t *emit);
Damien George41d02b62014-01-24 22:42:28 +0000179void emit_native_thumb_free(emit_t *emit);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200180void emit_native_arm_free(emit_t *emit);
Damien George41d02b62014-01-24 22:42:28 +0000181
Damien George41125902015-03-26 16:44:14 +0000182void mp_emit_bc_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope);
183void mp_emit_bc_end_pass(emit_t *emit);
184bool mp_emit_bc_last_emit_was_return_value(emit_t *emit);
185void mp_emit_bc_adjust_stack_size(emit_t *emit, mp_int_t delta);
186void mp_emit_bc_set_source_line(emit_t *emit, mp_uint_t line);
187
188void mp_emit_bc_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
189void mp_emit_bc_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
190void mp_emit_bc_load_name(emit_t *emit, qstr qst);
191void mp_emit_bc_load_global(emit_t *emit, qstr qst);
192void mp_emit_bc_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
193void mp_emit_bc_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
194void mp_emit_bc_store_name(emit_t *emit, qstr qst);
195void mp_emit_bc_store_global(emit_t *emit, qstr qst);
196void mp_emit_bc_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
197void mp_emit_bc_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num);
198void mp_emit_bc_delete_name(emit_t *emit, qstr qst);
199void mp_emit_bc_delete_global(emit_t *emit, qstr qst);
200
201void mp_emit_bc_label_assign(emit_t *emit, mp_uint_t l);
202void mp_emit_bc_import_name(emit_t *emit, qstr qst);
203void mp_emit_bc_import_from(emit_t *emit, qstr qst);
204void mp_emit_bc_import_star(emit_t *emit);
205void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok);
206void mp_emit_bc_load_const_small_int(emit_t *emit, mp_int_t arg);
Damien George59fba2d2015-06-25 14:42:13 +0000207void mp_emit_bc_load_const_str(emit_t *emit, qstr qst);
Damien George5d66b422015-11-27 12:41:25 +0000208void mp_emit_bc_load_const_obj(emit_t *emit, mp_obj_t obj);
Damien George41125902015-03-26 16:44:14 +0000209void mp_emit_bc_load_null(emit_t *emit);
210void mp_emit_bc_load_attr(emit_t *emit, qstr qst);
211void mp_emit_bc_load_method(emit_t *emit, qstr qst);
212void mp_emit_bc_load_build_class(emit_t *emit);
213void mp_emit_bc_load_subscr(emit_t *emit);
214void mp_emit_bc_store_attr(emit_t *emit, qstr qst);
215void mp_emit_bc_store_subscr(emit_t *emit);
216void mp_emit_bc_delete_attr(emit_t *emit, qstr qst);
217void mp_emit_bc_delete_subscr(emit_t *emit);
218void mp_emit_bc_dup_top(emit_t *emit);
219void mp_emit_bc_dup_top_two(emit_t *emit);
220void mp_emit_bc_pop_top(emit_t *emit);
221void mp_emit_bc_rot_two(emit_t *emit);
222void mp_emit_bc_rot_three(emit_t *emit);
223void mp_emit_bc_jump(emit_t *emit, mp_uint_t label);
224void mp_emit_bc_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label);
225void mp_emit_bc_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label);
226void mp_emit_bc_unwind_jump(emit_t *emit, mp_uint_t label, mp_uint_t except_depth);
227#define mp_emit_bc_break_loop mp_emit_bc_unwind_jump
228#define mp_emit_bc_continue_loop mp_emit_bc_unwind_jump
229void mp_emit_bc_setup_with(emit_t *emit, mp_uint_t label);
Damien Georgece8b4e82016-04-07 08:50:38 +0100230void mp_emit_bc_with_cleanup(emit_t *emit, mp_uint_t label);
Damien George41125902015-03-26 16:44:14 +0000231void mp_emit_bc_setup_except(emit_t *emit, mp_uint_t label);
232void mp_emit_bc_setup_finally(emit_t *emit, mp_uint_t label);
233void mp_emit_bc_end_finally(emit_t *emit);
234void mp_emit_bc_get_iter(emit_t *emit);
235void mp_emit_bc_for_iter(emit_t *emit, mp_uint_t label);
236void mp_emit_bc_for_iter_end(emit_t *emit);
237void mp_emit_bc_pop_block(emit_t *emit);
238void mp_emit_bc_pop_except(emit_t *emit);
239void mp_emit_bc_unary_op(emit_t *emit, mp_unary_op_t op);
240void mp_emit_bc_binary_op(emit_t *emit, mp_binary_op_t op);
241void mp_emit_bc_build_tuple(emit_t *emit, mp_uint_t n_args);
242void mp_emit_bc_build_list(emit_t *emit, mp_uint_t n_args);
243void mp_emit_bc_list_append(emit_t *emit, mp_uint_t list_stack_index);
244void mp_emit_bc_build_map(emit_t *emit, mp_uint_t n_args);
245void mp_emit_bc_store_map(emit_t *emit);
246void mp_emit_bc_map_add(emit_t *emit, mp_uint_t map_stack_index);
247#if MICROPY_PY_BUILTINS_SET
248void mp_emit_bc_build_set(emit_t *emit, mp_uint_t n_args);
249void mp_emit_bc_set_add(emit_t *emit, mp_uint_t set_stack_index);
250#endif
251#if MICROPY_PY_BUILTINS_SLICE
252void mp_emit_bc_build_slice(emit_t *emit, mp_uint_t n_args);
253#endif
254void mp_emit_bc_unpack_sequence(emit_t *emit, mp_uint_t n_args);
255void mp_emit_bc_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right);
256void mp_emit_bc_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
257void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults);
258void mp_emit_bc_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
259void mp_emit_bc_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags);
260void mp_emit_bc_return_value(emit_t *emit);
261void mp_emit_bc_raise_varargs(emit_t *emit, mp_uint_t n_args);
262void mp_emit_bc_yield_value(emit_t *emit);
263void mp_emit_bc_yield_from(emit_t *emit);
264void mp_emit_bc_start_except_handler(emit_t *emit);
265void mp_emit_bc_end_except_handler(emit_t *emit);
266
Damien826005c2013-10-05 23:17:28 +0100267typedef struct _emit_inline_asm_t emit_inline_asm_t;
268
269typedef struct _emit_inline_asm_method_table_t {
Damien George8dfbd2d2015-02-13 01:00:51 +0000270 void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope, mp_obj_t *error_slot);
Damien George8f54c082016-01-15 15:20:43 +0000271 void (*end_pass)(emit_inline_asm_t *emit, mp_uint_t type_sig);
Damien George7ff996c2014-09-08 23:05:16 +0100272 mp_uint_t (*count_params)(emit_inline_asm_t *emit, mp_uint_t n_params, mp_parse_node_t *pn_params);
Damien George9c5cabb2015-03-03 17:08:02 +0000273 bool (*label)(emit_inline_asm_t *emit, mp_uint_t label_num, qstr label_id);
Damien George7ff996c2014-09-08 23:05:16 +0100274 void (*align)(emit_inline_asm_t *emit, mp_uint_t align);
275 void (*data)(emit_inline_asm_t *emit, mp_uint_t bytesize, mp_uint_t val);
276 void (*op)(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, mp_parse_node_t *pn_args);
Damien826005c2013-10-05 23:17:28 +0100277} emit_inline_asm_method_table_t;
278
279extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
280
Damien George7ff996c2014-09-08 23:05:16 +0100281emit_inline_asm_t *emit_inline_thumb_new(mp_uint_t max_num_labels);
Damien George41d02b62014-01-24 22:42:28 +0000282void emit_inline_thumb_free(emit_inline_asm_t *emit);
Paul Sokolovsky8ab6f902014-12-25 23:29:19 +0200283
Paul Sokolovsky8a8c1fc2015-01-01 09:29:28 +0200284#if MICROPY_WARNINGS
285void mp_emitter_warning(pass_kind_t pass, const char *msg);
286#else
287#define mp_emitter_warning(pass, msg)
288#endif
289
Paul Sokolovsky8ab6f902014-12-25 23:29:19 +0200290#endif // __MICROPY_INCLUDED_PY_EMIT_H__