Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 1 | /* Notes on passes: |
| 2 | * We don't know exactly the opcodes in pass 1 because they depend on the |
| 3 | * closing over of variables (LOAD_CLOSURE, BUILD_TUPLE, MAKE_CLOSURE), which |
| 4 | * depends on determining the scope of variables in each function, and this |
| 5 | * is not known until the end of pass 1. |
| 6 | * As a consequence, we don't know the maximum stack size until the end of pass 2. |
| 7 | * This is problematic for some emitters (x64) since they need to know the maximum |
| 8 | * stack size to compile the entry to the function, and this effects code size. |
| 9 | */ |
| 10 | |
| 11 | typedef enum { |
| 12 | PASS_1 = 1, // work out id's and their kind, and number of labels |
| 13 | PASS_2 = 2, // work out stack size and code size and label offsets |
| 14 | PASS_3 = 3, // emit code |
| 15 | } pass_kind_t; |
| 16 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame^] | 17 | typedef struct _emit_t emit_t; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 18 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame^] | 19 | typedef struct _emit_method_table_t { |
| 20 | void (*set_native_types)(emit_t *emit, bool do_native_types); |
| 21 | void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope); |
| 22 | void (*end_pass)(emit_t *emit); |
| 23 | bool (*last_emit_was_return_value)(emit_t *emit); |
| 24 | int (*get_stack_size)(emit_t *emit); |
| 25 | void (*set_stack_size)(emit_t *emit, int size); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 26 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame^] | 27 | int (*label_new)(emit_t *emit); |
| 28 | void (*label_assign)(emit_t *emit, int l); |
| 29 | void (*import_name)(emit_t *emit, qstr qstr); |
| 30 | void (*import_from)(emit_t *emit, qstr qstr); |
| 31 | void (*import_star)(emit_t *emit); |
| 32 | void (*load_const_tok)(emit_t *emit, py_token_kind_t tok); |
| 33 | void (*load_const_small_int)(emit_t *emit, int arg); |
| 34 | void (*load_const_int)(emit_t *emit, qstr qstr); |
| 35 | void (*load_const_dec)(emit_t *emit, qstr qstr); |
| 36 | void (*load_const_id)(emit_t *emit, qstr qstr); |
| 37 | void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes); |
| 38 | void (*load_const_verbatim_start)(emit_t *emit); |
| 39 | void (*load_const_verbatim_int)(emit_t *emit, int val); |
| 40 | void (*load_const_verbatim_str)(emit_t *emit, const char *str); |
| 41 | void (*load_const_verbatim_strn)(emit_t *emit, const char *str, int len); |
| 42 | void (*load_const_verbatim_quoted_str)(emit_t *emit, qstr qstr, bool bytes); |
| 43 | void (*load_const_verbatim_end)(emit_t *emit); |
| 44 | void (*load_fast)(emit_t *emit, qstr qstr, int local_num); |
| 45 | void (*load_name)(emit_t *emit, qstr qstr); |
| 46 | void (*load_global)(emit_t *emit, qstr qstr); |
| 47 | void (*load_deref)(emit_t *emit, qstr qstr); |
| 48 | void (*load_closure)(emit_t *emit, qstr qstr); |
| 49 | void (*load_attr)(emit_t *emit, qstr qstr); |
| 50 | void (*load_method)(emit_t *emit, qstr qstr); |
| 51 | void (*load_build_class)(emit_t *emit); |
| 52 | void (*store_fast)(emit_t *emit, qstr qstr, int local_num); |
| 53 | void (*store_name)(emit_t *emit, qstr qstr); |
| 54 | void (*store_global)(emit_t *emit, qstr qstr); |
| 55 | void (*store_deref)(emit_t *emit, qstr qstr); |
| 56 | void (*store_attr)(emit_t *emit, qstr qstr); |
| 57 | void (*store_locals)(emit_t *emit); |
| 58 | void (*store_subscr)(emit_t *emit); |
| 59 | void (*delete_fast)(emit_t *emit, qstr qstr, int local_num); |
| 60 | void (*delete_name)(emit_t *emit, qstr qstr); |
| 61 | void (*delete_global)(emit_t *emit, qstr qstr); |
| 62 | void (*delete_deref)(emit_t *emit, qstr qstr); |
| 63 | void (*delete_attr)(emit_t *emit, qstr qstr); |
| 64 | void (*delete_subscr)(emit_t *emit); |
| 65 | void (*dup_top)(emit_t *emit); |
| 66 | void (*dup_top_two)(emit_t *emit); |
| 67 | void (*pop_top)(emit_t *emit); |
| 68 | void (*rot_two)(emit_t *emit); |
| 69 | void (*rot_three)(emit_t *emit); |
| 70 | void (*jump)(emit_t *emit, int label); |
| 71 | void (*pop_jump_if_true)(emit_t *emit, int label); |
| 72 | void (*pop_jump_if_false)(emit_t *emit, int label); |
| 73 | void (*jump_if_true_or_pop)(emit_t *emit, int label); |
| 74 | void (*jump_if_false_or_pop)(emit_t *emit, int label); |
| 75 | void (*setup_loop)(emit_t *emit, int label); |
| 76 | void (*break_loop)(emit_t *emit, int label); |
| 77 | void (*continue_loop)(emit_t *emit, int label); |
| 78 | void (*setup_with)(emit_t *emit, int label); |
| 79 | void (*with_cleanup)(emit_t *emit); |
| 80 | void (*setup_except)(emit_t *emit, int label); |
| 81 | void (*setup_finally)(emit_t *emit, int label); |
| 82 | void (*end_finally)(emit_t *emit); |
| 83 | void (*get_iter)(emit_t *emit); // tos = getiter(tos) |
| 84 | void (*for_iter)(emit_t *emit, int label); |
| 85 | void (*for_iter_end)(emit_t *emit); |
| 86 | void (*pop_block)(emit_t *emit); |
| 87 | void (*pop_except)(emit_t *emit); |
| 88 | void (*unary_op)(emit_t *emit, rt_unary_op_t op); |
| 89 | void (*binary_op)(emit_t *emit, rt_binary_op_t op); |
| 90 | void (*compare_op)(emit_t *emit, rt_compare_op_t op); |
| 91 | void (*build_tuple)(emit_t *emit, int n_args); |
| 92 | void (*build_list)(emit_t *emit, int n_args); |
| 93 | void (*list_append)(emit_t *emit, int list_stack_index); |
| 94 | void (*build_map)(emit_t *emit, int n_args); |
| 95 | void (*store_map)(emit_t *emit); |
| 96 | void (*map_add)(emit_t *emit, int map_stack_index); |
| 97 | void (*build_set)(emit_t *emit, int n_args); |
| 98 | void (*set_add)(emit_t *emit, int set_stack_index); |
| 99 | void (*build_slice)(emit_t *emit, int n_args); |
| 100 | void (*unpack_sequence)(emit_t *emit, int n_args); |
| 101 | void (*unpack_ex)(emit_t *emit, int n_left, int n_right); |
| 102 | void (*make_function)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 103 | void (*make_closure)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 104 | void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 105 | void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 106 | void (*return_value)(emit_t *emit); |
| 107 | void (*raise_varargs)(emit_t *emit, int n_args); |
| 108 | void (*yield_value)(emit_t *emit); |
| 109 | void (*yield_from)(emit_t *emit); |
| 110 | } emit_method_table_t; |
| 111 | |
| 112 | void emit_common_load_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr___class__, qstr qstr); |
| 113 | void emit_common_store_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr); |
| 114 | void emit_common_delete_id(pass_kind_t pass, scope_t *scope, emit_t *emit, const emit_method_table_t *emit_method_table, qstr qstr); |
| 115 | |
| 116 | void emit_new_cpython(emit_t **emit, const emit_method_table_t **emit_method_table); |
| 117 | void emit_new_bc(emit_t **emit, const emit_method_table_t **emit_method_table); |
| 118 | void emit_new_x64(emit_t **emit, const emit_method_table_t **emit_method_table); |
| 119 | void emit_new_thumb(emit_t **emit, const emit_method_table_t **emit_method_table); |
| 120 | |
| 121 | /* |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 122 | void emit_set_native_types(emitter_t *emit, bool do_native_types); |
| 123 | void emit_start_pass(emitter_t *emit, pass_kind_t pass, scope_t *scope); |
| 124 | void emit_end_pass(emitter_t *emit); |
| 125 | bool emit_last_emit_was_return_value(emitter_t *emit); |
| 126 | int emit_get_stack_size(emitter_t *emit); |
| 127 | void emit_set_stack_size(emitter_t *emit, int size); |
| 128 | |
| 129 | int emit_label_new(emitter_t *emit); |
| 130 | void emit_label_assign(emitter_t *emit, int l); |
| 131 | void emit_import_name(emitter_t *emit, qstr qstr); |
| 132 | void emit_import_from(emitter_t *emit, qstr qstr); |
| 133 | void emit_import_star(emitter_t *emit); |
| 134 | void emit_load_const_tok(emitter_t *emit, py_token_kind_t tok); |
| 135 | void emit_load_const_small_int(emitter_t *emit, int arg); |
| 136 | void emit_load_const_int(emitter_t *emit, qstr qstr); |
| 137 | void emit_load_const_dec(emitter_t *emit, qstr qstr); |
| 138 | void emit_load_const_id(emitter_t *emit, qstr qstr); |
| 139 | void emit_load_const_str(emitter_t *emit, qstr qstr, bool bytes); |
| 140 | void emit_load_const_verbatim_start(emitter_t *emit); |
| 141 | void emit_load_const_verbatim_int(emitter_t *emit, int val); |
| 142 | void emit_load_const_verbatim_str(emitter_t *emit, const char *str); |
| 143 | void emit_load_const_verbatim_strn(emitter_t *emit, const char *str, int len); |
| 144 | void emit_load_const_verbatim_quoted_str(emitter_t *emit, qstr qstr, bool bytes); |
| 145 | void emit_load_const_verbatim_end(emitter_t *emit); |
| 146 | void emit_load_fast(emitter_t *emit, qstr qstr, int local_num); |
| 147 | void emit_load_name(emitter_t *emit, qstr qstr); |
| 148 | void emit_load_global(emitter_t *emit, qstr qstr); |
| 149 | void emit_load_deref(emitter_t *emit, qstr qstr); |
| 150 | void emit_load_closure(emitter_t *emit, qstr qstr); |
| 151 | void emit_load_attr(emitter_t *emit, qstr qstr); |
| 152 | void emit_load_method(emitter_t *emit, qstr qstr); |
| 153 | void emit_load_build_class(emitter_t *emit); |
| 154 | void emit_store_fast(emitter_t *emit, qstr qstr, int local_num); |
| 155 | void emit_store_name(emitter_t *emit, qstr qstr); |
| 156 | void emit_store_global(emitter_t *emit, qstr qstr); |
| 157 | void emit_store_deref(emitter_t *emit, qstr qstr); |
| 158 | void emit_store_attr(emitter_t *emit, qstr qstr); |
| 159 | void emit_store_locals(emitter_t *emit); |
| 160 | void emit_store_subscr(emitter_t *emit); |
| 161 | void emit_delete_fast(emitter_t *emit, qstr qstr, int local_num); |
| 162 | void emit_delete_name(emitter_t *emit, qstr qstr); |
| 163 | void emit_delete_global(emitter_t *emit, qstr qstr); |
| 164 | void emit_delete_deref(emitter_t *emit, qstr qstr); |
| 165 | void emit_delete_attr(emitter_t *emit, qstr qstr); |
| 166 | void emit_delete_subscr(emitter_t *emit); |
| 167 | void emit_dup_top(emitter_t *emit); |
| 168 | void emit_dup_top_two(emitter_t *emit); |
| 169 | void emit_pop_top(emitter_t *emit); |
| 170 | void emit_rot_two(emitter_t *emit); |
| 171 | void emit_rot_three(emitter_t *emit); |
| 172 | void emit_jump(emitter_t *emit, int label); |
| 173 | void emit_pop_jump_if_true(emitter_t *emit, int label); |
| 174 | void emit_pop_jump_if_false(emitter_t *emit, int label); |
| 175 | void emit_jump_if_true_or_pop(emitter_t *emit, int label); |
| 176 | void emit_jump_if_false_or_pop(emitter_t *emit, int label); |
| 177 | void emit_setup_loop(emitter_t *emit, int label); |
| 178 | void emit_break_loop(emitter_t *emit, int label); |
| 179 | void emit_continue_loop(emitter_t *emit, int label); |
| 180 | void emit_setup_with(emitter_t *emit, int label); |
| 181 | void emit_with_cleanup(emitter_t *emit); |
| 182 | void emit_setup_except(emitter_t *emit, int label); |
| 183 | void emit_setup_finally(emitter_t *emit, int label); |
| 184 | void emit_end_finally(emitter_t *emit); |
| 185 | void emit_get_iter(emitter_t *emit); // tos = getiter(tos) |
| 186 | void emit_for_iter(emitter_t *emit, int label); |
| 187 | void emit_for_iter_end(emitter_t *emit); |
| 188 | void emit_pop_block(emitter_t *emit); |
| 189 | void emit_pop_except(emitter_t *emit); |
| 190 | void emit_unary_op(emitter_t *emit, rt_unary_op_t op); |
| 191 | void emit_binary_op(emitter_t *emit, rt_binary_op_t op); |
| 192 | void emit_compare_op(emitter_t *emit, rt_compare_op_t op); |
| 193 | void emit_build_tuple(emitter_t *emit, int n_args); |
| 194 | void emit_build_list(emitter_t *emit, int n_args); |
| 195 | void emit_list_append(emitter_t *emit, int list_stack_index); |
| 196 | void emit_build_map(emitter_t *emit, int n_args); |
| 197 | void emit_store_map(emitter_t *emit); |
| 198 | void emit_map_add(emitter_t *emit, int map_stack_index); |
| 199 | void emit_build_set(emitter_t *emit, int n_args); |
| 200 | void emit_set_add(emitter_t *emit, int set_stack_index); |
| 201 | void emit_build_slice(emitter_t *emit, int n_args); |
| 202 | void emit_unpack_sequence(emitter_t *emit, int n_args); |
| 203 | void emit_unpack_ex(emitter_t *emit, int n_left, int n_right); |
| 204 | void emit_make_function(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 205 | void emit_make_closure(emitter_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 206 | void emit_call_function(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 207 | void emit_call_method(emitter_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 208 | void emit_return_value(emitter_t *emit); |
| 209 | void emit_raise_varargs(emitter_t *emit, int n_args); |
| 210 | void emit_yield_value(emitter_t *emit); |
| 211 | void emit_yield_from(emitter_t *emit); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame^] | 212 | */ |