blob: 5a3b27d8393d0afec7d527f7cec5ffb26a1f1122 [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
Damien429d7192013-10-04 19:53:11 +010027/* Notes on passes:
28 * We don't know exactly the opcodes in pass 1 because they depend on the
29 * closing over of variables (LOAD_CLOSURE, BUILD_TUPLE, MAKE_CLOSURE), which
30 * depends on determining the scope of variables in each function, and this
31 * is not known until the end of pass 1.
32 * As a consequence, we don't know the maximum stack size until the end of pass 2.
33 * This is problematic for some emitters (x64) since they need to know the maximum
Paul Sokolovskyfe039b42014-01-06 17:49:21 +020034 * stack size to compile the entry to the function, and this affects code size.
Damien429d7192013-10-04 19:53:11 +010035 */
36
37typedef enum {
Damien George36db6bc2014-05-07 17:24:22 +010038 MP_PASS_SCOPE = 1, // work out id's and their kind, and number of labels
39 MP_PASS_STACK_SIZE = 2, // work out maximum stack size
40 MP_PASS_CODE_SIZE = 3, // work out code size and label offsets
41 MP_PASS_EMIT = 4, // emit code
Damien429d7192013-10-04 19:53:11 +010042} pass_kind_t;
43
Damien George922ddd62014-04-09 12:43:17 +010044#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
45#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
46
Damien415eb6f2013-10-05 12:19:06 +010047typedef struct _emit_t emit_t;
Damien429d7192013-10-04 19:53:11 +010048
Damien415eb6f2013-10-05 12:19:06 +010049typedef struct _emit_method_table_t {
50 void (*set_native_types)(emit_t *emit, bool do_native_types);
51 void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
52 void (*end_pass)(emit_t *emit);
53 bool (*last_emit_was_return_value)(emit_t *emit);
Damien Georged66ae182014-04-10 17:28:54 +000054 void (*adjust_stack_size)(emit_t *emit, int delta);
Damien George08335002014-01-18 23:24:36 +000055 void (*set_line_number)(emit_t *emit, int line);
Damien429d7192013-10-04 19:53:11 +010056
Damien4b03e772013-10-05 14:17:09 +010057 void (*load_id)(emit_t *emit, qstr qstr);
58 void (*store_id)(emit_t *emit, qstr qstr);
59 void (*delete_id)(emit_t *emit, qstr qstr);
60
Damien George6f355fd2014-04-10 14:11:31 +010061 void (*label_assign)(emit_t *emit, uint l);
Damien415eb6f2013-10-05 12:19:06 +010062 void (*import_name)(emit_t *emit, qstr qstr);
63 void (*import_from)(emit_t *emit, qstr qstr);
64 void (*import_star)(emit_t *emit);
Damiend99b0522013-12-21 18:17:45 +000065 void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
Damien George08d07552014-01-29 18:58:52 +000066 void (*load_const_small_int)(emit_t *emit, machine_int_t arg);
Damien415eb6f2013-10-05 12:19:06 +010067 void (*load_const_int)(emit_t *emit, qstr qstr);
68 void (*load_const_dec)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010069 void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
Damien George3558f622014-04-20 17:50:40 +010070 void (*load_null)(emit_t *emit);
Damien George2bf7c092014-04-09 15:26:46 +010071 void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num);
Damien27fb45e2013-10-20 15:07:49 +010072 void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000073 void (*load_name)(emit_t *emit, qstr qstr);
74 void (*load_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010075 void (*load_attr)(emit_t *emit, qstr qstr);
76 void (*load_method)(emit_t *emit, qstr qstr);
77 void (*load_build_class)(emit_t *emit);
Damien George729f7b42014-04-17 22:10:53 +010078 void (*load_subscr)(emit_t *emit);
Damien415eb6f2013-10-05 12:19:06 +010079 void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000080 void (*store_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010081 void (*store_name)(emit_t *emit, qstr qstr);
82 void (*store_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010083 void (*store_attr)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010084 void (*store_subscr)(emit_t *emit);
85 void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000086 void (*delete_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010087 void (*delete_name)(emit_t *emit, qstr qstr);
88 void (*delete_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010089 void (*delete_attr)(emit_t *emit, qstr qstr);
90 void (*delete_subscr)(emit_t *emit);
91 void (*dup_top)(emit_t *emit);
92 void (*dup_top_two)(emit_t *emit);
93 void (*pop_top)(emit_t *emit);
94 void (*rot_two)(emit_t *emit);
95 void (*rot_three)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +010096 void (*jump)(emit_t *emit, uint label);
97 void (*pop_jump_if_true)(emit_t *emit, uint label);
98 void (*pop_jump_if_false)(emit_t *emit, uint label);
99 void (*jump_if_true_or_pop)(emit_t *emit, uint label);
100 void (*jump_if_false_or_pop)(emit_t *emit, uint label);
Damien George6f355fd2014-04-10 14:11:31 +0100101 void (*break_loop)(emit_t *emit, uint label, int except_depth);
102 void (*continue_loop)(emit_t *emit, uint label, int except_depth);
103 void (*setup_with)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100104 void (*with_cleanup)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +0100105 void (*setup_except)(emit_t *emit, uint label);
106 void (*setup_finally)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100107 void (*end_finally)(emit_t *emit);
Damien George2326d522014-03-27 23:26:35 +0000108 void (*get_iter)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +0100109 void (*for_iter)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100110 void (*for_iter_end)(emit_t *emit);
111 void (*pop_block)(emit_t *emit);
112 void (*pop_except)(emit_t *emit);
Damien Georged17926d2014-03-30 13:35:08 +0100113 void (*unary_op)(emit_t *emit, mp_unary_op_t op);
114 void (*binary_op)(emit_t *emit, mp_binary_op_t op);
Damien415eb6f2013-10-05 12:19:06 +0100115 void (*build_tuple)(emit_t *emit, int n_args);
116 void (*build_list)(emit_t *emit, int n_args);
117 void (*list_append)(emit_t *emit, int list_stack_index);
118 void (*build_map)(emit_t *emit, int n_args);
119 void (*store_map)(emit_t *emit);
120 void (*map_add)(emit_t *emit, int map_stack_index);
121 void (*build_set)(emit_t *emit, int n_args);
122 void (*set_add)(emit_t *emit, int set_stack_index);
123 void (*build_slice)(emit_t *emit, int n_args);
124 void (*unpack_sequence)(emit_t *emit, int n_args);
125 void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
Damien George30565092014-03-31 11:30:17 +0100126 void (*make_function)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults);
Damien George3558f622014-04-20 17:50:40 +0100127 void (*make_closure)(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults);
Damien George922ddd62014-04-09 12:43:17 +0100128 void (*call_function)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
129 void (*call_method)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
Damien415eb6f2013-10-05 12:19:06 +0100130 void (*return_value)(emit_t *emit);
131 void (*raise_varargs)(emit_t *emit, int n_args);
132 void (*yield_value)(emit_t *emit);
133 void (*yield_from)(emit_t *emit);
Damien George5f6a25f2014-04-20 18:02:27 +0100134
135#if MICROPY_EMIT_CPYTHON
136 // these methods are only needed for emitcpy
137 void (*load_const_verbatim_str)(emit_t *emit, const char *str);
138 void (*load_closure)(emit_t *emit, qstr qstr, int local_num);
139 void (*setup_loop)(emit_t *emit, uint label);
140#endif
141
Damien415eb6f2013-10-05 12:19:06 +0100142} emit_method_table_t;
143
Damien4b03e772013-10-05 14:17:09 +0100144void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
145void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
146void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +0100147
Damien6cdd3af2013-10-05 18:08:26 +0100148extern const emit_method_table_t emit_pass1_method_table;
149extern const emit_method_table_t emit_cpython_method_table;
150extern const emit_method_table_t emit_bc_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100151extern const emit_method_table_t emit_native_x64_method_table;
152extern const emit_method_table_t emit_native_thumb_method_table;
Damien6cdd3af2013-10-05 18:08:26 +0100153
Damien George35e2a4e2014-02-05 00:51:47 +0000154emit_t *emit_pass1_new(void);
Damien6cdd3af2013-10-05 18:08:26 +0100155emit_t *emit_cpython_new(uint max_num_labels);
Damien Georgecbd2f742014-01-19 11:48:48 +0000156emit_t *emit_bc_new(uint max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100157emit_t *emit_native_x64_new(uint max_num_labels);
158emit_t *emit_native_thumb_new(uint max_num_labels);
Damien826005c2013-10-05 23:17:28 +0100159
Damien George41d02b62014-01-24 22:42:28 +0000160void emit_pass1_free(emit_t *emit);
161void emit_bc_free(emit_t *emit);
162void emit_native_x64_free(emit_t *emit);
163void emit_native_thumb_free(emit_t *emit);
164
Damien826005c2013-10-05 23:17:28 +0100165typedef struct _emit_inline_asm_t emit_inline_asm_t;
166
167typedef struct _emit_inline_asm_method_table_t {
168 void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope);
Damien Georgea26dc502014-04-12 17:54:52 +0100169 bool (*end_pass)(emit_inline_asm_t *emit);
Damiend99b0522013-12-21 18:17:45 +0000170 int (*count_params)(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params);
Damien George6f355fd2014-04-10 14:11:31 +0100171 void (*label)(emit_inline_asm_t *emit, uint label_num, qstr label_id);
Damien Georgee5f8a772014-04-21 13:33:15 +0100172 void (*align)(emit_inline_asm_t *emit, uint align);
173 void (*data)(emit_inline_asm_t *emit, uint bytesize, uint val);
Damiend99b0522013-12-21 18:17:45 +0000174 void (*op)(emit_inline_asm_t *emit, qstr op, int n_args, mp_parse_node_t *pn_args);
Damien826005c2013-10-05 23:17:28 +0100175} emit_inline_asm_method_table_t;
176
177extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
178
179emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels);
Damien George41d02b62014-01-24 22:42:28 +0000180void emit_inline_thumb_free(emit_inline_asm_t *emit);
181