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 |
Paul Sokolovsky | fe039b4 | 2014-01-06 17:49:21 +0200 | [diff] [blame] | 8 | * 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] | 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 George | 0833500 | 2014-01-18 23:24:36 +0000 | [diff] [blame] | 26 | void (*set_line_number)(emit_t *emit, int line); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 28 | void (*load_id)(emit_t *emit, qstr qstr); |
| 29 | void (*store_id)(emit_t *emit, qstr qstr); |
| 30 | void (*delete_id)(emit_t *emit, qstr qstr); |
| 31 | |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 32 | void (*label_assign)(emit_t *emit, int l); |
| 33 | void (*import_name)(emit_t *emit, qstr qstr); |
| 34 | void (*import_from)(emit_t *emit, qstr qstr); |
| 35 | void (*import_star)(emit_t *emit); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 36 | void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok); |
Damien George | 08d0755 | 2014-01-29 18:58:52 +0000 | [diff] [blame] | 37 | void (*load_const_small_int)(emit_t *emit, machine_int_t arg); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 38 | void (*load_const_int)(emit_t *emit, qstr qstr); |
| 39 | void (*load_const_dec)(emit_t *emit, qstr qstr); |
| 40 | void (*load_const_id)(emit_t *emit, qstr qstr); |
| 41 | void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes); |
Damien | a1b2693 | 2013-12-12 15:34:40 +0000 | [diff] [blame] | 42 | void (*load_const_verbatim_str)(emit_t *emit, const char *str); // only needed for emitcpy |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 43 | void (*load_fast)(emit_t *emit, qstr qstr, int local_num); |
Damien | 27fb45e | 2013-10-20 15:07:49 +0100 | [diff] [blame] | 44 | void (*load_deref)(emit_t *emit, qstr qstr, int local_num); |
| 45 | void (*load_closure)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 46 | void (*load_name)(emit_t *emit, qstr qstr); |
| 47 | void (*load_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 48 | void (*load_attr)(emit_t *emit, qstr qstr); |
| 49 | void (*load_method)(emit_t *emit, qstr qstr); |
| 50 | void (*load_build_class)(emit_t *emit); |
| 51 | void (*store_fast)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 52 | void (*store_deref)(emit_t *emit, qstr qstr, int local_num); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 53 | void (*store_name)(emit_t *emit, qstr qstr); |
| 54 | void (*store_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 55 | void (*store_attr)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 56 | void (*store_subscr)(emit_t *emit); |
Damien | a397776 | 2013-10-09 23:10:10 +0100 | [diff] [blame] | 57 | void (*store_locals)(emit_t *emit); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 58 | void (*delete_fast)(emit_t *emit, qstr qstr, int local_num); |
Damien | 9ecbcff | 2013-12-11 00:41:43 +0000 | [diff] [blame] | 59 | void (*delete_deref)(emit_t *emit, qstr qstr, int local_num); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 60 | void (*delete_name)(emit_t *emit, qstr qstr); |
| 61 | void (*delete_global)(emit_t *emit, qstr qstr); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 62 | void (*delete_attr)(emit_t *emit, qstr qstr); |
| 63 | void (*delete_subscr)(emit_t *emit); |
| 64 | void (*dup_top)(emit_t *emit); |
| 65 | void (*dup_top_two)(emit_t *emit); |
| 66 | void (*pop_top)(emit_t *emit); |
| 67 | void (*rot_two)(emit_t *emit); |
| 68 | void (*rot_three)(emit_t *emit); |
| 69 | void (*jump)(emit_t *emit, int label); |
| 70 | void (*pop_jump_if_true)(emit_t *emit, int label); |
| 71 | void (*pop_jump_if_false)(emit_t *emit, int label); |
| 72 | void (*jump_if_true_or_pop)(emit_t *emit, int label); |
| 73 | void (*jump_if_false_or_pop)(emit_t *emit, int label); |
| 74 | void (*setup_loop)(emit_t *emit, int label); |
Damien George | cbddb27 | 2014-02-01 20:08:18 +0000 | [diff] [blame] | 75 | void (*break_loop)(emit_t *emit, int label, int except_depth); |
| 76 | void (*continue_loop)(emit_t *emit, int label, int except_depth); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 77 | void (*setup_with)(emit_t *emit, int label); |
| 78 | void (*with_cleanup)(emit_t *emit); |
| 79 | void (*setup_except)(emit_t *emit, int label); |
| 80 | void (*setup_finally)(emit_t *emit, int label); |
| 81 | void (*end_finally)(emit_t *emit); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 82 | void (*get_iter)(emit_t *emit); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 83 | void (*for_iter)(emit_t *emit, int label); |
| 84 | void (*for_iter_end)(emit_t *emit); |
| 85 | void (*pop_block)(emit_t *emit); |
| 86 | void (*pop_except)(emit_t *emit); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 87 | void (*unary_op)(emit_t *emit, mp_unary_op_t op); |
| 88 | void (*binary_op)(emit_t *emit, mp_binary_op_t op); |
Damien | 415eb6f | 2013-10-05 12:19:06 +0100 | [diff] [blame] | 89 | void (*build_tuple)(emit_t *emit, int n_args); |
| 90 | void (*build_list)(emit_t *emit, int n_args); |
| 91 | void (*list_append)(emit_t *emit, int list_stack_index); |
| 92 | void (*build_map)(emit_t *emit, int n_args); |
| 93 | void (*store_map)(emit_t *emit); |
| 94 | void (*map_add)(emit_t *emit, int map_stack_index); |
| 95 | void (*build_set)(emit_t *emit, int n_args); |
| 96 | void (*set_add)(emit_t *emit, int set_stack_index); |
| 97 | void (*build_slice)(emit_t *emit, int n_args); |
| 98 | void (*unpack_sequence)(emit_t *emit, int n_args); |
| 99 | void (*unpack_ex)(emit_t *emit, int n_left, int n_right); |
| 100 | void (*make_function)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 101 | void (*make_closure)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params); |
| 102 | void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 103 | void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg); |
| 104 | void (*return_value)(emit_t *emit); |
| 105 | void (*raise_varargs)(emit_t *emit, int n_args); |
| 106 | void (*yield_value)(emit_t *emit); |
| 107 | void (*yield_from)(emit_t *emit); |
| 108 | } emit_method_table_t; |
| 109 | |
Damien | 4b03e77 | 2013-10-05 14:17:09 +0100 | [diff] [blame] | 110 | void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); |
| 111 | void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr); |
| 112 | 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] | 113 | |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 114 | extern const emit_method_table_t emit_pass1_method_table; |
| 115 | extern const emit_method_table_t emit_cpython_method_table; |
| 116 | extern const emit_method_table_t emit_bc_method_table; |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 117 | extern const emit_method_table_t emit_native_x64_method_table; |
| 118 | extern const emit_method_table_t emit_native_thumb_method_table; |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 119 | |
Damien George | 35e2a4e | 2014-02-05 00:51:47 +0000 | [diff] [blame] | 120 | emit_t *emit_pass1_new(void); |
Damien | 6cdd3af | 2013-10-05 18:08:26 +0100 | [diff] [blame] | 121 | emit_t *emit_cpython_new(uint max_num_labels); |
Damien George | cbd2f74 | 2014-01-19 11:48:48 +0000 | [diff] [blame] | 122 | emit_t *emit_bc_new(uint max_num_labels); |
Damien | 13ed3a6 | 2013-10-08 09:05:10 +0100 | [diff] [blame] | 123 | emit_t *emit_native_x64_new(uint max_num_labels); |
| 124 | emit_t *emit_native_thumb_new(uint max_num_labels); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 125 | |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 126 | void emit_pass1_free(emit_t *emit); |
| 127 | void emit_bc_free(emit_t *emit); |
| 128 | void emit_native_x64_free(emit_t *emit); |
| 129 | void emit_native_thumb_free(emit_t *emit); |
| 130 | |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 131 | typedef struct _emit_inline_asm_t emit_inline_asm_t; |
| 132 | |
| 133 | typedef struct _emit_inline_asm_method_table_t { |
| 134 | void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope); |
| 135 | void (*end_pass)(emit_inline_asm_t *emit); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 136 | int (*count_params)(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params); |
Damien | 826005c | 2013-10-05 23:17:28 +0100 | [diff] [blame] | 137 | void (*label)(emit_inline_asm_t *emit, int label_num, qstr label_id); |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 138 | 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] | 139 | } emit_inline_asm_method_table_t; |
| 140 | |
| 141 | extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table; |
| 142 | |
| 143 | emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels); |
Damien George | 41d02b6 | 2014-01-24 22:42:28 +0000 | [diff] [blame] | 144 | void emit_inline_thumb_free(emit_inline_asm_t *emit); |
| 145 | |