blob: ce7417d28425d624d8c6349553aeae402e196911 [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 {
38 PASS_1 = 1, // work out id's and their kind, and number of labels
39 PASS_2 = 2, // work out stack size and code size and label offsets
40 PASS_3 = 3, // emit code
41} pass_kind_t;
42
Damien George922ddd62014-04-09 12:43:17 +010043#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
44#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
45
Damien415eb6f2013-10-05 12:19:06 +010046typedef struct _emit_t emit_t;
Damien429d7192013-10-04 19:53:11 +010047
Damien415eb6f2013-10-05 12:19:06 +010048typedef struct _emit_method_table_t {
49 void (*set_native_types)(emit_t *emit, bool do_native_types);
50 void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
51 void (*end_pass)(emit_t *emit);
52 bool (*last_emit_was_return_value)(emit_t *emit);
Damien Georged66ae182014-04-10 17:28:54 +000053 void (*adjust_stack_size)(emit_t *emit, int delta);
Damien George08335002014-01-18 23:24:36 +000054 void (*set_line_number)(emit_t *emit, int line);
Damien429d7192013-10-04 19:53:11 +010055
Damien4b03e772013-10-05 14:17:09 +010056 void (*load_id)(emit_t *emit, qstr qstr);
57 void (*store_id)(emit_t *emit, qstr qstr);
58 void (*delete_id)(emit_t *emit, qstr qstr);
59
Damien George6f355fd2014-04-10 14:11:31 +010060 void (*label_assign)(emit_t *emit, uint l);
Damien415eb6f2013-10-05 12:19:06 +010061 void (*import_name)(emit_t *emit, qstr qstr);
62 void (*import_from)(emit_t *emit, qstr qstr);
63 void (*import_star)(emit_t *emit);
Damiend99b0522013-12-21 18:17:45 +000064 void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
Damien George08d07552014-01-29 18:58:52 +000065 void (*load_const_small_int)(emit_t *emit, machine_int_t arg);
Damien415eb6f2013-10-05 12:19:06 +010066 void (*load_const_int)(emit_t *emit, qstr qstr);
67 void (*load_const_dec)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010068 void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
Damien George3558f622014-04-20 17:50:40 +010069 void (*load_null)(emit_t *emit);
Damien George2bf7c092014-04-09 15:26:46 +010070 void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num);
Damien27fb45e2013-10-20 15:07:49 +010071 void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000072 void (*load_name)(emit_t *emit, qstr qstr);
73 void (*load_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010074 void (*load_attr)(emit_t *emit, qstr qstr);
75 void (*load_method)(emit_t *emit, qstr qstr);
76 void (*load_build_class)(emit_t *emit);
Damien George729f7b42014-04-17 22:10:53 +010077 void (*load_subscr)(emit_t *emit);
Damien415eb6f2013-10-05 12:19:06 +010078 void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000079 void (*store_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010080 void (*store_name)(emit_t *emit, qstr qstr);
81 void (*store_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010082 void (*store_attr)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010083 void (*store_subscr)(emit_t *emit);
84 void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000085 void (*delete_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010086 void (*delete_name)(emit_t *emit, qstr qstr);
87 void (*delete_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010088 void (*delete_attr)(emit_t *emit, qstr qstr);
89 void (*delete_subscr)(emit_t *emit);
90 void (*dup_top)(emit_t *emit);
91 void (*dup_top_two)(emit_t *emit);
92 void (*pop_top)(emit_t *emit);
93 void (*rot_two)(emit_t *emit);
94 void (*rot_three)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +010095 void (*jump)(emit_t *emit, uint label);
96 void (*pop_jump_if_true)(emit_t *emit, uint label);
97 void (*pop_jump_if_false)(emit_t *emit, uint label);
98 void (*jump_if_true_or_pop)(emit_t *emit, uint label);
99 void (*jump_if_false_or_pop)(emit_t *emit, uint label);
Damien George6f355fd2014-04-10 14:11:31 +0100100 void (*break_loop)(emit_t *emit, uint label, int except_depth);
101 void (*continue_loop)(emit_t *emit, uint label, int except_depth);
102 void (*setup_with)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100103 void (*with_cleanup)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +0100104 void (*setup_except)(emit_t *emit, uint label);
105 void (*setup_finally)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100106 void (*end_finally)(emit_t *emit);
Damien George2326d522014-03-27 23:26:35 +0000107 void (*get_iter)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +0100108 void (*for_iter)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +0100109 void (*for_iter_end)(emit_t *emit);
110 void (*pop_block)(emit_t *emit);
111 void (*pop_except)(emit_t *emit);
Damien Georged17926d2014-03-30 13:35:08 +0100112 void (*unary_op)(emit_t *emit, mp_unary_op_t op);
113 void (*binary_op)(emit_t *emit, mp_binary_op_t op);
Damien415eb6f2013-10-05 12:19:06 +0100114 void (*build_tuple)(emit_t *emit, int n_args);
115 void (*build_list)(emit_t *emit, int n_args);
116 void (*list_append)(emit_t *emit, int list_stack_index);
117 void (*build_map)(emit_t *emit, int n_args);
118 void (*store_map)(emit_t *emit);
119 void (*map_add)(emit_t *emit, int map_stack_index);
120 void (*build_set)(emit_t *emit, int n_args);
121 void (*set_add)(emit_t *emit, int set_stack_index);
122 void (*build_slice)(emit_t *emit, int n_args);
123 void (*unpack_sequence)(emit_t *emit, int n_args);
124 void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
Damien George30565092014-03-31 11:30:17 +0100125 void (*make_function)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults);
Damien George3558f622014-04-20 17:50:40 +0100126 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 +0100127 void (*call_function)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
128 void (*call_method)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
Damien415eb6f2013-10-05 12:19:06 +0100129 void (*return_value)(emit_t *emit);
130 void (*raise_varargs)(emit_t *emit, int n_args);
131 void (*yield_value)(emit_t *emit);
132 void (*yield_from)(emit_t *emit);
Damien George5f6a25f2014-04-20 18:02:27 +0100133
134#if MICROPY_EMIT_CPYTHON
135 // these methods are only needed for emitcpy
136 void (*load_const_verbatim_str)(emit_t *emit, const char *str);
137 void (*load_closure)(emit_t *emit, qstr qstr, int local_num);
138 void (*setup_loop)(emit_t *emit, uint label);
139#endif
140
Damien415eb6f2013-10-05 12:19:06 +0100141} emit_method_table_t;
142
Damien4b03e772013-10-05 14:17:09 +0100143void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
144void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
145void 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 +0100146
Damien6cdd3af2013-10-05 18:08:26 +0100147extern const emit_method_table_t emit_pass1_method_table;
148extern const emit_method_table_t emit_cpython_method_table;
149extern const emit_method_table_t emit_bc_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100150extern const emit_method_table_t emit_native_x64_method_table;
151extern const emit_method_table_t emit_native_thumb_method_table;
Damien6cdd3af2013-10-05 18:08:26 +0100152
Damien George35e2a4e2014-02-05 00:51:47 +0000153emit_t *emit_pass1_new(void);
Damien6cdd3af2013-10-05 18:08:26 +0100154emit_t *emit_cpython_new(uint max_num_labels);
Damien Georgecbd2f742014-01-19 11:48:48 +0000155emit_t *emit_bc_new(uint max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100156emit_t *emit_native_x64_new(uint max_num_labels);
157emit_t *emit_native_thumb_new(uint max_num_labels);
Damien826005c2013-10-05 23:17:28 +0100158
Damien George41d02b62014-01-24 22:42:28 +0000159void emit_pass1_free(emit_t *emit);
160void emit_bc_free(emit_t *emit);
161void emit_native_x64_free(emit_t *emit);
162void emit_native_thumb_free(emit_t *emit);
163
Damien826005c2013-10-05 23:17:28 +0100164typedef struct _emit_inline_asm_t emit_inline_asm_t;
165
166typedef struct _emit_inline_asm_method_table_t {
167 void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope);
Damien Georgea26dc502014-04-12 17:54:52 +0100168 bool (*end_pass)(emit_inline_asm_t *emit);
Damiend99b0522013-12-21 18:17:45 +0000169 int (*count_params)(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params);
Damien George6f355fd2014-04-10 14:11:31 +0100170 void (*label)(emit_inline_asm_t *emit, uint label_num, qstr label_id);
Damien Georgee5f8a772014-04-21 13:33:15 +0100171 void (*align)(emit_inline_asm_t *emit, uint align);
172 void (*data)(emit_inline_asm_t *emit, uint bytesize, uint val);
Damiend99b0522013-12-21 18:17:45 +0000173 void (*op)(emit_inline_asm_t *emit, qstr op, int n_args, mp_parse_node_t *pn_args);
Damien826005c2013-10-05 23:17:28 +0100174} emit_inline_asm_method_table_t;
175
176extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
177
178emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels);
Damien George41d02b62014-01-24 22:42:28 +0000179void emit_inline_thumb_free(emit_inline_asm_t *emit);
180