Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | /* 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 Sokolovsky | fe039b4 | 2014-01-06 17:49:21 +0200 | [diff] [blame] | 34 | * stack size to compile the entry to the function, and this affects code size. |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | typedef enum { |
Damien George | 36db6bc | 2014-05-07 17:24:22 +0100 | [diff] [blame] | 38 | 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 |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 42 | } pass_kind_t; |
| 43 | |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 44 | #define MP_EMIT_STAR_FLAG_SINGLE (0x01) |
| 45 | #define MP_EMIT_STAR_FLAG_DOUBLE (0x02) |
| 46 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 47 | typedef struct _emit_t emit_t; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 48 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 49 | typedef 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 George | d66ae18 | 2014-04-10 17:28:54 +0000 | [diff] [blame] | 54 | void (*adjust_stack_size)(emit_t *emit, int delta); |
Damien George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 55 | void (*set_line_number)(emit_t *emit, int line); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 56 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 57 | 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 George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 61 | void (*label_assign)(emit_t *emit, uint l); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 62 | void (*import_name)(emit_t *emit, qstr qstr); |
| 63 | void (*import_from)(emit_t *emit, qstr qstr); |
| 64 | void (*import_star)(emit_t *emit); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 65 | void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok); |
Damien George | 08d0755 | 2014-01-29 18:58:52 +0000 | [diff] [blame] | 66 | void (*load_const_small_int)(emit_t *emit, machine_int_t arg); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 67 | void (*load_const_int)(emit_t *emit, qstr qstr); |
| 68 | void (*load_const_dec)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 69 | void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes); |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 70 | void (*load_null)(emit_t *emit); |
Damien George | 2bf7c09 | 2014-04-09 15:26:46 +0100 | [diff] [blame] | 71 | void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num); |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 72 | void (*load_deref)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 73 | void (*load_name)(emit_t *emit, qstr qstr); |
| 74 | void (*load_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 75 | 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 George | 729f7b4 | 2014-04-17 22:10:53 +0100 | [diff] [blame] | 78 | void (*load_subscr)(emit_t *emit); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 79 | void (*store_fast)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 80 | void (*store_deref)(emit_t *emit, qstr qstr, int local_num); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 81 | void (*store_name)(emit_t *emit, qstr qstr); |
| 82 | void (*store_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 83 | void (*store_attr)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 84 | void (*store_subscr)(emit_t *emit); |
| 85 | void (*delete_fast)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 86 | void (*delete_deref)(emit_t *emit, qstr qstr, int local_num); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 87 | void (*delete_name)(emit_t *emit, qstr qstr); |
| 88 | void (*delete_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 89 | 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 George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 96 | 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 George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 101 | 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); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 104 | void (*with_cleanup)(emit_t *emit); |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 105 | void (*setup_except)(emit_t *emit, uint label); |
| 106 | void (*setup_finally)(emit_t *emit, uint label); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 107 | void (*end_finally)(emit_t *emit); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 108 | void (*get_iter)(emit_t *emit); |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 109 | void (*for_iter)(emit_t *emit, uint label); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 110 | void (*for_iter_end)(emit_t *emit); |
| 111 | void (*pop_block)(emit_t *emit); |
| 112 | void (*pop_except)(emit_t *emit); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 113 | void (*unary_op)(emit_t *emit, mp_unary_op_t op); |
| 114 | void (*binary_op)(emit_t *emit, mp_binary_op_t op); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 115 | 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 George | 3056509 | 2014-03-31 11:30:17 +0100 | [diff] [blame] | 126 | void (*make_function)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults); |
Damien George | 3558f62 | 2014-04-20 17:50:40 +0100 | [diff] [blame] | 127 | void (*make_closure)(emit_t *emit, scope_t *scope, uint n_closed_over, uint n_pos_defaults, uint n_kw_defaults); |
Damien George | 922ddd6 | 2014-04-09 12:43:17 +0100 | [diff] [blame] | 128 | 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); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 130 | 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 George | 5f6a25f | 2014-04-20 18:02:27 +0100 | [diff] [blame] | 134 | |
| 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 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 142 | } emit_method_table_t; |
| 143 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 144 | void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); |
| 145 | void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); |
| 146 | void emit_common_delete_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 147 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 148 | extern const emit_method_table_t emit_pass1_method_table; |
| 149 | extern const emit_method_table_t emit_cpython_method_table; |
| 150 | extern const emit_method_table_t emit_bc_method_table; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 151 | extern const emit_method_table_t emit_native_x64_method_table; |
| 152 | extern const emit_method_table_t emit_native_thumb_method_table; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 153 | |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 154 | emit_t *emit_pass1_new(void); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 155 | emit_t *emit_cpython_new(uint max_num_labels); |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 156 | emit_t *emit_bc_new(uint max_num_labels); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 157 | emit_t *emit_native_x64_new(uint max_num_labels); |
| 158 | emit_t *emit_native_thumb_new(uint max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 159 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 160 | void emit_pass1_free(emit_t *emit); |
| 161 | void emit_bc_free(emit_t *emit); |
| 162 | void emit_native_x64_free(emit_t *emit); |
| 163 | void emit_native_thumb_free(emit_t *emit); |
| 164 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 165 | typedef struct _emit_inline_asm_t emit_inline_asm_t; |
| 166 | |
| 167 | typedef struct _emit_inline_asm_method_table_t { |
| 168 | void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope); |
Damien George | a26dc50 | 2014-04-12 17:54:52 +0100 | [diff] [blame] | 169 | bool (*end_pass)(emit_inline_asm_t *emit); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 170 | int (*count_params)(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params); |
Damien George | 6f355fd | 2014-04-10 14:11:31 +0100 | [diff] [blame] | 171 | void (*label)(emit_inline_asm_t *emit, uint label_num, qstr label_id); |
Damien George | e5f8a77 | 2014-04-21 13:33:15 +0100 | [diff] [blame] | 172 | void (*align)(emit_inline_asm_t *emit, uint align); |
| 173 | void (*data)(emit_inline_asm_t *emit, uint bytesize, uint val); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 174 | void (*op)(emit_inline_asm_t *emit, qstr op, int n_args, mp_parse_node_t *pn_args); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 175 | } emit_inline_asm_method_table_t; |
| 176 | |
| 177 | extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table; |
| 178 | |
| 179 | emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 180 | void emit_inline_thumb_free(emit_inline_asm_t *emit); |
| 181 | |