blob: 99eac79253528855b4ddefea47a139b31117aefe [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;
570 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100571 int stack_start;
572 int stack_size;
573
574 bool last_emit_was_return_value;
575
Damien13ed3a62013-10-08 09:05:10 +0100576 scope_t *scope;
577
Damien Georgec90f59e2014-09-06 23:06:36 +0100578 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100579};
580
Damien Georgec8b60f02015-04-20 13:29:31 +0000581emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100582 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000583 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100584 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100585 return emit;
586}
587
Damien George41d02b62014-01-24 22:42:28 +0000588void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100589 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100590 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
591 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200592 m_del_obj(emit_t, emit);
593}
594
Damien George2ac4af62014-08-15 16:45:41 +0100595STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
596 switch (op) {
597 case MP_EMIT_NATIVE_TYPE_ENABLE:
598 emit->do_viper_types = arg1;
599 break;
600
601 default: {
602 vtype_kind_t type;
603 switch (arg2) {
604 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
605 case MP_QSTR_bool: type = VTYPE_BOOL; break;
606 case MP_QSTR_int: type = VTYPE_INT; break;
607 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100608 case MP_QSTR_ptr: type = VTYPE_PTR; break;
609 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
610 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgeb8f9ac52015-10-13 00:50:17 +0100611 case MP_QSTR_ptr32: type = VTYPE_PTR32; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000612 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100613 }
614 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
615 emit->return_vtype = type;
616 } else {
617 assert(arg1 < emit->local_vtype_alloc);
618 emit->local_vtype[arg1] = type;
619 }
620 break;
621 }
622 }
Damien13ed3a62013-10-08 09:05:10 +0100623}
624
Damien Georgefa5950e2015-04-03 15:03:24 +0000625STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000626STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000627STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000628STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000629
Damien George99886182015-04-06 22:38:53 +0100630#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
631
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200632STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000633 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
634
Damien13ed3a62013-10-08 09:05:10 +0100635 emit->pass = pass;
636 emit->stack_start = 0;
637 emit->stack_size = 0;
638 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100639 emit->scope = scope;
640
Damien George36db6bc2014-05-07 17:24:22 +0100641 // allocate memory for keeping track of the types of locals
642 if (emit->local_vtype_alloc < scope->num_locals) {
643 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
644 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100645 }
Damien George36db6bc2014-05-07 17:24:22 +0100646
647 // allocate memory for keeping track of the objects on the stack
648 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000649 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100650 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000651 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100652 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100653 }
654
Damien George99886182015-04-06 22:38:53 +0100655 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100656 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100657
658 // set default type for arguments
659 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
660 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
661 num_args += 1;
662 }
663 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
664 num_args += 1;
665 }
666 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100667 emit->local_vtype[i] = VTYPE_PYOBJ;
668 }
Damien Georgea5190a72014-08-15 22:39:08 +0100669
670 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100671 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100672 emit->local_vtype[i] = VTYPE_UNBOUND;
673 }
674
675 // values on stack begin unbound
676 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100677 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100678 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100679 }
680
Damien Georgec90f59e2014-09-06 23:06:36 +0100681 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100682
Damien George99886182015-04-06 22:38:53 +0100683 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100684
Damien George99886182015-04-06 22:38:53 +0100685 if (emit->do_viper_types) {
686
Damien Georgef17e6632015-07-23 14:30:37 +0100687 // right now we have a restriction of maximum of 4 arguments
688 if (scope->num_pos_args >= 5) {
Damien George84d59c22015-07-27 22:20:00 +0100689 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
Damien Georgef17e6632015-07-23 14:30:37 +0100690 return;
691 }
692
Damien George99886182015-04-06 22:38:53 +0100693 // entry to function
694 int num_locals = 0;
695 if (pass > MP_PASS_SCOPE) {
696 num_locals = scope->num_locals - REG_LOCAL_NUM;
697 if (num_locals < 0) {
698 num_locals = 0;
699 }
700 emit->stack_start = num_locals;
701 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100702 }
Damien George99886182015-04-06 22:38:53 +0100703 ASM_ENTRY(emit->as, num_locals);
704
Damien Georgec39093d2015-08-12 23:31:19 +0100705 // TODO don't load r7 if we don't need it
706 #if N_THUMB
707 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
708 #elif N_ARM
709 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
710 #endif
711
Damien George99886182015-04-06 22:38:53 +0100712 #if N_X86
713 for (int i = 0; i < scope->num_pos_args; i++) {
714 if (i == 0) {
715 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
716 } else if (i == 1) {
717 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
718 } else if (i == 2) {
719 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
720 } else {
721 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
722 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
723 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100724 }
Damien George99886182015-04-06 22:38:53 +0100725 #else
726 for (int i = 0; i < scope->num_pos_args; i++) {
727 if (i == 0) {
728 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
729 } else if (i == 1) {
730 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
731 } else if (i == 2) {
732 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
733 } else if (i == 3) {
734 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
735 } else {
736 // TODO not implemented
737 assert(0);
738 }
739 }
740 #endif
741
742 } else {
743 // work out size of state (locals plus stack)
744 emit->n_state = scope->num_locals + scope->stack_size;
745
746 // allocate space on C-stack for code_state structure, which includes state
747 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
748
Damien Georgec39093d2015-08-12 23:31:19 +0100749 // TODO don't load r7 if we don't need it
750 #if N_THUMB
751 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
752 #elif N_ARM
753 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
754 #endif
755
Damien George99886182015-04-06 22:38:53 +0100756 // prepare incoming arguments for call to mp_setup_code_state
757 #if N_X86
758 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
759 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
760 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
761 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
762 #else
763 #if N_THUMB
764 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300765 #elif N_ARM
766 ASM_MOV_REG_REG(emit->as, ASM_ARM_REG_R4, REG_ARG_4);
Damien George99886182015-04-06 22:38:53 +0100767 #else
768 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
769 #endif
770 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
771 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
772 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
773 #endif
774
Damien George99886182015-04-06 22:38:53 +0100775 // set code_state.ip (offset from start of this function to prelude info)
776 // XXX this encoding may change size
777 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
778
779 // set code_state.n_state
780 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
781
782 // put address of code_state into first arg
783 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
784
785 // call mp_setup_code_state to prepare code_state structure
786 #if N_THUMB
787 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
788 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
789 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300790 #elif N_ARM
791 asm_arm_push(emit->as, 1 << ASM_ARM_REG_R4); // push 5th arg
792 asm_arm_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
793 asm_arm_pop(emit->as, 1 << REG_RET); // pop dummy (was 5th arg)
Damien George99886182015-04-06 22:38:53 +0100794 #else
795 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
796 #endif
797
798 // cache some locals in registers
799 if (scope->num_locals > 0) {
800 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
801 if (scope->num_locals > 1) {
802 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
803 if (scope->num_locals > 2) {
804 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
805 }
806 }
807 }
808
809 // set the type of closed over variables
810 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
811 id_info_t *id = &scope->id_info[i];
812 if (id->kind == ID_INFO_KIND_CELL) {
813 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
814 }
Damien13ed3a62013-10-08 09:05:10 +0100815 }
816 }
817
Damien13ed3a62013-10-08 09:05:10 +0100818}
819
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200820STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100821 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100822 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100823 }
Damien George99886182015-04-06 22:38:53 +0100824
825 if (!emit->do_viper_types) {
Damien George9b7f5832015-03-18 17:47:47 +0000826 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
Damien George3a3db4d2015-10-22 23:45:37 +0100827 ASM_DATA(emit->as, 1, emit->scope->scope_flags);
828 ASM_DATA(emit->as, 1, emit->scope->num_pos_args);
829 ASM_DATA(emit->as, 1, emit->scope->num_kwonly_args);
830 ASM_DATA(emit->as, 1, emit->scope->num_def_pos_args);
Damien George99886182015-04-06 22:38:53 +0100831 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
Damien George9b7f5832015-03-18 17:47:47 +0000832
833 // write argument names as qstr objects
Damien George9a42eb52015-05-06 13:55:33 +0100834 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100835 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100836 qstr qst = MP_QSTR__star_;
837 for (int j = 0; j < emit->scope->id_info_len; ++j) {
838 id_info_t *id = &emit->scope->id_info[j];
839 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
840 qst = id->qst;
841 break;
842 }
843 }
844 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100845 }
846
Damien George9b7f5832015-03-18 17:47:47 +0000847 // write dummy code info (for mp_setup_code_state to parse)
848 ASM_DATA(emit->as, 1, 1);
849
Damien George99886182015-04-06 22:38:53 +0100850 // bytecode prelude: initialise closed over variables
Damien George99886182015-04-06 22:38:53 +0100851 for (int i = 0; i < emit->scope->id_info_len; i++) {
852 id_info_t *id = &emit->scope->id_info[i];
853 if (id->kind == ID_INFO_KIND_CELL) {
854 assert(id->local_num < 255);
855 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
856 }
857 }
858 ASM_DATA(emit->as, 1, 255); // end of list sentinel
859 }
860
Damien Georgec90f59e2014-09-06 23:06:36 +0100861 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100862
863 // check stack is back to zero size
864 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100865 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100866 }
867
Damien George36db6bc2014-05-07 17:24:22 +0100868 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100869 void *f = ASM_GET_CODE(emit->as);
870 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100871
872 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100873 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100874 mp_uint_t type_sig = emit->return_vtype & 3;
875 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
876 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
877 }
878
Damien George99886182015-04-06 22:38:53 +0100879 mp_emit_glue_assign_native(emit->scope->raw_code,
880 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
Damien George3a3db4d2015-10-22 23:45:37 +0100881 f, f_len, emit->scope->num_pos_args, emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100882 }
883}
884
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200885STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100886 return emit->last_emit_was_return_value;
887}
888
Damien Georged6230f62014-09-23 14:10:03 +0000889STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000890 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100891 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100892 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100893 emit->scope->stack_size = emit->stack_size;
894 }
Damien George21ca2d72014-10-19 19:00:51 +0100895#ifdef DEBUG_PRINT
896 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
897 for (int i = 0; i < emit->stack_size; i++) {
898 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000899 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100900 }
901 DEBUG_printf("\n");
902#endif
Damien13ed3a62013-10-08 09:05:10 +0100903}
904
Damien Georged6230f62014-09-23 14:10:03 +0000905STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100906 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000907 // If we are adjusting the stack in a positive direction (pushing) then we
908 // need to fill in values for the stack kind and vtype of the newly-pushed
909 // entries. These should be set to "value" (ie not reg or imm) because we
910 // should only need to adjust the stack due to a jump to this part in the
911 // code (and hence we have settled the stack before the jump).
912 for (mp_int_t i = 0; i < delta; i++) {
913 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
914 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100915 // TODO we don't know the vtype to use here. At the moment this is a
916 // hack to get the case of multi comparison working.
917 if (delta == 1) {
918 si->vtype = emit->saved_stack_vtype;
919 } else {
920 si->vtype = VTYPE_PYOBJ;
921 }
Damien Georged6230f62014-09-23 14:10:03 +0000922 }
923 adjust_stack(emit, delta);
924}
925
926STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000927 (void)emit;
928 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000929}
930
Damienff8ed772013-10-08 22:18:32 +0100931/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200932STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100933 adjust_stack(emit, stack_size_delta);
934 emit->last_emit_was_return_value = false;
935}
Damienff8ed772013-10-08 22:18:32 +0100936*/
Damien13ed3a62013-10-08 09:05:10 +0100937
Damienff8ed772013-10-08 22:18:32 +0100938// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000939STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100940 emit->last_emit_was_return_value = false;
941 // settle the stack
942 /*
943 if (regs_needed != 0) {
944 for (int i = 0; i < emit->stack_size; i++) {
945 switch (emit->stack_info[i].kind) {
946 case STACK_VALUE:
947 break;
948
949 case STACK_REG:
950 // TODO only push reg if in regs_needed
951 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000952 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100953 break;
954
955 case STACK_IMM:
956 // don't think we ever need to push imms for settling
957 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
958 break;
959 }
960 }
961 }
962 */
Damien13ed3a62013-10-08 09:05:10 +0100963}
964
Damien George3112cde2014-09-29 18:45:42 +0100965// depth==0 is top, depth==1 is before top, etc
966STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
967 return &emit->stack_info[emit->stack_size - 1 - depth];
968}
969
970// depth==0 is top, depth==1 is before top, etc
971STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
972 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100973}
Damien13ed3a62013-10-08 09:05:10 +0100974
Damiend2755ec2013-10-16 23:58:48 +0100975// pos=1 is TOS, pos=2 is next, etc
976// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200977STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100978 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100979 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100980 if (i != skip_stack_pos) {
981 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000982 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100983 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000984 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100985 }
Damienff8ed772013-10-08 22:18:32 +0100986 }
987 }
988}
Damien13ed3a62013-10-08 09:05:10 +0100989
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200990STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100991 for (int i = 0; i < emit->stack_size; i++) {
992 stack_info_t *si = &emit->stack_info[i];
993 if (si->kind == STACK_REG) {
994 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000995 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100996 }
Damien13ed3a62013-10-08 09:05:10 +0100997 }
998}
999
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001000STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001001 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +01001002 for (int i = 0; i < emit->stack_size; i++) {
1003 stack_info_t *si = &emit->stack_info[i];
1004 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +00001005 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001006 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001007 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +01001008 }
1009 }
1010 for (int i = 0; i < emit->stack_size; i++) {
1011 stack_info_t *si = &emit->stack_info[i];
1012 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +00001013 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +01001014 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001015 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +01001016 }
1017 }
1018}
1019
1020// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001021STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001022 need_reg_single(emit, reg_dest, pos);
1023 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001024 *vtype = si->vtype;
1025 switch (si->kind) {
1026 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001027 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001028 break;
1029
Damienff8ed772013-10-08 22:18:32 +01001030 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001031 if (si->data.u_reg != reg_dest) {
1032 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001033 }
1034 break;
1035
Damienff8ed772013-10-08 22:18:32 +01001036 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001037 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001038 break;
1039 }
Damien13ed3a62013-10-08 09:05:10 +01001040}
1041
Damien Georgee9dac3b2014-09-29 22:10:41 +01001042// does an efficient X=pop(); discard(); push(X)
1043// needs a (non-temp) register in case the poped element was stored in the stack
1044STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1045 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1046 si[0] = si[1];
1047 if (si->kind == STACK_VALUE) {
1048 // if folded element was on the stack we need to put it in a register
1049 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1050 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001051 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001052 }
1053 adjust_stack(emit, -1);
1054}
1055
1056// If stacked value is in a register and the register is not r1 or r2, then
1057// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1058STATIC 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 +01001059 emit->last_emit_was_return_value = false;
1060 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001061 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001062 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001063 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001064 need_reg_single(emit, *reg_dest, 1);
1065 } else {
1066 emit_access_stack(emit, 1, vtype, *reg_dest);
1067 }
1068 adjust_stack(emit, -1);
1069}
1070
Damien Georgee6ce10a2014-09-06 18:38:20 +01001071STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001072 emit->last_emit_was_return_value = false;
1073 adjust_stack(emit, -1);
1074}
1075
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001076STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001077 emit->last_emit_was_return_value = false;
1078 emit_access_stack(emit, 1, vtype, reg_dest);
1079 adjust_stack(emit, -1);
1080}
1081
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001082STATIC 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 +01001083 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001084 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001085}
1086
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001087STATIC 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 +01001088 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001089 emit_pre_pop_reg(emit, vtypeb, regb);
1090 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001091}
1092
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001093STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001094 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001095}
1096
Damien Georgee9dac3b2014-09-29 22:10:41 +01001097STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1098 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1099 si->vtype = new_vtype;
1100}
1101
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001102STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001103 stack_info_t *si = &emit->stack_info[emit->stack_size];
1104 si->vtype = vtype;
1105 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001106 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001107 adjust_stack(emit, 1);
1108}
1109
Damien George40f3c022014-07-03 13:25:24 +01001110STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001111 stack_info_t *si = &emit->stack_info[emit->stack_size];
1112 si->vtype = vtype;
1113 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001114 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001115 adjust_stack(emit, 1);
1116}
1117
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001118STATIC 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 +01001119 emit_post_push_reg(emit, vtypea, rega);
1120 emit_post_push_reg(emit, vtypeb, regb);
1121}
1122
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001123STATIC 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 +01001124 emit_post_push_reg(emit, vtypea, rega);
1125 emit_post_push_reg(emit, vtypeb, regb);
1126 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001127}
1128
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001129STATIC 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 +01001130 emit_post_push_reg(emit, vtypea, rega);
1131 emit_post_push_reg(emit, vtypeb, regb);
1132 emit_post_push_reg(emit, vtypec, regc);
1133 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001134}
1135
Damien George7fe21912014-08-16 22:31:57 +01001136STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001137 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001138 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001139}
1140
Damien George7fe21912014-08-16 22:31:57 +01001141STATIC 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 +01001142 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001143 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1144 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001145}
1146
Damien George40f3c022014-07-03 13:25:24 +01001147// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001148STATIC 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 +01001149 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001150 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1151 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001152}
1153
Damien George7fe21912014-08-16 22:31:57 +01001154STATIC 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 +00001155 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001156 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1157 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1158 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001159}
1160
Damien George40f3c022014-07-03 13:25:24 +01001161// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001162STATIC 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 +01001163 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001164 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1165 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1166 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1167 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001168}
1169
Damien George86de21b2014-08-16 22:06:11 +01001170// vtype of all n_pop objects is VTYPE_PYOBJ
1171// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1172// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1173// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1174STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1175 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001176
Damien George86de21b2014-08-16 22:06:11 +01001177 // First, store any immediate values to their respective place on the stack.
1178 for (mp_uint_t i = 0; i < n_pop; i++) {
1179 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1180 // must push any imm's to stack
1181 // must convert them to VTYPE_PYOBJ for viper code
1182 if (si->kind == STACK_IMM) {
1183 si->kind = STACK_VALUE;
1184 switch (si->vtype) {
1185 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001186 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 +01001187 break;
1188 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001189 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001190 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 +01001191 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001192 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 +01001193 }
1194 si->vtype = VTYPE_PYOBJ;
1195 break;
1196 case VTYPE_INT:
1197 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001198 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 +01001199 si->vtype = VTYPE_PYOBJ;
1200 break;
1201 default:
1202 // not handled
1203 assert(0);
1204 }
1205 }
1206
1207 // verify that this value is on the stack
1208 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001209 }
Damien George86de21b2014-08-16 22:06:11 +01001210
1211 // Second, convert any non-VTYPE_PYOBJ to that type.
1212 for (mp_uint_t i = 0; i < n_pop; i++) {
1213 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1214 if (si->vtype != VTYPE_PYOBJ) {
1215 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001216 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001217 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 +01001218 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001219 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001220 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001221 }
1222 }
1223
1224 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1225 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001226 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001227}
1228
1229// vtype of all n_push objects is VTYPE_PYOBJ
1230STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1231 need_reg_all(emit);
1232 for (mp_uint_t i = 0; i < n_push; i++) {
1233 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1234 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1235 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001236 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001237 adjust_stack(emit, n_push);
1238}
1239
Damien George7ff996c2014-09-08 23:05:16 +01001240STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001241 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001242 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001243 // need to commit stack because we can jump here from elsewhere
1244 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001245 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001246 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001247}
1248
Damien Georgecdd96df2014-04-06 12:58:40 +01001249STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1250 DEBUG_printf("import_name %s\n", qstr_str(qst));
1251 vtype_kind_t vtype_fromlist;
1252 vtype_kind_t vtype_level;
1253 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1254 assert(vtype_fromlist == VTYPE_PYOBJ);
1255 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001256 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001257 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001258}
1259
Damien Georgecdd96df2014-04-06 12:58:40 +01001260STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1261 DEBUG_printf("import_from %s\n", qstr_str(qst));
1262 emit_native_pre(emit);
1263 vtype_kind_t vtype_module;
1264 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1265 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001266 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001267 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001268}
1269
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001270STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001271 DEBUG_printf("import_star\n");
1272 vtype_kind_t vtype_module;
1273 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1274 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001275 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001276 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001277}
1278
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001279STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001280 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001281 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001282 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001283 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001284 if (emit->do_viper_types) {
1285 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001286 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1287 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1288 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001289 no_other_choice1:
1290 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1291 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001292 }
1293 } else {
1294 vtype = VTYPE_PYOBJ;
1295 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001296 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1297 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1298 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001299 no_other_choice2:
1300 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1301 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001302 }
1303 }
1304 emit_post_push_imm(emit, vtype, val);
1305}
1306
Damien George40f3c022014-07-03 13:25:24 +01001307STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001308 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001309 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001310 if (emit->do_viper_types) {
1311 emit_post_push_imm(emit, VTYPE_INT, arg);
1312 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001313 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001314 }
1315}
1316
Damien George59fba2d2015-06-25 14:42:13 +00001317STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001318 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001319 // TODO: Eventually we want to be able to work with raw pointers in viper to
1320 // do native array access. For now we just load them as any other object.
1321 /*
Damien13ed3a62013-10-08 09:05:10 +01001322 if (emit->do_viper_types) {
1323 // not implemented properly
1324 // load a pointer to the asciiz string?
1325 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001326 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001327 } else
1328 */
1329 {
Damien George59fba2d2015-06-25 14:42:13 +00001330 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien13ed3a62013-10-08 09:05:10 +01001331 }
1332}
1333
Damien Georgedab13852015-01-13 15:55:54 +00001334STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1335 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001336 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001337 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1338 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1339}
1340
Damien George3558f622014-04-20 17:50:40 +01001341STATIC void emit_native_load_null(emit_t *emit) {
1342 emit_native_pre(emit);
1343 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1344}
1345
Damien George0abb5602015-01-16 12:24:49 +00001346STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1347 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001348 vtype_kind_t vtype = emit->local_vtype[local_num];
1349 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001350 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001351 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001352 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001353 if (local_num == 0) {
1354 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001355 } else if (local_num == 1) {
1356 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1357 } else if (local_num == 2) {
1358 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001359 } else {
Damien George0b610de2014-09-29 16:25:04 +01001360 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001361 if (emit->do_viper_types) {
1362 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1363 } else {
1364 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1365 }
Damien George0b610de2014-09-29 16:25:04 +01001366 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001367 }
Damien13ed3a62013-10-08 09:05:10 +01001368}
1369
Damien George7ff996c2014-09-08 23:05:16 +01001370STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001371 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1372 need_reg_single(emit, REG_RET, 0);
1373 emit_native_load_fast(emit, qst, local_num);
1374 vtype_kind_t vtype;
1375 int reg_base = REG_RET;
1376 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1377 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1378 // closed over vars are always Python objects
1379 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001380}
1381
Damien George7ff996c2014-09-08 23:05:16 +01001382STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001383 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001384 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001385 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001386 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1387}
1388
Damien George7ff996c2014-09-08 23:05:16 +01001389STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001390 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001391 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001392 // check for builtin casting operators
1393 if (emit->do_viper_types && qst == MP_QSTR_int) {
1394 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1395 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1396 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1397 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1398 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1399 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1400 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1401 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1402 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001403 } else if (emit->do_viper_types && qst == MP_QSTR_ptr32) {
1404 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR32);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001405 } else {
1406 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1407 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1408 }
Damien13ed3a62013-10-08 09:05:10 +01001409}
1410
Damien George7ff996c2014-09-08 23:05:16 +01001411STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001412 // depends on type of subject:
1413 // - integer, function, pointer to integers: error
1414 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001415 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001416 vtype_kind_t vtype_base;
1417 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1418 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001419 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001420 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1421}
1422
Damien George7ff996c2014-09-08 23:05:16 +01001423STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001424 vtype_kind_t vtype_base;
1425 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1426 assert(vtype_base == VTYPE_PYOBJ);
1427 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001428 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001429}
1430
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001431STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001432 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001433 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001434 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001435}
1436
Damien George729f7b42014-04-17 22:10:53 +01001437STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001438 DEBUG_printf("load_subscr\n");
1439 // need to compile: base[index]
1440
1441 // pop: index, base
1442 // optimise case where index is an immediate
1443 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1444
1445 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001446 // standard Python subscr
1447 // TODO factor this implicit cast code with other uses of it
1448 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1449 if (vtype_index == VTYPE_PYOBJ) {
1450 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
1451 } else {
1452 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
1453 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
1454 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1455 }
1456 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001457 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 +01001458 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1459 } else {
Damien George91cfd412014-10-12 16:59:29 +01001460 // viper load
1461 // TODO The different machine architectures have very different
1462 // capabilities and requirements for loads, so probably best to
1463 // write a completely separate load-optimiser for each one.
1464 stack_info_t *top = peek_stack(emit, 0);
1465 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1466 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001467 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001468 emit_pre_pop_discard(emit); // discard index
1469 int reg_base = REG_ARG_1;
1470 int reg_index = REG_ARG_2;
1471 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1472 switch (vtype_base) {
1473 case VTYPE_PTR8: {
1474 // pointer to 8-bit memory
1475 // TODO optimise to use thumb ldrb r1, [r2, r3]
1476 if (index_value != 0) {
1477 // index is non-zero
1478 #if N_THUMB
1479 if (index_value > 0 && index_value < 32) {
1480 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1481 break;
1482 }
1483 #endif
1484 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1485 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1486 reg_base = reg_index;
1487 }
1488 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1489 break;
1490 }
1491 case VTYPE_PTR16: {
1492 // pointer to 16-bit memory
1493 if (index_value != 0) {
1494 // index is a non-zero immediate
1495 #if N_THUMB
1496 if (index_value > 0 && index_value < 32) {
1497 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1498 break;
1499 }
1500 #endif
1501 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1502 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1503 reg_base = reg_index;
1504 }
1505 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1506 break;
1507 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001508 case VTYPE_PTR32: {
1509 // pointer to 32-bit memory
1510 if (index_value != 0) {
1511 // index is a non-zero immediate
1512 #if N_THUMB
1513 if (index_value > 0 && index_value < 32) {
1514 asm_thumb_ldr_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1515 break;
1516 }
1517 #endif
1518 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1519 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1520 reg_base = reg_index;
1521 }
1522 ASM_LOAD32_REG_REG(emit->as, REG_RET, reg_base); // load from (base+4*index)
1523 break;
1524 }
Damien George91cfd412014-10-12 16:59:29 +01001525 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001526 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1527 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001528 }
1529 } else {
1530 // index is not an immediate
1531 vtype_kind_t vtype_index;
1532 int reg_index = REG_ARG_2;
1533 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1534 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1535 switch (vtype_base) {
1536 case VTYPE_PTR8: {
1537 // pointer to 8-bit memory
1538 // TODO optimise to use thumb ldrb r1, [r2, r3]
1539 assert(vtype_index == VTYPE_INT);
1540 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1541 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1542 break;
1543 }
1544 case VTYPE_PTR16: {
1545 // pointer to 16-bit memory
1546 assert(vtype_index == VTYPE_INT);
1547 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1548 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1549 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1550 break;
1551 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001552 case VTYPE_PTR32: {
1553 // pointer to word-size memory
1554 assert(vtype_index == VTYPE_INT);
1555 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1556 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1557 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1558 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1559 ASM_LOAD32_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+4*index)
1560 break;
1561 }
Damien George91cfd412014-10-12 16:59:29 +01001562 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001563 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1564 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001565 }
1566 }
1567 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001568 }
1569}
1570
Damien George7ff996c2014-09-08 23:05:16 +01001571STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001572 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001573 if (local_num == 0) {
1574 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001575 } else if (local_num == 1) {
1576 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1577 } else if (local_num == 2) {
1578 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001579 } else {
Damien George0b610de2014-09-29 16:25:04 +01001580 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001581 if (emit->do_viper_types) {
1582 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1583 } else {
1584 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1585 }
Damien13ed3a62013-10-08 09:05:10 +01001586 }
Damien13ed3a62013-10-08 09:05:10 +01001587 emit_post(emit);
1588
1589 // check types
1590 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1591 // first time this local is assigned, so give it a type of the object stored in it
1592 emit->local_vtype[local_num] = vtype;
1593 } else if (emit->local_vtype[local_num] != vtype) {
1594 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001595 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1596 "local '%q' has type '%q' but source is '%q'",
1597 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001598 }
1599}
1600
Damien George7ff996c2014-09-08 23:05:16 +01001601STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001602 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1603 need_reg_single(emit, REG_TEMP0, 0);
1604 need_reg_single(emit, REG_TEMP1, 0);
1605 emit_native_load_fast(emit, qst, local_num);
1606 vtype_kind_t vtype;
1607 int reg_base = REG_TEMP0;
1608 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1609 int reg_src = REG_TEMP1;
1610 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1611 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1612 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001613}
1614
Damien George7ff996c2014-09-08 23:05:16 +01001615STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001616 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001617 vtype_kind_t vtype;
1618 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1619 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001620 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001621 emit_post(emit);
1622}
1623
Damien George7ff996c2014-09-08 23:05:16 +01001624STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001625 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001626 if (vtype == VTYPE_PYOBJ) {
1627 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1628 } else {
1629 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001630 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 +01001631 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001632 }
Damien George7ff996c2014-09-08 23:05:16 +01001633 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001634 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001635}
1636
Damien George7ff996c2014-09-08 23:05:16 +01001637STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001638 vtype_kind_t vtype_base, vtype_val;
1639 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1640 assert(vtype_base == VTYPE_PYOBJ);
1641 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001642 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001643 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001644}
1645
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001646STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001647 DEBUG_printf("store_subscr\n");
1648 // need to compile: base[index] = value
1649
1650 // pop: index, base, value
1651 // optimise case where index is an immediate
1652 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1653
1654 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001655 // standard Python subscr
1656 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1657 vtype_kind_t vtype_value = peek_vtype(emit, 2);
1658 if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
1659 // need to implicitly convert non-objects to objects
1660 // TODO do this properly
1661 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
1662 adjust_stack(emit, 3);
1663 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001664 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 +01001665 emit_call(emit, MP_F_OBJ_SUBSCR);
1666 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001667 // viper store
1668 // TODO The different machine architectures have very different
1669 // capabilities and requirements for stores, so probably best to
1670 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001671 stack_info_t *top = peek_stack(emit, 0);
1672 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1673 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001674 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001675 emit_pre_pop_discard(emit); // discard index
1676 vtype_kind_t vtype_value;
1677 int reg_base = REG_ARG_1;
1678 int reg_index = REG_ARG_2;
1679 int reg_value = REG_ARG_3;
1680 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001681 #if N_X86
1682 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1683 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1684 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001685 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001686 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001687 switch (vtype_base) {
1688 case VTYPE_PTR8: {
1689 // pointer to 8-bit memory
1690 // TODO optimise to use thumb strb r1, [r2, r3]
1691 if (index_value != 0) {
1692 // index is non-zero
1693 #if N_THUMB
1694 if (index_value > 0 && index_value < 32) {
1695 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1696 break;
1697 }
1698 #endif
1699 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001700 #if N_ARM
1701 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1702 return;
1703 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001704 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1705 reg_base = reg_index;
1706 }
1707 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1708 break;
1709 }
1710 case VTYPE_PTR16: {
1711 // pointer to 16-bit memory
1712 if (index_value != 0) {
1713 // index is a non-zero immediate
1714 #if N_THUMB
1715 if (index_value > 0 && index_value < 32) {
1716 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1717 break;
1718 }
1719 #endif
1720 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001721 #if N_ARM
1722 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1723 return;
1724 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001725 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1726 reg_base = reg_index;
1727 }
1728 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1729 break;
1730 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001731 case VTYPE_PTR32: {
1732 // pointer to 32-bit memory
1733 if (index_value != 0) {
1734 // index is a non-zero immediate
1735 #if N_THUMB
1736 if (index_value > 0 && index_value < 32) {
1737 asm_thumb_str_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1738 break;
1739 }
1740 #endif
1741 ASM_MOV_IMM_TO_REG(emit->as, index_value << 2, reg_index);
1742 #if N_ARM
1743 asm_arm_str_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1744 return;
1745 #endif
1746 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 4*index to base
1747 reg_base = reg_index;
1748 }
1749 ASM_STORE32_REG_REG(emit->as, reg_value, reg_base); // store value to (base+4*index)
1750 break;
1751 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001752 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001753 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1754 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001755 }
1756 } else {
1757 // index is not an immediate
1758 vtype_kind_t vtype_index, vtype_value;
1759 int reg_index = REG_ARG_2;
1760 int reg_value = REG_ARG_3;
1761 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1762 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001763 #if N_X86
1764 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1765 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1766 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001767 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001768 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001769 switch (vtype_base) {
1770 case VTYPE_PTR8: {
1771 // pointer to 8-bit memory
1772 // TODO optimise to use thumb strb r1, [r2, r3]
1773 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001774 #if N_ARM
1775 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1776 break;
1777 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001778 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1779 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1780 break;
1781 }
1782 case VTYPE_PTR16: {
1783 // pointer to 16-bit memory
1784 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001785 #if N_ARM
1786 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1787 break;
1788 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001789 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1790 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1791 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1792 break;
1793 }
Damien Georgeb8f9ac52015-10-13 00:50:17 +01001794 case VTYPE_PTR32: {
1795 // pointer to 32-bit memory
1796 assert(vtype_index == VTYPE_INT);
1797 #if N_ARM
1798 asm_arm_str_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1799 break;
1800 #endif
1801 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1802 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_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1805 ASM_STORE32_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+4*index)
1806 break;
1807 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001808 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001809 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1810 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001811 }
1812 }
1813
1814 }
Damien13ed3a62013-10-08 09:05:10 +01001815}
1816
Damien George7ff996c2014-09-08 23:05:16 +01001817STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001818 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1819 // to mark deleted vars but then every var would need to be checked on
1820 // each access. Very inefficient, so just set value to None to enable GC.
1821 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1822 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001823}
1824
Damien George7ff996c2014-09-08 23:05:16 +01001825STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001826 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001827 (void)emit;
1828 (void)qst;
1829 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001830}
1831
Damien Georgee6ce10a2014-09-06 18:38:20 +01001832STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1833 emit_native_pre(emit);
1834 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1835 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001836}
1837
Damien Georgee6ce10a2014-09-06 18:38:20 +01001838STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1839 emit_native_pre(emit);
1840 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1841 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001842}
1843
Damien Georgee6ce10a2014-09-06 18:38:20 +01001844STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001845 vtype_kind_t vtype_base;
1846 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1847 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001848 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 +01001849 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001850}
1851
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001852STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001853 vtype_kind_t vtype_index, vtype_base;
1854 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1855 assert(vtype_index == VTYPE_PYOBJ);
1856 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001857 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 +01001858}
1859
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001860STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001861 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001862 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001863 int reg = REG_TEMP0;
1864 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1865 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001866}
1867
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001868STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001869 vtype_kind_t vtype0, vtype1;
1870 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1871 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1872}
1873
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001874STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001875 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001876 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001877 emit_post(emit);
1878}
1879
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001880STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001881 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +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(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001885}
1886
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001887STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001888 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001889 vtype_kind_t vtype0, vtype1, vtype2;
1890 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1891 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1892}
1893
Damien George7ff996c2014-09-08 23:05:16 +01001894STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001895 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001896 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001897 // need to commit stack because we are jumping elsewhere
1898 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001899 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001900 emit_post(emit);
1901}
1902
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001903STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001904 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001905 if (vtype == VTYPE_PYOBJ) {
1906 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1907 if (!pop) {
1908 adjust_stack(emit, 1);
1909 }
1910 emit_call(emit, MP_F_OBJ_IS_TRUE);
1911 } else {
1912 emit_pre_pop_reg(emit, &vtype, REG_RET);
1913 if (!pop) {
1914 adjust_stack(emit, 1);
1915 }
1916 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1917 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1918 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1919 }
Damien13ed3a62013-10-08 09:05:10 +01001920 }
Damien George21ca2d72014-10-19 19:00:51 +01001921 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1922 // can use it. This is a bit of a hack.
1923 if (!pop) {
1924 emit->saved_stack_vtype = vtype;
1925 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001926 // need to commit stack because we may jump elsewhere
1927 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001928}
1929
Damien George63f38322015-02-28 15:04:06 +00001930STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1931 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001932 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001933 if (cond) {
1934 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1935 } else {
1936 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1937 }
Damien1a6633a2013-11-03 13:58:19 +00001938 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001939}
Damien1a6633a2013-11-03 13:58:19 +00001940
Damien George63f38322015-02-28 15:04:06 +00001941STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1942 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001943 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001944 if (cond) {
1945 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1946 } else {
1947 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1948 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001949 adjust_stack(emit, -1);
1950 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001951}
1952
Damien George7ff996c2014-09-08 23:05:16 +01001953STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001954 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001955 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001956}
Damien Georgea32c1e42014-05-07 18:30:52 +01001957
Damien George7ff996c2014-09-08 23:05:16 +01001958STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001959 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001960 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001961}
Damien Georgea32c1e42014-05-07 18:30:52 +01001962
Damien George7ff996c2014-09-08 23:05:16 +01001963STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001964 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001965 (void)emit;
1966 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001967 assert(0);
1968}
Damien Georgeb601d952014-06-30 05:17:25 +01001969
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001970STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001971 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001972 assert(0);
1973}
Damien Georgeb601d952014-06-30 05:17:25 +01001974
Damien George7ff996c2014-09-08 23:05:16 +01001975STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001976 emit_native_pre(emit);
1977 // need to commit stack because we may jump elsewhere
1978 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001979 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 +01001980 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001981 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001982 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001983}
Damien Georgeb601d952014-06-30 05:17:25 +01001984
Damien George7ff996c2014-09-08 23:05:16 +01001985STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001986 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001987}
Damien Georgeb601d952014-06-30 05:17:25 +01001988
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001989STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001990 // logic:
1991 // exc = pop_stack
1992 // if exc == None: pass
1993 // else: raise exc
1994 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1995 vtype_kind_t vtype;
1996 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1997 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001998 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001999}
Damiend2755ec2013-10-16 23:58:48 +01002000
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002001STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002002 // perhaps the difficult one, as we want to rewrite for loops using native code
2003 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01002004
2005 vtype_kind_t vtype;
2006 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2007 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002008 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01002009 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002010}
Damiend2755ec2013-10-16 23:58:48 +01002011
Damien George7ff996c2014-09-08 23:05:16 +01002012STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002013 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002014 vtype_kind_t vtype;
2015 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
2016 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002017 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01002018 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
2019 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01002020 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2021}
2022
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002023STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01002024 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00002025 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01002026 adjust_stack(emit, -1);
2027 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002028}
2029
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002030STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002031 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002032 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002033 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01002034 emit_post(emit);
2035}
2036
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002037STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002038 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01002039 /*
2040 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002041 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01002042 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01002043 emit_post(emit);
2044 */
Damien13ed3a62013-10-08 09:05:10 +01002045}
2046
Damien Georged17926d2014-03-30 13:35:08 +01002047STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002048 vtype_kind_t vtype;
2049 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
Damien George9f5f1562015-10-08 13:08:59 +01002050 if (vtype == VTYPE_PYOBJ) {
2051 if (op == MP_UNARY_OP_NOT) {
2052 // we need to synthesise this operation by converting to bool first
2053 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
2054 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
2055 }
2056 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
2057 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2058 } else {
2059 adjust_stack(emit, 1);
2060 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2061 "unary op %q not implemented", mp_unary_op_method_name[op]);
Damien Georgeb601d952014-06-30 05:17:25 +01002062 }
Damien13ed3a62013-10-08 09:05:10 +01002063}
2064
Damien Georged17926d2014-03-30 13:35:08 +01002065STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00002066 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01002067 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
2068 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01002069 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01002070 #if N_X64 || N_X86
2071 // special cases for x86 and shifting
2072 if (op == MP_BINARY_OP_LSHIFT
2073 || op == MP_BINARY_OP_INPLACE_LSHIFT
2074 || op == MP_BINARY_OP_RSHIFT
2075 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2076 #if N_X64
2077 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
2078 #else
2079 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
2080 #endif
2081 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2082 ASM_LSL_REG(emit->as, REG_RET);
2083 } else {
2084 ASM_ASR_REG(emit->as, REG_RET);
2085 }
2086 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
2087 return;
2088 }
2089 #endif
2090 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002091 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002092 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
2093 if (0) {
2094 // dummy
2095 #if !(N_X64 || N_X86)
2096 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2097 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002098 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002099 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2100 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2101 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2102 #endif
Damien George1ef23482014-10-12 14:21:06 +01002103 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2104 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2105 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2106 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2107 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2108 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2109 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2110 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2111 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002112 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2113 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2114 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2115 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2116 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2117 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George567b3492015-06-04 14:00:29 +00002118 } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2119 ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2120 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002121 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2122 // comparison ops are (in enum order):
2123 // MP_BINARY_OP_LESS
2124 // MP_BINARY_OP_MORE
2125 // MP_BINARY_OP_EQUAL
2126 // MP_BINARY_OP_LESS_EQUAL
2127 // MP_BINARY_OP_MORE_EQUAL
2128 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002129 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002130 #if N_X64
2131 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2132 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2133 static byte ops[6] = {
2134 ASM_X64_CC_JL,
2135 ASM_X64_CC_JG,
2136 ASM_X64_CC_JE,
2137 ASM_X64_CC_JLE,
2138 ASM_X64_CC_JGE,
2139 ASM_X64_CC_JNE,
2140 };
2141 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2142 #elif N_X86
2143 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2144 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2145 static byte ops[6] = {
2146 ASM_X86_CC_JL,
2147 ASM_X86_CC_JG,
2148 ASM_X86_CC_JE,
2149 ASM_X86_CC_JLE,
2150 ASM_X86_CC_JGE,
2151 ASM_X86_CC_JNE,
2152 };
2153 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2154 #elif N_THUMB
2155 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2156 static uint16_t ops[6] = {
2157 ASM_THUMB_OP_ITE_GE,
2158 ASM_THUMB_OP_ITE_GT,
2159 ASM_THUMB_OP_ITE_EQ,
2160 ASM_THUMB_OP_ITE_GT,
2161 ASM_THUMB_OP_ITE_GE,
2162 ASM_THUMB_OP_ITE_EQ,
2163 };
2164 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2165 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2166 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2167 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2168 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002169 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2170 static uint ccs[6] = {
2171 ASM_ARM_CC_LT,
2172 ASM_ARM_CC_GT,
2173 ASM_ARM_CC_EQ,
2174 ASM_ARM_CC_LE,
2175 ASM_ARM_CC_GE,
2176 ASM_ARM_CC_NE,
2177 };
2178 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002179 #else
2180 #error not implemented
2181 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002182 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2183 } else {
2184 // TODO other ops not yet implemented
Damien George4d9cad12015-06-04 11:52:16 +01002185 adjust_stack(emit, 1);
2186 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2187 "binary op %q not implemented", mp_binary_op_method_name[op]);
Damien Georgebc1d3692014-01-11 09:47:06 +00002188 }
Damien13ed3a62013-10-08 09:05:10 +01002189 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002190 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002191 bool invert = false;
2192 if (op == MP_BINARY_OP_NOT_IN) {
2193 invert = true;
2194 op = MP_BINARY_OP_IN;
2195 } else if (op == MP_BINARY_OP_IS_NOT) {
2196 invert = true;
2197 op = MP_BINARY_OP_IS;
2198 }
Damien George7fe21912014-08-16 22:31:57 +01002199 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002200 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002201 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002202 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2203 }
Damien13ed3a62013-10-08 09:05:10 +01002204 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2205 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002206 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002207 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2208 "can't do binary op between '%q' and '%q'",
2209 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002210 }
2211}
2212
Damien George7ff996c2014-09-08 23:05:16 +01002213STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002214 // for viper: call runtime, with types of args
2215 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002216 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002217 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002218 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002219 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002220}
2221
Damien George7ff996c2014-09-08 23:05:16 +01002222STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002223 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002224 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002225 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002226 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2227}
2228
Damien George7ff996c2014-09-08 23:05:16 +01002229STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002230 // only used in list comprehension
2231 vtype_kind_t vtype_list, vtype_item;
2232 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2233 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2234 assert(vtype_list == VTYPE_PYOBJ);
2235 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002236 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002237 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002238}
2239
Damien George7ff996c2014-09-08 23:05:16 +01002240STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002241 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002242 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002243 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2244}
2245
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002246STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002247 vtype_kind_t vtype_key, vtype_value, vtype_map;
2248 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
2249 assert(vtype_key == VTYPE_PYOBJ);
2250 assert(vtype_value == VTYPE_PYOBJ);
2251 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002252 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002253 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2254}
2255
Damien George7ff996c2014-09-08 23:05:16 +01002256STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002257 // only used in list comprehension
2258 vtype_kind_t vtype_map, vtype_key, vtype_value;
2259 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2260 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2261 assert(vtype_map == VTYPE_PYOBJ);
2262 assert(vtype_key == VTYPE_PYOBJ);
2263 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002264 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002265 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002266}
2267
Damien Georgee37dcaa2014-12-27 17:07:16 +00002268#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002269STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002270 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002271 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002272 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002273 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2274}
2275
Damien George7ff996c2014-09-08 23:05:16 +01002276STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002277 // only used in set comprehension
2278 vtype_kind_t vtype_set, vtype_item;
2279 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2280 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2281 assert(vtype_set == VTYPE_PYOBJ);
2282 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002283 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002284 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002285}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002286#endif
Damiend2755ec2013-10-16 23:58:48 +01002287
Damien George83204f32014-12-27 17:20:41 +00002288#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002289STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002290 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002291 if (n_args == 2) {
2292 vtype_kind_t vtype_start, vtype_stop;
2293 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2294 assert(vtype_start == VTYPE_PYOBJ);
2295 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002296 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 +01002297 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2298 } else {
2299 assert(n_args == 3);
2300 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2301 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
2302 assert(vtype_start == VTYPE_PYOBJ);
2303 assert(vtype_stop == VTYPE_PYOBJ);
2304 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002305 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002306 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2307 }
Damien13ed3a62013-10-08 09:05:10 +01002308}
Damien George83204f32014-12-27 17:20:41 +00002309#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002310
Damien George7ff996c2014-09-08 23:05:16 +01002311STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002312 DEBUG_printf("unpack_sequence %d\n", n_args);
2313 vtype_kind_t vtype_base;
2314 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2315 assert(vtype_base == VTYPE_PYOBJ);
2316 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002317 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002318}
Damien Georgecdd96df2014-04-06 12:58:40 +01002319
Damien George7ff996c2014-09-08 23:05:16 +01002320STATIC 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 +01002321 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2322 vtype_kind_t vtype_base;
2323 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2324 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002325 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 +01002326 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 +01002327}
2328
Damien George7ff996c2014-09-08 23:05:16 +01002329STATIC 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 +01002330 // 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 +00002331 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002332 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002333 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 +01002334 } else {
2335 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2336 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2337 assert(vtype_def_tuple == VTYPE_PYOBJ);
2338 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002339 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 +01002340 }
Damien13ed3a62013-10-08 09:05:10 +01002341 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2342}
2343
Damien George7ff996c2014-09-08 23:05:16 +01002344STATIC 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 +00002345 emit_native_pre(emit);
2346 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2347 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2348 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2349 } else {
2350 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2351 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2352 }
2353 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2354 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2355 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002356}
2357
Damien George7ff996c2014-09-08 23:05:16 +01002358STATIC 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 +00002359 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2360
Damien Georgee9dac3b2014-09-29 22:10:41 +01002361 // TODO: in viper mode, call special runtime routine with type info for args,
2362 // and wanted type info for return, to remove need for boxing/unboxing
2363
Damien Georgee9dac3b2014-09-29 22:10:41 +01002364 emit_native_pre(emit);
2365 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2366 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2367 // casting operator
2368 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002369 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002370 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002371 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002372 switch (peek_vtype(emit, 0)) {
2373 case VTYPE_PYOBJ: {
2374 vtype_kind_t vtype;
2375 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2376 emit_pre_pop_discard(emit);
2377 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2378 emit_post_push_reg(emit, vtype_cast, REG_RET);
2379 break;
2380 }
2381 case VTYPE_BOOL:
2382 case VTYPE_INT:
2383 case VTYPE_UINT:
2384 case VTYPE_PTR:
2385 case VTYPE_PTR8:
2386 case VTYPE_PTR16:
Damien Georgeb8f9ac52015-10-13 00:50:17 +01002387 case VTYPE_PTR32:
Damien Georgee9dac3b2014-09-29 22:10:41 +01002388 case VTYPE_PTR_NONE:
2389 emit_fold_stack_top(emit, REG_ARG_1);
2390 emit_post_top_set_vtype(emit, vtype_cast);
2391 break;
2392 default:
2393 assert(!"TODO: convert obj to int");
2394 }
2395 } else {
Damien13ed3a62013-10-08 09:05:10 +01002396 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002397 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002398 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2399 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);
2400 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2401 } else {
2402 if (n_positional != 0 || n_keyword != 0) {
2403 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2404 }
2405 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2406 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2407 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2408 }
Damien Georgecd82e022014-02-02 13:11:48 +00002409 }
Damien13ed3a62013-10-08 09:05:10 +01002410}
2411
Damien George7ff996c2014-09-08 23:05:16 +01002412STATIC 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 +01002413 if (star_flags) {
Damien George78772ad2015-04-06 22:48:21 +01002414 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2415 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);
2416 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2417 } else {
2418 emit_native_pre(emit);
2419 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
2420 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2421 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2422 }
Damien13ed3a62013-10-08 09:05:10 +01002423}
2424
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002425STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002426 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002427 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002428 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2429 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002430 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002431 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002432 } else {
2433 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002434 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002435 } else {
2436 vtype_kind_t vtype;
2437 emit_pre_pop_reg(emit, &vtype, REG_RET);
2438 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002439 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2440 "return expected '%q' but got '%q'",
2441 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002442 }
Damien George2ac4af62014-08-15 16:45:41 +01002443 }
Damien13ed3a62013-10-08 09:05:10 +01002444 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002445 vtype_kind_t vtype;
2446 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002447 assert(vtype == VTYPE_PYOBJ);
2448 }
2449 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002450 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2451 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002452}
2453
Damien George7ff996c2014-09-08 23:05:16 +01002454STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002455 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002456 vtype_kind_t vtype_exc;
2457 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2458 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002459 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002460 }
2461 // 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 +01002462 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002463}
Damien Georgeb601d952014-06-30 05:17:25 +01002464
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002465STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002466 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002467 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002468 assert(0);
2469}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002470STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002471 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002472 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002473 assert(0);
2474}
2475
Damien Georgeb601d952014-06-30 05:17:25 +01002476STATIC void emit_native_start_except_handler(emit_t *emit) {
2477 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2478 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2479 // the first 2 elements, so we can get the thrown value.
2480 adjust_stack(emit, 2);
2481 vtype_kind_t vtype_nlr;
2482 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002483 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002484 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
2485}
2486
2487STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002488 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002489}
2490
Damien13ed3a62013-10-08 09:05:10 +01002491const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002492 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002493 emit_native_start_pass,
2494 emit_native_end_pass,
2495 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002496 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002497 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002498
Damien George542bd6b2015-03-26 14:42:40 +00002499 {
2500 emit_native_load_fast,
2501 emit_native_load_deref,
2502 emit_native_load_name,
2503 emit_native_load_global,
2504 },
2505 {
2506 emit_native_store_fast,
2507 emit_native_store_deref,
2508 emit_native_store_name,
2509 emit_native_store_global,
2510 },
2511 {
2512 emit_native_delete_fast,
2513 emit_native_delete_deref,
2514 emit_native_delete_name,
2515 emit_native_delete_global,
2516 },
Damien13ed3a62013-10-08 09:05:10 +01002517
2518 emit_native_label_assign,
2519 emit_native_import_name,
2520 emit_native_import_from,
2521 emit_native_import_star,
2522 emit_native_load_const_tok,
2523 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002524 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002525 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002526 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002527 emit_native_load_attr,
2528 emit_native_load_method,
2529 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002530 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002531 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002532 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002533 emit_native_delete_attr,
2534 emit_native_delete_subscr,
2535 emit_native_dup_top,
2536 emit_native_dup_top_two,
2537 emit_native_pop_top,
2538 emit_native_rot_two,
2539 emit_native_rot_three,
2540 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002541 emit_native_pop_jump_if,
2542 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002543 emit_native_break_loop,
2544 emit_native_continue_loop,
2545 emit_native_setup_with,
2546 emit_native_with_cleanup,
2547 emit_native_setup_except,
2548 emit_native_setup_finally,
2549 emit_native_end_finally,
2550 emit_native_get_iter,
2551 emit_native_for_iter,
2552 emit_native_for_iter_end,
2553 emit_native_pop_block,
2554 emit_native_pop_except,
2555 emit_native_unary_op,
2556 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002557 emit_native_build_tuple,
2558 emit_native_build_list,
2559 emit_native_list_append,
2560 emit_native_build_map,
2561 emit_native_store_map,
2562 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002563 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002564 emit_native_build_set,
2565 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002566 #endif
Damien George83204f32014-12-27 17:20:41 +00002567 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002568 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002569 #endif
Damien13ed3a62013-10-08 09:05:10 +01002570 emit_native_unpack_sequence,
2571 emit_native_unpack_ex,
2572 emit_native_make_function,
2573 emit_native_make_closure,
2574 emit_native_call_function,
2575 emit_native_call_method,
2576 emit_native_return_value,
2577 emit_native_raise_varargs,
2578 emit_native_yield_value,
2579 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002580
2581 emit_native_start_except_handler,
2582 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002583};
2584
Damien Georgec90f59e2014-09-06 23:06:36 +01002585#endif