blob: d0614d303aa174b195687e691247d5b2852887ce [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
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
Damien13ed3a62013-10-08 09:05:10 +010027// Essentially normal Python has 1 type: Python objects
28// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
29// If you declare everything in Viper as a Python object (ie omit type decls) then
30// it should in principle be exactly the same as Python native.
31// Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
32// In practice we won't have a VM but rather do this in asm which is actually very minimal.
33
34// Because it breaks strict Python equivalence it should be a completely separate
35// decorator. It breaks equivalence because overflow on integers wraps around.
36// It shouldn't break equivalence if you don't use the new types, but since the
37// type decls might be used in normal Python for other reasons, it's probably safest,
38// cleanest and clearest to make it a separate decorator.
39
40// Actually, it does break equivalence because integers default to native integers,
41// not Python objects.
42
43// for x in l[0:8]: can be compiled into a native loop if l has pointer type
44
Damien13ed3a62013-10-08 09:05:10 +010045#include <stdio.h>
46#include <string.h>
47#include <assert.h>
48
Damien George51dfcb42015-01-01 20:27:54 +000049#include "py/nlr.h"
50#include "py/emit.h"
Damien George99886182015-04-06 22:38:53 +010051#include "py/bc.h"
Damien13ed3a62013-10-08 09:05:10 +010052
Damien Georgecdd96df2014-04-06 12:58:40 +010053#if 0 // print debugging info
54#define DEBUG_PRINT (1)
55#define DEBUG_printf DEBUG_printf
56#else // don't print debugging info
57#define DEBUG_printf(...) (void)0
58#endif
59
Damien13ed3a62013-10-08 09:05:10 +010060// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010061#if (MICROPY_EMIT_X64 && N_X64) \
62 || (MICROPY_EMIT_X86 && N_X86) \
63 || (MICROPY_EMIT_THUMB && N_THUMB) \
64 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010065
Damien3ef4abb2013-10-12 16:53:13 +010066#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010067
68// x64 specific stuff
69
Damien George51dfcb42015-01-01 20:27:54 +000070#include "py/asmx64.h"
Damien13ed3a62013-10-08 09:05:10 +010071
Damien13ed3a62013-10-08 09:05:10 +010072#define EXPORT_FUN(name) emit_native_x64_##name
73
Damien George99886182015-04-06 22:38:53 +010074#define ASM_WORD_SIZE (8)
75
Damien George0b610de2014-09-29 16:25:04 +010076#define REG_RET ASM_X64_REG_RAX
77#define REG_ARG_1 ASM_X64_REG_RDI
78#define REG_ARG_2 ASM_X64_REG_RSI
79#define REG_ARG_3 ASM_X64_REG_RDX
80#define REG_ARG_4 ASM_X64_REG_RCX
Damien George99886182015-04-06 22:38:53 +010081#define REG_ARG_5 ASM_X64_REG_R08
Damien Georgec90f59e2014-09-06 23:06:36 +010082
Damien George81057362014-09-07 01:06:19 +010083// caller-save
Damien George0b610de2014-09-29 16:25:04 +010084#define REG_TEMP0 ASM_X64_REG_RAX
85#define REG_TEMP1 ASM_X64_REG_RDI
86#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010087
88// callee-save
Damien George0b610de2014-09-29 16:25:04 +010089#define REG_LOCAL_1 ASM_X64_REG_RBX
90#define REG_LOCAL_2 ASM_X64_REG_R12
91#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +010092#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +010093
94#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
95#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
96
97#define ASM_T asm_x64_t
98#define ASM_NEW asm_x64_new
99#define ASM_FREE asm_x64_free
100#define ASM_GET_CODE asm_x64_get_code
Damien George99886182015-04-06 22:38:53 +0100101#define ASM_GET_CODE_POS asm_x64_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100102#define ASM_GET_CODE_SIZE asm_x64_get_code_size
103#define ASM_START_PASS asm_x64_start_pass
104#define ASM_END_PASS asm_x64_end_pass
105#define ASM_ENTRY asm_x64_entry
106#define ASM_EXIT asm_x64_exit
107
Damien George99886182015-04-06 22:38:53 +0100108#define ASM_ALIGN asm_x64_align
109#define ASM_DATA asm_x64_data
110
Damien Georgec90f59e2014-09-06 23:06:36 +0100111#define ASM_LABEL_ASSIGN asm_x64_label_assign
112#define ASM_JUMP asm_x64_jmp_label
113#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
114 do { \
115 asm_x64_test_r8_with_r8(as, reg, reg); \
116 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
117 } while (0)
118#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
119 do { \
120 asm_x64_test_r8_with_r8(as, reg, reg); \
121 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
122 } while (0)
123#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
124 do { \
125 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
126 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
127 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100128#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100129
130#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
131#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
132#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
133#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
134 do { \
135 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
136 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
137 } while (false)
138#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100139#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x64_mov_r64_r64((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100140#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
141
Damien George3112cde2014-09-29 18:45:42 +0100142#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
143#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100144#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
145#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
146#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100147#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
148#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
Damien George567b3492015-06-04 14:00:29 +0000149#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x64_mul_r64_r64((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100150
Damien George91cfd412014-10-12 16:59:29 +0100151#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
Damien George4cd9ced2015-01-15 14:41:41 +0000152#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x64_mov_mem64_to_r64((as), (reg_base), 8 * (word_offset), (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100153#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
154#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100155#define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem32_to_r64zx((as), (reg_base), 0, (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100156
157#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000158#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 8 * (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100159#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
160#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100161#define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_x64_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100162
Damien Georgec90f59e2014-09-06 23:06:36 +0100163#elif N_X86
164
165// x86 specific stuff
166
Damien George51dfcb42015-01-01 20:27:54 +0000167#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100168
169STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
170 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
171 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100172 [MP_F_LOAD_NAME] = 1,
173 [MP_F_LOAD_GLOBAL] = 1,
174 [MP_F_LOAD_BUILD_CLASS] = 0,
175 [MP_F_LOAD_ATTR] = 2,
176 [MP_F_LOAD_METHOD] = 3,
177 [MP_F_STORE_NAME] = 2,
178 [MP_F_STORE_GLOBAL] = 2,
179 [MP_F_STORE_ATTR] = 3,
180 [MP_F_OBJ_SUBSCR] = 3,
181 [MP_F_OBJ_IS_TRUE] = 1,
182 [MP_F_UNARY_OP] = 2,
183 [MP_F_BINARY_OP] = 3,
184 [MP_F_BUILD_TUPLE] = 2,
185 [MP_F_BUILD_LIST] = 2,
186 [MP_F_LIST_APPEND] = 2,
187 [MP_F_BUILD_MAP] = 1,
188 [MP_F_STORE_MAP] = 3,
189#if MICROPY_PY_BUILTINS_SET
190 [MP_F_BUILD_SET] = 2,
191 [MP_F_STORE_SET] = 2,
192#endif
193 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
194 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
195 [MP_F_CALL_METHOD_N_KW] = 3,
Damien George78772ad2015-04-06 22:48:21 +0100196 [MP_F_CALL_METHOD_N_KW_VAR] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100197 [MP_F_GETITER] = 1,
198 [MP_F_ITERNEXT] = 1,
199 [MP_F_NLR_PUSH] = 1,
200 [MP_F_NLR_POP] = 0,
201 [MP_F_NATIVE_RAISE] = 1,
202 [MP_F_IMPORT_NAME] = 3,
203 [MP_F_IMPORT_FROM] = 2,
204 [MP_F_IMPORT_ALL] = 1,
205#if MICROPY_PY_BUILTINS_SLICE
206 [MP_F_NEW_SLICE] = 3,
207#endif
208 [MP_F_UNPACK_SEQUENCE] = 3,
209 [MP_F_UNPACK_EX] = 3,
210 [MP_F_DELETE_NAME] = 1,
211 [MP_F_DELETE_GLOBAL] = 1,
Damien George99957382015-04-03 14:38:41 +0000212 [MP_F_NEW_CELL] = 1,
213 [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
Damien George99886182015-04-06 22:38:53 +0100214 [MP_F_SETUP_CODE_STATE] = 5,
Damien Georgec90f59e2014-09-06 23:06:36 +0100215};
216
217#define EXPORT_FUN(name) emit_native_x86_##name
218
Damien George99886182015-04-06 22:38:53 +0100219#define ASM_WORD_SIZE (4)
220
Damien George0b610de2014-09-29 16:25:04 +0100221#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000222#define REG_ARG_1 ASM_X86_REG_ARG_1
223#define REG_ARG_2 ASM_X86_REG_ARG_2
224#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George99886182015-04-06 22:38:53 +0100225#define REG_ARG_4 ASM_X86_REG_ARG_4
226#define REG_ARG_5 ASM_X86_REG_ARG_5
Damien George81057362014-09-07 01:06:19 +0100227
Damien George25d90412014-09-06 23:24:32 +0000228// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100229#define REG_TEMP0 ASM_X86_REG_EAX
230#define REG_TEMP1 ASM_X86_REG_ECX
231#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100232
Damien George25d90412014-09-06 23:24:32 +0000233// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100234#define REG_LOCAL_1 ASM_X86_REG_EBX
235#define REG_LOCAL_2 ASM_X86_REG_ESI
236#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000237#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100238
239#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
240#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
241
242#define ASM_T asm_x86_t
243#define ASM_NEW asm_x86_new
244#define ASM_FREE asm_x86_free
245#define ASM_GET_CODE asm_x86_get_code
Damien George99886182015-04-06 22:38:53 +0100246#define ASM_GET_CODE_POS asm_x86_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100247#define ASM_GET_CODE_SIZE asm_x86_get_code_size
248#define ASM_START_PASS asm_x86_start_pass
249#define ASM_END_PASS asm_x86_end_pass
250#define ASM_ENTRY asm_x86_entry
251#define ASM_EXIT asm_x86_exit
252
Damien George99886182015-04-06 22:38:53 +0100253#define ASM_ALIGN asm_x86_align
254#define ASM_DATA asm_x86_data
255
Damien Georgec90f59e2014-09-06 23:06:36 +0100256#define ASM_LABEL_ASSIGN asm_x86_label_assign
257#define ASM_JUMP asm_x86_jmp_label
258#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
259 do { \
260 asm_x86_test_r8_with_r8(as, reg, reg); \
261 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
262 } while (0)
263#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
264 do { \
265 asm_x86_test_r8_with_r8(as, reg, reg); \
266 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
267 } while (0)
268#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
269 do { \
270 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
271 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
272 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100273#define ASM_CALL_IND(as, ptr, idx) asm_x86_call_ind(as, ptr, mp_f_n_args[idx], ASM_X86_REG_EAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100274
275#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
276#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
277#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
278#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
279 do { \
280 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
281 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
282 } while (false)
283#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100284#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x86_mov_r32_r32((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100285#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100286
Damien George3112cde2014-09-29 18:45:42 +0100287#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
288#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100289#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
290#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
291#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x86_and_r32_r32((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100292#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
293#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
Damien George567b3492015-06-04 14:00:29 +0000294#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_x86_mul_r32_r32((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100295
Damien George91cfd412014-10-12 16:59:29 +0100296#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
Damien George99957382015-04-03 14:38:41 +0000297#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_x86_mov_mem32_to_r32((as), (reg_base), 4 * (word_offset), (reg_dest))
Damien George3c34d412014-10-12 16:10:25 +0000298#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
299#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem16_to_r32zx((as), (reg_base), 0, (reg_dest))
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100300#define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100301
302#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
Damien George99957382015-04-03 14:38:41 +0000303#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 4 * (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100304#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
305#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100306#define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100307
Damien3ef4abb2013-10-12 16:53:13 +0100308#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100309
310// thumb specific stuff
311
Damien George51dfcb42015-01-01 20:27:54 +0000312#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100313
Damien13ed3a62013-10-08 09:05:10 +0100314#define EXPORT_FUN(name) emit_native_thumb_##name
315
Damien George99886182015-04-06 22:38:53 +0100316#define ASM_WORD_SIZE (4)
317
Damien George0b610de2014-09-29 16:25:04 +0100318#define REG_RET ASM_THUMB_REG_R0
319#define REG_ARG_1 ASM_THUMB_REG_R0
320#define REG_ARG_2 ASM_THUMB_REG_R1
321#define REG_ARG_3 ASM_THUMB_REG_R2
322#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George99886182015-04-06 22:38:53 +0100323// rest of args go on stack
Damien George81057362014-09-07 01:06:19 +0100324
Damien George0b610de2014-09-29 16:25:04 +0100325#define REG_TEMP0 ASM_THUMB_REG_R0
326#define REG_TEMP1 ASM_THUMB_REG_R1
327#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100328
Damien George0b610de2014-09-29 16:25:04 +0100329#define REG_LOCAL_1 ASM_THUMB_REG_R4
330#define REG_LOCAL_2 ASM_THUMB_REG_R5
331#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100332#define REG_LOCAL_NUM (3)
333
334#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
335#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
336
337#define ASM_T asm_thumb_t
338#define ASM_NEW asm_thumb_new
339#define ASM_FREE asm_thumb_free
340#define ASM_GET_CODE asm_thumb_get_code
Damien George99886182015-04-06 22:38:53 +0100341#define ASM_GET_CODE_POS asm_thumb_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100342#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
343#define ASM_START_PASS asm_thumb_start_pass
344#define ASM_END_PASS asm_thumb_end_pass
345#define ASM_ENTRY asm_thumb_entry
346#define ASM_EXIT asm_thumb_exit
347
Damien George99886182015-04-06 22:38:53 +0100348#define ASM_ALIGN asm_thumb_align
349#define ASM_DATA asm_thumb_data
350
Damien Georgec90f59e2014-09-06 23:06:36 +0100351#define ASM_LABEL_ASSIGN asm_thumb_label_assign
352#define ASM_JUMP asm_thumb_b_label
353#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
354 do { \
355 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100356 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100357 } while (0)
358#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
359 do { \
360 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100361 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100362 } while (0)
363#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
364 do { \
365 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100366 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100367 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100368#define ASM_CALL_IND(as, ptr, idx) asm_thumb_bl_ind(as, ptr, idx, ASM_THUMB_REG_R3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100369
370#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
371#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
372#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
373#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
374 do { \
375 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
376 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
377 } while (false)
378#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local(as, (reg), (local_num))
Damien George3112cde2014-09-29 18:45:42 +0100379#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_thumb_mov_reg_reg((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100380#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_thumb_mov_reg_local_addr(as, (reg), (local_num))
Damien13ed3a62013-10-08 09:05:10 +0100381
Damien George3112cde2014-09-29 18:45:42 +0100382#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
383#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ASR, (reg_dest), (reg_shift))
Damien George1ef23482014-10-12 14:21:06 +0100384#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
385#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
386#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_AND, (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100387#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
388#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
Damien George567b3492015-06-04 14:00:29 +0000389#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_MUL, (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100390
Damien George91cfd412014-10-12 16:59:29 +0100391#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000392#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100393#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
394#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100395#define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
Damien George91cfd412014-10-12 16:59:29 +0100396
Damien Georgee9dac3b2014-09-29 22:10:41 +0100397#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
Damien George4cd9ced2015-01-15 14:41:41 +0000398#define ASM_STORE_REG_REG_OFFSET(as, reg_src, reg_base, word_offset) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), (word_offset))
Damien Georgee9dac3b2014-09-29 22:10:41 +0100399#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
400#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100401#define ASM_STORE32_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100402
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200403#elif N_ARM
404
405// ARM specific stuff
406
Damien George51dfcb42015-01-01 20:27:54 +0000407#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200408
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300409#define ASM_WORD_SIZE (4)
410
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200411#define EXPORT_FUN(name) emit_native_arm_##name
412
Damien George0b610de2014-09-29 16:25:04 +0100413#define REG_RET ASM_ARM_REG_R0
414#define REG_ARG_1 ASM_ARM_REG_R0
415#define REG_ARG_2 ASM_ARM_REG_R1
416#define REG_ARG_3 ASM_ARM_REG_R2
417#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100418
Damien George0b610de2014-09-29 16:25:04 +0100419#define REG_TEMP0 ASM_ARM_REG_R0
420#define REG_TEMP1 ASM_ARM_REG_R1
421#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100422
Damien George0b610de2014-09-29 16:25:04 +0100423#define REG_LOCAL_1 ASM_ARM_REG_R4
424#define REG_LOCAL_2 ASM_ARM_REG_R5
425#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100426#define REG_LOCAL_NUM (3)
427
428#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
429#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
430
431#define ASM_T asm_arm_t
432#define ASM_NEW asm_arm_new
433#define ASM_FREE asm_arm_free
434#define ASM_GET_CODE asm_arm_get_code
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300435#define ASM_GET_CODE_POS asm_arm_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100436#define ASM_GET_CODE_SIZE asm_arm_get_code_size
437#define ASM_START_PASS asm_arm_start_pass
438#define ASM_END_PASS asm_arm_end_pass
439#define ASM_ENTRY asm_arm_entry
440#define ASM_EXIT asm_arm_exit
441
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300442#define ASM_ALIGN asm_arm_align
443#define ASM_DATA asm_arm_data
444
Damien Georgec90f59e2014-09-06 23:06:36 +0100445#define ASM_LABEL_ASSIGN asm_arm_label_assign
446#define ASM_JUMP asm_arm_b_label
447#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
448 do { \
449 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100450 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100451 } while (0)
452#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
453 do { \
454 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100455 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100456 } while (0)
457#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
458 do { \
459 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100460 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100461 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100462#define ASM_CALL_IND(as, ptr, idx) asm_arm_bl_ind(as, ptr, idx, ASM_ARM_REG_R3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100463
464#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
465#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
466#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
467#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
468 do { \
469 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
470 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
471 } while (false)
472#define ASM_MOV_LOCAL_TO_REG(as, local_num, reg) asm_arm_mov_reg_local(as, (reg), (local_num))
Damien George3112cde2014-09-29 18:45:42 +0100473#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_arm_mov_reg_reg((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100474#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
475
Fabian Vogte5268962014-10-04 00:53:46 +0200476#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
477#define ASM_ASR_REG_REG(as, reg_dest, reg_shift) asm_arm_asr_reg_reg((as), (reg_dest), (reg_shift))
Damien George1ef23482014-10-12 14:21:06 +0100478#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
479#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
480#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_arm_and_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100481#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
482#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
Damien George567b3492015-06-04 14:00:29 +0000483#define ASM_MUL_REG_REG(as, reg_dest, reg_src) asm_arm_mul_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100484
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300485#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 0)
486#define ASM_LOAD_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 4 * (word_offset))
Damien George91cfd412014-10-12 16:59:29 +0100487#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
488#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
Damien Georgefcce1482015-10-14 12:40:54 +0100489#define ASM_LOAD32_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 0)
Damien George91cfd412014-10-12 16:59:29 +0100490
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300491#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base), 0)
492#define ASM_STORE_REG_REG_OFFSET(as, reg_dest, reg_base, word_offset) asm_arm_str_reg_reg((as), (reg_dest), (reg_base), 4 * (word_offset))
Fabian Vogte5268962014-10-04 00:53:46 +0200493#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
494#define ASM_STORE16_REG_REG(as, reg_value, reg_base) asm_arm_strh_reg_reg((as), (reg_value), (reg_base))
Damien Georgefcce1482015-10-14 12:40:54 +0100495#define ASM_STORE32_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100496
Damien Georgec90f59e2014-09-06 23:06:36 +0100497#else
498
499#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200500
Damien13ed3a62013-10-08 09:05:10 +0100501#endif
502
Damien Georgec8b60f02015-04-20 13:29:31 +0000503#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
504 *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
505 } while (0)
506
Damien13ed3a62013-10-08 09:05:10 +0100507typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100508 STACK_VALUE,
509 STACK_REG,
510 STACK_IMM,
511} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100512
Damien Georgee9dac3b2014-09-29 22:10:41 +0100513// these enums must be distinct and the bottom 2 bits
514// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100515typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100516 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
517 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
518 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
519 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
520
521 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
522 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
523 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100524 VTYPE_PTR32 = 0x40 | MP_NATIVE_TYPE_UINT,
525 VTYPE_PTR_NONE = 0x50 | MP_NATIVE_TYPE_UINT,
Damien Georgee9dac3b2014-09-29 22:10:41 +0100526
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100527 VTYPE_UNBOUND = 0x60 | MP_NATIVE_TYPE_OBJ,
528 VTYPE_BUILTIN_CAST = 0x70 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100529} vtype_kind_t;
530
Damien Georgec8b60f02015-04-20 13:29:31 +0000531STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
532 switch (vtype) {
533 case VTYPE_PYOBJ: return MP_QSTR_object;
534 case VTYPE_BOOL: return MP_QSTR_bool;
535 case VTYPE_INT: return MP_QSTR_int;
536 case VTYPE_UINT: return MP_QSTR_uint;
537 case VTYPE_PTR: return MP_QSTR_ptr;
538 case VTYPE_PTR8: return MP_QSTR_ptr8;
539 case VTYPE_PTR16: return MP_QSTR_ptr16;
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100540 case VTYPE_PTR32: return MP_QSTR_ptr32;
Damien Georgec8b60f02015-04-20 13:29:31 +0000541 case VTYPE_PTR_NONE: default: return MP_QSTR_None;
542 }
543}
544
Damienff8ed772013-10-08 22:18:32 +0100545typedef struct _stack_info_t {
546 vtype_kind_t vtype;
547 stack_info_kind_t kind;
548 union {
549 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100550 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000551 } data;
Damienff8ed772013-10-08 22:18:32 +0100552} stack_info_t;
553
Damien13ed3a62013-10-08 09:05:10 +0100554struct _emit_t {
Damien Georgec8b60f02015-04-20 13:29:31 +0000555 mp_obj_t *error_slot;
Damien13ed3a62013-10-08 09:05:10 +0100556 int pass;
557
558 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100559
Damien George2ac4af62014-08-15 16:45:41 +0100560 vtype_kind_t return_vtype;
561
Damien George7ff996c2014-09-08 23:05:16 +0100562 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100563 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100564
Damien George7ff996c2014-09-08 23:05:16 +0100565 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100566 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100567 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100568
Damien George99886182015-04-06 22:38:53 +0100569 int code_info_size;
570 int code_info_offset;
571 int prelude_offset;
572 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100573 int stack_start;
574 int stack_size;
575
576 bool last_emit_was_return_value;
577
Damien13ed3a62013-10-08 09:05:10 +0100578 scope_t *scope;
579
Damien Georgec90f59e2014-09-06 23:06:36 +0100580 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100581};
582
Damien Georgec8b60f02015-04-20 13:29:31 +0000583emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100584 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000585 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100586 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100587 return emit;
588}
589
Damien George41d02b62014-01-24 22:42:28 +0000590void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100591 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100592 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
593 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200594 m_del_obj(emit_t, emit);
595}
596
Damien George2ac4af62014-08-15 16:45:41 +0100597STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
598 switch (op) {
599 case MP_EMIT_NATIVE_TYPE_ENABLE:
600 emit->do_viper_types = arg1;
601 break;
602
603 default: {
604 vtype_kind_t type;
605 switch (arg2) {
606 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
607 case MP_QSTR_bool: type = VTYPE_BOOL; break;
608 case MP_QSTR_int: type = VTYPE_INT; break;
609 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100610 case MP_QSTR_ptr: type = VTYPE_PTR; break;
611 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
612 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100613 case MP_QSTR_ptr32: type = VTYPE_PTR32; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000614 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100615 }
616 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
617 emit->return_vtype = type;
618 } else {
619 assert(arg1 < emit->local_vtype_alloc);
620 emit->local_vtype[arg1] = type;
621 }
622 break;
623 }
624 }
Damien13ed3a62013-10-08 09:05:10 +0100625}
626
Damien Georgefa5950e2015-04-03 15:03:24 +0000627STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000628STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000629STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000630STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000631
Damien George99886182015-04-06 22:38:53 +0100632#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
633
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200634STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000635 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
636
Damien13ed3a62013-10-08 09:05:10 +0100637 emit->pass = pass;
638 emit->stack_start = 0;
639 emit->stack_size = 0;
640 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100641 emit->scope = scope;
642
Damien George36db6bc2014-05-07 17:24:22 +0100643 // allocate memory for keeping track of the types of locals
644 if (emit->local_vtype_alloc < scope->num_locals) {
645 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
646 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100647 }
Damien George36db6bc2014-05-07 17:24:22 +0100648
649 // allocate memory for keeping track of the objects on the stack
650 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000651 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100652 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000653 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100654 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100655 }
656
Damien George99886182015-04-06 22:38:53 +0100657 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100658 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100659
660 // set default type for arguments
661 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
662 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
663 num_args += 1;
664 }
665 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
666 num_args += 1;
667 }
668 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100669 emit->local_vtype[i] = VTYPE_PYOBJ;
670 }
Damien Georgea5190a72014-08-15 22:39:08 +0100671
672 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100673 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100674 emit->local_vtype[i] = VTYPE_UNBOUND;
675 }
676
677 // values on stack begin unbound
678 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100679 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100680 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100681 }
682
Damien Georgec90f59e2014-09-06 23:06:36 +0100683 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100684
Damien George99886182015-04-06 22:38:53 +0100685 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100686
Damien George99886182015-04-06 22:38:53 +0100687 if (emit->do_viper_types) {
688
Damien Georgef17e6632015-07-23 14:30:37 +0100689 // right now we have a restriction of maximum of 4 arguments
690 if (scope->num_pos_args >= 5) {
Damien George84d59c22015-07-27 22:20:00 +0100691 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
Damien Georgef17e6632015-07-23 14:30:37 +0100692 return;
693 }
694
Damien George99886182015-04-06 22:38:53 +0100695 // entry to function
696 int num_locals = 0;
697 if (pass > MP_PASS_SCOPE) {
698 num_locals = scope->num_locals - REG_LOCAL_NUM;
699 if (num_locals < 0) {
700 num_locals = 0;
701 }
702 emit->stack_start = num_locals;
703 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100704 }
Damien George99886182015-04-06 22:38:53 +0100705 ASM_ENTRY(emit->as, num_locals);
706
Damien Georgec39093d2015-08-12 23:31:19 +0100707 // TODO don't load r7 if we don't need it
708 #if N_THUMB
709 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
710 #elif N_ARM
711 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
712 #endif
713
Damien George99886182015-04-06 22:38:53 +0100714 #if N_X86
715 for (int i = 0; i < scope->num_pos_args; i++) {
716 if (i == 0) {
717 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
718 } else if (i == 1) {
719 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
720 } else if (i == 2) {
721 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
722 } else {
723 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
724 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
725 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100726 }
Damien George99886182015-04-06 22:38:53 +0100727 #else
728 for (int i = 0; i < scope->num_pos_args; i++) {
729 if (i == 0) {
730 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
731 } else if (i == 1) {
732 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
733 } else if (i == 2) {
734 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
735 } else if (i == 3) {
736 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
737 } else {
738 // TODO not implemented
739 assert(0);
740 }
741 }
742 #endif
743
744 } else {
745 // work out size of state (locals plus stack)
746 emit->n_state = scope->num_locals + scope->stack_size;
747
748 // allocate space on C-stack for code_state structure, which includes state
749 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
750
Damien Georgec39093d2015-08-12 23:31:19 +0100751 // TODO don't load r7 if we don't need it
752 #if N_THUMB
753 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
754 #elif N_ARM
755 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
756 #endif
757
Damien George99886182015-04-06 22:38:53 +0100758 // prepare incoming arguments for call to mp_setup_code_state
759 #if N_X86
760 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
761 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
762 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
763 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
764 #else
765 #if N_THUMB
766 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300767 #elif N_ARM
768 ASM_MOV_REG_REG(emit->as, ASM_ARM_REG_R4, REG_ARG_4);
Damien George99886182015-04-06 22:38:53 +0100769 #else
770 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
771 #endif
772 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
773 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
774 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
775 #endif
776
777 // set code_state.code_info (offset from start of this function to code_info data)
778 // XXX this encoding may change size
779 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->code_info_offset, offsetof(mp_code_state, code_info) / sizeof(mp_uint_t), REG_ARG_1);
780
781 // set code_state.ip (offset from start of this function to prelude info)
782 // XXX this encoding may change size
783 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
784
785 // set code_state.n_state
786 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
787
788 // put address of code_state into first arg
789 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
790
791 // call mp_setup_code_state to prepare code_state structure
792 #if N_THUMB
793 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
794 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
795 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300796 #elif N_ARM
797 asm_arm_push(emit->as, 1 << ASM_ARM_REG_R4); // push 5th arg
798 asm_arm_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
799 asm_arm_pop(emit->as, 1 << REG_RET); // pop dummy (was 5th arg)
Damien George99886182015-04-06 22:38:53 +0100800 #else
801 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
802 #endif
803
804 // cache some locals in registers
805 if (scope->num_locals > 0) {
806 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
807 if (scope->num_locals > 1) {
808 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
809 if (scope->num_locals > 2) {
810 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
811 }
812 }
813 }
814
815 // set the type of closed over variables
816 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
817 id_info_t *id = &scope->id_info[i];
818 if (id->kind == ID_INFO_KIND_CELL) {
819 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
820 }
Damien13ed3a62013-10-08 09:05:10 +0100821 }
822 }
823
Damien13ed3a62013-10-08 09:05:10 +0100824}
825
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200826STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100827 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100828 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100829 }
Damien George99886182015-04-06 22:38:53 +0100830
831 if (!emit->do_viper_types) {
832 // write dummy code info (for mp_setup_code_state to parse) and arg names
833 emit->code_info_offset = ASM_GET_CODE_POS(emit->as);
834 ASM_DATA(emit->as, 1, emit->code_info_size);
835 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
836 emit->code_info_size = ASM_GET_CODE_POS(emit->as) - emit->code_info_offset;
Damien George9a42eb52015-05-06 13:55:33 +0100837 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100838 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100839 qstr qst = MP_QSTR__star_;
840 for (int j = 0; j < emit->scope->id_info_len; ++j) {
841 id_info_t *id = &emit->scope->id_info[j];
842 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
843 qst = id->qst;
844 break;
845 }
846 }
847 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100848 }
849
850 // bytecode prelude: initialise closed over variables
851 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
852 for (int i = 0; i < emit->scope->id_info_len; i++) {
853 id_info_t *id = &emit->scope->id_info[i];
854 if (id->kind == ID_INFO_KIND_CELL) {
855 assert(id->local_num < 255);
856 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
857 }
858 }
859 ASM_DATA(emit->as, 1, 255); // end of list sentinel
860 }
861
Damien Georgec90f59e2014-09-06 23:06:36 +0100862 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100863
864 // check stack is back to zero size
865 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100866 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100867 }
868
Damien George36db6bc2014-05-07 17:24:22 +0100869 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100870 void *f = ASM_GET_CODE(emit->as);
871 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100872
873 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100874 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100875 mp_uint_t type_sig = emit->return_vtype & 3;
876 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
877 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
878 }
879
Damien George99886182015-04-06 22:38:53 +0100880 mp_emit_glue_assign_native(emit->scope->raw_code,
881 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
882 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
883 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100884 }
885}
886
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200887STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100888 return emit->last_emit_was_return_value;
889}
890
Damien Georged6230f62014-09-23 14:10:03 +0000891STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000892 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100893 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100894 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100895 emit->scope->stack_size = emit->stack_size;
896 }
Damien George21ca2d72014-10-19 19:00:51 +0100897#ifdef DEBUG_PRINT
898 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
899 for (int i = 0; i < emit->stack_size; i++) {
900 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000901 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100902 }
903 DEBUG_printf("\n");
904#endif
Damien13ed3a62013-10-08 09:05:10 +0100905}
906
Damien Georged6230f62014-09-23 14:10:03 +0000907STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100908 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000909 // If we are adjusting the stack in a positive direction (pushing) then we
910 // need to fill in values for the stack kind and vtype of the newly-pushed
911 // entries. These should be set to "value" (ie not reg or imm) because we
912 // should only need to adjust the stack due to a jump to this part in the
913 // code (and hence we have settled the stack before the jump).
914 for (mp_int_t i = 0; i < delta; i++) {
915 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
916 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100917 // TODO we don't know the vtype to use here. At the moment this is a
918 // hack to get the case of multi comparison working.
919 if (delta == 1) {
920 si->vtype = emit->saved_stack_vtype;
921 } else {
922 si->vtype = VTYPE_PYOBJ;
923 }
Damien Georged6230f62014-09-23 14:10:03 +0000924 }
925 adjust_stack(emit, delta);
926}
927
928STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000929 (void)emit;
930 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000931}
932
Damienff8ed772013-10-08 22:18:32 +0100933/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200934STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100935 adjust_stack(emit, stack_size_delta);
936 emit->last_emit_was_return_value = false;
937}
Damienff8ed772013-10-08 22:18:32 +0100938*/
Damien13ed3a62013-10-08 09:05:10 +0100939
Damienff8ed772013-10-08 22:18:32 +0100940// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000941STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100942 emit->last_emit_was_return_value = false;
943 // settle the stack
944 /*
945 if (regs_needed != 0) {
946 for (int i = 0; i < emit->stack_size; i++) {
947 switch (emit->stack_info[i].kind) {
948 case STACK_VALUE:
949 break;
950
951 case STACK_REG:
952 // TODO only push reg if in regs_needed
953 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000954 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100955 break;
956
957 case STACK_IMM:
958 // don't think we ever need to push imms for settling
959 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
960 break;
961 }
962 }
963 }
964 */
Damien13ed3a62013-10-08 09:05:10 +0100965}
966
Damien George3112cde2014-09-29 18:45:42 +0100967// depth==0 is top, depth==1 is before top, etc
968STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
969 return &emit->stack_info[emit->stack_size - 1 - depth];
970}
971
972// depth==0 is top, depth==1 is before top, etc
973STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
974 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100975}
Damien13ed3a62013-10-08 09:05:10 +0100976
Damiend2755ec2013-10-16 23:58:48 +0100977// pos=1 is TOS, pos=2 is next, etc
978// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200979STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100980 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100981 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100982 if (i != skip_stack_pos) {
983 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000984 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100985 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000986 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100987 }
Damienff8ed772013-10-08 22:18:32 +0100988 }
989 }
990}
Damien13ed3a62013-10-08 09:05:10 +0100991
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200992STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100993 for (int i = 0; i < emit->stack_size; i++) {
994 stack_info_t *si = &emit->stack_info[i];
995 if (si->kind == STACK_REG) {
996 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000997 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100998 }
Damien13ed3a62013-10-08 09:05:10 +0100999 }
1000}
1001
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001002STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001003 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +01001004 for (int i = 0; i < emit->stack_size; i++) {
1005 stack_info_t *si = &emit->stack_info[i];
1006 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +00001007 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001008 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001009 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001010 }
1011 }
1012 for (int i = 0; i < emit->stack_size; i++) {
1013 stack_info_t *si = &emit->stack_info[i];
1014 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +00001015 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +01001016 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001017 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +01001018 }
1019 }
1020}
1021
1022// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001023STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001024 need_reg_single(emit, reg_dest, pos);
1025 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001026 *vtype = si->vtype;
1027 switch (si->kind) {
1028 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001029 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001030 break;
1031
Damienff8ed772013-10-08 22:18:32 +01001032 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001033 if (si->data.u_reg != reg_dest) {
1034 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001035 }
1036 break;
1037
Damienff8ed772013-10-08 22:18:32 +01001038 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001039 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001040 break;
1041 }
Damien13ed3a62013-10-08 09:05:10 +01001042}
1043
Damien Georgee9dac3b2014-09-29 22:10:41 +01001044// does an efficient X=pop(); discard(); push(X)
1045// needs a (non-temp) register in case the poped element was stored in the stack
1046STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1047 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1048 si[0] = si[1];
1049 if (si->kind == STACK_VALUE) {
1050 // if folded element was on the stack we need to put it in a register
1051 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1052 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001053 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001054 }
1055 adjust_stack(emit, -1);
1056}
1057
1058// If stacked value is in a register and the register is not r1 or r2, then
1059// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1060STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001061 emit->last_emit_was_return_value = false;
1062 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001063 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001064 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001065 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001066 need_reg_single(emit, *reg_dest, 1);
1067 } else {
1068 emit_access_stack(emit, 1, vtype, *reg_dest);
1069 }
1070 adjust_stack(emit, -1);
1071}
1072
Damien Georgee6ce10a2014-09-06 18:38:20 +01001073STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001074 emit->last_emit_was_return_value = false;
1075 adjust_stack(emit, -1);
1076}
1077
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001078STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001079 emit->last_emit_was_return_value = false;
1080 emit_access_stack(emit, 1, vtype, reg_dest);
1081 adjust_stack(emit, -1);
1082}
1083
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001084STATIC void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
Damien13ed3a62013-10-08 09:05:10 +01001085 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001086 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001087}
1088
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001089STATIC void emit_pre_pop_reg_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb, vtype_kind_t *vtypec, int regc) {
Damien13ed3a62013-10-08 09:05:10 +01001090 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001091 emit_pre_pop_reg(emit, vtypeb, regb);
1092 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001093}
1094
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001095STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001096 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001097}
1098
Damien Georgee9dac3b2014-09-29 22:10:41 +01001099STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1100 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1101 si->vtype = new_vtype;
1102}
1103
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001104STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001105 stack_info_t *si = &emit->stack_info[emit->stack_size];
1106 si->vtype = vtype;
1107 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001108 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001109 adjust_stack(emit, 1);
1110}
1111
Damien George40f3c022014-07-03 13:25:24 +01001112STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001113 stack_info_t *si = &emit->stack_info[emit->stack_size];
1114 si->vtype = vtype;
1115 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001116 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001117 adjust_stack(emit, 1);
1118}
1119
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001120STATIC void emit_post_push_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb) {
Damienff8ed772013-10-08 22:18:32 +01001121 emit_post_push_reg(emit, vtypea, rega);
1122 emit_post_push_reg(emit, vtypeb, regb);
1123}
1124
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001125STATIC void emit_post_push_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc) {
Damienff8ed772013-10-08 22:18:32 +01001126 emit_post_push_reg(emit, vtypea, rega);
1127 emit_post_push_reg(emit, vtypeb, regb);
1128 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001129}
1130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001131STATIC void emit_post_push_reg_reg_reg_reg(emit_t *emit, vtype_kind_t vtypea, int rega, vtype_kind_t vtypeb, int regb, vtype_kind_t vtypec, int regc, vtype_kind_t vtyped, int regd) {
Damienff8ed772013-10-08 22:18:32 +01001132 emit_post_push_reg(emit, vtypea, rega);
1133 emit_post_push_reg(emit, vtypeb, regb);
1134 emit_post_push_reg(emit, vtypec, regc);
1135 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001136}
1137
Damien George7fe21912014-08-16 22:31:57 +01001138STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001139 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001140 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001141}
1142
Damien George7fe21912014-08-16 22:31:57 +01001143STATIC void emit_call_with_imm_arg(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg) {
Damieneb19efb2013-10-10 22:06:54 +01001144 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001145 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1146 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001147}
1148
Damien George40f3c022014-07-03 13:25:24 +01001149// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001150STATIC void emit_call_with_imm_arg_aligned(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val, int arg_reg) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001151 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001152 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1153 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001154}
1155
Damien George7fe21912014-08-16 22:31:57 +01001156STATIC void emit_call_with_2_imm_args(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2) {
Damien Georgecd82e022014-02-02 13:11:48 +00001157 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001158 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1159 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1160 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001161}
1162
Damien George40f3c022014-07-03 13:25:24 +01001163// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001164STATIC void emit_call_with_3_imm_args_and_first_aligned(emit_t *emit, mp_fun_kind_t fun_kind, mp_int_t arg_val1, int arg_reg1, mp_int_t arg_val2, int arg_reg2, mp_int_t arg_val3, int arg_reg3) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001165 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001166 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1167 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1168 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1169 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001170}
1171
Damien George86de21b2014-08-16 22:06:11 +01001172// vtype of all n_pop objects is VTYPE_PYOBJ
1173// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1174// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1175// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1176STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1177 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001178
Damien George86de21b2014-08-16 22:06:11 +01001179 // First, store any immediate values to their respective place on the stack.
1180 for (mp_uint_t i = 0; i < n_pop; i++) {
1181 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1182 // must push any imm's to stack
1183 // must convert them to VTYPE_PYOBJ for viper code
1184 if (si->kind == STACK_IMM) {
1185 si->kind = STACK_VALUE;
1186 switch (si->vtype) {
1187 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001188 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001189 break;
1190 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001191 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001192 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (mp_uint_t)mp_const_false, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001193 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001194 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (mp_uint_t)mp_const_true, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001195 }
1196 si->vtype = VTYPE_PYOBJ;
1197 break;
1198 case VTYPE_INT:
1199 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001200 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->data.u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001201 si->vtype = VTYPE_PYOBJ;
1202 break;
1203 default:
1204 // not handled
1205 assert(0);
1206 }
1207 }
1208
1209 // verify that this value is on the stack
1210 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001211 }
Damien George86de21b2014-08-16 22:06:11 +01001212
1213 // Second, convert any non-VTYPE_PYOBJ to that type.
1214 for (mp_uint_t i = 0; i < n_pop; i++) {
1215 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1216 if (si->vtype != VTYPE_PYOBJ) {
1217 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001218 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001219 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, si->vtype, REG_ARG_2); // arg2 = type
Damien Georgec90f59e2014-09-06 23:06:36 +01001220 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001221 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001222 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001223 }
1224 }
1225
1226 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1227 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001228 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001229}
1230
1231// vtype of all n_push objects is VTYPE_PYOBJ
1232STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1233 need_reg_all(emit);
1234 for (mp_uint_t i = 0; i < n_push; i++) {
1235 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1236 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1237 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001238 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001239 adjust_stack(emit, n_push);
1240}
1241
Damien George7ff996c2014-09-08 23:05:16 +01001242STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001243 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001244 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001245 // need to commit stack because we can jump here from elsewhere
1246 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001247 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001248 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001249}
1250
Damien Georgecdd96df2014-04-06 12:58:40 +01001251STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1252 DEBUG_printf("import_name %s\n", qstr_str(qst));
1253 vtype_kind_t vtype_fromlist;
1254 vtype_kind_t vtype_level;
1255 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1256 assert(vtype_fromlist == VTYPE_PYOBJ);
1257 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001258 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001259 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001260}
1261
Damien Georgecdd96df2014-04-06 12:58:40 +01001262STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1263 DEBUG_printf("import_from %s\n", qstr_str(qst));
1264 emit_native_pre(emit);
1265 vtype_kind_t vtype_module;
1266 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1267 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001268 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001269 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001270}
1271
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001272STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001273 DEBUG_printf("import_star\n");
1274 vtype_kind_t vtype_module;
1275 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1276 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001277 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001278 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001279}
1280
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001281STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001282 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001283 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001284 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001285 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001286 if (emit->do_viper_types) {
1287 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001288 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1289 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1290 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001291 no_other_choice1:
1292 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1293 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001294 }
1295 } else {
1296 vtype = VTYPE_PYOBJ;
1297 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001298 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1299 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1300 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001301 no_other_choice2:
1302 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1303 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001304 }
1305 }
1306 emit_post_push_imm(emit, vtype, val);
1307}
1308
Damien George40f3c022014-07-03 13:25:24 +01001309STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001310 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001311 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001312 if (emit->do_viper_types) {
1313 emit_post_push_imm(emit, VTYPE_INT, arg);
1314 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001315 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001316 }
1317}
1318
Damien George59fba2d2015-06-25 14:42:13 +00001319STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001320 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001321 // TODO: Eventually we want to be able to work with raw pointers in viper to
1322 // do native array access. For now we just load them as any other object.
1323 /*
Damien13ed3a62013-10-08 09:05:10 +01001324 if (emit->do_viper_types) {
1325 // not implemented properly
1326 // load a pointer to the asciiz string?
1327 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001328 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001329 } else
1330 */
1331 {
Damien George59fba2d2015-06-25 14:42:13 +00001332 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien13ed3a62013-10-08 09:05:10 +01001333 }
1334}
1335
Damien Georgedab13852015-01-13 15:55:54 +00001336STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1337 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001338 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001339 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1340 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1341}
1342
Damien George3558f622014-04-20 17:50:40 +01001343STATIC void emit_native_load_null(emit_t *emit) {
1344 emit_native_pre(emit);
1345 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1346}
1347
Damien George0abb5602015-01-16 12:24:49 +00001348STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1349 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001350 vtype_kind_t vtype = emit->local_vtype[local_num];
1351 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001352 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001353 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001354 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001355 if (local_num == 0) {
1356 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001357 } else if (local_num == 1) {
1358 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1359 } else if (local_num == 2) {
1360 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001361 } else {
Damien George0b610de2014-09-29 16:25:04 +01001362 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001363 if (emit->do_viper_types) {
1364 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1365 } else {
1366 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1367 }
Damien George0b610de2014-09-29 16:25:04 +01001368 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001369 }
Damien13ed3a62013-10-08 09:05:10 +01001370}
1371
Damien George7ff996c2014-09-08 23:05:16 +01001372STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001373 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1374 need_reg_single(emit, REG_RET, 0);
1375 emit_native_load_fast(emit, qst, local_num);
1376 vtype_kind_t vtype;
1377 int reg_base = REG_RET;
1378 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1379 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1380 // closed over vars are always Python objects
1381 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001382}
1383
Damien George7ff996c2014-09-08 23:05:16 +01001384STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001385 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001386 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001387 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001388 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1389}
1390
Damien George7ff996c2014-09-08 23:05:16 +01001391STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001392 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001393 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001394 // check for builtin casting operators
1395 if (emit->do_viper_types && qst == MP_QSTR_int) {
1396 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1397 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1398 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1399 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1400 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1401 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1402 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1403 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1404 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001405 } else if (emit->do_viper_types && qst == MP_QSTR_ptr32) {
1406 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001407 } else {
1408 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1409 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1410 }
Damien13ed3a62013-10-08 09:05:10 +01001411}
1412
Damien George7ff996c2014-09-08 23:05:16 +01001413STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001414 // depends on type of subject:
1415 // - integer, function, pointer to integers: error
1416 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001417 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001418 vtype_kind_t vtype_base;
1419 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1420 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001421 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001422 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1423}
1424
Damien George7ff996c2014-09-08 23:05:16 +01001425STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001426 vtype_kind_t vtype_base;
1427 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1428 assert(vtype_base == VTYPE_PYOBJ);
1429 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001430 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001431}
1432
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001433STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001434 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001435 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001436 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001437}
1438
Damien George729f7b42014-04-17 22:10:53 +01001439STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001440 DEBUG_printf("load_subscr\n");
1441 // need to compile: base[index]
1442
1443 // pop: index, base
1444 // optimise case where index is an immediate
1445 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1446
1447 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001448 // standard Python subscr
1449 // TODO factor this implicit cast code with other uses of it
1450 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1451 if (vtype_index == VTYPE_PYOBJ) {
1452 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
1453 } else {
1454 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
1455 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
1456 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1457 }
1458 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001459 emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_SENTINEL, REG_ARG_3);
Damien George729f7b42014-04-17 22:10:53 +01001460 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1461 } else {
Damien George91cfd412014-10-12 16:59:29 +01001462 // viper load
1463 // TODO The different machine architectures have very different
1464 // capabilities and requirements for loads, so probably best to
1465 // write a completely separate load-optimiser for each one.
1466 stack_info_t *top = peek_stack(emit, 0);
1467 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1468 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001469 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001470 emit_pre_pop_discard(emit); // discard index
1471 int reg_base = REG_ARG_1;
1472 int reg_index = REG_ARG_2;
1473 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1474 switch (vtype_base) {
1475 case VTYPE_PTR8: {
1476 // pointer to 8-bit memory
1477 // TODO optimise to use thumb ldrb r1, [r2, r3]
1478 if (index_value != 0) {
1479 // index is non-zero
1480 #if N_THUMB
1481 if (index_value > 0 && index_value < 32) {
1482 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1483 break;
1484 }
1485 #endif
1486 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1487 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1488 reg_base = reg_index;
1489 }
1490 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1491 break;
1492 }
1493 case VTYPE_PTR16: {
1494 // pointer to 16-bit memory
1495 if (index_value != 0) {
1496 // index is a non-zero immediate
1497 #if N_THUMB
1498 if (index_value > 0 && index_value < 32) {
1499 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1500 break;
1501 }
1502 #endif
1503 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1504 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1505 reg_base = reg_index;
1506 }
1507 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1508 break;
1509 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001510 case VTYPE_PTR32: {
1511 // pointer to 32-bit memory
1512 if (index_value != 0) {
1513 // index is a non-zero immediate
1514 #if N_THUMB
1515 if (index_value > 0 && index_value < 32) {
1516 asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1517 break;
1518 }
1519 #endif
1520 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1521 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1522 reg_base = reg_index;
1523 }
1524 ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
1525 break;
1526 }
Damien George91cfd412014-10-12 16:59:29 +01001527 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001528 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1529 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001530 }
1531 } else {
1532 // index is not an immediate
1533 vtype_kind_t vtype_index;
1534 int reg_index = REG_ARG_2;
1535 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1536 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1537 switch (vtype_base) {
1538 case VTYPE_PTR8: {
1539 // pointer to 8-bit memory
1540 // TODO optimise to use thumb ldrb r1, [r2, r3]
1541 assert(vtype_index == VTYPE_INT);
1542 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1543 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1544 break;
1545 }
1546 case VTYPE_PTR16: {
1547 // pointer to 16-bit memory
1548 assert(vtype_index == VTYPE_INT);
1549 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1550 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1551 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1552 break;
1553 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001554 case VTYPE_PTR32: {
1555 // pointer to word-size memory
1556 assert(vtype_index == VTYPE_INT);
1557 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1558 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1559 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1560 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1561 ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
1562 break;
1563 }
Damien George91cfd412014-10-12 16:59:29 +01001564 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001565 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1566 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001567 }
1568 }
1569 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001570 }
1571}
1572
Damien George7ff996c2014-09-08 23:05:16 +01001573STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001574 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001575 if (local_num == 0) {
1576 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001577 } else if (local_num == 1) {
1578 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1579 } else if (local_num == 2) {
1580 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001581 } else {
Damien George0b610de2014-09-29 16:25:04 +01001582 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001583 if (emit->do_viper_types) {
1584 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1585 } else {
1586 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1587 }
Damien13ed3a62013-10-08 09:05:10 +01001588 }
Damien13ed3a62013-10-08 09:05:10 +01001589 emit_post(emit);
1590
1591 // check types
1592 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1593 // first time this local is assigned, so give it a type of the object stored in it
1594 emit->local_vtype[local_num] = vtype;
1595 } else if (emit->local_vtype[local_num] != vtype) {
1596 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001597 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1598 "local '%q' has type '%q' but source is '%q'",
1599 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001600 }
1601}
1602
Damien George7ff996c2014-09-08 23:05:16 +01001603STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001604 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1605 need_reg_single(emit, REG_TEMP0, 0);
1606 need_reg_single(emit, REG_TEMP1, 0);
1607 emit_native_load_fast(emit, qst, local_num);
1608 vtype_kind_t vtype;
1609 int reg_base = REG_TEMP0;
1610 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1611 int reg_src = REG_TEMP1;
1612 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1613 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1614 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001615}
1616
Damien George7ff996c2014-09-08 23:05:16 +01001617STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001618 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001619 vtype_kind_t vtype;
1620 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1621 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001622 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001623 emit_post(emit);
1624}
1625
Damien George7ff996c2014-09-08 23:05:16 +01001626STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001627 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001628 if (vtype == VTYPE_PYOBJ) {
1629 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1630 } else {
1631 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001632 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype, REG_ARG_2); // arg2 = type
Damien George3112cde2014-09-29 18:45:42 +01001633 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001634 }
Damien George7ff996c2014-09-08 23:05:16 +01001635 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001636 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001637}
1638
Damien George7ff996c2014-09-08 23:05:16 +01001639STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001640 vtype_kind_t vtype_base, vtype_val;
1641 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1642 assert(vtype_base == VTYPE_PYOBJ);
1643 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001644 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001645 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001646}
1647
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001648STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001649 DEBUG_printf("store_subscr\n");
1650 // need to compile: base[index] = value
1651
1652 // pop: index, base, value
1653 // optimise case where index is an immediate
1654 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1655
1656 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001657 // standard Python subscr
1658 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1659 vtype_kind_t vtype_value = peek_vtype(emit, 2);
1660 if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
1661 // need to implicitly convert non-objects to objects
1662 // TODO do this properly
1663 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
1664 adjust_stack(emit, 3);
1665 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001666 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001667 emit_call(emit, MP_F_OBJ_SUBSCR);
1668 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001669 // viper store
1670 // TODO The different machine architectures have very different
1671 // capabilities and requirements for stores, so probably best to
1672 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001673 stack_info_t *top = peek_stack(emit, 0);
1674 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1675 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001676 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001677 emit_pre_pop_discard(emit); // discard index
1678 vtype_kind_t vtype_value;
1679 int reg_base = REG_ARG_1;
1680 int reg_index = REG_ARG_2;
1681 int reg_value = REG_ARG_3;
1682 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001683 #if N_X86
1684 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1685 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1686 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001687 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001688 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001689 switch (vtype_base) {
1690 case VTYPE_PTR8: {
1691 // pointer to 8-bit memory
1692 // TODO optimise to use thumb strb r1, [r2, r3]
1693 if (index_value != 0) {
1694 // index is non-zero
1695 #if N_THUMB
1696 if (index_value > 0 && index_value < 32) {
1697 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1698 break;
1699 }
1700 #endif
1701 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001702 #if N_ARM
1703 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1704 return;
1705 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001706 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1707 reg_base = reg_index;
1708 }
1709 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1710 break;
1711 }
1712 case VTYPE_PTR16: {
1713 // pointer to 16-bit memory
1714 if (index_value != 0) {
1715 // index is a non-zero immediate
1716 #if N_THUMB
1717 if (index_value > 0 && index_value < 32) {
1718 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1719 break;
1720 }
1721 #endif
1722 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001723 #if N_ARM
1724 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1725 return;
1726 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001727 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1728 reg_base = reg_index;
1729 }
1730 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1731 break;
1732 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001733 case VTYPE_PTR32: {
1734 // pointer to 32-bit memory
1735 if (index_value != 0) {
1736 // index is a non-zero immediate
1737 #if N_THUMB
1738 if (index_value > 0 && index_value < 32) {
1739 asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1740 break;
1741 }
1742 #endif
1743 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1744 #if N_ARM
1745 asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1746 return;
1747 #endif
1748 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1749 reg_base = reg_index;
1750 }
1751 ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
1752 break;
1753 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001754 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001755 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1756 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001757 }
1758 } else {
1759 // index is not an immediate
1760 vtype_kind_t vtype_index, vtype_value;
1761 int reg_index = REG_ARG_2;
1762 int reg_value = REG_ARG_3;
1763 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1764 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001765 #if N_X86
1766 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1767 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1768 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001769 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001770 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001771 switch (vtype_base) {
1772 case VTYPE_PTR8: {
1773 // pointer to 8-bit memory
1774 // TODO optimise to use thumb strb r1, [r2, r3]
1775 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001776 #if N_ARM
1777 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1778 break;
1779 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001780 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1781 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1782 break;
1783 }
1784 case VTYPE_PTR16: {
1785 // pointer to 16-bit memory
1786 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001787 #if N_ARM
1788 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1789 break;
1790 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001791 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1792 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1793 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1794 break;
1795 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001796 case VTYPE_PTR32: {
1797 // pointer to 32-bit memory
1798 assert(vtype_index == VTYPE_INT);
1799 #if N_ARM
1800 asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1801 break;
1802 #endif
1803 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1804 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1805 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1806 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1807 ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
1808 break;
1809 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001810 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001811 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1812 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001813 }
1814 }
1815
1816 }
Damien13ed3a62013-10-08 09:05:10 +01001817}
1818
Damien George7ff996c2014-09-08 23:05:16 +01001819STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001820 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1821 // to mark deleted vars but then every var would need to be checked on
1822 // each access. Very inefficient, so just set value to None to enable GC.
1823 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1824 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001825}
1826
Damien George7ff996c2014-09-08 23:05:16 +01001827STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001828 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001829 (void)emit;
1830 (void)qst;
1831 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001832}
1833
Damien Georgee6ce10a2014-09-06 18:38:20 +01001834STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1835 emit_native_pre(emit);
1836 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1837 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001838}
1839
Damien Georgee6ce10a2014-09-06 18:38:20 +01001840STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1841 emit_native_pre(emit);
1842 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1843 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001844}
1845
Damien Georgee6ce10a2014-09-06 18:38:20 +01001846STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001847 vtype_kind_t vtype_base;
1848 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1849 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001850 emit_call_with_2_imm_args(emit, MP_F_STORE_ATTR, qst, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3); // arg2 = attribute name, arg3 = value (null for delete)
Damien George780e54c2014-06-22 18:35:04 +01001851 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001852}
1853
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001854STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001855 vtype_kind_t vtype_index, vtype_base;
1856 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1857 assert(vtype_index == VTYPE_PYOBJ);
1858 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001859 emit_call_with_imm_arg(emit, MP_F_OBJ_SUBSCR, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +01001860}
1861
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001862STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001863 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001864 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001865 int reg = REG_TEMP0;
1866 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1867 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001868}
1869
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001870STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001871 vtype_kind_t vtype0, vtype1;
1872 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1873 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1874}
1875
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001876STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001877 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001878 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001879 emit_post(emit);
1880}
1881
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001882STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001883 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001884 vtype_kind_t vtype0, vtype1;
1885 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1886 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001887}
1888
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001889STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001890 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001891 vtype_kind_t vtype0, vtype1, vtype2;
1892 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1893 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1894}
1895
Damien George7ff996c2014-09-08 23:05:16 +01001896STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001897 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001898 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001899 // need to commit stack because we are jumping elsewhere
1900 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001901 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001902 emit_post(emit);
1903}
1904
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001905STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001906 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001907 if (vtype == VTYPE_PYOBJ) {
1908 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1909 if (!pop) {
1910 adjust_stack(emit, 1);
1911 }
1912 emit_call(emit, MP_F_OBJ_IS_TRUE);
1913 } else {
1914 emit_pre_pop_reg(emit, &vtype, REG_RET);
1915 if (!pop) {
1916 adjust_stack(emit, 1);
1917 }
1918 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1919 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1920 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1921 }
Damien13ed3a62013-10-08 09:05:10 +01001922 }
Damien George21ca2d72014-10-19 19:00:51 +01001923 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1924 // can use it. This is a bit of a hack.
1925 if (!pop) {
1926 emit->saved_stack_vtype = vtype;
1927 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001928 // need to commit stack because we may jump elsewhere
1929 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001930}
1931
Damien George63f38322015-02-28 15:04:06 +00001932STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1933 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001934 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001935 if (cond) {
1936 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1937 } else {
1938 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1939 }
Damien1a6633a2013-11-03 13:58:19 +00001940 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001941}
Damien1a6633a2013-11-03 13:58:19 +00001942
Damien George63f38322015-02-28 15:04:06 +00001943STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1944 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001945 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001946 if (cond) {
1947 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1948 } else {
1949 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1950 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001951 adjust_stack(emit, -1);
1952 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001953}
1954
Damien George7ff996c2014-09-08 23:05:16 +01001955STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001956 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001957 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001958}
Damien Georgea32c1e42014-05-07 18:30:52 +01001959
Damien George7ff996c2014-09-08 23:05:16 +01001960STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001961 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001962 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001963}
Damien Georgea32c1e42014-05-07 18:30:52 +01001964
Damien George7ff996c2014-09-08 23:05:16 +01001965STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001966 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001967 (void)emit;
1968 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001969 assert(0);
1970}
Damien Georgeb601d952014-06-30 05:17:25 +01001971
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001972STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001973 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001974 assert(0);
1975}
Damien Georgeb601d952014-06-30 05:17:25 +01001976
Damien George7ff996c2014-09-08 23:05:16 +01001977STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001978 emit_native_pre(emit);
1979 // need to commit stack because we may jump elsewhere
1980 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001981 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_1, sizeof(nlr_buf_t) / sizeof(mp_uint_t)); // arg1 = pointer to nlr buf
Damien George7fe21912014-08-16 22:31:57 +01001982 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001983 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001984 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001985}
Damien Georgeb601d952014-06-30 05:17:25 +01001986
Damien George7ff996c2014-09-08 23:05:16 +01001987STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001988 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001989}
Damien Georgeb601d952014-06-30 05:17:25 +01001990
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001991STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001992 // logic:
1993 // exc = pop_stack
1994 // if exc == None: pass
1995 // else: raise exc
1996 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1997 vtype_kind_t vtype;
1998 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1999 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01002000 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002001}
Damiend2755ec2013-10-16 23:58:48 +01002002
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002003STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002004 // perhaps the difficult one, as we want to rewrite for loops using native code
2005 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01002006
2007 vtype_kind_t vtype;
2008 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2009 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002010 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01002011 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002012}
Damiend2755ec2013-10-16 23:58:48 +01002013
Damien George7ff996c2014-09-08 23:05:16 +01002014STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002015 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002016 vtype_kind_t vtype;
2017 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
2018 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002019 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01002020 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
2021 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01002022 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2023}
2024
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002025STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01002026 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00002027 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002028 adjust_stack(emit, -1);
2029 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002030}
2031
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002032STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002033 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002034 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002035 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01002036 emit_post(emit);
2037}
2038
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002039STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002040 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01002041 /*
2042 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002043 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002044 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01002045 emit_post(emit);
2046 */
Damien13ed3a62013-10-08 09:05:10 +01002047}
2048
Damien Georged17926d2014-03-30 13:35:08 +01002049STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002050 vtype_kind_t vtype;
2051 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
Damien George9f5f1562015-10-08 13:08:59 +01002052 if (vtype == VTYPE_PYOBJ) {
2053 if (op == MP_UNARY_OP_NOT) {
2054 // we need to synthesise this operation by converting to bool first
2055 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
2056 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
2057 }
2058 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
2059 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2060 } else {
2061 adjust_stack(emit, 1);
2062 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2063 "unary op %q not implemented", mp_unary_op_method_name[op]);
Damien Georgeb601d952014-06-30 05:17:25 +01002064 }
Damien13ed3a62013-10-08 09:05:10 +01002065}
2066
Damien Georged17926d2014-03-30 13:35:08 +01002067STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002068 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01002069 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
2070 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01002071 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01002072 #if N_X64 || N_X86
2073 // special cases for x86 and shifting
2074 if (op == MP_BINARY_OP_LSHIFT
2075 || op == MP_BINARY_OP_INPLACE_LSHIFT
2076 || op == MP_BINARY_OP_RSHIFT
2077 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2078 #if N_X64
2079 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
2080 #else
2081 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
2082 #endif
2083 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2084 ASM_LSL_REG(emit->as, REG_RET);
2085 } else {
2086 ASM_ASR_REG(emit->as, REG_RET);
2087 }
2088 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
2089 return;
2090 }
2091 #endif
2092 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002093 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002094 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
2095 if (0) {
2096 // dummy
2097 #if !(N_X64 || N_X86)
2098 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2099 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002100 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002101 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2102 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2103 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2104 #endif
Damien George1ef23482014-10-12 14:21:06 +01002105 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2106 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2107 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2108 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2109 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2110 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2111 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2112 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2113 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002114 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2115 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2116 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2117 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2118 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2119 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George567b3492015-06-04 14:00:29 +00002120 } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2121 ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2122 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002123 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2124 // comparison ops are (in enum order):
2125 // MP_BINARY_OP_LESS
2126 // MP_BINARY_OP_MORE
2127 // MP_BINARY_OP_EQUAL
2128 // MP_BINARY_OP_LESS_EQUAL
2129 // MP_BINARY_OP_MORE_EQUAL
2130 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002131 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002132 #if N_X64
2133 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2134 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2135 static byte ops[6] = {
2136 ASM_X64_CC_JL,
2137 ASM_X64_CC_JG,
2138 ASM_X64_CC_JE,
2139 ASM_X64_CC_JLE,
2140 ASM_X64_CC_JGE,
2141 ASM_X64_CC_JNE,
2142 };
2143 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2144 #elif N_X86
2145 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2146 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2147 static byte ops[6] = {
2148 ASM_X86_CC_JL,
2149 ASM_X86_CC_JG,
2150 ASM_X86_CC_JE,
2151 ASM_X86_CC_JLE,
2152 ASM_X86_CC_JGE,
2153 ASM_X86_CC_JNE,
2154 };
2155 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2156 #elif N_THUMB
2157 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2158 static uint16_t ops[6] = {
2159 ASM_THUMB_OP_ITE_GE,
2160 ASM_THUMB_OP_ITE_GT,
2161 ASM_THUMB_OP_ITE_EQ,
2162 ASM_THUMB_OP_ITE_GT,
2163 ASM_THUMB_OP_ITE_GE,
2164 ASM_THUMB_OP_ITE_EQ,
2165 };
2166 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2167 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2168 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2169 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2170 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002171 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2172 static uint ccs[6] = {
2173 ASM_ARM_CC_LT,
2174 ASM_ARM_CC_GT,
2175 ASM_ARM_CC_EQ,
2176 ASM_ARM_CC_LE,
2177 ASM_ARM_CC_GE,
2178 ASM_ARM_CC_NE,
2179 };
2180 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002181 #else
2182 #error not implemented
2183 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002184 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2185 } else {
2186 // TODO other ops not yet implemented
Damien George4d9cad12015-06-04 11:52:16 +01002187 adjust_stack(emit, 1);
2188 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2189 "binary op %q not implemented", mp_binary_op_method_name[op]);
Damien Georgebc1d3692014-01-11 09:47:06 +00002190 }
Damien13ed3a62013-10-08 09:05:10 +01002191 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002192 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002193 bool invert = false;
2194 if (op == MP_BINARY_OP_NOT_IN) {
2195 invert = true;
2196 op = MP_BINARY_OP_IN;
2197 } else if (op == MP_BINARY_OP_IS_NOT) {
2198 invert = true;
2199 op = MP_BINARY_OP_IS;
2200 }
Damien George7fe21912014-08-16 22:31:57 +01002201 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002202 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002203 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002204 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2205 }
Damien13ed3a62013-10-08 09:05:10 +01002206 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2207 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002208 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002209 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2210 "can't do binary op between '%q' and '%q'",
2211 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002212 }
2213}
2214
Damien George7ff996c2014-09-08 23:05:16 +01002215STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002216 // for viper: call runtime, with types of args
2217 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002218 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002219 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002220 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002221 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002222}
2223
Damien George7ff996c2014-09-08 23:05:16 +01002224STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002225 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002226 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002227 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002228 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2229}
2230
Damien George7ff996c2014-09-08 23:05:16 +01002231STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002232 // only used in list comprehension
2233 vtype_kind_t vtype_list, vtype_item;
2234 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2235 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2236 assert(vtype_list == VTYPE_PYOBJ);
2237 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002238 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002239 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002240}
2241
Damien George7ff996c2014-09-08 23:05:16 +01002242STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002243 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002244 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002245 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2246}
2247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002248STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002249 vtype_kind_t vtype_key, vtype_value, vtype_map;
2250 emit_pre_pop_reg_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3, &vtype_map, REG_ARG_1); // key, value, map
2251 assert(vtype_key == VTYPE_PYOBJ);
2252 assert(vtype_value == VTYPE_PYOBJ);
2253 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002254 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002255 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2256}
2257
Damien George7ff996c2014-09-08 23:05:16 +01002258STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002259 // only used in list comprehension
2260 vtype_kind_t vtype_map, vtype_key, vtype_value;
2261 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2262 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2263 assert(vtype_map == VTYPE_PYOBJ);
2264 assert(vtype_key == VTYPE_PYOBJ);
2265 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002266 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002267 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002268}
2269
Damien Georgee37dcaa2014-12-27 17:07:16 +00002270#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002271STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002272 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002273 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002274 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002275 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2276}
2277
Damien George7ff996c2014-09-08 23:05:16 +01002278STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002279 // only used in set comprehension
2280 vtype_kind_t vtype_set, vtype_item;
2281 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2282 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2283 assert(vtype_set == VTYPE_PYOBJ);
2284 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002285 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002286 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002287}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002288#endif
Damiend2755ec2013-10-16 23:58:48 +01002289
Damien George83204f32014-12-27 17:20:41 +00002290#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002291STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002292 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002293 if (n_args == 2) {
2294 vtype_kind_t vtype_start, vtype_stop;
2295 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2296 assert(vtype_start == VTYPE_PYOBJ);
2297 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002298 emit_call_with_imm_arg(emit, MP_F_NEW_SLICE, (mp_uint_t)mp_const_none, REG_ARG_3); // arg3 = step
Damien Georgeb601d952014-06-30 05:17:25 +01002299 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2300 } else {
2301 assert(n_args == 3);
2302 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2303 emit_pre_pop_reg_reg_reg(emit, &vtype_step, REG_ARG_3, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop, arg3 = step
2304 assert(vtype_start == VTYPE_PYOBJ);
2305 assert(vtype_stop == VTYPE_PYOBJ);
2306 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002307 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002308 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2309 }
Damien13ed3a62013-10-08 09:05:10 +01002310}
Damien George83204f32014-12-27 17:20:41 +00002311#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002312
Damien George7ff996c2014-09-08 23:05:16 +01002313STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002314 DEBUG_printf("unpack_sequence %d\n", n_args);
2315 vtype_kind_t vtype_base;
2316 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2317 assert(vtype_base == VTYPE_PYOBJ);
2318 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002319 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002320}
Damien Georgecdd96df2014-04-06 12:58:40 +01002321
Damien George7ff996c2014-09-08 23:05:16 +01002322STATIC void emit_native_unpack_ex(emit_t *emit, mp_uint_t n_left, mp_uint_t n_right) {
Damien Georgea32c1e42014-05-07 18:30:52 +01002323 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2324 vtype_kind_t vtype_base;
2325 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2326 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002327 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_left + n_right + 1); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002328 emit_call_with_imm_arg(emit, MP_F_UNPACK_EX, n_left | (n_right << 8), REG_ARG_2); // arg2 = n_left + n_right
Damien13ed3a62013-10-08 09:05:10 +01002329}
2330
Damien George7ff996c2014-09-08 23:05:16 +01002331STATIC void emit_native_make_function(emit_t *emit, scope_t *scope, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien13ed3a62013-10-08 09:05:10 +01002332 // call runtime, with type info for args, or don't support dict/default params, or only support Python objects for them
Damien Georgece8f07a2014-03-27 23:30:26 +00002333 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002334 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002335 emit_call_with_3_imm_args_and_first_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, (mp_uint_t)scope->raw_code, REG_ARG_1, (mp_uint_t)MP_OBJ_NULL, REG_ARG_2, (mp_uint_t)MP_OBJ_NULL, REG_ARG_3);
Damien Georgea32c1e42014-05-07 18:30:52 +01002336 } else {
2337 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2338 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2339 assert(vtype_def_tuple == VTYPE_PYOBJ);
2340 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002341 emit_call_with_imm_arg_aligned(emit, MP_F_MAKE_FUNCTION_FROM_RAW_CODE, (mp_uint_t)scope->raw_code, REG_ARG_1);
Damien Georgea32c1e42014-05-07 18:30:52 +01002342 }
Damien13ed3a62013-10-08 09:05:10 +01002343 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2344}
2345
Damien George7ff996c2014-09-08 23:05:16 +01002346STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien George4cd9ced2015-01-15 14:41:41 +00002347 emit_native_pre(emit);
2348 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2349 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2350 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2351 } else {
2352 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2353 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2354 }
2355 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2356 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2357 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002358}
2359
Damien George7ff996c2014-09-08 23:05:16 +01002360STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien Georged6230f62014-09-23 14:10:03 +00002361 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2362
Damien Georgee9dac3b2014-09-29 22:10:41 +01002363 // TODO: in viper mode, call special runtime routine with type info for args,
2364 // and wanted type info for return, to remove need for boxing/unboxing
2365
Damien Georgee9dac3b2014-09-29 22:10:41 +01002366 emit_native_pre(emit);
2367 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2368 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2369 // casting operator
2370 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002371 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002372 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002373 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002374 switch (peek_vtype(emit, 0)) {
2375 case VTYPE_PYOBJ: {
2376 vtype_kind_t vtype;
2377 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2378 emit_pre_pop_discard(emit);
2379 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2380 emit_post_push_reg(emit, vtype_cast, REG_RET);
2381 break;
2382 }
2383 case VTYPE_BOOL:
2384 case VTYPE_INT:
2385 case VTYPE_UINT:
2386 case VTYPE_PTR:
2387 case VTYPE_PTR8:
2388 case VTYPE_PTR16:
Damien Georgeb8f9ac52015-10-13 00:50:17 +01002389 case VTYPE_PTR32:
Damien Georgee9dac3b2014-09-29 22:10:41 +01002390 case VTYPE_PTR_NONE:
2391 emit_fold_stack_top(emit, REG_ARG_1);
2392 emit_post_top_set_vtype(emit, vtype_cast);
2393 break;
2394 default:
2395 assert(!"TODO: convert obj to int");
2396 }
2397 } else {
Damien13ed3a62013-10-08 09:05:10 +01002398 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002399 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002400 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2401 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 0, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
2402 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2403 } else {
2404 if (n_positional != 0 || n_keyword != 0) {
2405 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2406 }
2407 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2408 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2409 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2410 }
Damien Georgecd82e022014-02-02 13:11:48 +00002411 }
Damien13ed3a62013-10-08 09:05:10 +01002412}
2413
Damien George7ff996c2014-09-08 23:05:16 +01002414STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002415 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002416 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2417 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 1, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
2418 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2419 } else {
2420 emit_native_pre(emit);
2421 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, 2 + n_positional + 2 * n_keyword); // pointer to items, including meth and self
2422 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2423 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2424 }
Damien13ed3a62013-10-08 09:05:10 +01002425}
2426
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002427STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002428 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002429 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002430 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2431 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002432 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002433 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002434 } else {
2435 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002436 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002437 } else {
2438 vtype_kind_t vtype;
2439 emit_pre_pop_reg(emit, &vtype, REG_RET);
2440 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002441 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2442 "return expected '%q' but got '%q'",
2443 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002444 }
Damien George2ac4af62014-08-15 16:45:41 +01002445 }
Damien13ed3a62013-10-08 09:05:10 +01002446 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002447 vtype_kind_t vtype;
2448 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002449 assert(vtype == VTYPE_PYOBJ);
2450 }
2451 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002452 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2453 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002454}
2455
Damien George7ff996c2014-09-08 23:05:16 +01002456STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002457 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002458 vtype_kind_t vtype_exc;
2459 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2460 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002461 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002462 }
2463 // TODO probably make this 1 call to the runtime (which could even call convert, native_raise(obj, type))
Damien George7fe21912014-08-16 22:31:57 +01002464 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002465}
Damien Georgeb601d952014-06-30 05:17:25 +01002466
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002467STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002468 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002469 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002470 assert(0);
2471}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002472STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002473 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002474 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002475 assert(0);
2476}
2477
Damien Georgeb601d952014-06-30 05:17:25 +01002478STATIC void emit_native_start_except_handler(emit_t *emit) {
2479 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2480 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2481 // the first 2 elements, so we can get the thrown value.
2482 adjust_stack(emit, 2);
2483 vtype_kind_t vtype_nlr;
2484 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002485 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002486 emit_post_push_reg_reg_reg(emit, VTYPE_PYOBJ, REG_ARG_1, VTYPE_PYOBJ, REG_ARG_1, VTYPE_PYOBJ, REG_ARG_1); // push the 3 exception items
2487}
2488
2489STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002490 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002491}
2492
Damien13ed3a62013-10-08 09:05:10 +01002493const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002494 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002495 emit_native_start_pass,
2496 emit_native_end_pass,
2497 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002498 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002499 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002500
Damien George542bd6b2015-03-26 14:42:40 +00002501 {
2502 emit_native_load_fast,
2503 emit_native_load_deref,
2504 emit_native_load_name,
2505 emit_native_load_global,
2506 },
2507 {
2508 emit_native_store_fast,
2509 emit_native_store_deref,
2510 emit_native_store_name,
2511 emit_native_store_global,
2512 },
2513 {
2514 emit_native_delete_fast,
2515 emit_native_delete_deref,
2516 emit_native_delete_name,
2517 emit_native_delete_global,
2518 },
Damien13ed3a62013-10-08 09:05:10 +01002519
2520 emit_native_label_assign,
2521 emit_native_import_name,
2522 emit_native_import_from,
2523 emit_native_import_star,
2524 emit_native_load_const_tok,
2525 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002526 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002527 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002528 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002529 emit_native_load_attr,
2530 emit_native_load_method,
2531 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002532 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002533 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002534 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002535 emit_native_delete_attr,
2536 emit_native_delete_subscr,
2537 emit_native_dup_top,
2538 emit_native_dup_top_two,
2539 emit_native_pop_top,
2540 emit_native_rot_two,
2541 emit_native_rot_three,
2542 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002543 emit_native_pop_jump_if,
2544 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002545 emit_native_break_loop,
2546 emit_native_continue_loop,
2547 emit_native_setup_with,
2548 emit_native_with_cleanup,
2549 emit_native_setup_except,
2550 emit_native_setup_finally,
2551 emit_native_end_finally,
2552 emit_native_get_iter,
2553 emit_native_for_iter,
2554 emit_native_for_iter_end,
2555 emit_native_pop_block,
2556 emit_native_pop_except,
2557 emit_native_unary_op,
2558 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002559 emit_native_build_tuple,
2560 emit_native_build_list,
2561 emit_native_list_append,
2562 emit_native_build_map,
2563 emit_native_store_map,
2564 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002565 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002566 emit_native_build_set,
2567 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002568 #endif
Damien George83204f32014-12-27 17:20:41 +00002569 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002570 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002571 #endif
Damien13ed3a62013-10-08 09:05:10 +01002572 emit_native_unpack_sequence,
2573 emit_native_unpack_ex,
2574 emit_native_make_function,
2575 emit_native_make_closure,
2576 emit_native_call_function,
2577 emit_native_call_method,
2578 emit_native_return_value,
2579 emit_native_raise_varargs,
2580 emit_native_yield_value,
2581 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002582
2583 emit_native_start_except_handler,
2584 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002585};
2586
Damien Georgec90f59e2014-09-06 23:06:36 +01002587#endif