blob: 64c2c854809be0fe9b25fe7189bae6478c4da917 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001/* 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 Sokolovskyfe039b42014-01-06 17:49:21 +02008 * stack size to compile the entry to the function, and this affects code size.
Damien429d7192013-10-04 19:53:11 +01009 */
10
11typedef 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 George922ddd62014-04-09 12:43:17 +010017#define MP_EMIT_STAR_FLAG_SINGLE (0x01)
18#define MP_EMIT_STAR_FLAG_DOUBLE (0x02)
19
Damien415eb6f2013-10-05 12:19:06 +010020typedef struct _emit_t emit_t;
Damien429d7192013-10-04 19:53:11 +010021
Damien415eb6f2013-10-05 12:19:06 +010022typedef struct _emit_method_table_t {
23 void (*set_native_types)(emit_t *emit, bool do_native_types);
24 void (*start_pass)(emit_t *emit, pass_kind_t pass, scope_t *scope);
25 void (*end_pass)(emit_t *emit);
26 bool (*last_emit_was_return_value)(emit_t *emit);
Damien Georged66ae182014-04-10 17:28:54 +000027 void (*adjust_stack_size)(emit_t *emit, int delta);
Damien George08335002014-01-18 23:24:36 +000028 void (*set_line_number)(emit_t *emit, int line);
Damien429d7192013-10-04 19:53:11 +010029
Damien4b03e772013-10-05 14:17:09 +010030 void (*load_id)(emit_t *emit, qstr qstr);
31 void (*store_id)(emit_t *emit, qstr qstr);
32 void (*delete_id)(emit_t *emit, qstr qstr);
33
Damien George6f355fd2014-04-10 14:11:31 +010034 void (*label_assign)(emit_t *emit, uint l);
Damien415eb6f2013-10-05 12:19:06 +010035 void (*import_name)(emit_t *emit, qstr qstr);
36 void (*import_from)(emit_t *emit, qstr qstr);
37 void (*import_star)(emit_t *emit);
Damiend99b0522013-12-21 18:17:45 +000038 void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok);
Damien George08d07552014-01-29 18:58:52 +000039 void (*load_const_small_int)(emit_t *emit, machine_int_t arg);
Damien415eb6f2013-10-05 12:19:06 +010040 void (*load_const_int)(emit_t *emit, qstr qstr);
41 void (*load_const_dec)(emit_t *emit, qstr qstr);
42 void (*load_const_id)(emit_t *emit, qstr qstr);
43 void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
Damiena1b26932013-12-12 15:34:40 +000044 void (*load_const_verbatim_str)(emit_t *emit, const char *str); // only needed for emitcpy
Damien George3558f622014-04-20 17:50:40 +010045 void (*load_null)(emit_t *emit);
Damien George2bf7c092014-04-09 15:26:46 +010046 void (*load_fast)(emit_t *emit, qstr qstr, uint id_flags, int local_num);
Damien27fb45e2013-10-20 15:07:49 +010047 void (*load_deref)(emit_t *emit, qstr qstr, int local_num);
Damien George2bf7c092014-04-09 15:26:46 +010048 void (*load_closure)(emit_t *emit, qstr qstr, int local_num); // only needed for emitcpy
Damien9ecbcff2013-12-11 00:41:43 +000049 void (*load_name)(emit_t *emit, qstr qstr);
50 void (*load_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010051 void (*load_attr)(emit_t *emit, qstr qstr);
52 void (*load_method)(emit_t *emit, qstr qstr);
53 void (*load_build_class)(emit_t *emit);
Damien George729f7b42014-04-17 22:10:53 +010054 void (*load_subscr)(emit_t *emit);
Damien415eb6f2013-10-05 12:19:06 +010055 void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000056 void (*store_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010057 void (*store_name)(emit_t *emit, qstr qstr);
58 void (*store_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010059 void (*store_attr)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010060 void (*store_subscr)(emit_t *emit);
61 void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
Damien9ecbcff2013-12-11 00:41:43 +000062 void (*delete_deref)(emit_t *emit, qstr qstr, int local_num);
Damien415eb6f2013-10-05 12:19:06 +010063 void (*delete_name)(emit_t *emit, qstr qstr);
64 void (*delete_global)(emit_t *emit, qstr qstr);
Damien415eb6f2013-10-05 12:19:06 +010065 void (*delete_attr)(emit_t *emit, qstr qstr);
66 void (*delete_subscr)(emit_t *emit);
67 void (*dup_top)(emit_t *emit);
68 void (*dup_top_two)(emit_t *emit);
69 void (*pop_top)(emit_t *emit);
70 void (*rot_two)(emit_t *emit);
71 void (*rot_three)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +010072 void (*jump)(emit_t *emit, uint label);
73 void (*pop_jump_if_true)(emit_t *emit, uint label);
74 void (*pop_jump_if_false)(emit_t *emit, uint label);
75 void (*jump_if_true_or_pop)(emit_t *emit, uint label);
76 void (*jump_if_false_or_pop)(emit_t *emit, uint label);
77 void (*setup_loop)(emit_t *emit, uint label);
78 void (*break_loop)(emit_t *emit, uint label, int except_depth);
79 void (*continue_loop)(emit_t *emit, uint label, int except_depth);
80 void (*setup_with)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +010081 void (*with_cleanup)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +010082 void (*setup_except)(emit_t *emit, uint label);
83 void (*setup_finally)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +010084 void (*end_finally)(emit_t *emit);
Damien George2326d522014-03-27 23:26:35 +000085 void (*get_iter)(emit_t *emit);
Damien George6f355fd2014-04-10 14:11:31 +010086 void (*for_iter)(emit_t *emit, uint label);
Damien415eb6f2013-10-05 12:19:06 +010087 void (*for_iter_end)(emit_t *emit);
88 void (*pop_block)(emit_t *emit);
89 void (*pop_except)(emit_t *emit);
Damien Georged17926d2014-03-30 13:35:08 +010090 void (*unary_op)(emit_t *emit, mp_unary_op_t op);
91 void (*binary_op)(emit_t *emit, mp_binary_op_t op);
Damien415eb6f2013-10-05 12:19:06 +010092 void (*build_tuple)(emit_t *emit, int n_args);
93 void (*build_list)(emit_t *emit, int n_args);
94 void (*list_append)(emit_t *emit, int list_stack_index);
95 void (*build_map)(emit_t *emit, int n_args);
96 void (*store_map)(emit_t *emit);
97 void (*map_add)(emit_t *emit, int map_stack_index);
98 void (*build_set)(emit_t *emit, int n_args);
99 void (*set_add)(emit_t *emit, int set_stack_index);
100 void (*build_slice)(emit_t *emit, int n_args);
101 void (*unpack_sequence)(emit_t *emit, int n_args);
102 void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
Damien George30565092014-03-31 11:30:17 +0100103 void (*make_function)(emit_t *emit, scope_t *scope, uint n_pos_defaults, uint n_kw_defaults);
Damien George3558f622014-04-20 17:50:40 +0100104 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 +0100105 void (*call_function)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
106 void (*call_method)(emit_t *emit, int n_positional, int n_keyword, uint star_flags);
Damien415eb6f2013-10-05 12:19:06 +0100107 void (*return_value)(emit_t *emit);
108 void (*raise_varargs)(emit_t *emit, int n_args);
109 void (*yield_value)(emit_t *emit);
110 void (*yield_from)(emit_t *emit);
111} emit_method_table_t;
112
Damien4b03e772013-10-05 14:17:09 +0100113void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
114void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
115void 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 +0100116
Damien6cdd3af2013-10-05 18:08:26 +0100117extern const emit_method_table_t emit_pass1_method_table;
118extern const emit_method_table_t emit_cpython_method_table;
119extern const emit_method_table_t emit_bc_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100120extern const emit_method_table_t emit_native_x64_method_table;
121extern const emit_method_table_t emit_native_thumb_method_table;
Damien6cdd3af2013-10-05 18:08:26 +0100122
Damien George35e2a4e2014-02-05 00:51:47 +0000123emit_t *emit_pass1_new(void);
Damien6cdd3af2013-10-05 18:08:26 +0100124emit_t *emit_cpython_new(uint max_num_labels);
Damien Georgecbd2f742014-01-19 11:48:48 +0000125emit_t *emit_bc_new(uint max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100126emit_t *emit_native_x64_new(uint max_num_labels);
127emit_t *emit_native_thumb_new(uint max_num_labels);
Damien826005c2013-10-05 23:17:28 +0100128
Damien George41d02b62014-01-24 22:42:28 +0000129void emit_pass1_free(emit_t *emit);
130void emit_bc_free(emit_t *emit);
131void emit_native_x64_free(emit_t *emit);
132void emit_native_thumb_free(emit_t *emit);
133
Damien826005c2013-10-05 23:17:28 +0100134typedef struct _emit_inline_asm_t emit_inline_asm_t;
135
136typedef struct _emit_inline_asm_method_table_t {
137 void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope);
Damien Georgea26dc502014-04-12 17:54:52 +0100138 bool (*end_pass)(emit_inline_asm_t *emit);
Damiend99b0522013-12-21 18:17:45 +0000139 int (*count_params)(emit_inline_asm_t *emit, int n_params, mp_parse_node_t *pn_params);
Damien George6f355fd2014-04-10 14:11:31 +0100140 void (*label)(emit_inline_asm_t *emit, uint label_num, qstr label_id);
Damiend99b0522013-12-21 18:17:45 +0000141 void (*op)(emit_inline_asm_t *emit, qstr op, int n_args, mp_parse_node_t *pn_args);
Damien826005c2013-10-05 23:17:28 +0100142} emit_inline_asm_method_table_t;
143
144extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
145
146emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels);
Damien George41d02b62014-01-24 22:42:28 +0000147void emit_inline_thumb_free(emit_inline_asm_t *emit);
148