blob: 93c7298d377a243f76c817b407035671c9d6cab7 [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
8 * stack size to compile the entry to the function, and this effects code size.
9 */
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
Damien415eb6f2013-10-05 12:19:06 +010017typedef struct _emit_t emit_t;
Damien429d7192013-10-04 19:53:11 +010018
Damien415eb6f2013-10-05 12:19:06 +010019typedef 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);
Damien429d7192013-10-04 19:53:11 +010026
Damien4b03e772013-10-05 14:17:09 +010027 void (*load_id)(emit_t *emit, qstr qstr);
28 void (*store_id)(emit_t *emit, qstr qstr);
29 void (*delete_id)(emit_t *emit, qstr qstr);
30
Damien415eb6f2013-10-05 12:19:06 +010031 void (*label_assign)(emit_t *emit, int l);
32 void (*import_name)(emit_t *emit, qstr qstr);
33 void (*import_from)(emit_t *emit, qstr qstr);
34 void (*import_star)(emit_t *emit);
35 void (*load_const_tok)(emit_t *emit, py_token_kind_t tok);
36 void (*load_const_small_int)(emit_t *emit, int arg);
37 void (*load_const_int)(emit_t *emit, qstr qstr);
38 void (*load_const_dec)(emit_t *emit, qstr qstr);
39 void (*load_const_id)(emit_t *emit, qstr qstr);
40 void (*load_const_str)(emit_t *emit, qstr qstr, bool bytes);
41 void (*load_const_verbatim_start)(emit_t *emit);
42 void (*load_const_verbatim_int)(emit_t *emit, int val);
43 void (*load_const_verbatim_str)(emit_t *emit, const char *str);
44 void (*load_const_verbatim_strn)(emit_t *emit, const char *str, int len);
45 void (*load_const_verbatim_quoted_str)(emit_t *emit, qstr qstr, bool bytes);
46 void (*load_const_verbatim_end)(emit_t *emit);
47 void (*load_fast)(emit_t *emit, qstr qstr, int local_num);
48 void (*load_name)(emit_t *emit, qstr qstr);
49 void (*load_global)(emit_t *emit, qstr qstr);
50 void (*load_deref)(emit_t *emit, qstr qstr);
51 void (*load_closure)(emit_t *emit, qstr qstr);
52 void (*load_attr)(emit_t *emit, qstr qstr);
53 void (*load_method)(emit_t *emit, qstr qstr);
54 void (*load_build_class)(emit_t *emit);
55 void (*store_fast)(emit_t *emit, qstr qstr, int local_num);
56 void (*store_name)(emit_t *emit, qstr qstr);
57 void (*store_global)(emit_t *emit, qstr qstr);
58 void (*store_deref)(emit_t *emit, qstr qstr);
59 void (*store_attr)(emit_t *emit, qstr qstr);
60 void (*store_locals)(emit_t *emit);
61 void (*store_subscr)(emit_t *emit);
62 void (*delete_fast)(emit_t *emit, qstr qstr, int local_num);
63 void (*delete_name)(emit_t *emit, qstr qstr);
64 void (*delete_global)(emit_t *emit, qstr qstr);
65 void (*delete_deref)(emit_t *emit, qstr qstr);
66 void (*delete_attr)(emit_t *emit, qstr qstr);
67 void (*delete_subscr)(emit_t *emit);
68 void (*dup_top)(emit_t *emit);
69 void (*dup_top_two)(emit_t *emit);
70 void (*pop_top)(emit_t *emit);
71 void (*rot_two)(emit_t *emit);
72 void (*rot_three)(emit_t *emit);
73 void (*jump)(emit_t *emit, int label);
74 void (*pop_jump_if_true)(emit_t *emit, int label);
75 void (*pop_jump_if_false)(emit_t *emit, int label);
76 void (*jump_if_true_or_pop)(emit_t *emit, int label);
77 void (*jump_if_false_or_pop)(emit_t *emit, int label);
78 void (*setup_loop)(emit_t *emit, int label);
79 void (*break_loop)(emit_t *emit, int label);
80 void (*continue_loop)(emit_t *emit, int label);
81 void (*setup_with)(emit_t *emit, int label);
82 void (*with_cleanup)(emit_t *emit);
83 void (*setup_except)(emit_t *emit, int label);
84 void (*setup_finally)(emit_t *emit, int label);
85 void (*end_finally)(emit_t *emit);
86 void (*get_iter)(emit_t *emit); // tos = getiter(tos)
87 void (*for_iter)(emit_t *emit, int label);
88 void (*for_iter_end)(emit_t *emit);
89 void (*pop_block)(emit_t *emit);
90 void (*pop_except)(emit_t *emit);
91 void (*unary_op)(emit_t *emit, rt_unary_op_t op);
92 void (*binary_op)(emit_t *emit, rt_binary_op_t op);
93 void (*compare_op)(emit_t *emit, rt_compare_op_t op);
94 void (*build_tuple)(emit_t *emit, int n_args);
95 void (*build_list)(emit_t *emit, int n_args);
96 void (*list_append)(emit_t *emit, int list_stack_index);
97 void (*build_map)(emit_t *emit, int n_args);
98 void (*store_map)(emit_t *emit);
99 void (*map_add)(emit_t *emit, int map_stack_index);
100 void (*build_set)(emit_t *emit, int n_args);
101 void (*set_add)(emit_t *emit, int set_stack_index);
102 void (*build_slice)(emit_t *emit, int n_args);
103 void (*unpack_sequence)(emit_t *emit, int n_args);
104 void (*unpack_ex)(emit_t *emit, int n_left, int n_right);
105 void (*make_function)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
106 void (*make_closure)(emit_t *emit, scope_t *scope, int n_dict_params, int n_default_params);
107 void (*call_function)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
108 void (*call_method)(emit_t *emit, int n_positional, int n_keyword, bool have_star_arg, bool have_dbl_star_arg);
109 void (*return_value)(emit_t *emit);
110 void (*raise_varargs)(emit_t *emit, int n_args);
111 void (*yield_value)(emit_t *emit);
112 void (*yield_from)(emit_t *emit);
113} emit_method_table_t;
114
Damien4b03e772013-10-05 14:17:09 +0100115void emit_common_load_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
116void emit_common_store_id(emit_t *emit, const emit_method_table_t *emit_method_table, scope_t *scope, qstr qstr);
117void 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 +0100118
Damien6cdd3af2013-10-05 18:08:26 +0100119extern const emit_method_table_t emit_pass1_method_table;
120extern const emit_method_table_t emit_cpython_method_table;
121extern const emit_method_table_t emit_bc_method_table;
Damien13ed3a62013-10-08 09:05:10 +0100122extern const emit_method_table_t emit_native_x64_method_table;
123extern const emit_method_table_t emit_native_thumb_method_table;
Damien6cdd3af2013-10-05 18:08:26 +0100124
125emit_t *emit_pass1_new(qstr qstr___class__);
126void emit_pass1_free(emit_t *emit);
127emit_t *emit_cpython_new(uint max_num_labels);
128emit_t *emit_bc_new(uint max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100129emit_t *emit_native_x64_new(uint max_num_labels);
130emit_t *emit_native_thumb_new(uint max_num_labels);
Damien826005c2013-10-05 23:17:28 +0100131
132typedef struct _emit_inline_asm_t emit_inline_asm_t;
133
134typedef struct _emit_inline_asm_method_table_t {
135 void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope);
136 void (*end_pass)(emit_inline_asm_t *emit);
Damiena2f2f7d2013-10-06 00:14:13 +0100137 int (*count_params)(emit_inline_asm_t *emit, int n_params, py_parse_node_t *pn_params);
Damien826005c2013-10-05 23:17:28 +0100138 void (*label)(emit_inline_asm_t *emit, int label_num, qstr label_id);
Damiena2f2f7d2013-10-06 00:14:13 +0100139 void (*op)(emit_inline_asm_t *emit, qstr op, int n_args, py_parse_node_t *pn_args);
Damien826005c2013-10-05 23:17:28 +0100140} emit_inline_asm_method_table_t;
141
142extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;
143
144emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels);