blob: 65908070fdc93ba5ea0a369f42d574f42b98a0fe [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
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000833 // write code info
834 #if MICROPY_PERSISTENT_CODE
Damien George713ea182015-10-23 01:23:11 +0100835 ASM_DATA(emit->as, 1, 5);
Damien Georgec8e9c0d2015-11-02 17:27:18 +0000836 ASM_DATA(emit->as, 1, emit->scope->simple_name);
837 ASM_DATA(emit->as, 1, emit->scope->simple_name >> 8);
838 ASM_DATA(emit->as, 1, emit->scope->source_file);
839 ASM_DATA(emit->as, 1, emit->scope->source_file >> 8);
840 #else
841 ASM_DATA(emit->as, 1, 1);
842 #endif
Damien George713ea182015-10-23 01:23:11 +0100843
844 // bytecode prelude: initialise closed over variables
845 for (int i = 0; i < emit->scope->id_info_len; i++) {
846 id_info_t *id = &emit->scope->id_info[i];
847 if (id->kind == ID_INFO_KIND_CELL) {
848 assert(id->local_num < 255);
849 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
850 }
851 }
852 ASM_DATA(emit->as, 1, 255); // end of list sentinel
853
Damien George99886182015-04-06 22:38:53 +0100854 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
Damien George713ea182015-10-23 01:23:11 +0100855 emit->const_table_offset = ASM_GET_CODE_POS(emit->as);
Damien George9b7f5832015-03-18 17:47:47 +0000856
857 // write argument names as qstr objects
Damien George9a42eb52015-05-06 13:55:33 +0100858 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100859 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100860 qstr qst = MP_QSTR__star_;
861 for (int j = 0; j < emit->scope->id_info_len; ++j) {
862 id_info_t *id = &emit->scope->id_info[j];
863 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
864 qst = id->qst;
865 break;
866 }
867 }
868 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100869 }
870
Damien George99886182015-04-06 22:38:53 +0100871 }
872
Damien Georgec90f59e2014-09-06 23:06:36 +0100873 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100874
875 // check stack is back to zero size
876 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100877 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100878 }
879
Damien George36db6bc2014-05-07 17:24:22 +0100880 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100881 void *f = ASM_GET_CODE(emit->as);
882 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100883
884 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100885 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100886 mp_uint_t type_sig = emit->return_vtype & 3;
887 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
888 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
889 }
890
Damien George99886182015-04-06 22:38:53 +0100891 mp_emit_glue_assign_native(emit->scope->raw_code,
892 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
Damien George713ea182015-10-23 01:23:11 +0100893 f, f_len, (mp_uint_t*)((byte*)f + emit->const_table_offset),
894 emit->scope->num_pos_args, emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100895 }
896}
897
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200898STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100899 return emit->last_emit_was_return_value;
900}
901
Damien Georged6230f62014-09-23 14:10:03 +0000902STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000903 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100904 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100905 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100906 emit->scope->stack_size = emit->stack_size;
907 }
Damien George21ca2d72014-10-19 19:00:51 +0100908#ifdef DEBUG_PRINT
909 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
910 for (int i = 0; i < emit->stack_size; i++) {
911 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000912 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100913 }
914 DEBUG_printf("\n");
915#endif
Damien13ed3a62013-10-08 09:05:10 +0100916}
917
Damien Georged6230f62014-09-23 14:10:03 +0000918STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100919 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000920 // If we are adjusting the stack in a positive direction (pushing) then we
921 // need to fill in values for the stack kind and vtype of the newly-pushed
922 // entries. These should be set to "value" (ie not reg or imm) because we
923 // should only need to adjust the stack due to a jump to this part in the
924 // code (and hence we have settled the stack before the jump).
925 for (mp_int_t i = 0; i < delta; i++) {
926 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
927 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100928 // TODO we don't know the vtype to use here. At the moment this is a
929 // hack to get the case of multi comparison working.
930 if (delta == 1) {
931 si->vtype = emit->saved_stack_vtype;
932 } else {
933 si->vtype = VTYPE_PYOBJ;
934 }
Damien Georged6230f62014-09-23 14:10:03 +0000935 }
936 adjust_stack(emit, delta);
937}
938
939STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000940 (void)emit;
941 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000942}
943
Damienff8ed772013-10-08 22:18:32 +0100944/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200945STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100946 adjust_stack(emit, stack_size_delta);
947 emit->last_emit_was_return_value = false;
948}
Damienff8ed772013-10-08 22:18:32 +0100949*/
Damien13ed3a62013-10-08 09:05:10 +0100950
Damienff8ed772013-10-08 22:18:32 +0100951// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000952STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100953 emit->last_emit_was_return_value = false;
954 // settle the stack
955 /*
956 if (regs_needed != 0) {
957 for (int i = 0; i < emit->stack_size; i++) {
958 switch (emit->stack_info[i].kind) {
959 case STACK_VALUE:
960 break;
961
962 case STACK_REG:
963 // TODO only push reg if in regs_needed
964 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000965 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100966 break;
967
968 case STACK_IMM:
969 // don't think we ever need to push imms for settling
970 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
971 break;
972 }
973 }
974 }
975 */
Damien13ed3a62013-10-08 09:05:10 +0100976}
977
Damien George3112cde2014-09-29 18:45:42 +0100978// depth==0 is top, depth==1 is before top, etc
979STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
980 return &emit->stack_info[emit->stack_size - 1 - depth];
981}
982
983// depth==0 is top, depth==1 is before top, etc
984STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
985 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100986}
Damien13ed3a62013-10-08 09:05:10 +0100987
Damiend2755ec2013-10-16 23:58:48 +0100988// pos=1 is TOS, pos=2 is next, etc
989// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200990STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100991 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100992 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100993 if (i != skip_stack_pos) {
994 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000995 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100996 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000997 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100998 }
Damienff8ed772013-10-08 22:18:32 +0100999 }
1000 }
1001}
Damien13ed3a62013-10-08 09:05:10 +01001002
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001003STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +01001004 for (int i = 0; i < emit->stack_size; i++) {
1005 stack_info_t *si = &emit->stack_info[i];
1006 if (si->kind == STACK_REG) {
1007 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001008 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +01001009 }
Damien13ed3a62013-10-08 09:05:10 +01001010 }
1011}
1012
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001013STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001014 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +01001015 for (int i = 0; i < emit->stack_size; i++) {
1016 stack_info_t *si = &emit->stack_info[i];
1017 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +00001018 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001019 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001020 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001021 }
1022 }
1023 for (int i = 0; i < emit->stack_size; i++) {
1024 stack_info_t *si = &emit->stack_info[i];
1025 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +00001026 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +01001027 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001028 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +01001029 }
1030 }
1031}
1032
1033// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001034STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001035 need_reg_single(emit, reg_dest, pos);
1036 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001037 *vtype = si->vtype;
1038 switch (si->kind) {
1039 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001040 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001041 break;
1042
Damienff8ed772013-10-08 22:18:32 +01001043 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001044 if (si->data.u_reg != reg_dest) {
1045 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001046 }
1047 break;
1048
Damienff8ed772013-10-08 22:18:32 +01001049 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001050 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001051 break;
1052 }
Damien13ed3a62013-10-08 09:05:10 +01001053}
1054
Damien Georgee9dac3b2014-09-29 22:10:41 +01001055// does an efficient X=pop(); discard(); push(X)
1056// needs a (non-temp) register in case the poped element was stored in the stack
1057STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1058 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1059 si[0] = si[1];
1060 if (si->kind == STACK_VALUE) {
1061 // if folded element was on the stack we need to put it in a register
1062 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1063 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001064 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001065 }
1066 adjust_stack(emit, -1);
1067}
1068
1069// If stacked value is in a register and the register is not r1 or r2, then
1070// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1071STATIC 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 +01001072 emit->last_emit_was_return_value = false;
1073 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001074 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001075 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001076 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001077 need_reg_single(emit, *reg_dest, 1);
1078 } else {
1079 emit_access_stack(emit, 1, vtype, *reg_dest);
1080 }
1081 adjust_stack(emit, -1);
1082}
1083
Damien Georgee6ce10a2014-09-06 18:38:20 +01001084STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001085 emit->last_emit_was_return_value = false;
1086 adjust_stack(emit, -1);
1087}
1088
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001089STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001090 emit->last_emit_was_return_value = false;
1091 emit_access_stack(emit, 1, vtype, reg_dest);
1092 adjust_stack(emit, -1);
1093}
1094
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001095STATIC 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 +01001096 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001097 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001098}
1099
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001100STATIC 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 +01001101 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001102 emit_pre_pop_reg(emit, vtypeb, regb);
1103 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001104}
1105
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001106STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001107 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001108}
1109
Damien Georgee9dac3b2014-09-29 22:10:41 +01001110STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1111 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1112 si->vtype = new_vtype;
1113}
1114
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001115STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001116 stack_info_t *si = &emit->stack_info[emit->stack_size];
1117 si->vtype = vtype;
1118 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001119 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001120 adjust_stack(emit, 1);
1121}
1122
Damien George40f3c022014-07-03 13:25:24 +01001123STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001124 stack_info_t *si = &emit->stack_info[emit->stack_size];
1125 si->vtype = vtype;
1126 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001127 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001128 adjust_stack(emit, 1);
1129}
1130
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001131STATIC 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 +01001132 emit_post_push_reg(emit, vtypea, rega);
1133 emit_post_push_reg(emit, vtypeb, regb);
1134}
1135
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001136STATIC 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 +01001137 emit_post_push_reg(emit, vtypea, rega);
1138 emit_post_push_reg(emit, vtypeb, regb);
1139 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001140}
1141
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001142STATIC 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 +01001143 emit_post_push_reg(emit, vtypea, rega);
1144 emit_post_push_reg(emit, vtypeb, regb);
1145 emit_post_push_reg(emit, vtypec, regc);
1146 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001147}
1148
Damien George7fe21912014-08-16 22:31:57 +01001149STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001150 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001151 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001152}
1153
Damien George7fe21912014-08-16 22:31:57 +01001154STATIC 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 +01001155 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001156 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1157 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001158}
1159
Damien George40f3c022014-07-03 13:25:24 +01001160// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001161STATIC 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 +01001162 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001163 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1164 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001165}
1166
Damien George7fe21912014-08-16 22:31:57 +01001167STATIC 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 +00001168 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001169 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1170 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1171 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001172}
1173
Damien George40f3c022014-07-03 13:25:24 +01001174// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001175STATIC 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 +01001176 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001177 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1178 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1179 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1180 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001181}
1182
Damien George86de21b2014-08-16 22:06:11 +01001183// vtype of all n_pop objects is VTYPE_PYOBJ
1184// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1185// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1186// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1187STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1188 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001189
Damien George86de21b2014-08-16 22:06:11 +01001190 // First, store any immediate values to their respective place on the stack.
1191 for (mp_uint_t i = 0; i < n_pop; i++) {
1192 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1193 // must push any imm's to stack
1194 // must convert them to VTYPE_PYOBJ for viper code
1195 if (si->kind == STACK_IMM) {
1196 si->kind = STACK_VALUE;
1197 switch (si->vtype) {
1198 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001199 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 +01001200 break;
1201 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001202 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001203 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 +01001204 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001205 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 +01001206 }
1207 si->vtype = VTYPE_PYOBJ;
1208 break;
1209 case VTYPE_INT:
1210 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001211 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 +01001212 si->vtype = VTYPE_PYOBJ;
1213 break;
1214 default:
1215 // not handled
1216 assert(0);
1217 }
1218 }
1219
1220 // verify that this value is on the stack
1221 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001222 }
Damien George86de21b2014-08-16 22:06:11 +01001223
1224 // Second, convert any non-VTYPE_PYOBJ to that type.
1225 for (mp_uint_t i = 0; i < n_pop; i++) {
1226 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1227 if (si->vtype != VTYPE_PYOBJ) {
1228 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001229 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001230 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 +01001231 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001232 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001233 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001234 }
1235 }
1236
1237 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1238 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001239 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001240}
1241
1242// vtype of all n_push objects is VTYPE_PYOBJ
1243STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1244 need_reg_all(emit);
1245 for (mp_uint_t i = 0; i < n_push; i++) {
1246 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1247 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1248 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001249 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001250 adjust_stack(emit, n_push);
1251}
1252
Damien George7ff996c2014-09-08 23:05:16 +01001253STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001254 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001255 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001256 // need to commit stack because we can jump here from elsewhere
1257 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001258 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001259 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001260}
1261
Damien Georgecdd96df2014-04-06 12:58:40 +01001262STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1263 DEBUG_printf("import_name %s\n", qstr_str(qst));
1264 vtype_kind_t vtype_fromlist;
1265 vtype_kind_t vtype_level;
1266 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1267 assert(vtype_fromlist == VTYPE_PYOBJ);
1268 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001269 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001270 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001271}
1272
Damien Georgecdd96df2014-04-06 12:58:40 +01001273STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1274 DEBUG_printf("import_from %s\n", qstr_str(qst));
1275 emit_native_pre(emit);
1276 vtype_kind_t vtype_module;
1277 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1278 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001279 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001280 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001281}
1282
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001283STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001284 DEBUG_printf("import_star\n");
1285 vtype_kind_t vtype_module;
1286 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1287 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001288 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001289 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001290}
1291
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001292STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001293 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001294 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001295 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001296 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001297 if (emit->do_viper_types) {
1298 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001299 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1300 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1301 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001302 no_other_choice1:
1303 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1304 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001305 }
1306 } else {
1307 vtype = VTYPE_PYOBJ;
1308 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001309 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1310 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1311 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001312 no_other_choice2:
1313 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1314 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001315 }
1316 }
1317 emit_post_push_imm(emit, vtype, val);
1318}
1319
Damien George40f3c022014-07-03 13:25:24 +01001320STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001321 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001322 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001323 if (emit->do_viper_types) {
1324 emit_post_push_imm(emit, VTYPE_INT, arg);
1325 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001326 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001327 }
1328}
1329
Damien George59fba2d2015-06-25 14:42:13 +00001330STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001331 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001332 // TODO: Eventually we want to be able to work with raw pointers in viper to
1333 // do native array access. For now we just load them as any other object.
1334 /*
Damien13ed3a62013-10-08 09:05:10 +01001335 if (emit->do_viper_types) {
1336 // not implemented properly
1337 // load a pointer to the asciiz string?
1338 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001339 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001340 } else
1341 */
1342 {
Damien George59fba2d2015-06-25 14:42:13 +00001343 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien13ed3a62013-10-08 09:05:10 +01001344 }
1345}
1346
Damien George5d66b422015-11-27 12:41:25 +00001347STATIC void emit_native_load_const_obj(emit_t *emit, mp_obj_t obj) {
Damien Georgedab13852015-01-13 15:55:54 +00001348 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001349 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001350 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1351 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1352}
1353
Damien George3558f622014-04-20 17:50:40 +01001354STATIC void emit_native_load_null(emit_t *emit) {
1355 emit_native_pre(emit);
1356 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1357}
1358
Damien George0abb5602015-01-16 12:24:49 +00001359STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1360 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001361 vtype_kind_t vtype = emit->local_vtype[local_num];
1362 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001363 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001364 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001365 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001366 if (local_num == 0) {
1367 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001368 } else if (local_num == 1) {
1369 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1370 } else if (local_num == 2) {
1371 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001372 } else {
Damien George0b610de2014-09-29 16:25:04 +01001373 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001374 if (emit->do_viper_types) {
1375 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1376 } else {
1377 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1378 }
Damien George0b610de2014-09-29 16:25:04 +01001379 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001380 }
Damien13ed3a62013-10-08 09:05:10 +01001381}
1382
Damien George7ff996c2014-09-08 23:05:16 +01001383STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001384 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1385 need_reg_single(emit, REG_RET, 0);
1386 emit_native_load_fast(emit, qst, local_num);
1387 vtype_kind_t vtype;
1388 int reg_base = REG_RET;
1389 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1390 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1391 // closed over vars are always Python objects
1392 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001393}
1394
Damien George7ff996c2014-09-08 23:05:16 +01001395STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001396 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001397 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001398 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001399 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1400}
1401
Damien George7ff996c2014-09-08 23:05:16 +01001402STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001403 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001404 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001405 // check for builtin casting operators
1406 if (emit->do_viper_types && qst == MP_QSTR_int) {
1407 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1408 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1409 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1410 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1411 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1412 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1413 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1414 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1415 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001416 } else if (emit->do_viper_types && qst == MP_QSTR_ptr32) {
1417 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001418 } else {
1419 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1420 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1421 }
Damien13ed3a62013-10-08 09:05:10 +01001422}
1423
Damien George7ff996c2014-09-08 23:05:16 +01001424STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001425 // depends on type of subject:
1426 // - integer, function, pointer to integers: error
1427 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001428 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001429 vtype_kind_t vtype_base;
1430 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1431 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001432 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001433 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1434}
1435
Damien George7ff996c2014-09-08 23:05:16 +01001436STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001437 vtype_kind_t vtype_base;
1438 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1439 assert(vtype_base == VTYPE_PYOBJ);
1440 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001441 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001442}
1443
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001444STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001445 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001446 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001447 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001448}
1449
Damien George729f7b42014-04-17 22:10:53 +01001450STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001451 DEBUG_printf("load_subscr\n");
1452 // need to compile: base[index]
1453
1454 // pop: index, base
1455 // optimise case where index is an immediate
1456 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1457
1458 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001459 // standard Python subscr
1460 // TODO factor this implicit cast code with other uses of it
1461 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1462 if (vtype_index == VTYPE_PYOBJ) {
1463 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
1464 } else {
1465 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
1466 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
1467 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1468 }
1469 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001470 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 +01001471 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1472 } else {
Damien George91cfd412014-10-12 16:59:29 +01001473 // viper load
1474 // TODO The different machine architectures have very different
1475 // capabilities and requirements for loads, so probably best to
1476 // write a completely separate load-optimiser for each one.
1477 stack_info_t *top = peek_stack(emit, 0);
1478 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1479 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001480 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001481 emit_pre_pop_discard(emit); // discard index
1482 int reg_base = REG_ARG_1;
1483 int reg_index = REG_ARG_2;
1484 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1485 switch (vtype_base) {
1486 case VTYPE_PTR8: {
1487 // pointer to 8-bit memory
1488 // TODO optimise to use thumb ldrb r1, [r2, r3]
1489 if (index_value != 0) {
1490 // index is non-zero
1491 #if N_THUMB
1492 if (index_value > 0 && index_value < 32) {
1493 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1494 break;
1495 }
1496 #endif
1497 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1498 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1499 reg_base = reg_index;
1500 }
1501 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1502 break;
1503 }
1504 case VTYPE_PTR16: {
1505 // pointer to 16-bit memory
1506 if (index_value != 0) {
1507 // index is a non-zero immediate
1508 #if N_THUMB
1509 if (index_value > 0 && index_value < 32) {
1510 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1511 break;
1512 }
1513 #endif
1514 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1515 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1516 reg_base = reg_index;
1517 }
1518 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1519 break;
1520 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001521 case VTYPE_PTR32: {
1522 // pointer to 32-bit memory
1523 if (index_value != 0) {
1524 // index is a non-zero immediate
1525 #if N_THUMB
1526 if (index_value > 0 && index_value < 32) {
1527 asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1528 break;
1529 }
1530 #endif
1531 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1532 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1533 reg_base = reg_index;
1534 }
1535 ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
1536 break;
1537 }
Damien George91cfd412014-10-12 16:59:29 +01001538 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001539 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1540 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001541 }
1542 } else {
1543 // index is not an immediate
1544 vtype_kind_t vtype_index;
1545 int reg_index = REG_ARG_2;
1546 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1547 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1548 switch (vtype_base) {
1549 case VTYPE_PTR8: {
1550 // pointer to 8-bit memory
1551 // TODO optimise to use thumb ldrb r1, [r2, r3]
1552 assert(vtype_index == VTYPE_INT);
1553 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1554 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1555 break;
1556 }
1557 case VTYPE_PTR16: {
1558 // pointer to 16-bit memory
1559 assert(vtype_index == VTYPE_INT);
1560 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1561 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1562 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1563 break;
1564 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001565 case VTYPE_PTR32: {
1566 // pointer to word-size memory
1567 assert(vtype_index == VTYPE_INT);
1568 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1569 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1570 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1571 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1572 ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
1573 break;
1574 }
Damien George91cfd412014-10-12 16:59:29 +01001575 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001576 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1577 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001578 }
1579 }
1580 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001581 }
1582}
1583
Damien George7ff996c2014-09-08 23:05:16 +01001584STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001585 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001586 if (local_num == 0) {
1587 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001588 } else if (local_num == 1) {
1589 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1590 } else if (local_num == 2) {
1591 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001592 } else {
Damien George0b610de2014-09-29 16:25:04 +01001593 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001594 if (emit->do_viper_types) {
1595 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1596 } else {
1597 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1598 }
Damien13ed3a62013-10-08 09:05:10 +01001599 }
Damien13ed3a62013-10-08 09:05:10 +01001600 emit_post(emit);
1601
1602 // check types
1603 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1604 // first time this local is assigned, so give it a type of the object stored in it
1605 emit->local_vtype[local_num] = vtype;
1606 } else if (emit->local_vtype[local_num] != vtype) {
1607 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001608 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1609 "local '%q' has type '%q' but source is '%q'",
1610 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001611 }
1612}
1613
Damien George7ff996c2014-09-08 23:05:16 +01001614STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001615 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1616 need_reg_single(emit, REG_TEMP0, 0);
1617 need_reg_single(emit, REG_TEMP1, 0);
1618 emit_native_load_fast(emit, qst, local_num);
1619 vtype_kind_t vtype;
1620 int reg_base = REG_TEMP0;
1621 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1622 int reg_src = REG_TEMP1;
1623 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1624 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1625 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001626}
1627
Damien George7ff996c2014-09-08 23:05:16 +01001628STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001629 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001630 vtype_kind_t vtype;
1631 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1632 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001633 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001634 emit_post(emit);
1635}
1636
Damien George7ff996c2014-09-08 23:05:16 +01001637STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001638 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001639 if (vtype == VTYPE_PYOBJ) {
1640 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1641 } else {
1642 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001643 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 +01001644 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001645 }
Damien George7ff996c2014-09-08 23:05:16 +01001646 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001647 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001648}
1649
Damien George7ff996c2014-09-08 23:05:16 +01001650STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001651 vtype_kind_t vtype_base, vtype_val;
1652 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1653 assert(vtype_base == VTYPE_PYOBJ);
1654 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001655 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001656 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001657}
1658
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001659STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001660 DEBUG_printf("store_subscr\n");
1661 // need to compile: base[index] = value
1662
1663 // pop: index, base, value
1664 // optimise case where index is an immediate
1665 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1666
1667 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001668 // standard Python subscr
1669 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1670 vtype_kind_t vtype_value = peek_vtype(emit, 2);
1671 if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
1672 // need to implicitly convert non-objects to objects
1673 // TODO do this properly
1674 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
1675 adjust_stack(emit, 3);
1676 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001677 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 +01001678 emit_call(emit, MP_F_OBJ_SUBSCR);
1679 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001680 // viper store
1681 // TODO The different machine architectures have very different
1682 // capabilities and requirements for stores, so probably best to
1683 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001684 stack_info_t *top = peek_stack(emit, 0);
1685 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1686 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001687 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001688 emit_pre_pop_discard(emit); // discard index
1689 vtype_kind_t vtype_value;
1690 int reg_base = REG_ARG_1;
1691 int reg_index = REG_ARG_2;
1692 int reg_value = REG_ARG_3;
1693 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001694 #if N_X86
1695 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1696 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1697 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001698 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001699 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001700 switch (vtype_base) {
1701 case VTYPE_PTR8: {
1702 // pointer to 8-bit memory
1703 // TODO optimise to use thumb strb r1, [r2, r3]
1704 if (index_value != 0) {
1705 // index is non-zero
1706 #if N_THUMB
1707 if (index_value > 0 && index_value < 32) {
1708 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1709 break;
1710 }
1711 #endif
1712 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001713 #if N_ARM
1714 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1715 return;
1716 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001717 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1718 reg_base = reg_index;
1719 }
1720 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1721 break;
1722 }
1723 case VTYPE_PTR16: {
1724 // pointer to 16-bit memory
1725 if (index_value != 0) {
1726 // index is a non-zero immediate
1727 #if N_THUMB
1728 if (index_value > 0 && index_value < 32) {
1729 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1730 break;
1731 }
1732 #endif
1733 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001734 #if N_ARM
1735 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1736 return;
1737 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001738 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1739 reg_base = reg_index;
1740 }
1741 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1742 break;
1743 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001744 case VTYPE_PTR32: {
1745 // pointer to 32-bit memory
1746 if (index_value != 0) {
1747 // index is a non-zero immediate
1748 #if N_THUMB
1749 if (index_value > 0 && index_value < 32) {
1750 asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1751 break;
1752 }
1753 #endif
1754 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1755 #if N_ARM
1756 asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1757 return;
1758 #endif
1759 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1760 reg_base = reg_index;
1761 }
1762 ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
1763 break;
1764 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001765 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001766 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1767 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001768 }
1769 } else {
1770 // index is not an immediate
1771 vtype_kind_t vtype_index, vtype_value;
1772 int reg_index = REG_ARG_2;
1773 int reg_value = REG_ARG_3;
1774 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1775 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001776 #if N_X86
1777 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1778 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1779 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001780 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001781 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001782 switch (vtype_base) {
1783 case VTYPE_PTR8: {
1784 // pointer to 8-bit memory
1785 // TODO optimise to use thumb strb r1, [r2, r3]
1786 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001787 #if N_ARM
1788 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1789 break;
1790 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001791 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1792 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1793 break;
1794 }
1795 case VTYPE_PTR16: {
1796 // pointer to 16-bit memory
1797 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001798 #if N_ARM
1799 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1800 break;
1801 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001802 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1803 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1804 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1805 break;
1806 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001807 case VTYPE_PTR32: {
1808 // pointer to 32-bit memory
1809 assert(vtype_index == VTYPE_INT);
1810 #if N_ARM
1811 asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1812 break;
1813 #endif
1814 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1815 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1816 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1817 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1818 ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
1819 break;
1820 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001821 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001822 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1823 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001824 }
1825 }
1826
1827 }
Damien13ed3a62013-10-08 09:05:10 +01001828}
1829
Damien George7ff996c2014-09-08 23:05:16 +01001830STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001831 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1832 // to mark deleted vars but then every var would need to be checked on
1833 // each access. Very inefficient, so just set value to None to enable GC.
1834 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1835 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001836}
1837
Damien George7ff996c2014-09-08 23:05:16 +01001838STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001839 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001840 (void)emit;
1841 (void)qst;
1842 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001843}
1844
Damien Georgee6ce10a2014-09-06 18:38:20 +01001845STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1846 emit_native_pre(emit);
1847 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, 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_global(emit_t *emit, qstr qst) {
1852 emit_native_pre(emit);
1853 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1854 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001855}
1856
Damien Georgee6ce10a2014-09-06 18:38:20 +01001857STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001858 vtype_kind_t vtype_base;
1859 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1860 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001861 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 +01001862 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001863}
1864
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001865STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001866 vtype_kind_t vtype_index, vtype_base;
1867 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1868 assert(vtype_index == VTYPE_PYOBJ);
1869 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001870 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 +01001871}
1872
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001873STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001874 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001875 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001876 int reg = REG_TEMP0;
1877 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1878 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001879}
1880
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001881STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001882 vtype_kind_t vtype0, vtype1;
1883 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1884 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1885}
1886
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001887STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001888 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001889 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001890 emit_post(emit);
1891}
1892
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001893STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001894 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001895 vtype_kind_t vtype0, vtype1;
1896 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1897 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001898}
1899
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001900STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001901 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001902 vtype_kind_t vtype0, vtype1, vtype2;
1903 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1904 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1905}
1906
Damien George7ff996c2014-09-08 23:05:16 +01001907STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001908 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001909 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001910 // need to commit stack because we are jumping elsewhere
1911 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001912 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001913 emit_post(emit);
1914}
1915
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001916STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001917 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001918 if (vtype == VTYPE_PYOBJ) {
1919 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1920 if (!pop) {
1921 adjust_stack(emit, 1);
1922 }
1923 emit_call(emit, MP_F_OBJ_IS_TRUE);
1924 } else {
1925 emit_pre_pop_reg(emit, &vtype, REG_RET);
1926 if (!pop) {
1927 adjust_stack(emit, 1);
1928 }
1929 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1930 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1931 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1932 }
Damien13ed3a62013-10-08 09:05:10 +01001933 }
Damien George21ca2d72014-10-19 19:00:51 +01001934 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1935 // can use it. This is a bit of a hack.
1936 if (!pop) {
1937 emit->saved_stack_vtype = vtype;
1938 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001939 // need to commit stack because we may jump elsewhere
1940 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001941}
1942
Damien George63f38322015-02-28 15:04:06 +00001943STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1944 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001945 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001946 if (cond) {
1947 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1948 } else {
1949 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1950 }
Damien1a6633a2013-11-03 13:58:19 +00001951 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001952}
Damien1a6633a2013-11-03 13:58:19 +00001953
Damien George63f38322015-02-28 15:04:06 +00001954STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1955 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001956 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001957 if (cond) {
1958 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1959 } else {
1960 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1961 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001962 adjust_stack(emit, -1);
1963 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001964}
1965
Damien George7ff996c2014-09-08 23:05:16 +01001966STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001967 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001968 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001969}
Damien Georgea32c1e42014-05-07 18:30:52 +01001970
Damien George7ff996c2014-09-08 23:05:16 +01001971STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001972 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001973 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001974}
Damien Georgea32c1e42014-05-07 18:30:52 +01001975
Damien George7ff996c2014-09-08 23:05:16 +01001976STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001977 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001978 (void)emit;
1979 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001980 assert(0);
1981}
Damien Georgeb601d952014-06-30 05:17:25 +01001982
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001983STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001984 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001985 assert(0);
1986}
Damien Georgeb601d952014-06-30 05:17:25 +01001987
Damien George7ff996c2014-09-08 23:05:16 +01001988STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001989 emit_native_pre(emit);
1990 // need to commit stack because we may jump elsewhere
1991 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001992 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 +01001993 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001994 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001995 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001996}
Damien Georgeb601d952014-06-30 05:17:25 +01001997
Damien George7ff996c2014-09-08 23:05:16 +01001998STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001999 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01002000}
Damien Georgeb601d952014-06-30 05:17:25 +01002001
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002002STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00002003 // logic:
2004 // exc = pop_stack
2005 // if exc == None: pass
2006 // else: raise exc
2007 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
2008 vtype_kind_t vtype;
2009 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2010 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01002011 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002012}
Damiend2755ec2013-10-16 23:58:48 +01002013
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002014STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002015 // perhaps the difficult one, as we want to rewrite for loops using native code
2016 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01002017
2018 vtype_kind_t vtype;
2019 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2020 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002021 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01002022 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002023}
Damiend2755ec2013-10-16 23:58:48 +01002024
Damien George7ff996c2014-09-08 23:05:16 +01002025STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002026 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002027 vtype_kind_t vtype;
2028 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
2029 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002030 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01002031 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
2032 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01002033 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2034}
2035
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002036STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01002037 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00002038 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002039 adjust_stack(emit, -1);
2040 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002041}
2042
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002043STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002044 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002045 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002046 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01002047 emit_post(emit);
2048}
2049
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002050STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002051 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01002052 /*
2053 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002054 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002055 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01002056 emit_post(emit);
2057 */
Damien13ed3a62013-10-08 09:05:10 +01002058}
2059
Damien Georged17926d2014-03-30 13:35:08 +01002060STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002061 vtype_kind_t vtype;
2062 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
Damien George9f5f1562015-10-08 13:08:59 +01002063 if (vtype == VTYPE_PYOBJ) {
Damien George9f5f1562015-10-08 13:08:59 +01002064 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
2065 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2066 } else {
2067 adjust_stack(emit, 1);
2068 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2069 "unary op %q not implemented", mp_unary_op_method_name[op]);
Damien Georgeb601d952014-06-30 05:17:25 +01002070 }
Damien13ed3a62013-10-08 09:05:10 +01002071}
2072
Damien Georged17926d2014-03-30 13:35:08 +01002073STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002074 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01002075 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
2076 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01002077 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01002078 #if N_X64 || N_X86
2079 // special cases for x86 and shifting
2080 if (op == MP_BINARY_OP_LSHIFT
2081 || op == MP_BINARY_OP_INPLACE_LSHIFT
2082 || op == MP_BINARY_OP_RSHIFT
2083 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2084 #if N_X64
2085 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
2086 #else
2087 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
2088 #endif
2089 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2090 ASM_LSL_REG(emit->as, REG_RET);
2091 } else {
2092 ASM_ASR_REG(emit->as, REG_RET);
2093 }
2094 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
2095 return;
2096 }
2097 #endif
2098 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002099 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002100 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
2101 if (0) {
2102 // dummy
2103 #if !(N_X64 || N_X86)
2104 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2105 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002106 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002107 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2108 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2109 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2110 #endif
Damien George1ef23482014-10-12 14:21:06 +01002111 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2112 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2113 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2114 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2115 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2116 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2117 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2118 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2119 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002120 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2121 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2122 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2123 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2124 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2125 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George567b3492015-06-04 14:00:29 +00002126 } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2127 ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2128 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002129 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2130 // comparison ops are (in enum order):
2131 // MP_BINARY_OP_LESS
2132 // MP_BINARY_OP_MORE
2133 // MP_BINARY_OP_EQUAL
2134 // MP_BINARY_OP_LESS_EQUAL
2135 // MP_BINARY_OP_MORE_EQUAL
2136 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002137 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002138 #if N_X64
2139 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2140 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2141 static byte ops[6] = {
2142 ASM_X64_CC_JL,
2143 ASM_X64_CC_JG,
2144 ASM_X64_CC_JE,
2145 ASM_X64_CC_JLE,
2146 ASM_X64_CC_JGE,
2147 ASM_X64_CC_JNE,
2148 };
2149 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2150 #elif N_X86
2151 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2152 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2153 static byte ops[6] = {
2154 ASM_X86_CC_JL,
2155 ASM_X86_CC_JG,
2156 ASM_X86_CC_JE,
2157 ASM_X86_CC_JLE,
2158 ASM_X86_CC_JGE,
2159 ASM_X86_CC_JNE,
2160 };
2161 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2162 #elif N_THUMB
2163 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2164 static uint16_t ops[6] = {
2165 ASM_THUMB_OP_ITE_GE,
2166 ASM_THUMB_OP_ITE_GT,
2167 ASM_THUMB_OP_ITE_EQ,
2168 ASM_THUMB_OP_ITE_GT,
2169 ASM_THUMB_OP_ITE_GE,
2170 ASM_THUMB_OP_ITE_EQ,
2171 };
2172 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2173 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2174 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2175 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2176 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002177 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2178 static uint ccs[6] = {
2179 ASM_ARM_CC_LT,
2180 ASM_ARM_CC_GT,
2181 ASM_ARM_CC_EQ,
2182 ASM_ARM_CC_LE,
2183 ASM_ARM_CC_GE,
2184 ASM_ARM_CC_NE,
2185 };
2186 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002187 #else
2188 #error not implemented
2189 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002190 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2191 } else {
2192 // TODO other ops not yet implemented
Damien George4d9cad12015-06-04 11:52:16 +01002193 adjust_stack(emit, 1);
2194 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2195 "binary op %q not implemented", mp_binary_op_method_name[op]);
Damien Georgebc1d3692014-01-11 09:47:06 +00002196 }
Damien13ed3a62013-10-08 09:05:10 +01002197 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002198 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002199 bool invert = false;
2200 if (op == MP_BINARY_OP_NOT_IN) {
2201 invert = true;
2202 op = MP_BINARY_OP_IN;
2203 } else if (op == MP_BINARY_OP_IS_NOT) {
2204 invert = true;
2205 op = MP_BINARY_OP_IS;
2206 }
Damien George7fe21912014-08-16 22:31:57 +01002207 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002208 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002209 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002210 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2211 }
Damien13ed3a62013-10-08 09:05:10 +01002212 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2213 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002214 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002215 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2216 "can't do binary op between '%q' and '%q'",
2217 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002218 }
2219}
2220
Damien George7ff996c2014-09-08 23:05:16 +01002221STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002222 // for viper: call runtime, with types of args
2223 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002224 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002225 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002226 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002227 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002228}
2229
Damien George7ff996c2014-09-08 23:05:16 +01002230STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002231 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002232 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002233 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002234 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2235}
2236
Damien George7ff996c2014-09-08 23:05:16 +01002237STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002238 // only used in list comprehension
2239 vtype_kind_t vtype_list, vtype_item;
2240 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2241 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2242 assert(vtype_list == VTYPE_PYOBJ);
2243 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002244 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002245 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002246}
2247
Damien George7ff996c2014-09-08 23:05:16 +01002248STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002249 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002250 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002251 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2252}
2253
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002254STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002255 vtype_kind_t vtype_key, vtype_value, vtype_map;
2256 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
2257 assert(vtype_key == VTYPE_PYOBJ);
2258 assert(vtype_value == VTYPE_PYOBJ);
2259 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002260 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002261 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2262}
2263
Damien George7ff996c2014-09-08 23:05:16 +01002264STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002265 // only used in list comprehension
2266 vtype_kind_t vtype_map, vtype_key, vtype_value;
2267 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2268 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2269 assert(vtype_map == VTYPE_PYOBJ);
2270 assert(vtype_key == VTYPE_PYOBJ);
2271 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002272 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002273 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002274}
2275
Damien Georgee37dcaa2014-12-27 17:07:16 +00002276#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002277STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002278 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002279 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002280 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002281 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2282}
2283
Damien George7ff996c2014-09-08 23:05:16 +01002284STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002285 // only used in set comprehension
2286 vtype_kind_t vtype_set, vtype_item;
2287 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2288 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2289 assert(vtype_set == VTYPE_PYOBJ);
2290 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002291 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002292 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002293}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002294#endif
Damiend2755ec2013-10-16 23:58:48 +01002295
Damien George83204f32014-12-27 17:20:41 +00002296#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002297STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002298 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002299 if (n_args == 2) {
2300 vtype_kind_t vtype_start, vtype_stop;
2301 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2302 assert(vtype_start == VTYPE_PYOBJ);
2303 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002304 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 +01002305 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2306 } else {
2307 assert(n_args == 3);
2308 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2309 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
2310 assert(vtype_start == VTYPE_PYOBJ);
2311 assert(vtype_stop == VTYPE_PYOBJ);
2312 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002313 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002314 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2315 }
Damien13ed3a62013-10-08 09:05:10 +01002316}
Damien George83204f32014-12-27 17:20:41 +00002317#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002318
Damien George7ff996c2014-09-08 23:05:16 +01002319STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002320 DEBUG_printf("unpack_sequence %d\n", n_args);
2321 vtype_kind_t vtype_base;
2322 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2323 assert(vtype_base == VTYPE_PYOBJ);
2324 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002325 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002326}
Damien Georgecdd96df2014-04-06 12:58:40 +01002327
Damien George7ff996c2014-09-08 23:05:16 +01002328STATIC 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 +01002329 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2330 vtype_kind_t vtype_base;
2331 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2332 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002333 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 +01002334 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 +01002335}
2336
Damien George7ff996c2014-09-08 23:05:16 +01002337STATIC 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 +01002338 // 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 +00002339 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002340 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002341 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 +01002342 } else {
2343 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2344 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2345 assert(vtype_def_tuple == VTYPE_PYOBJ);
2346 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002347 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 +01002348 }
Damien13ed3a62013-10-08 09:05:10 +01002349 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2350}
2351
Damien George7ff996c2014-09-08 23:05:16 +01002352STATIC 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 +00002353 emit_native_pre(emit);
2354 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2355 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2356 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2357 } else {
2358 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2359 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2360 }
2361 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2362 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2363 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002364}
2365
Damien George7ff996c2014-09-08 23:05:16 +01002366STATIC 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 +00002367 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2368
Damien Georgee9dac3b2014-09-29 22:10:41 +01002369 // TODO: in viper mode, call special runtime routine with type info for args,
2370 // and wanted type info for return, to remove need for boxing/unboxing
2371
Damien Georgee9dac3b2014-09-29 22:10:41 +01002372 emit_native_pre(emit);
2373 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2374 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2375 // casting operator
2376 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002377 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002378 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002379 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002380 switch (peek_vtype(emit, 0)) {
2381 case VTYPE_PYOBJ: {
2382 vtype_kind_t vtype;
2383 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2384 emit_pre_pop_discard(emit);
2385 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2386 emit_post_push_reg(emit, vtype_cast, REG_RET);
2387 break;
2388 }
2389 case VTYPE_BOOL:
2390 case VTYPE_INT:
2391 case VTYPE_UINT:
2392 case VTYPE_PTR:
2393 case VTYPE_PTR8:
2394 case VTYPE_PTR16:
Damien Georgeb8f9ac52015-10-13 00:50:17 +01002395 case VTYPE_PTR32:
Damien Georgee9dac3b2014-09-29 22:10:41 +01002396 case VTYPE_PTR_NONE:
2397 emit_fold_stack_top(emit, REG_ARG_1);
2398 emit_post_top_set_vtype(emit, vtype_cast);
2399 break;
2400 default:
2401 assert(!"TODO: convert obj to int");
2402 }
2403 } else {
Damien13ed3a62013-10-08 09:05:10 +01002404 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002405 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002406 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2407 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);
2408 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2409 } else {
2410 if (n_positional != 0 || n_keyword != 0) {
2411 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2412 }
2413 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2414 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2415 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2416 }
Damien Georgecd82e022014-02-02 13:11:48 +00002417 }
Damien13ed3a62013-10-08 09:05:10 +01002418}
2419
Damien George7ff996c2014-09-08 23:05:16 +01002420STATIC 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 +01002421 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002422 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2423 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);
2424 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2425 } else {
2426 emit_native_pre(emit);
2427 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
2428 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2429 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2430 }
Damien13ed3a62013-10-08 09:05:10 +01002431}
2432
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002433STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002434 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002435 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002436 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2437 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002438 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002439 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002440 } else {
2441 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002442 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002443 } else {
2444 vtype_kind_t vtype;
2445 emit_pre_pop_reg(emit, &vtype, REG_RET);
2446 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002447 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2448 "return expected '%q' but got '%q'",
2449 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002450 }
Damien George2ac4af62014-08-15 16:45:41 +01002451 }
Damien13ed3a62013-10-08 09:05:10 +01002452 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002453 vtype_kind_t vtype;
2454 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002455 assert(vtype == VTYPE_PYOBJ);
2456 }
2457 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002458 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2459 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002460}
2461
Damien George7ff996c2014-09-08 23:05:16 +01002462STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002463 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002464 vtype_kind_t vtype_exc;
2465 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2466 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002467 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002468 }
2469 // 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 +01002470 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002471}
Damien Georgeb601d952014-06-30 05:17:25 +01002472
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002473STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002474 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002475 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002476 assert(0);
2477}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002478STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002479 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002480 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002481 assert(0);
2482}
2483
Damien Georgeb601d952014-06-30 05:17:25 +01002484STATIC void emit_native_start_except_handler(emit_t *emit) {
2485 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2486 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2487 // the first 2 elements, so we can get the thrown value.
2488 adjust_stack(emit, 2);
2489 vtype_kind_t vtype_nlr;
2490 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002491 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002492 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
2493}
2494
2495STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002496 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002497}
2498
Damien13ed3a62013-10-08 09:05:10 +01002499const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002500 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002501 emit_native_start_pass,
2502 emit_native_end_pass,
2503 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002504 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002505 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002506
Damien George542bd6b2015-03-26 14:42:40 +00002507 {
2508 emit_native_load_fast,
2509 emit_native_load_deref,
2510 emit_native_load_name,
2511 emit_native_load_global,
2512 },
2513 {
2514 emit_native_store_fast,
2515 emit_native_store_deref,
2516 emit_native_store_name,
2517 emit_native_store_global,
2518 },
2519 {
2520 emit_native_delete_fast,
2521 emit_native_delete_deref,
2522 emit_native_delete_name,
2523 emit_native_delete_global,
2524 },
Damien13ed3a62013-10-08 09:05:10 +01002525
2526 emit_native_label_assign,
2527 emit_native_import_name,
2528 emit_native_import_from,
2529 emit_native_import_star,
2530 emit_native_load_const_tok,
2531 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002532 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002533 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002534 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002535 emit_native_load_attr,
2536 emit_native_load_method,
2537 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002538 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002539 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002540 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002541 emit_native_delete_attr,
2542 emit_native_delete_subscr,
2543 emit_native_dup_top,
2544 emit_native_dup_top_two,
2545 emit_native_pop_top,
2546 emit_native_rot_two,
2547 emit_native_rot_three,
2548 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002549 emit_native_pop_jump_if,
2550 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002551 emit_native_break_loop,
2552 emit_native_continue_loop,
2553 emit_native_setup_with,
2554 emit_native_with_cleanup,
2555 emit_native_setup_except,
2556 emit_native_setup_finally,
2557 emit_native_end_finally,
2558 emit_native_get_iter,
2559 emit_native_for_iter,
2560 emit_native_for_iter_end,
2561 emit_native_pop_block,
2562 emit_native_pop_except,
2563 emit_native_unary_op,
2564 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002565 emit_native_build_tuple,
2566 emit_native_build_list,
2567 emit_native_list_append,
2568 emit_native_build_map,
2569 emit_native_store_map,
2570 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002571 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002572 emit_native_build_set,
2573 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002574 #endif
Damien George83204f32014-12-27 17:20:41 +00002575 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002576 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002577 #endif
Damien13ed3a62013-10-08 09:05:10 +01002578 emit_native_unpack_sequence,
2579 emit_native_unpack_ex,
2580 emit_native_make_function,
2581 emit_native_make_closure,
2582 emit_native_call_function,
2583 emit_native_call_method,
2584 emit_native_return_value,
2585 emit_native_raise_varargs,
2586 emit_native_yield_value,
2587 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002588
2589 emit_native_start_except_handler,
2590 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002591};
2592
Damien Georgec90f59e2014-09-06 23:06:36 +01002593#endif