blob: d8f1640c0f4bb47823af23a6d1df7ee8018ab40d [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 prelude_offset;
Damien George713ea182015-10-23 01:23:11 +0100570 int const_table_offset;
Damien George99886182015-04-06 22:38:53 +0100571 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100572 int stack_start;
573 int stack_size;
574
575 bool last_emit_was_return_value;
576
Damien13ed3a62013-10-08 09:05:10 +0100577 scope_t *scope;
578
Damien Georgec90f59e2014-09-06 23:06:36 +0100579 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100580};
581
Damien Georgec8b60f02015-04-20 13:29:31 +0000582emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100583 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000584 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100585 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100586 return emit;
587}
588
Damien George41d02b62014-01-24 22:42:28 +0000589void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100590 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100591 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
592 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200593 m_del_obj(emit_t, emit);
594}
595
Damien George2ac4af62014-08-15 16:45:41 +0100596STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
597 switch (op) {
598 case MP_EMIT_NATIVE_TYPE_ENABLE:
599 emit->do_viper_types = arg1;
600 break;
601
602 default: {
603 vtype_kind_t type;
604 switch (arg2) {
605 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
606 case MP_QSTR_bool: type = VTYPE_BOOL; break;
607 case MP_QSTR_int: type = VTYPE_INT; break;
608 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100609 case MP_QSTR_ptr: type = VTYPE_PTR; break;
610 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
611 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100612 case MP_QSTR_ptr32: type = VTYPE_PTR32; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000613 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100614 }
615 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
616 emit->return_vtype = type;
617 } else {
618 assert(arg1 < emit->local_vtype_alloc);
619 emit->local_vtype[arg1] = type;
620 }
621 break;
622 }
623 }
Damien13ed3a62013-10-08 09:05:10 +0100624}
625
Damien Georgefa5950e2015-04-03 15:03:24 +0000626STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000627STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000628STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000629STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000630
Damien George99886182015-04-06 22:38:53 +0100631#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
632
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200633STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000634 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
635
Damien13ed3a62013-10-08 09:05:10 +0100636 emit->pass = pass;
637 emit->stack_start = 0;
638 emit->stack_size = 0;
639 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100640 emit->scope = scope;
641
Damien George36db6bc2014-05-07 17:24:22 +0100642 // allocate memory for keeping track of the types of locals
643 if (emit->local_vtype_alloc < scope->num_locals) {
644 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
645 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100646 }
Damien George36db6bc2014-05-07 17:24:22 +0100647
648 // allocate memory for keeping track of the objects on the stack
649 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000650 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100651 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000652 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100653 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100654 }
655
Damien George99886182015-04-06 22:38:53 +0100656 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100657 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100658
659 // set default type for arguments
660 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
661 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
662 num_args += 1;
663 }
664 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
665 num_args += 1;
666 }
667 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100668 emit->local_vtype[i] = VTYPE_PYOBJ;
669 }
Damien Georgea5190a72014-08-15 22:39:08 +0100670
671 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100672 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100673 emit->local_vtype[i] = VTYPE_UNBOUND;
674 }
675
676 // values on stack begin unbound
677 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100678 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100679 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100680 }
681
Damien Georgec90f59e2014-09-06 23:06:36 +0100682 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100683
Damien George99886182015-04-06 22:38:53 +0100684 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100685
Damien George99886182015-04-06 22:38:53 +0100686 if (emit->do_viper_types) {
687
Damien Georgef17e6632015-07-23 14:30:37 +0100688 // right now we have a restriction of maximum of 4 arguments
689 if (scope->num_pos_args >= 5) {
Damien George84d59c22015-07-27 22:20:00 +0100690 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
Damien Georgef17e6632015-07-23 14:30:37 +0100691 return;
692 }
693
Damien George99886182015-04-06 22:38:53 +0100694 // entry to function
695 int num_locals = 0;
696 if (pass > MP_PASS_SCOPE) {
697 num_locals = scope->num_locals - REG_LOCAL_NUM;
698 if (num_locals < 0) {
699 num_locals = 0;
700 }
701 emit->stack_start = num_locals;
702 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100703 }
Damien George99886182015-04-06 22:38:53 +0100704 ASM_ENTRY(emit->as, num_locals);
705
Damien Georgec39093d2015-08-12 23:31:19 +0100706 // TODO don't load r7 if we don't need it
707 #if N_THUMB
708 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
709 #elif N_ARM
710 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
711 #endif
712
Damien George99886182015-04-06 22:38:53 +0100713 #if N_X86
714 for (int i = 0; i < scope->num_pos_args; i++) {
715 if (i == 0) {
716 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
717 } else if (i == 1) {
718 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
719 } else if (i == 2) {
720 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
721 } else {
722 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
723 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
724 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100725 }
Damien George99886182015-04-06 22:38:53 +0100726 #else
727 for (int i = 0; i < scope->num_pos_args; i++) {
728 if (i == 0) {
729 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
730 } else if (i == 1) {
731 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
732 } else if (i == 2) {
733 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
734 } else if (i == 3) {
735 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
736 } else {
737 // TODO not implemented
738 assert(0);
739 }
740 }
741 #endif
742
743 } else {
744 // work out size of state (locals plus stack)
745 emit->n_state = scope->num_locals + scope->stack_size;
746
747 // allocate space on C-stack for code_state structure, which includes state
748 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
749
Damien Georgec39093d2015-08-12 23:31:19 +0100750 // TODO don't load r7 if we don't need it
751 #if N_THUMB
752 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
753 #elif N_ARM
754 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
755 #endif
756
Damien George99886182015-04-06 22:38:53 +0100757 // prepare incoming arguments for call to mp_setup_code_state
758 #if N_X86
759 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
760 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
761 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
762 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
763 #else
764 #if N_THUMB
765 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300766 #elif N_ARM
767 ASM_MOV_REG_REG(emit->as, ASM_ARM_REG_R4, REG_ARG_4);
Damien George99886182015-04-06 22:38:53 +0100768 #else
769 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
770 #endif
771 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
772 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
773 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
774 #endif
775
Damien George99886182015-04-06 22:38:53 +0100776 // set code_state.ip (offset from start of this function to prelude info)
777 // XXX this encoding may change size
778 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
779
780 // set code_state.n_state
781 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
782
783 // put address of code_state into first arg
784 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
785
786 // call mp_setup_code_state to prepare code_state structure
787 #if N_THUMB
788 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
789 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
790 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300791 #elif N_ARM
792 asm_arm_push(emit->as, 1 << ASM_ARM_REG_R4); // push 5th arg
793 asm_arm_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
794 asm_arm_pop(emit->as, 1 << REG_RET); // pop dummy (was 5th arg)
Damien George99886182015-04-06 22:38:53 +0100795 #else
796 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
797 #endif
798
799 // cache some locals in registers
800 if (scope->num_locals > 0) {
801 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
802 if (scope->num_locals > 1) {
803 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
804 if (scope->num_locals > 2) {
805 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
806 }
807 }
808 }
809
810 // set the type of closed over variables
811 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
812 id_info_t *id = &scope->id_info[i];
813 if (id->kind == ID_INFO_KIND_CELL) {
814 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
815 }
Damien13ed3a62013-10-08 09:05:10 +0100816 }
817 }
818
Damien13ed3a62013-10-08 09:05:10 +0100819}
820
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200821STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100822 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100823 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100824 }
Damien George99886182015-04-06 22:38:53 +0100825
826 if (!emit->do_viper_types) {
Damien George9b7f5832015-03-18 17:47:47 +0000827 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
Damien George3a3db4d2015-10-22 23:45:37 +0100828 ASM_DATA(emit->as, 1, emit->scope->scope_flags);
829 ASM_DATA(emit->as, 1, emit->scope->num_pos_args);
830 ASM_DATA(emit->as, 1, emit->scope->num_kwonly_args);
831 ASM_DATA(emit->as, 1, emit->scope->num_def_pos_args);
Damien George713ea182015-10-23 01:23:11 +0100832
833 // write code info (just contains block name and source file)
834 ASM_DATA(emit->as, 1, 5);
835 ASM_DATA(emit->as, 2, emit->scope->simple_name);
836 ASM_DATA(emit->as, 2, emit->scope->source_file);
837
838 // bytecode prelude: initialise closed over variables
839 for (int i = 0; i < emit->scope->id_info_len; i++) {
840 id_info_t *id = &emit->scope->id_info[i];
841 if (id->kind == ID_INFO_KIND_CELL) {
842 assert(id->local_num < 255);
843 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
844 }
845 }
846 ASM_DATA(emit->as, 1, 255); // end of list sentinel
847
Damien George99886182015-04-06 22:38:53 +0100848 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
Damien George713ea182015-10-23 01:23:11 +0100849 emit->const_table_offset = ASM_GET_CODE_POS(emit->as);
Damien George9b7f5832015-03-18 17:47:47 +0000850
851 // write argument names as qstr objects
Damien George9a42eb52015-05-06 13:55:33 +0100852 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100853 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100854 qstr qst = MP_QSTR__star_;
855 for (int j = 0; j < emit->scope->id_info_len; ++j) {
856 id_info_t *id = &emit->scope->id_info[j];
857 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
858 qst = id->qst;
859 break;
860 }
861 }
862 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100863 }
864
Damien George99886182015-04-06 22:38:53 +0100865 }
866
Damien Georgec90f59e2014-09-06 23:06:36 +0100867 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100868
869 // check stack is back to zero size
870 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100871 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100872 }
873
Damien George36db6bc2014-05-07 17:24:22 +0100874 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100875 void *f = ASM_GET_CODE(emit->as);
876 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100877
878 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100879 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100880 mp_uint_t type_sig = emit->return_vtype & 3;
881 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
882 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
883 }
884
Damien George99886182015-04-06 22:38:53 +0100885 mp_emit_glue_assign_native(emit->scope->raw_code,
886 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
Damien George713ea182015-10-23 01:23:11 +0100887 f, f_len, (mp_uint_t*)((byte*)f + emit->const_table_offset),
888 emit->scope->num_pos_args, emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100889 }
890}
891
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200892STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100893 return emit->last_emit_was_return_value;
894}
895
Damien Georged6230f62014-09-23 14:10:03 +0000896STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000897 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100898 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100899 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100900 emit->scope->stack_size = emit->stack_size;
901 }
Damien George21ca2d72014-10-19 19:00:51 +0100902#ifdef DEBUG_PRINT
903 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
904 for (int i = 0; i < emit->stack_size; i++) {
905 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000906 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100907 }
908 DEBUG_printf("\n");
909#endif
Damien13ed3a62013-10-08 09:05:10 +0100910}
911
Damien Georged6230f62014-09-23 14:10:03 +0000912STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100913 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000914 // If we are adjusting the stack in a positive direction (pushing) then we
915 // need to fill in values for the stack kind and vtype of the newly-pushed
916 // entries. These should be set to "value" (ie not reg or imm) because we
917 // should only need to adjust the stack due to a jump to this part in the
918 // code (and hence we have settled the stack before the jump).
919 for (mp_int_t i = 0; i < delta; i++) {
920 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
921 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100922 // TODO we don't know the vtype to use here. At the moment this is a
923 // hack to get the case of multi comparison working.
924 if (delta == 1) {
925 si->vtype = emit->saved_stack_vtype;
926 } else {
927 si->vtype = VTYPE_PYOBJ;
928 }
Damien Georged6230f62014-09-23 14:10:03 +0000929 }
930 adjust_stack(emit, delta);
931}
932
933STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000934 (void)emit;
935 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000936}
937
Damienff8ed772013-10-08 22:18:32 +0100938/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200939STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100940 adjust_stack(emit, stack_size_delta);
941 emit->last_emit_was_return_value = false;
942}
Damienff8ed772013-10-08 22:18:32 +0100943*/
Damien13ed3a62013-10-08 09:05:10 +0100944
Damienff8ed772013-10-08 22:18:32 +0100945// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000946STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100947 emit->last_emit_was_return_value = false;
948 // settle the stack
949 /*
950 if (regs_needed != 0) {
951 for (int i = 0; i < emit->stack_size; i++) {
952 switch (emit->stack_info[i].kind) {
953 case STACK_VALUE:
954 break;
955
956 case STACK_REG:
957 // TODO only push reg if in regs_needed
958 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000959 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100960 break;
961
962 case STACK_IMM:
963 // don't think we ever need to push imms for settling
964 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
965 break;
966 }
967 }
968 }
969 */
Damien13ed3a62013-10-08 09:05:10 +0100970}
971
Damien George3112cde2014-09-29 18:45:42 +0100972// depth==0 is top, depth==1 is before top, etc
973STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
974 return &emit->stack_info[emit->stack_size - 1 - depth];
975}
976
977// depth==0 is top, depth==1 is before top, etc
978STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
979 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100980}
Damien13ed3a62013-10-08 09:05:10 +0100981
Damiend2755ec2013-10-16 23:58:48 +0100982// pos=1 is TOS, pos=2 is next, etc
983// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200984STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100985 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100986 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100987 if (i != skip_stack_pos) {
988 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000989 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100990 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000991 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100992 }
Damienff8ed772013-10-08 22:18:32 +0100993 }
994 }
995}
Damien13ed3a62013-10-08 09:05:10 +0100996
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200997STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100998 for (int i = 0; i < emit->stack_size; i++) {
999 stack_info_t *si = &emit->stack_info[i];
1000 if (si->kind == STACK_REG) {
1001 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001002 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +01001003 }
Damien13ed3a62013-10-08 09:05:10 +01001004 }
1005}
1006
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001007STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001008 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +01001009 for (int i = 0; i < emit->stack_size; i++) {
1010 stack_info_t *si = &emit->stack_info[i];
1011 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +00001012 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001013 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001014 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001015 }
1016 }
1017 for (int i = 0; i < emit->stack_size; i++) {
1018 stack_info_t *si = &emit->stack_info[i];
1019 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +00001020 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +01001021 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001022 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +01001023 }
1024 }
1025}
1026
1027// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001028STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001029 need_reg_single(emit, reg_dest, pos);
1030 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001031 *vtype = si->vtype;
1032 switch (si->kind) {
1033 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001034 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001035 break;
1036
Damienff8ed772013-10-08 22:18:32 +01001037 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001038 if (si->data.u_reg != reg_dest) {
1039 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001040 }
1041 break;
1042
Damienff8ed772013-10-08 22:18:32 +01001043 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001044 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001045 break;
1046 }
Damien13ed3a62013-10-08 09:05:10 +01001047}
1048
Damien Georgee9dac3b2014-09-29 22:10:41 +01001049// does an efficient X=pop(); discard(); push(X)
1050// needs a (non-temp) register in case the poped element was stored in the stack
1051STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1052 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1053 si[0] = si[1];
1054 if (si->kind == STACK_VALUE) {
1055 // if folded element was on the stack we need to put it in a register
1056 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1057 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001058 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001059 }
1060 adjust_stack(emit, -1);
1061}
1062
1063// If stacked value is in a register and the register is not r1 or r2, then
1064// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1065STATIC 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 +01001066 emit->last_emit_was_return_value = false;
1067 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001068 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001069 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001070 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001071 need_reg_single(emit, *reg_dest, 1);
1072 } else {
1073 emit_access_stack(emit, 1, vtype, *reg_dest);
1074 }
1075 adjust_stack(emit, -1);
1076}
1077
Damien Georgee6ce10a2014-09-06 18:38:20 +01001078STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001079 emit->last_emit_was_return_value = false;
1080 adjust_stack(emit, -1);
1081}
1082
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001083STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001084 emit->last_emit_was_return_value = false;
1085 emit_access_stack(emit, 1, vtype, reg_dest);
1086 adjust_stack(emit, -1);
1087}
1088
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001089STATIC 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 +01001090 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001091 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001092}
1093
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001094STATIC 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 +01001095 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001096 emit_pre_pop_reg(emit, vtypeb, regb);
1097 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001098}
1099
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001100STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001101 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001102}
1103
Damien Georgee9dac3b2014-09-29 22:10:41 +01001104STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1105 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1106 si->vtype = new_vtype;
1107}
1108
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001109STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001110 stack_info_t *si = &emit->stack_info[emit->stack_size];
1111 si->vtype = vtype;
1112 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001113 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001114 adjust_stack(emit, 1);
1115}
1116
Damien George40f3c022014-07-03 13:25:24 +01001117STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001118 stack_info_t *si = &emit->stack_info[emit->stack_size];
1119 si->vtype = vtype;
1120 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001121 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001122 adjust_stack(emit, 1);
1123}
1124
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001125STATIC 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 +01001126 emit_post_push_reg(emit, vtypea, rega);
1127 emit_post_push_reg(emit, vtypeb, regb);
1128}
1129
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001130STATIC 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 +01001131 emit_post_push_reg(emit, vtypea, rega);
1132 emit_post_push_reg(emit, vtypeb, regb);
1133 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001134}
1135
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001136STATIC 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 +01001137 emit_post_push_reg(emit, vtypea, rega);
1138 emit_post_push_reg(emit, vtypeb, regb);
1139 emit_post_push_reg(emit, vtypec, regc);
1140 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001141}
1142
Damien George7fe21912014-08-16 22:31:57 +01001143STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001144 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001145 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001146}
1147
Damien George7fe21912014-08-16 22:31:57 +01001148STATIC 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 +01001149 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001150 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1151 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001152}
1153
Damien George40f3c022014-07-03 13:25:24 +01001154// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001155STATIC 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 +01001156 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001157 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1158 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001159}
1160
Damien George7fe21912014-08-16 22:31:57 +01001161STATIC 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 +00001162 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001163 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1164 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1165 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001166}
1167
Damien George40f3c022014-07-03 13:25:24 +01001168// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001169STATIC 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 +01001170 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001171 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1172 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1173 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1174 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001175}
1176
Damien George86de21b2014-08-16 22:06:11 +01001177// vtype of all n_pop objects is VTYPE_PYOBJ
1178// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1179// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1180// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1181STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1182 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001183
Damien George86de21b2014-08-16 22:06:11 +01001184 // First, store any immediate values to their respective place on the stack.
1185 for (mp_uint_t i = 0; i < n_pop; i++) {
1186 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1187 // must push any imm's to stack
1188 // must convert them to VTYPE_PYOBJ for viper code
1189 if (si->kind == STACK_IMM) {
1190 si->kind = STACK_VALUE;
1191 switch (si->vtype) {
1192 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001193 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 +01001194 break;
1195 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001196 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001197 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 +01001198 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001199 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 +01001200 }
1201 si->vtype = VTYPE_PYOBJ;
1202 break;
1203 case VTYPE_INT:
1204 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001205 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 +01001206 si->vtype = VTYPE_PYOBJ;
1207 break;
1208 default:
1209 // not handled
1210 assert(0);
1211 }
1212 }
1213
1214 // verify that this value is on the stack
1215 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001216 }
Damien George86de21b2014-08-16 22:06:11 +01001217
1218 // Second, convert any non-VTYPE_PYOBJ to that type.
1219 for (mp_uint_t i = 0; i < n_pop; i++) {
1220 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1221 if (si->vtype != VTYPE_PYOBJ) {
1222 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001223 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001224 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 +01001225 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001226 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001227 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001228 }
1229 }
1230
1231 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1232 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001233 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001234}
1235
1236// vtype of all n_push objects is VTYPE_PYOBJ
1237STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1238 need_reg_all(emit);
1239 for (mp_uint_t i = 0; i < n_push; i++) {
1240 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1241 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1242 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001243 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001244 adjust_stack(emit, n_push);
1245}
1246
Damien George7ff996c2014-09-08 23:05:16 +01001247STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001248 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001249 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001250 // need to commit stack because we can jump here from elsewhere
1251 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001252 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001253 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001254}
1255
Damien Georgecdd96df2014-04-06 12:58:40 +01001256STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1257 DEBUG_printf("import_name %s\n", qstr_str(qst));
1258 vtype_kind_t vtype_fromlist;
1259 vtype_kind_t vtype_level;
1260 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1261 assert(vtype_fromlist == VTYPE_PYOBJ);
1262 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001263 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001264 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001265}
1266
Damien Georgecdd96df2014-04-06 12:58:40 +01001267STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1268 DEBUG_printf("import_from %s\n", qstr_str(qst));
1269 emit_native_pre(emit);
1270 vtype_kind_t vtype_module;
1271 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1272 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001273 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001274 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001275}
1276
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001277STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001278 DEBUG_printf("import_star\n");
1279 vtype_kind_t vtype_module;
1280 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1281 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001282 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001283 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001284}
1285
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001286STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001287 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001288 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001289 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001290 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001291 if (emit->do_viper_types) {
1292 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001293 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1294 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1295 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001296 no_other_choice1:
1297 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1298 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001299 }
1300 } else {
1301 vtype = VTYPE_PYOBJ;
1302 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001303 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1304 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1305 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001306 no_other_choice2:
1307 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1308 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001309 }
1310 }
1311 emit_post_push_imm(emit, vtype, val);
1312}
1313
Damien George40f3c022014-07-03 13:25:24 +01001314STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001315 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001316 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001317 if (emit->do_viper_types) {
1318 emit_post_push_imm(emit, VTYPE_INT, arg);
1319 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001320 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001321 }
1322}
1323
Damien George59fba2d2015-06-25 14:42:13 +00001324STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001325 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001326 // TODO: Eventually we want to be able to work with raw pointers in viper to
1327 // do native array access. For now we just load them as any other object.
1328 /*
Damien13ed3a62013-10-08 09:05:10 +01001329 if (emit->do_viper_types) {
1330 // not implemented properly
1331 // load a pointer to the asciiz string?
1332 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001333 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001334 } else
1335 */
1336 {
Damien George59fba2d2015-06-25 14:42:13 +00001337 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien13ed3a62013-10-08 09:05:10 +01001338 }
1339}
1340
Damien Georgedab13852015-01-13 15:55:54 +00001341STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1342 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001343 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001344 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1345 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1346}
1347
Damien George3558f622014-04-20 17:50:40 +01001348STATIC void emit_native_load_null(emit_t *emit) {
1349 emit_native_pre(emit);
1350 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1351}
1352
Damien George0abb5602015-01-16 12:24:49 +00001353STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1354 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001355 vtype_kind_t vtype = emit->local_vtype[local_num];
1356 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001357 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001358 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001359 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001360 if (local_num == 0) {
1361 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001362 } else if (local_num == 1) {
1363 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1364 } else if (local_num == 2) {
1365 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001366 } else {
Damien George0b610de2014-09-29 16:25:04 +01001367 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001368 if (emit->do_viper_types) {
1369 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1370 } else {
1371 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1372 }
Damien George0b610de2014-09-29 16:25:04 +01001373 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001374 }
Damien13ed3a62013-10-08 09:05:10 +01001375}
1376
Damien George7ff996c2014-09-08 23:05:16 +01001377STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001378 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1379 need_reg_single(emit, REG_RET, 0);
1380 emit_native_load_fast(emit, qst, local_num);
1381 vtype_kind_t vtype;
1382 int reg_base = REG_RET;
1383 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1384 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1385 // closed over vars are always Python objects
1386 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001387}
1388
Damien George7ff996c2014-09-08 23:05:16 +01001389STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001390 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001391 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001392 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001393 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1394}
1395
Damien George7ff996c2014-09-08 23:05:16 +01001396STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001397 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001398 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001399 // check for builtin casting operators
1400 if (emit->do_viper_types && qst == MP_QSTR_int) {
1401 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1402 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1403 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1404 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1405 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1406 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1407 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1408 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1409 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001410 } else if (emit->do_viper_types && qst == MP_QSTR_ptr32) {
1411 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001412 } else {
1413 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1414 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1415 }
Damien13ed3a62013-10-08 09:05:10 +01001416}
1417
Damien George7ff996c2014-09-08 23:05:16 +01001418STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001419 // depends on type of subject:
1420 // - integer, function, pointer to integers: error
1421 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001422 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001423 vtype_kind_t vtype_base;
1424 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1425 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001426 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001427 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1428}
1429
Damien George7ff996c2014-09-08 23:05:16 +01001430STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001431 vtype_kind_t vtype_base;
1432 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1433 assert(vtype_base == VTYPE_PYOBJ);
1434 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001435 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001436}
1437
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001438STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001439 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001440 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001441 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001442}
1443
Damien George729f7b42014-04-17 22:10:53 +01001444STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001445 DEBUG_printf("load_subscr\n");
1446 // need to compile: base[index]
1447
1448 // pop: index, base
1449 // optimise case where index is an immediate
1450 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1451
1452 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001453 // standard Python subscr
1454 // TODO factor this implicit cast code with other uses of it
1455 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1456 if (vtype_index == VTYPE_PYOBJ) {
1457 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
1458 } else {
1459 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
1460 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
1461 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1462 }
1463 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001464 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 +01001465 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1466 } else {
Damien George91cfd412014-10-12 16:59:29 +01001467 // viper load
1468 // TODO The different machine architectures have very different
1469 // capabilities and requirements for loads, so probably best to
1470 // write a completely separate load-optimiser for each one.
1471 stack_info_t *top = peek_stack(emit, 0);
1472 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1473 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001474 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001475 emit_pre_pop_discard(emit); // discard index
1476 int reg_base = REG_ARG_1;
1477 int reg_index = REG_ARG_2;
1478 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1479 switch (vtype_base) {
1480 case VTYPE_PTR8: {
1481 // pointer to 8-bit memory
1482 // TODO optimise to use thumb ldrb r1, [r2, r3]
1483 if (index_value != 0) {
1484 // index is non-zero
1485 #if N_THUMB
1486 if (index_value > 0 && index_value < 32) {
1487 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1488 break;
1489 }
1490 #endif
1491 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1492 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1493 reg_base = reg_index;
1494 }
1495 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1496 break;
1497 }
1498 case VTYPE_PTR16: {
1499 // pointer to 16-bit memory
1500 if (index_value != 0) {
1501 // index is a non-zero immediate
1502 #if N_THUMB
1503 if (index_value > 0 && index_value < 32) {
1504 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1505 break;
1506 }
1507 #endif
1508 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1509 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1510 reg_base = reg_index;
1511 }
1512 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1513 break;
1514 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001515 case VTYPE_PTR32: {
1516 // pointer to 32-bit memory
1517 if (index_value != 0) {
1518 // index is a non-zero immediate
1519 #if N_THUMB
1520 if (index_value > 0 && index_value < 32) {
1521 asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1522 break;
1523 }
1524 #endif
1525 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1526 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1527 reg_base = reg_index;
1528 }
1529 ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
1530 break;
1531 }
Damien George91cfd412014-10-12 16:59:29 +01001532 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001533 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1534 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001535 }
1536 } else {
1537 // index is not an immediate
1538 vtype_kind_t vtype_index;
1539 int reg_index = REG_ARG_2;
1540 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1541 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1542 switch (vtype_base) {
1543 case VTYPE_PTR8: {
1544 // pointer to 8-bit memory
1545 // TODO optimise to use thumb ldrb r1, [r2, r3]
1546 assert(vtype_index == VTYPE_INT);
1547 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1548 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1549 break;
1550 }
1551 case VTYPE_PTR16: {
1552 // pointer to 16-bit memory
1553 assert(vtype_index == VTYPE_INT);
1554 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1555 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1556 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1557 break;
1558 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001559 case VTYPE_PTR32: {
1560 // pointer to word-size memory
1561 assert(vtype_index == VTYPE_INT);
1562 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1563 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1564 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1565 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1566 ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
1567 break;
1568 }
Damien George91cfd412014-10-12 16:59:29 +01001569 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001570 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1571 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001572 }
1573 }
1574 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001575 }
1576}
1577
Damien George7ff996c2014-09-08 23:05:16 +01001578STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001579 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001580 if (local_num == 0) {
1581 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001582 } else if (local_num == 1) {
1583 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1584 } else if (local_num == 2) {
1585 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001586 } else {
Damien George0b610de2014-09-29 16:25:04 +01001587 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001588 if (emit->do_viper_types) {
1589 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1590 } else {
1591 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1592 }
Damien13ed3a62013-10-08 09:05:10 +01001593 }
Damien13ed3a62013-10-08 09:05:10 +01001594 emit_post(emit);
1595
1596 // check types
1597 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1598 // first time this local is assigned, so give it a type of the object stored in it
1599 emit->local_vtype[local_num] = vtype;
1600 } else if (emit->local_vtype[local_num] != vtype) {
1601 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001602 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1603 "local '%q' has type '%q' but source is '%q'",
1604 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001605 }
1606}
1607
Damien George7ff996c2014-09-08 23:05:16 +01001608STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001609 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1610 need_reg_single(emit, REG_TEMP0, 0);
1611 need_reg_single(emit, REG_TEMP1, 0);
1612 emit_native_load_fast(emit, qst, local_num);
1613 vtype_kind_t vtype;
1614 int reg_base = REG_TEMP0;
1615 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1616 int reg_src = REG_TEMP1;
1617 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1618 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1619 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001620}
1621
Damien George7ff996c2014-09-08 23:05:16 +01001622STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001623 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001624 vtype_kind_t vtype;
1625 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1626 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001627 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001628 emit_post(emit);
1629}
1630
Damien George7ff996c2014-09-08 23:05:16 +01001631STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001632 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001633 if (vtype == VTYPE_PYOBJ) {
1634 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1635 } else {
1636 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001637 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 +01001638 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001639 }
Damien George7ff996c2014-09-08 23:05:16 +01001640 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001641 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001642}
1643
Damien George7ff996c2014-09-08 23:05:16 +01001644STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001645 vtype_kind_t vtype_base, vtype_val;
1646 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1647 assert(vtype_base == VTYPE_PYOBJ);
1648 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001649 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001650 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001651}
1652
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001653STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001654 DEBUG_printf("store_subscr\n");
1655 // need to compile: base[index] = value
1656
1657 // pop: index, base, value
1658 // optimise case where index is an immediate
1659 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1660
1661 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001662 // standard Python subscr
1663 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1664 vtype_kind_t vtype_value = peek_vtype(emit, 2);
1665 if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
1666 // need to implicitly convert non-objects to objects
1667 // TODO do this properly
1668 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
1669 adjust_stack(emit, 3);
1670 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001671 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 +01001672 emit_call(emit, MP_F_OBJ_SUBSCR);
1673 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001674 // viper store
1675 // TODO The different machine architectures have very different
1676 // capabilities and requirements for stores, so probably best to
1677 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001678 stack_info_t *top = peek_stack(emit, 0);
1679 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1680 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001681 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001682 emit_pre_pop_discard(emit); // discard index
1683 vtype_kind_t vtype_value;
1684 int reg_base = REG_ARG_1;
1685 int reg_index = REG_ARG_2;
1686 int reg_value = REG_ARG_3;
1687 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001688 #if N_X86
1689 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1690 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1691 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001692 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001693 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001694 switch (vtype_base) {
1695 case VTYPE_PTR8: {
1696 // pointer to 8-bit memory
1697 // TODO optimise to use thumb strb r1, [r2, r3]
1698 if (index_value != 0) {
1699 // index is non-zero
1700 #if N_THUMB
1701 if (index_value > 0 && index_value < 32) {
1702 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1703 break;
1704 }
1705 #endif
1706 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001707 #if N_ARM
1708 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1709 return;
1710 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001711 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1712 reg_base = reg_index;
1713 }
1714 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1715 break;
1716 }
1717 case VTYPE_PTR16: {
1718 // pointer to 16-bit memory
1719 if (index_value != 0) {
1720 // index is a non-zero immediate
1721 #if N_THUMB
1722 if (index_value > 0 && index_value < 32) {
1723 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1724 break;
1725 }
1726 #endif
1727 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001728 #if N_ARM
1729 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1730 return;
1731 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001732 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1733 reg_base = reg_index;
1734 }
1735 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1736 break;
1737 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001738 case VTYPE_PTR32: {
1739 // pointer to 32-bit memory
1740 if (index_value != 0) {
1741 // index is a non-zero immediate
1742 #if N_THUMB
1743 if (index_value > 0 && index_value < 32) {
1744 asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1745 break;
1746 }
1747 #endif
1748 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1749 #if N_ARM
1750 asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1751 return;
1752 #endif
1753 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1754 reg_base = reg_index;
1755 }
1756 ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
1757 break;
1758 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001759 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001760 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1761 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001762 }
1763 } else {
1764 // index is not an immediate
1765 vtype_kind_t vtype_index, vtype_value;
1766 int reg_index = REG_ARG_2;
1767 int reg_value = REG_ARG_3;
1768 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1769 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001770 #if N_X86
1771 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1772 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1773 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001774 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001775 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001776 switch (vtype_base) {
1777 case VTYPE_PTR8: {
1778 // pointer to 8-bit memory
1779 // TODO optimise to use thumb strb r1, [r2, r3]
1780 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001781 #if N_ARM
1782 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1783 break;
1784 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001785 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1786 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1787 break;
1788 }
1789 case VTYPE_PTR16: {
1790 // pointer to 16-bit memory
1791 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001792 #if N_ARM
1793 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1794 break;
1795 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001796 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1797 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1798 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1799 break;
1800 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001801 case VTYPE_PTR32: {
1802 // pointer to 32-bit memory
1803 assert(vtype_index == VTYPE_INT);
1804 #if N_ARM
1805 asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1806 break;
1807 #endif
1808 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1809 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1810 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1811 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1812 ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
1813 break;
1814 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001815 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001816 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1817 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001818 }
1819 }
1820
1821 }
Damien13ed3a62013-10-08 09:05:10 +01001822}
1823
Damien George7ff996c2014-09-08 23:05:16 +01001824STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001825 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1826 // to mark deleted vars but then every var would need to be checked on
1827 // each access. Very inefficient, so just set value to None to enable GC.
1828 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1829 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001830}
1831
Damien George7ff996c2014-09-08 23:05:16 +01001832STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001833 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001834 (void)emit;
1835 (void)qst;
1836 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001837}
1838
Damien Georgee6ce10a2014-09-06 18:38:20 +01001839STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1840 emit_native_pre(emit);
1841 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1842 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001843}
1844
Damien Georgee6ce10a2014-09-06 18:38:20 +01001845STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1846 emit_native_pre(emit);
1847 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1848 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001849}
1850
Damien Georgee6ce10a2014-09-06 18:38:20 +01001851STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001852 vtype_kind_t vtype_base;
1853 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1854 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001855 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 +01001856 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001857}
1858
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001859STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001860 vtype_kind_t vtype_index, vtype_base;
1861 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1862 assert(vtype_index == VTYPE_PYOBJ);
1863 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001864 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 +01001865}
1866
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001867STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001868 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001869 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001870 int reg = REG_TEMP0;
1871 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1872 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001873}
1874
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001875STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001876 vtype_kind_t vtype0, vtype1;
1877 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1878 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1879}
1880
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001881STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001882 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001883 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001884 emit_post(emit);
1885}
1886
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001887STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001888 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001889 vtype_kind_t vtype0, vtype1;
1890 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1891 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001892}
1893
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001894STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001895 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001896 vtype_kind_t vtype0, vtype1, vtype2;
1897 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1898 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1899}
1900
Damien George7ff996c2014-09-08 23:05:16 +01001901STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001902 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001903 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001904 // need to commit stack because we are jumping elsewhere
1905 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001906 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001907 emit_post(emit);
1908}
1909
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001910STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001911 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001912 if (vtype == VTYPE_PYOBJ) {
1913 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1914 if (!pop) {
1915 adjust_stack(emit, 1);
1916 }
1917 emit_call(emit, MP_F_OBJ_IS_TRUE);
1918 } else {
1919 emit_pre_pop_reg(emit, &vtype, REG_RET);
1920 if (!pop) {
1921 adjust_stack(emit, 1);
1922 }
1923 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1924 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1925 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1926 }
Damien13ed3a62013-10-08 09:05:10 +01001927 }
Damien George21ca2d72014-10-19 19:00:51 +01001928 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1929 // can use it. This is a bit of a hack.
1930 if (!pop) {
1931 emit->saved_stack_vtype = vtype;
1932 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001933 // need to commit stack because we may jump elsewhere
1934 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001935}
1936
Damien George63f38322015-02-28 15:04:06 +00001937STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1938 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001939 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001940 if (cond) {
1941 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1942 } else {
1943 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1944 }
Damien1a6633a2013-11-03 13:58:19 +00001945 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001946}
Damien1a6633a2013-11-03 13:58:19 +00001947
Damien George63f38322015-02-28 15:04:06 +00001948STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1949 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001950 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001951 if (cond) {
1952 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1953 } else {
1954 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1955 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001956 adjust_stack(emit, -1);
1957 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001958}
1959
Damien George7ff996c2014-09-08 23:05:16 +01001960STATIC void emit_native_break_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 George25c84642014-05-30 15:20:41 +01001962 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // 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_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001966 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001967 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001968}
Damien Georgea32c1e42014-05-07 18:30:52 +01001969
Damien George7ff996c2014-09-08 23:05:16 +01001970STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001971 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001972 (void)emit;
1973 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001974 assert(0);
1975}
Damien Georgeb601d952014-06-30 05:17:25 +01001976
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001977STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001978 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001979 assert(0);
1980}
Damien Georgeb601d952014-06-30 05:17:25 +01001981
Damien George7ff996c2014-09-08 23:05:16 +01001982STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001983 emit_native_pre(emit);
1984 // need to commit stack because we may jump elsewhere
1985 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001986 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 +01001987 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001988 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001989 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001990}
Damien Georgeb601d952014-06-30 05:17:25 +01001991
Damien George7ff996c2014-09-08 23:05:16 +01001992STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001993 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001994}
Damien Georgeb601d952014-06-30 05:17:25 +01001995
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001996STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001997 // logic:
1998 // exc = pop_stack
1999 // if exc == None: pass
2000 // else: raise exc
2001 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
2002 vtype_kind_t vtype;
2003 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2004 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01002005 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002006}
Damiend2755ec2013-10-16 23:58:48 +01002007
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002008STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002009 // perhaps the difficult one, as we want to rewrite for loops using native code
2010 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01002011
2012 vtype_kind_t vtype;
2013 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2014 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002015 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01002016 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002017}
Damiend2755ec2013-10-16 23:58:48 +01002018
Damien George7ff996c2014-09-08 23:05:16 +01002019STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002020 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002021 vtype_kind_t vtype;
2022 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
2023 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002024 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01002025 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
2026 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01002027 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2028}
2029
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002030STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01002031 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00002032 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002033 adjust_stack(emit, -1);
2034 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002035}
2036
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002037STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002038 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002039 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002040 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01002041 emit_post(emit);
2042}
2043
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002044STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002045 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01002046 /*
2047 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002048 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002049 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01002050 emit_post(emit);
2051 */
Damien13ed3a62013-10-08 09:05:10 +01002052}
2053
Damien Georged17926d2014-03-30 13:35:08 +01002054STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002055 vtype_kind_t vtype;
2056 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
Damien George9f5f1562015-10-08 13:08:59 +01002057 if (vtype == VTYPE_PYOBJ) {
2058 if (op == MP_UNARY_OP_NOT) {
2059 // we need to synthesise this operation by converting to bool first
2060 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
2061 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
2062 }
2063 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
2064 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2065 } else {
2066 adjust_stack(emit, 1);
2067 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2068 "unary op %q not implemented", mp_unary_op_method_name[op]);
Damien Georgeb601d952014-06-30 05:17:25 +01002069 }
Damien13ed3a62013-10-08 09:05:10 +01002070}
2071
Damien Georged17926d2014-03-30 13:35:08 +01002072STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002073 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01002074 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
2075 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01002076 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01002077 #if N_X64 || N_X86
2078 // special cases for x86 and shifting
2079 if (op == MP_BINARY_OP_LSHIFT
2080 || op == MP_BINARY_OP_INPLACE_LSHIFT
2081 || op == MP_BINARY_OP_RSHIFT
2082 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2083 #if N_X64
2084 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
2085 #else
2086 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
2087 #endif
2088 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2089 ASM_LSL_REG(emit->as, REG_RET);
2090 } else {
2091 ASM_ASR_REG(emit->as, REG_RET);
2092 }
2093 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
2094 return;
2095 }
2096 #endif
2097 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002098 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002099 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
2100 if (0) {
2101 // dummy
2102 #if !(N_X64 || N_X86)
2103 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2104 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002105 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002106 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2107 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2108 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2109 #endif
Damien George1ef23482014-10-12 14:21:06 +01002110 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2111 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2112 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2113 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2114 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2115 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2116 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2117 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2118 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002119 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2120 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2121 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2122 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2123 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2124 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George567b3492015-06-04 14:00:29 +00002125 } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2126 ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2127 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002128 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2129 // comparison ops are (in enum order):
2130 // MP_BINARY_OP_LESS
2131 // MP_BINARY_OP_MORE
2132 // MP_BINARY_OP_EQUAL
2133 // MP_BINARY_OP_LESS_EQUAL
2134 // MP_BINARY_OP_MORE_EQUAL
2135 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002136 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002137 #if N_X64
2138 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2139 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2140 static byte ops[6] = {
2141 ASM_X64_CC_JL,
2142 ASM_X64_CC_JG,
2143 ASM_X64_CC_JE,
2144 ASM_X64_CC_JLE,
2145 ASM_X64_CC_JGE,
2146 ASM_X64_CC_JNE,
2147 };
2148 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2149 #elif N_X86
2150 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2151 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2152 static byte ops[6] = {
2153 ASM_X86_CC_JL,
2154 ASM_X86_CC_JG,
2155 ASM_X86_CC_JE,
2156 ASM_X86_CC_JLE,
2157 ASM_X86_CC_JGE,
2158 ASM_X86_CC_JNE,
2159 };
2160 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2161 #elif N_THUMB
2162 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2163 static uint16_t ops[6] = {
2164 ASM_THUMB_OP_ITE_GE,
2165 ASM_THUMB_OP_ITE_GT,
2166 ASM_THUMB_OP_ITE_EQ,
2167 ASM_THUMB_OP_ITE_GT,
2168 ASM_THUMB_OP_ITE_GE,
2169 ASM_THUMB_OP_ITE_EQ,
2170 };
2171 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2172 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2173 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2174 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2175 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002176 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2177 static uint ccs[6] = {
2178 ASM_ARM_CC_LT,
2179 ASM_ARM_CC_GT,
2180 ASM_ARM_CC_EQ,
2181 ASM_ARM_CC_LE,
2182 ASM_ARM_CC_GE,
2183 ASM_ARM_CC_NE,
2184 };
2185 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002186 #else
2187 #error not implemented
2188 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002189 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2190 } else {
2191 // TODO other ops not yet implemented
Damien George4d9cad12015-06-04 11:52:16 +01002192 adjust_stack(emit, 1);
2193 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2194 "binary op %q not implemented", mp_binary_op_method_name[op]);
Damien Georgebc1d3692014-01-11 09:47:06 +00002195 }
Damien13ed3a62013-10-08 09:05:10 +01002196 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002197 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002198 bool invert = false;
2199 if (op == MP_BINARY_OP_NOT_IN) {
2200 invert = true;
2201 op = MP_BINARY_OP_IN;
2202 } else if (op == MP_BINARY_OP_IS_NOT) {
2203 invert = true;
2204 op = MP_BINARY_OP_IS;
2205 }
Damien George7fe21912014-08-16 22:31:57 +01002206 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002207 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002208 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002209 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2210 }
Damien13ed3a62013-10-08 09:05:10 +01002211 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2212 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002213 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002214 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2215 "can't do binary op between '%q' and '%q'",
2216 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002217 }
2218}
2219
Damien George7ff996c2014-09-08 23:05:16 +01002220STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002221 // for viper: call runtime, with types of args
2222 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002223 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002224 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002225 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002226 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002227}
2228
Damien George7ff996c2014-09-08 23:05:16 +01002229STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002230 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002231 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002232 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002233 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2234}
2235
Damien George7ff996c2014-09-08 23:05:16 +01002236STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002237 // only used in list comprehension
2238 vtype_kind_t vtype_list, vtype_item;
2239 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2240 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2241 assert(vtype_list == VTYPE_PYOBJ);
2242 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002243 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002244 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002245}
2246
Damien George7ff996c2014-09-08 23:05:16 +01002247STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002248 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002249 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002250 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2251}
2252
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002253STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002254 vtype_kind_t vtype_key, vtype_value, vtype_map;
2255 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
2256 assert(vtype_key == VTYPE_PYOBJ);
2257 assert(vtype_value == VTYPE_PYOBJ);
2258 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002259 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002260 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2261}
2262
Damien George7ff996c2014-09-08 23:05:16 +01002263STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002264 // only used in list comprehension
2265 vtype_kind_t vtype_map, vtype_key, vtype_value;
2266 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2267 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2268 assert(vtype_map == VTYPE_PYOBJ);
2269 assert(vtype_key == VTYPE_PYOBJ);
2270 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002271 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002272 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002273}
2274
Damien Georgee37dcaa2014-12-27 17:07:16 +00002275#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002276STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002277 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002278 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002279 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002280 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2281}
2282
Damien George7ff996c2014-09-08 23:05:16 +01002283STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002284 // only used in set comprehension
2285 vtype_kind_t vtype_set, vtype_item;
2286 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2287 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2288 assert(vtype_set == VTYPE_PYOBJ);
2289 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002290 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002291 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002292}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002293#endif
Damiend2755ec2013-10-16 23:58:48 +01002294
Damien George83204f32014-12-27 17:20:41 +00002295#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002296STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002297 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002298 if (n_args == 2) {
2299 vtype_kind_t vtype_start, vtype_stop;
2300 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2301 assert(vtype_start == VTYPE_PYOBJ);
2302 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002303 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 +01002304 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2305 } else {
2306 assert(n_args == 3);
2307 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2308 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
2309 assert(vtype_start == VTYPE_PYOBJ);
2310 assert(vtype_stop == VTYPE_PYOBJ);
2311 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002312 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002313 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2314 }
Damien13ed3a62013-10-08 09:05:10 +01002315}
Damien George83204f32014-12-27 17:20:41 +00002316#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002317
Damien George7ff996c2014-09-08 23:05:16 +01002318STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002319 DEBUG_printf("unpack_sequence %d\n", n_args);
2320 vtype_kind_t vtype_base;
2321 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2322 assert(vtype_base == VTYPE_PYOBJ);
2323 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002324 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002325}
Damien Georgecdd96df2014-04-06 12:58:40 +01002326
Damien George7ff996c2014-09-08 23:05:16 +01002327STATIC 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 +01002328 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2329 vtype_kind_t vtype_base;
2330 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2331 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002332 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 +01002333 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 +01002334}
2335
Damien George7ff996c2014-09-08 23:05:16 +01002336STATIC 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 +01002337 // 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 +00002338 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002339 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002340 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 +01002341 } else {
2342 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2343 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2344 assert(vtype_def_tuple == VTYPE_PYOBJ);
2345 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002346 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 +01002347 }
Damien13ed3a62013-10-08 09:05:10 +01002348 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2349}
2350
Damien George7ff996c2014-09-08 23:05:16 +01002351STATIC 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 +00002352 emit_native_pre(emit);
2353 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2354 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2355 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2356 } else {
2357 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2358 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2359 }
2360 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2361 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2362 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002363}
2364
Damien George7ff996c2014-09-08 23:05:16 +01002365STATIC 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 +00002366 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2367
Damien Georgee9dac3b2014-09-29 22:10:41 +01002368 // TODO: in viper mode, call special runtime routine with type info for args,
2369 // and wanted type info for return, to remove need for boxing/unboxing
2370
Damien Georgee9dac3b2014-09-29 22:10:41 +01002371 emit_native_pre(emit);
2372 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2373 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2374 // casting operator
2375 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002376 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002377 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002378 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002379 switch (peek_vtype(emit, 0)) {
2380 case VTYPE_PYOBJ: {
2381 vtype_kind_t vtype;
2382 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2383 emit_pre_pop_discard(emit);
2384 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2385 emit_post_push_reg(emit, vtype_cast, REG_RET);
2386 break;
2387 }
2388 case VTYPE_BOOL:
2389 case VTYPE_INT:
2390 case VTYPE_UINT:
2391 case VTYPE_PTR:
2392 case VTYPE_PTR8:
2393 case VTYPE_PTR16:
Damien Georgeb8f9ac52015-10-13 00:50:17 +01002394 case VTYPE_PTR32:
Damien Georgee9dac3b2014-09-29 22:10:41 +01002395 case VTYPE_PTR_NONE:
2396 emit_fold_stack_top(emit, REG_ARG_1);
2397 emit_post_top_set_vtype(emit, vtype_cast);
2398 break;
2399 default:
2400 assert(!"TODO: convert obj to int");
2401 }
2402 } else {
Damien13ed3a62013-10-08 09:05:10 +01002403 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002404 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002405 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2406 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);
2407 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2408 } else {
2409 if (n_positional != 0 || n_keyword != 0) {
2410 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2411 }
2412 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2413 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2414 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2415 }
Damien Georgecd82e022014-02-02 13:11:48 +00002416 }
Damien13ed3a62013-10-08 09:05:10 +01002417}
2418
Damien George7ff996c2014-09-08 23:05:16 +01002419STATIC 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 +01002420 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002421 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2422 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);
2423 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2424 } else {
2425 emit_native_pre(emit);
2426 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
2427 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2428 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2429 }
Damien13ed3a62013-10-08 09:05:10 +01002430}
2431
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002432STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002433 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002434 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002435 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2436 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002437 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002438 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002439 } else {
2440 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002441 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002442 } else {
2443 vtype_kind_t vtype;
2444 emit_pre_pop_reg(emit, &vtype, REG_RET);
2445 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002446 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2447 "return expected '%q' but got '%q'",
2448 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002449 }
Damien George2ac4af62014-08-15 16:45:41 +01002450 }
Damien13ed3a62013-10-08 09:05:10 +01002451 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002452 vtype_kind_t vtype;
2453 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002454 assert(vtype == VTYPE_PYOBJ);
2455 }
2456 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002457 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2458 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002459}
2460
Damien George7ff996c2014-09-08 23:05:16 +01002461STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002462 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002463 vtype_kind_t vtype_exc;
2464 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2465 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002466 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002467 }
2468 // 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 +01002469 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002470}
Damien Georgeb601d952014-06-30 05:17:25 +01002471
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002472STATIC void emit_native_yield_value(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}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002477STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002478 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002479 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002480 assert(0);
2481}
2482
Damien Georgeb601d952014-06-30 05:17:25 +01002483STATIC void emit_native_start_except_handler(emit_t *emit) {
2484 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2485 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2486 // the first 2 elements, so we can get the thrown value.
2487 adjust_stack(emit, 2);
2488 vtype_kind_t vtype_nlr;
2489 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002490 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002491 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
2492}
2493
2494STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002495 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002496}
2497
Damien13ed3a62013-10-08 09:05:10 +01002498const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002499 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002500 emit_native_start_pass,
2501 emit_native_end_pass,
2502 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002503 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002504 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002505
Damien George542bd6b2015-03-26 14:42:40 +00002506 {
2507 emit_native_load_fast,
2508 emit_native_load_deref,
2509 emit_native_load_name,
2510 emit_native_load_global,
2511 },
2512 {
2513 emit_native_store_fast,
2514 emit_native_store_deref,
2515 emit_native_store_name,
2516 emit_native_store_global,
2517 },
2518 {
2519 emit_native_delete_fast,
2520 emit_native_delete_deref,
2521 emit_native_delete_name,
2522 emit_native_delete_global,
2523 },
Damien13ed3a62013-10-08 09:05:10 +01002524
2525 emit_native_label_assign,
2526 emit_native_import_name,
2527 emit_native_import_from,
2528 emit_native_import_star,
2529 emit_native_load_const_tok,
2530 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002531 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002532 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002533 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002534 emit_native_load_attr,
2535 emit_native_load_method,
2536 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002537 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002538 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002539 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002540 emit_native_delete_attr,
2541 emit_native_delete_subscr,
2542 emit_native_dup_top,
2543 emit_native_dup_top_two,
2544 emit_native_pop_top,
2545 emit_native_rot_two,
2546 emit_native_rot_three,
2547 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002548 emit_native_pop_jump_if,
2549 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002550 emit_native_break_loop,
2551 emit_native_continue_loop,
2552 emit_native_setup_with,
2553 emit_native_with_cleanup,
2554 emit_native_setup_except,
2555 emit_native_setup_finally,
2556 emit_native_end_finally,
2557 emit_native_get_iter,
2558 emit_native_for_iter,
2559 emit_native_for_iter_end,
2560 emit_native_pop_block,
2561 emit_native_pop_except,
2562 emit_native_unary_op,
2563 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002564 emit_native_build_tuple,
2565 emit_native_build_list,
2566 emit_native_list_append,
2567 emit_native_build_map,
2568 emit_native_store_map,
2569 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002570 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002571 emit_native_build_set,
2572 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002573 #endif
Damien George83204f32014-12-27 17:20:41 +00002574 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002575 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002576 #endif
Damien13ed3a62013-10-08 09:05:10 +01002577 emit_native_unpack_sequence,
2578 emit_native_unpack_ex,
2579 emit_native_make_function,
2580 emit_native_make_closure,
2581 emit_native_call_function,
2582 emit_native_call_method,
2583 emit_native_return_value,
2584 emit_native_raise_varargs,
2585 emit_native_yield_value,
2586 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002587
2588 emit_native_start_except_handler,
2589 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002590};
2591
Damien Georgec90f59e2014-09-06 23:06:36 +01002592#endif