blob: 2a521c229c1497d4a542a143df13630b97df0db0 [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))
155
156#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 +0000157#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 +0100158#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
159#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100160
Damien Georgec90f59e2014-09-06 23:06:36 +0100161#elif N_X86
162
163// x86 specific stuff
164
Damien George51dfcb42015-01-01 20:27:54 +0000165#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100166
167STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
168 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
169 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100170 [MP_F_LOAD_NAME] = 1,
171 [MP_F_LOAD_GLOBAL] = 1,
172 [MP_F_LOAD_BUILD_CLASS] = 0,
173 [MP_F_LOAD_ATTR] = 2,
174 [MP_F_LOAD_METHOD] = 3,
175 [MP_F_STORE_NAME] = 2,
176 [MP_F_STORE_GLOBAL] = 2,
177 [MP_F_STORE_ATTR] = 3,
178 [MP_F_OBJ_SUBSCR] = 3,
179 [MP_F_OBJ_IS_TRUE] = 1,
180 [MP_F_UNARY_OP] = 2,
181 [MP_F_BINARY_OP] = 3,
182 [MP_F_BUILD_TUPLE] = 2,
183 [MP_F_BUILD_LIST] = 2,
184 [MP_F_LIST_APPEND] = 2,
185 [MP_F_BUILD_MAP] = 1,
186 [MP_F_STORE_MAP] = 3,
187#if MICROPY_PY_BUILTINS_SET
188 [MP_F_BUILD_SET] = 2,
189 [MP_F_STORE_SET] = 2,
190#endif
191 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
192 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
193 [MP_F_CALL_METHOD_N_KW] = 3,
Damien George78772ad2015-04-06 22:48:21 +0100194 [MP_F_CALL_METHOD_N_KW_VAR] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100195 [MP_F_GETITER] = 1,
196 [MP_F_ITERNEXT] = 1,
197 [MP_F_NLR_PUSH] = 1,
198 [MP_F_NLR_POP] = 0,
199 [MP_F_NATIVE_RAISE] = 1,
200 [MP_F_IMPORT_NAME] = 3,
201 [MP_F_IMPORT_FROM] = 2,
202 [MP_F_IMPORT_ALL] = 1,
203#if MICROPY_PY_BUILTINS_SLICE
204 [MP_F_NEW_SLICE] = 3,
205#endif
206 [MP_F_UNPACK_SEQUENCE] = 3,
207 [MP_F_UNPACK_EX] = 3,
208 [MP_F_DELETE_NAME] = 1,
209 [MP_F_DELETE_GLOBAL] = 1,
Damien George99957382015-04-03 14:38:41 +0000210 [MP_F_NEW_CELL] = 1,
211 [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
Damien George99886182015-04-06 22:38:53 +0100212 [MP_F_SETUP_CODE_STATE] = 5,
Damien Georgec90f59e2014-09-06 23:06:36 +0100213};
214
215#define EXPORT_FUN(name) emit_native_x86_##name
216
Damien George99886182015-04-06 22:38:53 +0100217#define ASM_WORD_SIZE (4)
218
Damien George0b610de2014-09-29 16:25:04 +0100219#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000220#define REG_ARG_1 ASM_X86_REG_ARG_1
221#define REG_ARG_2 ASM_X86_REG_ARG_2
222#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George99886182015-04-06 22:38:53 +0100223#define REG_ARG_4 ASM_X86_REG_ARG_4
224#define REG_ARG_5 ASM_X86_REG_ARG_5
Damien George81057362014-09-07 01:06:19 +0100225
Damien George25d90412014-09-06 23:24:32 +0000226// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100227#define REG_TEMP0 ASM_X86_REG_EAX
228#define REG_TEMP1 ASM_X86_REG_ECX
229#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100230
Damien George25d90412014-09-06 23:24:32 +0000231// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100232#define REG_LOCAL_1 ASM_X86_REG_EBX
233#define REG_LOCAL_2 ASM_X86_REG_ESI
234#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000235#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100236
237#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
238#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
239
240#define ASM_T asm_x86_t
241#define ASM_NEW asm_x86_new
242#define ASM_FREE asm_x86_free
243#define ASM_GET_CODE asm_x86_get_code
Damien George99886182015-04-06 22:38:53 +0100244#define ASM_GET_CODE_POS asm_x86_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100245#define ASM_GET_CODE_SIZE asm_x86_get_code_size
246#define ASM_START_PASS asm_x86_start_pass
247#define ASM_END_PASS asm_x86_end_pass
248#define ASM_ENTRY asm_x86_entry
249#define ASM_EXIT asm_x86_exit
250
Damien George99886182015-04-06 22:38:53 +0100251#define ASM_ALIGN asm_x86_align
252#define ASM_DATA asm_x86_data
253
Damien Georgec90f59e2014-09-06 23:06:36 +0100254#define ASM_LABEL_ASSIGN asm_x86_label_assign
255#define ASM_JUMP asm_x86_jmp_label
256#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
257 do { \
258 asm_x86_test_r8_with_r8(as, reg, reg); \
259 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
260 } while (0)
261#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
262 do { \
263 asm_x86_test_r8_with_r8(as, reg, reg); \
264 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
265 } while (0)
266#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
267 do { \
268 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
269 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
270 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100271#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 +0100272
273#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
274#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
275#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
276#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
277 do { \
278 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
279 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
280 } while (false)
281#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100282#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 +0100283#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100284
Damien George3112cde2014-09-29 18:45:42 +0100285#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
286#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100287#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
288#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
289#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 +0100290#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
291#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 +0000292#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 +0100293
Damien George91cfd412014-10-12 16:59:29 +0100294#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 +0000295#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 +0000296#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
297#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem16_to_r32zx((as), (reg_base), 0, (reg_dest))
Damien George91cfd412014-10-12 16:59:29 +0100298
299#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 +0000300#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 +0100301#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
302#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100303
Damien3ef4abb2013-10-12 16:53:13 +0100304#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100305
306// thumb specific stuff
307
Damien George51dfcb42015-01-01 20:27:54 +0000308#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100309
Damien13ed3a62013-10-08 09:05:10 +0100310#define EXPORT_FUN(name) emit_native_thumb_##name
311
Damien George99886182015-04-06 22:38:53 +0100312#define ASM_WORD_SIZE (4)
313
Damien George0b610de2014-09-29 16:25:04 +0100314#define REG_RET ASM_THUMB_REG_R0
315#define REG_ARG_1 ASM_THUMB_REG_R0
316#define REG_ARG_2 ASM_THUMB_REG_R1
317#define REG_ARG_3 ASM_THUMB_REG_R2
318#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George99886182015-04-06 22:38:53 +0100319// rest of args go on stack
Damien George81057362014-09-07 01:06:19 +0100320
Damien George0b610de2014-09-29 16:25:04 +0100321#define REG_TEMP0 ASM_THUMB_REG_R0
322#define REG_TEMP1 ASM_THUMB_REG_R1
323#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100324
Damien George0b610de2014-09-29 16:25:04 +0100325#define REG_LOCAL_1 ASM_THUMB_REG_R4
326#define REG_LOCAL_2 ASM_THUMB_REG_R5
327#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100328#define REG_LOCAL_NUM (3)
329
330#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
331#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
332
333#define ASM_T asm_thumb_t
334#define ASM_NEW asm_thumb_new
335#define ASM_FREE asm_thumb_free
336#define ASM_GET_CODE asm_thumb_get_code
Damien George99886182015-04-06 22:38:53 +0100337#define ASM_GET_CODE_POS asm_thumb_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100338#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
339#define ASM_START_PASS asm_thumb_start_pass
340#define ASM_END_PASS asm_thumb_end_pass
341#define ASM_ENTRY asm_thumb_entry
342#define ASM_EXIT asm_thumb_exit
343
Damien George99886182015-04-06 22:38:53 +0100344#define ASM_ALIGN asm_thumb_align
345#define ASM_DATA asm_thumb_data
346
Damien Georgec90f59e2014-09-06 23:06:36 +0100347#define ASM_LABEL_ASSIGN asm_thumb_label_assign
348#define ASM_JUMP asm_thumb_b_label
349#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
350 do { \
351 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100352 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100353 } while (0)
354#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
355 do { \
356 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100357 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100358 } while (0)
359#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
360 do { \
361 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100362 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100363 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100364#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 +0100365
366#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
367#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
368#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
369#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
370 do { \
371 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
372 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
373 } while (false)
374#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 +0100375#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 +0100376#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 +0100377
Damien George3112cde2014-09-29 18:45:42 +0100378#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
379#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 +0100380#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
381#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
382#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 +0100383#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
384#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 +0000385#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 +0100386
Damien George91cfd412014-10-12 16:59:29 +0100387#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 +0000388#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 +0100389#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
390#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
391
Damien Georgee9dac3b2014-09-29 22:10:41 +0100392#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 +0000393#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 +0100394#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
395#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
396
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200397#elif N_ARM
398
399// ARM specific stuff
400
Damien George51dfcb42015-01-01 20:27:54 +0000401#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200402
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300403#define ASM_WORD_SIZE (4)
404
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200405#define EXPORT_FUN(name) emit_native_arm_##name
406
Damien George0b610de2014-09-29 16:25:04 +0100407#define REG_RET ASM_ARM_REG_R0
408#define REG_ARG_1 ASM_ARM_REG_R0
409#define REG_ARG_2 ASM_ARM_REG_R1
410#define REG_ARG_3 ASM_ARM_REG_R2
411#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100412
Damien George0b610de2014-09-29 16:25:04 +0100413#define REG_TEMP0 ASM_ARM_REG_R0
414#define REG_TEMP1 ASM_ARM_REG_R1
415#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100416
Damien George0b610de2014-09-29 16:25:04 +0100417#define REG_LOCAL_1 ASM_ARM_REG_R4
418#define REG_LOCAL_2 ASM_ARM_REG_R5
419#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100420#define REG_LOCAL_NUM (3)
421
422#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
423#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
424
425#define ASM_T asm_arm_t
426#define ASM_NEW asm_arm_new
427#define ASM_FREE asm_arm_free
428#define ASM_GET_CODE asm_arm_get_code
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300429#define ASM_GET_CODE_POS asm_arm_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100430#define ASM_GET_CODE_SIZE asm_arm_get_code_size
431#define ASM_START_PASS asm_arm_start_pass
432#define ASM_END_PASS asm_arm_end_pass
433#define ASM_ENTRY asm_arm_entry
434#define ASM_EXIT asm_arm_exit
435
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300436#define ASM_ALIGN asm_arm_align
437#define ASM_DATA asm_arm_data
438
Damien Georgec90f59e2014-09-06 23:06:36 +0100439#define ASM_LABEL_ASSIGN asm_arm_label_assign
440#define ASM_JUMP asm_arm_b_label
441#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
442 do { \
443 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100444 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100445 } while (0)
446#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
447 do { \
448 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100449 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100450 } while (0)
451#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
452 do { \
453 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100454 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100455 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100456#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 +0100457
458#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
459#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
460#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
461#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
462 do { \
463 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
464 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
465 } while (false)
466#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 +0100467#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 +0100468#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
469
Fabian Vogte5268962014-10-04 00:53:46 +0200470#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
471#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 +0100472#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
473#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
474#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 +0100475#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
476#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 +0000477#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 +0100478
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300479#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 0)
480#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 +0100481#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
482#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
483
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300484#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base), 0)
485#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 +0200486#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
487#define ASM_STORE16_REG_REG(as, reg_value, reg_base) asm_arm_strh_reg_reg((as), (reg_value), (reg_base))
Damien Georgee9dac3b2014-09-29 22:10:41 +0100488
Damien Georgec90f59e2014-09-06 23:06:36 +0100489#else
490
491#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200492
Damien13ed3a62013-10-08 09:05:10 +0100493#endif
494
Damien Georgec8b60f02015-04-20 13:29:31 +0000495#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
496 *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
497 } while (0)
498
Damien13ed3a62013-10-08 09:05:10 +0100499typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100500 STACK_VALUE,
501 STACK_REG,
502 STACK_IMM,
503} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100504
Damien Georgee9dac3b2014-09-29 22:10:41 +0100505// these enums must be distinct and the bottom 2 bits
506// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100507typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100508 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
509 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
510 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
511 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
512
513 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
514 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
515 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
516 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
517
518 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
519 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100520} vtype_kind_t;
521
Damien Georgec8b60f02015-04-20 13:29:31 +0000522STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
523 switch (vtype) {
524 case VTYPE_PYOBJ: return MP_QSTR_object;
525 case VTYPE_BOOL: return MP_QSTR_bool;
526 case VTYPE_INT: return MP_QSTR_int;
527 case VTYPE_UINT: return MP_QSTR_uint;
528 case VTYPE_PTR: return MP_QSTR_ptr;
529 case VTYPE_PTR8: return MP_QSTR_ptr8;
530 case VTYPE_PTR16: return MP_QSTR_ptr16;
531 case VTYPE_PTR_NONE: default: return MP_QSTR_None;
532 }
533}
534
Damienff8ed772013-10-08 22:18:32 +0100535typedef struct _stack_info_t {
536 vtype_kind_t vtype;
537 stack_info_kind_t kind;
538 union {
539 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100540 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000541 } data;
Damienff8ed772013-10-08 22:18:32 +0100542} stack_info_t;
543
Damien13ed3a62013-10-08 09:05:10 +0100544struct _emit_t {
Damien Georgec8b60f02015-04-20 13:29:31 +0000545 mp_obj_t *error_slot;
Damien13ed3a62013-10-08 09:05:10 +0100546 int pass;
547
548 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100549
Damien George2ac4af62014-08-15 16:45:41 +0100550 vtype_kind_t return_vtype;
551
Damien George7ff996c2014-09-08 23:05:16 +0100552 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100553 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100554
Damien George7ff996c2014-09-08 23:05:16 +0100555 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100556 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100557 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100558
Damien George99886182015-04-06 22:38:53 +0100559 int code_info_size;
560 int code_info_offset;
561 int prelude_offset;
562 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100563 int stack_start;
564 int stack_size;
565
566 bool last_emit_was_return_value;
567
Damien13ed3a62013-10-08 09:05:10 +0100568 scope_t *scope;
569
Damien Georgec90f59e2014-09-06 23:06:36 +0100570 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100571};
572
Damien Georgec8b60f02015-04-20 13:29:31 +0000573emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100574 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000575 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100576 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100577 return emit;
578}
579
Damien George41d02b62014-01-24 22:42:28 +0000580void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100581 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100582 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
583 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200584 m_del_obj(emit_t, emit);
585}
586
Damien George2ac4af62014-08-15 16:45:41 +0100587STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
588 switch (op) {
589 case MP_EMIT_NATIVE_TYPE_ENABLE:
590 emit->do_viper_types = arg1;
591 break;
592
593 default: {
594 vtype_kind_t type;
595 switch (arg2) {
596 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
597 case MP_QSTR_bool: type = VTYPE_BOOL; break;
598 case MP_QSTR_int: type = VTYPE_INT; break;
599 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100600 case MP_QSTR_ptr: type = VTYPE_PTR; break;
601 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
602 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000603 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100604 }
605 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
606 emit->return_vtype = type;
607 } else {
608 assert(arg1 < emit->local_vtype_alloc);
609 emit->local_vtype[arg1] = type;
610 }
611 break;
612 }
613 }
Damien13ed3a62013-10-08 09:05:10 +0100614}
615
Damien Georgefa5950e2015-04-03 15:03:24 +0000616STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000617STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000618STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000619STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000620
Damien George99886182015-04-06 22:38:53 +0100621#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
622
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200623STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000624 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
625
Damien13ed3a62013-10-08 09:05:10 +0100626 emit->pass = pass;
627 emit->stack_start = 0;
628 emit->stack_size = 0;
629 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100630 emit->scope = scope;
631
Damien George36db6bc2014-05-07 17:24:22 +0100632 // allocate memory for keeping track of the types of locals
633 if (emit->local_vtype_alloc < scope->num_locals) {
634 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
635 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100636 }
Damien George36db6bc2014-05-07 17:24:22 +0100637
638 // allocate memory for keeping track of the objects on the stack
639 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000640 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100641 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000642 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100643 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100644 }
645
Damien George99886182015-04-06 22:38:53 +0100646 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100647 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100648
649 // set default type for arguments
650 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
651 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
652 num_args += 1;
653 }
654 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
655 num_args += 1;
656 }
657 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100658 emit->local_vtype[i] = VTYPE_PYOBJ;
659 }
Damien Georgea5190a72014-08-15 22:39:08 +0100660
661 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100662 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100663 emit->local_vtype[i] = VTYPE_UNBOUND;
664 }
665
666 // values on stack begin unbound
667 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100668 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100669 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100670 }
671
Damien Georgec90f59e2014-09-06 23:06:36 +0100672 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100673
Damien George99886182015-04-06 22:38:53 +0100674 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100675
Damien George99886182015-04-06 22:38:53 +0100676 if (emit->do_viper_types) {
677
Damien Georgef17e6632015-07-23 14:30:37 +0100678 // right now we have a restriction of maximum of 4 arguments
679 if (scope->num_pos_args >= 5) {
Damien George84d59c22015-07-27 22:20:00 +0100680 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "Viper functions don't currently support more than 4 arguments");
Damien Georgef17e6632015-07-23 14:30:37 +0100681 return;
682 }
683
Damien George99886182015-04-06 22:38:53 +0100684 // entry to function
685 int num_locals = 0;
686 if (pass > MP_PASS_SCOPE) {
687 num_locals = scope->num_locals - REG_LOCAL_NUM;
688 if (num_locals < 0) {
689 num_locals = 0;
690 }
691 emit->stack_start = num_locals;
692 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100693 }
Damien George99886182015-04-06 22:38:53 +0100694 ASM_ENTRY(emit->as, num_locals);
695
696 #if N_X86
697 for (int i = 0; i < scope->num_pos_args; i++) {
698 if (i == 0) {
699 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
700 } else if (i == 1) {
701 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
702 } else if (i == 2) {
703 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
704 } else {
705 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
706 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
707 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100708 }
Damien George99886182015-04-06 22:38:53 +0100709 #else
710 for (int i = 0; i < scope->num_pos_args; i++) {
711 if (i == 0) {
712 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
713 } else if (i == 1) {
714 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
715 } else if (i == 2) {
716 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
717 } else if (i == 3) {
718 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
719 } else {
720 // TODO not implemented
721 assert(0);
722 }
723 }
724 #endif
725
726 } else {
727 // work out size of state (locals plus stack)
728 emit->n_state = scope->num_locals + scope->stack_size;
729
730 // allocate space on C-stack for code_state structure, which includes state
731 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
732
733 // prepare incoming arguments for call to mp_setup_code_state
734 #if N_X86
735 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
736 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
737 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
738 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
739 #else
740 #if N_THUMB
741 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300742 #elif N_ARM
743 ASM_MOV_REG_REG(emit->as, ASM_ARM_REG_R4, REG_ARG_4);
Damien George99886182015-04-06 22:38:53 +0100744 #else
745 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
746 #endif
747 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
748 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
749 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
750 #endif
751
752 // set code_state.code_info (offset from start of this function to code_info data)
753 // XXX this encoding may change size
754 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->code_info_offset, offsetof(mp_code_state, code_info) / sizeof(mp_uint_t), REG_ARG_1);
755
756 // set code_state.ip (offset from start of this function to prelude info)
757 // XXX this encoding may change size
758 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
759
760 // set code_state.n_state
761 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
762
763 // put address of code_state into first arg
764 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
765
766 // call mp_setup_code_state to prepare code_state structure
767 #if N_THUMB
768 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
769 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
770 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300771 #elif N_ARM
772 asm_arm_push(emit->as, 1 << ASM_ARM_REG_R4); // push 5th arg
773 asm_arm_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
774 asm_arm_pop(emit->as, 1 << REG_RET); // pop dummy (was 5th arg)
Damien George99886182015-04-06 22:38:53 +0100775 #else
776 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
777 #endif
778
779 // cache some locals in registers
780 if (scope->num_locals > 0) {
781 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
782 if (scope->num_locals > 1) {
783 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
784 if (scope->num_locals > 2) {
785 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
786 }
787 }
788 }
789
790 // set the type of closed over variables
791 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
792 id_info_t *id = &scope->id_info[i];
793 if (id->kind == ID_INFO_KIND_CELL) {
794 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
795 }
Damien13ed3a62013-10-08 09:05:10 +0100796 }
797 }
798
Damien George99886182015-04-06 22:38:53 +0100799 #if N_THUMB
Damien Georgee9dac3b2014-09-29 22:10:41 +0100800 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100801 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100802 #endif
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200803
Damien George99886182015-04-06 22:38:53 +0100804 #if N_ARM
Damien Georgee9dac3b2014-09-29 22:10:41 +0100805 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100806 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100807 #endif
Damien13ed3a62013-10-08 09:05:10 +0100808}
809
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200810STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100811 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100812 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100813 }
Damien George99886182015-04-06 22:38:53 +0100814
815 if (!emit->do_viper_types) {
816 // write dummy code info (for mp_setup_code_state to parse) and arg names
817 emit->code_info_offset = ASM_GET_CODE_POS(emit->as);
818 ASM_DATA(emit->as, 1, emit->code_info_size);
819 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
820 emit->code_info_size = ASM_GET_CODE_POS(emit->as) - emit->code_info_offset;
Damien George9a42eb52015-05-06 13:55:33 +0100821 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100822 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100823 qstr qst = MP_QSTR__star_;
824 for (int j = 0; j < emit->scope->id_info_len; ++j) {
825 id_info_t *id = &emit->scope->id_info[j];
826 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
827 qst = id->qst;
828 break;
829 }
830 }
831 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100832 }
833
834 // bytecode prelude: initialise closed over variables
835 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
836 for (int i = 0; i < emit->scope->id_info_len; i++) {
837 id_info_t *id = &emit->scope->id_info[i];
838 if (id->kind == ID_INFO_KIND_CELL) {
839 assert(id->local_num < 255);
840 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
841 }
842 }
843 ASM_DATA(emit->as, 1, 255); // end of list sentinel
844 }
845
Damien Georgec90f59e2014-09-06 23:06:36 +0100846 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100847
848 // check stack is back to zero size
849 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100850 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100851 }
852
Damien George36db6bc2014-05-07 17:24:22 +0100853 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100854 void *f = ASM_GET_CODE(emit->as);
855 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100856
857 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100858 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100859 mp_uint_t type_sig = emit->return_vtype & 3;
860 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
861 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
862 }
863
Damien George99886182015-04-06 22:38:53 +0100864 mp_emit_glue_assign_native(emit->scope->raw_code,
865 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
866 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
867 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100868 }
869}
870
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200871STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100872 return emit->last_emit_was_return_value;
873}
874
Damien Georged6230f62014-09-23 14:10:03 +0000875STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000876 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100877 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100878 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100879 emit->scope->stack_size = emit->stack_size;
880 }
Damien George21ca2d72014-10-19 19:00:51 +0100881#ifdef DEBUG_PRINT
882 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
883 for (int i = 0; i < emit->stack_size; i++) {
884 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000885 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100886 }
887 DEBUG_printf("\n");
888#endif
Damien13ed3a62013-10-08 09:05:10 +0100889}
890
Damien Georged6230f62014-09-23 14:10:03 +0000891STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100892 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000893 // If we are adjusting the stack in a positive direction (pushing) then we
894 // need to fill in values for the stack kind and vtype of the newly-pushed
895 // entries. These should be set to "value" (ie not reg or imm) because we
896 // should only need to adjust the stack due to a jump to this part in the
897 // code (and hence we have settled the stack before the jump).
898 for (mp_int_t i = 0; i < delta; i++) {
899 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
900 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100901 // TODO we don't know the vtype to use here. At the moment this is a
902 // hack to get the case of multi comparison working.
903 if (delta == 1) {
904 si->vtype = emit->saved_stack_vtype;
905 } else {
906 si->vtype = VTYPE_PYOBJ;
907 }
Damien Georged6230f62014-09-23 14:10:03 +0000908 }
909 adjust_stack(emit, delta);
910}
911
912STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000913 (void)emit;
914 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000915}
916
Damienff8ed772013-10-08 22:18:32 +0100917/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200918STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100919 adjust_stack(emit, stack_size_delta);
920 emit->last_emit_was_return_value = false;
921}
Damienff8ed772013-10-08 22:18:32 +0100922*/
Damien13ed3a62013-10-08 09:05:10 +0100923
Damienff8ed772013-10-08 22:18:32 +0100924// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000925STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100926 emit->last_emit_was_return_value = false;
927 // settle the stack
928 /*
929 if (regs_needed != 0) {
930 for (int i = 0; i < emit->stack_size; i++) {
931 switch (emit->stack_info[i].kind) {
932 case STACK_VALUE:
933 break;
934
935 case STACK_REG:
936 // TODO only push reg if in regs_needed
937 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000938 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100939 break;
940
941 case STACK_IMM:
942 // don't think we ever need to push imms for settling
943 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
944 break;
945 }
946 }
947 }
948 */
Damien13ed3a62013-10-08 09:05:10 +0100949}
950
Damien George3112cde2014-09-29 18:45:42 +0100951// depth==0 is top, depth==1 is before top, etc
952STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
953 return &emit->stack_info[emit->stack_size - 1 - depth];
954}
955
956// depth==0 is top, depth==1 is before top, etc
957STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
958 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100959}
Damien13ed3a62013-10-08 09:05:10 +0100960
Damiend2755ec2013-10-16 23:58:48 +0100961// pos=1 is TOS, pos=2 is next, etc
962// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200963STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100964 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100965 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100966 if (i != skip_stack_pos) {
967 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000968 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100969 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000970 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100971 }
Damienff8ed772013-10-08 22:18:32 +0100972 }
973 }
974}
Damien13ed3a62013-10-08 09:05:10 +0100975
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200976STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100977 for (int i = 0; i < emit->stack_size; i++) {
978 stack_info_t *si = &emit->stack_info[i];
979 if (si->kind == STACK_REG) {
980 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000981 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100982 }
Damien13ed3a62013-10-08 09:05:10 +0100983 }
984}
985
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200986STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000987 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100988 for (int i = 0; i < emit->stack_size; i++) {
989 stack_info_t *si = &emit->stack_info[i];
990 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000991 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100992 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000993 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100994 }
995 }
996 for (int i = 0; i < emit->stack_size; i++) {
997 stack_info_t *si = &emit->stack_info[i];
998 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000999 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +01001000 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +00001001 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +01001002 }
1003 }
1004}
1005
1006// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001007STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001008 need_reg_single(emit, reg_dest, pos);
1009 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001010 *vtype = si->vtype;
1011 switch (si->kind) {
1012 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001013 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001014 break;
1015
Damienff8ed772013-10-08 22:18:32 +01001016 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001017 if (si->data.u_reg != reg_dest) {
1018 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001019 }
1020 break;
1021
Damienff8ed772013-10-08 22:18:32 +01001022 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001023 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001024 break;
1025 }
Damien13ed3a62013-10-08 09:05:10 +01001026}
1027
Damien Georgee9dac3b2014-09-29 22:10:41 +01001028// does an efficient X=pop(); discard(); push(X)
1029// needs a (non-temp) register in case the poped element was stored in the stack
1030STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1031 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1032 si[0] = si[1];
1033 if (si->kind == STACK_VALUE) {
1034 // if folded element was on the stack we need to put it in a register
1035 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1036 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001037 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001038 }
1039 adjust_stack(emit, -1);
1040}
1041
1042// If stacked value is in a register and the register is not r1 or r2, then
1043// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1044STATIC 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 +01001045 emit->last_emit_was_return_value = false;
1046 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001047 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001048 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001049 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001050 need_reg_single(emit, *reg_dest, 1);
1051 } else {
1052 emit_access_stack(emit, 1, vtype, *reg_dest);
1053 }
1054 adjust_stack(emit, -1);
1055}
1056
Damien Georgee6ce10a2014-09-06 18:38:20 +01001057STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001058 emit->last_emit_was_return_value = false;
1059 adjust_stack(emit, -1);
1060}
1061
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001062STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001063 emit->last_emit_was_return_value = false;
1064 emit_access_stack(emit, 1, vtype, reg_dest);
1065 adjust_stack(emit, -1);
1066}
1067
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001068STATIC 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 +01001069 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001070 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001071}
1072
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001073STATIC 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 +01001074 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001075 emit_pre_pop_reg(emit, vtypeb, regb);
1076 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001077}
1078
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001079STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001080 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001081}
1082
Damien Georgee9dac3b2014-09-29 22:10:41 +01001083STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1084 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1085 si->vtype = new_vtype;
1086}
1087
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001088STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001089 stack_info_t *si = &emit->stack_info[emit->stack_size];
1090 si->vtype = vtype;
1091 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001092 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001093 adjust_stack(emit, 1);
1094}
1095
Damien George40f3c022014-07-03 13:25:24 +01001096STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +01001097 stack_info_t *si = &emit->stack_info[emit->stack_size];
1098 si->vtype = vtype;
1099 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +00001100 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001101 adjust_stack(emit, 1);
1102}
1103
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001104STATIC 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 +01001105 emit_post_push_reg(emit, vtypea, rega);
1106 emit_post_push_reg(emit, vtypeb, regb);
1107}
1108
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001109STATIC 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 +01001110 emit_post_push_reg(emit, vtypea, rega);
1111 emit_post_push_reg(emit, vtypeb, regb);
1112 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001113}
1114
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001115STATIC 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 +01001116 emit_post_push_reg(emit, vtypea, rega);
1117 emit_post_push_reg(emit, vtypeb, regb);
1118 emit_post_push_reg(emit, vtypec, regc);
1119 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001120}
1121
Damien George7fe21912014-08-16 22:31:57 +01001122STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001123 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001124 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001125}
1126
Damien George7fe21912014-08-16 22:31:57 +01001127STATIC 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 +01001128 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001129 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1130 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001131}
1132
Damien George40f3c022014-07-03 13:25:24 +01001133// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001134STATIC 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 +01001135 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001136 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1137 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001138}
1139
Damien George7fe21912014-08-16 22:31:57 +01001140STATIC 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 +00001141 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001142 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1143 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1144 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001145}
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_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 +01001149 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001150 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1151 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1152 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1153 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001154}
1155
Damien George86de21b2014-08-16 22:06:11 +01001156// vtype of all n_pop objects is VTYPE_PYOBJ
1157// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1158// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1159// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1160STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1161 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001162
Damien George86de21b2014-08-16 22:06:11 +01001163 // First, store any immediate values to their respective place on the stack.
1164 for (mp_uint_t i = 0; i < n_pop; i++) {
1165 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1166 // must push any imm's to stack
1167 // must convert them to VTYPE_PYOBJ for viper code
1168 if (si->kind == STACK_IMM) {
1169 si->kind = STACK_VALUE;
1170 switch (si->vtype) {
1171 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001172 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 +01001173 break;
1174 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001175 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001176 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 +01001177 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001178 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 +01001179 }
1180 si->vtype = VTYPE_PYOBJ;
1181 break;
1182 case VTYPE_INT:
1183 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001184 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 +01001185 si->vtype = VTYPE_PYOBJ;
1186 break;
1187 default:
1188 // not handled
1189 assert(0);
1190 }
1191 }
1192
1193 // verify that this value is on the stack
1194 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001195 }
Damien George86de21b2014-08-16 22:06:11 +01001196
1197 // Second, convert any non-VTYPE_PYOBJ to that type.
1198 for (mp_uint_t i = 0; i < n_pop; i++) {
1199 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1200 if (si->vtype != VTYPE_PYOBJ) {
1201 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001202 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001203 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 +01001204 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001205 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001206 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001207 }
1208 }
1209
1210 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1211 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001212 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001213}
1214
1215// vtype of all n_push objects is VTYPE_PYOBJ
1216STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1217 need_reg_all(emit);
1218 for (mp_uint_t i = 0; i < n_push; i++) {
1219 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1220 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1221 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001222 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001223 adjust_stack(emit, n_push);
1224}
1225
Damien George7ff996c2014-09-08 23:05:16 +01001226STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001227 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001228 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001229 // need to commit stack because we can jump here from elsewhere
1230 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001231 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001232 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001233}
1234
Damien Georgecdd96df2014-04-06 12:58:40 +01001235STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1236 DEBUG_printf("import_name %s\n", qstr_str(qst));
1237 vtype_kind_t vtype_fromlist;
1238 vtype_kind_t vtype_level;
1239 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1240 assert(vtype_fromlist == VTYPE_PYOBJ);
1241 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001242 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001243 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001244}
1245
Damien Georgecdd96df2014-04-06 12:58:40 +01001246STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1247 DEBUG_printf("import_from %s\n", qstr_str(qst));
1248 emit_native_pre(emit);
1249 vtype_kind_t vtype_module;
1250 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1251 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001252 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001253 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001254}
1255
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001256STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001257 DEBUG_printf("import_star\n");
1258 vtype_kind_t vtype_module;
1259 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1260 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001261 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001262 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001263}
1264
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001265STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001266 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001267 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001268 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001269 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001270 if (emit->do_viper_types) {
1271 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001272 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1273 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1274 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001275 no_other_choice1:
1276 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1277 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001278 }
1279 } else {
1280 vtype = VTYPE_PYOBJ;
1281 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001282 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1283 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1284 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001285 no_other_choice2:
1286 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1287 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001288 }
1289 }
1290 emit_post_push_imm(emit, vtype, val);
1291}
1292
Damien George40f3c022014-07-03 13:25:24 +01001293STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001294 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001295 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001296 if (emit->do_viper_types) {
1297 emit_post_push_imm(emit, VTYPE_INT, arg);
1298 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001299 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001300 }
1301}
1302
Damien George59fba2d2015-06-25 14:42:13 +00001303STATIC void emit_native_load_const_str(emit_t *emit, qstr qst) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001304 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001305 // TODO: Eventually we want to be able to work with raw pointers in viper to
1306 // do native array access. For now we just load them as any other object.
1307 /*
Damien13ed3a62013-10-08 09:05:10 +01001308 if (emit->do_viper_types) {
1309 // not implemented properly
1310 // load a pointer to the asciiz string?
1311 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001312 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001313 } else
1314 */
1315 {
Damien George59fba2d2015-06-25 14:42:13 +00001316 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien13ed3a62013-10-08 09:05:10 +01001317 }
1318}
1319
Damien Georgedab13852015-01-13 15:55:54 +00001320STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1321 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001322 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001323 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1324 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1325}
1326
Damien George3558f622014-04-20 17:50:40 +01001327STATIC void emit_native_load_null(emit_t *emit) {
1328 emit_native_pre(emit);
1329 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1330}
1331
Damien George0abb5602015-01-16 12:24:49 +00001332STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1333 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001334 vtype_kind_t vtype = emit->local_vtype[local_num];
1335 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001336 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001337 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001338 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001339 if (local_num == 0) {
1340 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001341 } else if (local_num == 1) {
1342 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1343 } else if (local_num == 2) {
1344 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001345 } else {
Damien George0b610de2014-09-29 16:25:04 +01001346 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001347 if (emit->do_viper_types) {
1348 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1349 } else {
1350 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1351 }
Damien George0b610de2014-09-29 16:25:04 +01001352 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001353 }
Damien13ed3a62013-10-08 09:05:10 +01001354}
1355
Damien George7ff996c2014-09-08 23:05:16 +01001356STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001357 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1358 need_reg_single(emit, REG_RET, 0);
1359 emit_native_load_fast(emit, qst, local_num);
1360 vtype_kind_t vtype;
1361 int reg_base = REG_RET;
1362 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1363 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1364 // closed over vars are always Python objects
1365 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001366}
1367
Damien George7ff996c2014-09-08 23:05:16 +01001368STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001369 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001370 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001371 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001372 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1373}
1374
Damien George7ff996c2014-09-08 23:05:16 +01001375STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001376 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001377 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001378 // check for builtin casting operators
1379 if (emit->do_viper_types && qst == MP_QSTR_int) {
1380 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1381 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1382 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1383 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1384 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1385 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1386 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1387 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1388 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1389 } else {
1390 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1391 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1392 }
Damien13ed3a62013-10-08 09:05:10 +01001393}
1394
Damien George7ff996c2014-09-08 23:05:16 +01001395STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001396 // depends on type of subject:
1397 // - integer, function, pointer to integers: error
1398 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001399 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001400 vtype_kind_t vtype_base;
1401 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1402 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001403 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001404 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1405}
1406
Damien George7ff996c2014-09-08 23:05:16 +01001407STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001408 vtype_kind_t vtype_base;
1409 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1410 assert(vtype_base == VTYPE_PYOBJ);
1411 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001412 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001413}
1414
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001415STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001416 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001417 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001418 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001419}
1420
Damien George729f7b42014-04-17 22:10:53 +01001421STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001422 DEBUG_printf("load_subscr\n");
1423 // need to compile: base[index]
1424
1425 // pop: index, base
1426 // optimise case where index is an immediate
1427 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1428
1429 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001430 // standard Python subscr
1431 // TODO factor this implicit cast code with other uses of it
1432 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1433 if (vtype_index == VTYPE_PYOBJ) {
1434 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_2);
1435 } else {
1436 emit_pre_pop_reg(emit, &vtype_index, REG_ARG_1);
1437 emit_call_with_imm_arg(emit, MP_F_CONVERT_NATIVE_TO_OBJ, vtype_index, REG_ARG_2); // arg2 = type
1438 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
1439 }
1440 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001441 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 +01001442 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1443 } else {
Damien George91cfd412014-10-12 16:59:29 +01001444 // viper load
1445 // TODO The different machine architectures have very different
1446 // capabilities and requirements for loads, so probably best to
1447 // write a completely separate load-optimiser for each one.
1448 stack_info_t *top = peek_stack(emit, 0);
1449 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1450 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001451 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001452 emit_pre_pop_discard(emit); // discard index
1453 int reg_base = REG_ARG_1;
1454 int reg_index = REG_ARG_2;
1455 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1456 switch (vtype_base) {
1457 case VTYPE_PTR8: {
1458 // pointer to 8-bit memory
1459 // TODO optimise to use thumb ldrb r1, [r2, r3]
1460 if (index_value != 0) {
1461 // index is non-zero
1462 #if N_THUMB
1463 if (index_value > 0 && index_value < 32) {
1464 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1465 break;
1466 }
1467 #endif
1468 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1469 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1470 reg_base = reg_index;
1471 }
1472 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1473 break;
1474 }
1475 case VTYPE_PTR16: {
1476 // pointer to 16-bit memory
1477 if (index_value != 0) {
1478 // index is a non-zero immediate
1479 #if N_THUMB
1480 if (index_value > 0 && index_value < 32) {
1481 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1482 break;
1483 }
1484 #endif
1485 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1486 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1487 reg_base = reg_index;
1488 }
1489 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1490 break;
1491 }
1492 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001493 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1494 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001495 }
1496 } else {
1497 // index is not an immediate
1498 vtype_kind_t vtype_index;
1499 int reg_index = REG_ARG_2;
1500 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1501 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1502 switch (vtype_base) {
1503 case VTYPE_PTR8: {
1504 // pointer to 8-bit memory
1505 // TODO optimise to use thumb ldrb r1, [r2, r3]
1506 assert(vtype_index == VTYPE_INT);
1507 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1508 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1509 break;
1510 }
1511 case VTYPE_PTR16: {
1512 // pointer to 16-bit memory
1513 assert(vtype_index == VTYPE_INT);
1514 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1515 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1516 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1517 break;
1518 }
1519 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001520 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1521 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001522 }
1523 }
1524 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001525 }
1526}
1527
Damien George7ff996c2014-09-08 23:05:16 +01001528STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001529 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001530 if (local_num == 0) {
1531 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001532 } else if (local_num == 1) {
1533 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1534 } else if (local_num == 2) {
1535 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001536 } else {
Damien George0b610de2014-09-29 16:25:04 +01001537 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001538 if (emit->do_viper_types) {
1539 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1540 } else {
1541 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1542 }
Damien13ed3a62013-10-08 09:05:10 +01001543 }
Damien13ed3a62013-10-08 09:05:10 +01001544 emit_post(emit);
1545
1546 // check types
1547 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1548 // first time this local is assigned, so give it a type of the object stored in it
1549 emit->local_vtype[local_num] = vtype;
1550 } else if (emit->local_vtype[local_num] != vtype) {
1551 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001552 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1553 "local '%q' has type '%q' but source is '%q'",
1554 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001555 }
1556}
1557
Damien George7ff996c2014-09-08 23:05:16 +01001558STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001559 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1560 need_reg_single(emit, REG_TEMP0, 0);
1561 need_reg_single(emit, REG_TEMP1, 0);
1562 emit_native_load_fast(emit, qst, local_num);
1563 vtype_kind_t vtype;
1564 int reg_base = REG_TEMP0;
1565 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1566 int reg_src = REG_TEMP1;
1567 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1568 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1569 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001570}
1571
Damien George7ff996c2014-09-08 23:05:16 +01001572STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001573 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001574 vtype_kind_t vtype;
1575 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1576 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001577 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001578 emit_post(emit);
1579}
1580
Damien George7ff996c2014-09-08 23:05:16 +01001581STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001582 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001583 if (vtype == VTYPE_PYOBJ) {
1584 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1585 } else {
1586 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001587 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 +01001588 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001589 }
Damien George7ff996c2014-09-08 23:05:16 +01001590 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001591 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001592}
1593
Damien George7ff996c2014-09-08 23:05:16 +01001594STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001595 vtype_kind_t vtype_base, vtype_val;
1596 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1597 assert(vtype_base == VTYPE_PYOBJ);
1598 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001599 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001600 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001601}
1602
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001603STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001604 DEBUG_printf("store_subscr\n");
1605 // need to compile: base[index] = value
1606
1607 // pop: index, base, value
1608 // optimise case where index is an immediate
1609 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1610
1611 if (vtype_base == VTYPE_PYOBJ) {
Damien George4d9cad12015-06-04 11:52:16 +01001612 // standard Python subscr
1613 vtype_kind_t vtype_index = peek_vtype(emit, 0);
1614 vtype_kind_t vtype_value = peek_vtype(emit, 2);
1615 if (vtype_index != VTYPE_PYOBJ || vtype_value != VTYPE_PYOBJ) {
1616 // need to implicitly convert non-objects to objects
1617 // TODO do this properly
1618 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_1, 3);
1619 adjust_stack(emit, 3);
1620 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01001621 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 +01001622 emit_call(emit, MP_F_OBJ_SUBSCR);
1623 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001624 // viper store
1625 // TODO The different machine architectures have very different
1626 // capabilities and requirements for stores, so probably best to
1627 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001628 stack_info_t *top = peek_stack(emit, 0);
1629 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1630 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001631 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001632 emit_pre_pop_discard(emit); // discard index
1633 vtype_kind_t vtype_value;
1634 int reg_base = REG_ARG_1;
1635 int reg_index = REG_ARG_2;
1636 int reg_value = REG_ARG_3;
1637 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001638 #if N_X86
1639 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1640 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1641 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001642 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001643 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001644 switch (vtype_base) {
1645 case VTYPE_PTR8: {
1646 // pointer to 8-bit memory
1647 // TODO optimise to use thumb strb r1, [r2, r3]
1648 if (index_value != 0) {
1649 // index is non-zero
1650 #if N_THUMB
1651 if (index_value > 0 && index_value < 32) {
1652 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1653 break;
1654 }
1655 #endif
1656 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001657 #if N_ARM
1658 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1659 return;
1660 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001661 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1662 reg_base = reg_index;
1663 }
1664 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1665 break;
1666 }
1667 case VTYPE_PTR16: {
1668 // pointer to 16-bit memory
1669 if (index_value != 0) {
1670 // index is a non-zero immediate
1671 #if N_THUMB
1672 if (index_value > 0 && index_value < 32) {
1673 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1674 break;
1675 }
1676 #endif
1677 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001678 #if N_ARM
1679 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1680 return;
1681 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001682 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1683 reg_base = reg_index;
1684 }
1685 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1686 break;
1687 }
1688 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001689 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1690 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001691 }
1692 } else {
1693 // index is not an immediate
1694 vtype_kind_t vtype_index, vtype_value;
1695 int reg_index = REG_ARG_2;
1696 int reg_value = REG_ARG_3;
1697 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1698 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001699 #if N_X86
1700 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1701 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1702 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001703 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001704 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001705 switch (vtype_base) {
1706 case VTYPE_PTR8: {
1707 // pointer to 8-bit memory
1708 // TODO optimise to use thumb strb r1, [r2, r3]
1709 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001710 #if N_ARM
1711 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1712 break;
1713 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001714 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1715 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1716 break;
1717 }
1718 case VTYPE_PTR16: {
1719 // pointer to 16-bit memory
1720 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001721 #if N_ARM
1722 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1723 break;
1724 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001725 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1726 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1727 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1728 break;
1729 }
1730 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001731 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1732 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001733 }
1734 }
1735
1736 }
Damien13ed3a62013-10-08 09:05:10 +01001737}
1738
Damien George7ff996c2014-09-08 23:05:16 +01001739STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001740 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1741 // to mark deleted vars but then every var would need to be checked on
1742 // each access. Very inefficient, so just set value to None to enable GC.
1743 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1744 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001745}
1746
Damien George7ff996c2014-09-08 23:05:16 +01001747STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001748 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001749 (void)emit;
1750 (void)qst;
1751 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001752}
1753
Damien Georgee6ce10a2014-09-06 18:38:20 +01001754STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1755 emit_native_pre(emit);
1756 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1757 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001758}
1759
Damien Georgee6ce10a2014-09-06 18:38:20 +01001760STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1761 emit_native_pre(emit);
1762 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1763 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001764}
1765
Damien Georgee6ce10a2014-09-06 18:38:20 +01001766STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001767 vtype_kind_t vtype_base;
1768 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1769 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001770 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 +01001771 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001772}
1773
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001774STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001775 vtype_kind_t vtype_index, vtype_base;
1776 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1777 assert(vtype_index == VTYPE_PYOBJ);
1778 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001779 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 +01001780}
1781
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001782STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001783 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001784 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001785 int reg = REG_TEMP0;
1786 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1787 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001788}
1789
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001790STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001791 vtype_kind_t vtype0, vtype1;
1792 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1793 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1794}
1795
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001796STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001797 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001798 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001799 emit_post(emit);
1800}
1801
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001802STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001803 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001804 vtype_kind_t vtype0, vtype1;
1805 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1806 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001807}
1808
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001809STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001810 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001811 vtype_kind_t vtype0, vtype1, vtype2;
1812 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1813 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1814}
1815
Damien George7ff996c2014-09-08 23:05:16 +01001816STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001817 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001818 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001819 // need to commit stack because we are jumping elsewhere
1820 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001821 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001822 emit_post(emit);
1823}
1824
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001825STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001826 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001827 if (vtype == VTYPE_PYOBJ) {
1828 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1829 if (!pop) {
1830 adjust_stack(emit, 1);
1831 }
1832 emit_call(emit, MP_F_OBJ_IS_TRUE);
1833 } else {
1834 emit_pre_pop_reg(emit, &vtype, REG_RET);
1835 if (!pop) {
1836 adjust_stack(emit, 1);
1837 }
1838 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1839 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1840 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1841 }
Damien13ed3a62013-10-08 09:05:10 +01001842 }
Damien George21ca2d72014-10-19 19:00:51 +01001843 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1844 // can use it. This is a bit of a hack.
1845 if (!pop) {
1846 emit->saved_stack_vtype = vtype;
1847 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001848 // need to commit stack because we may jump elsewhere
1849 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001850}
1851
Damien George63f38322015-02-28 15:04:06 +00001852STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1853 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001854 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001855 if (cond) {
1856 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1857 } else {
1858 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1859 }
Damien1a6633a2013-11-03 13:58:19 +00001860 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001861}
Damien1a6633a2013-11-03 13:58:19 +00001862
Damien George63f38322015-02-28 15:04:06 +00001863STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1864 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001865 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001866 if (cond) {
1867 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1868 } else {
1869 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1870 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001871 adjust_stack(emit, -1);
1872 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001873}
1874
Damien George7ff996c2014-09-08 23:05:16 +01001875STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001876 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001877 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001878}
Damien Georgea32c1e42014-05-07 18:30:52 +01001879
Damien George7ff996c2014-09-08 23:05:16 +01001880STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001881 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001882 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001883}
Damien Georgea32c1e42014-05-07 18:30:52 +01001884
Damien George7ff996c2014-09-08 23:05:16 +01001885STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001886 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001887 (void)emit;
1888 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001889 assert(0);
1890}
Damien Georgeb601d952014-06-30 05:17:25 +01001891
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001892STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001893 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001894 assert(0);
1895}
Damien Georgeb601d952014-06-30 05:17:25 +01001896
Damien George7ff996c2014-09-08 23:05:16 +01001897STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001898 emit_native_pre(emit);
1899 // need to commit stack because we may jump elsewhere
1900 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001901 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 +01001902 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001903 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001904 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001905}
Damien Georgeb601d952014-06-30 05:17:25 +01001906
Damien George7ff996c2014-09-08 23:05:16 +01001907STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001908 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001909}
Damien Georgeb601d952014-06-30 05:17:25 +01001910
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001911STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001912 // logic:
1913 // exc = pop_stack
1914 // if exc == None: pass
1915 // else: raise exc
1916 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1917 vtype_kind_t vtype;
1918 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1919 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001920 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001921}
Damiend2755ec2013-10-16 23:58:48 +01001922
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001923STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001924 // perhaps the difficult one, as we want to rewrite for loops using native code
1925 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001926
1927 vtype_kind_t vtype;
1928 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1929 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001930 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001931 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001932}
Damiend2755ec2013-10-16 23:58:48 +01001933
Damien George7ff996c2014-09-08 23:05:16 +01001934STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001935 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001936 vtype_kind_t vtype;
1937 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1938 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001939 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001940 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1941 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001942 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1943}
1944
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001945STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001946 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001947 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001948 adjust_stack(emit, -1);
1949 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001950}
1951
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001952STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001953 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001954 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001955 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001956 emit_post(emit);
1957}
1958
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001959STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001960 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001961 /*
1962 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001963 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001964 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001965 emit_post(emit);
1966 */
Damien13ed3a62013-10-08 09:05:10 +01001967}
1968
Damien Georged17926d2014-03-30 13:35:08 +01001969STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001970 vtype_kind_t vtype;
1971 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1972 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001973 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001974 // we need to synthesise this operation by converting to bool first
1975 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001976 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001977 }
Damien Georged6230f62014-09-23 14:10:03 +00001978 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1979 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001980}
1981
Damien Georged17926d2014-03-30 13:35:08 +01001982STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001983 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001984 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1985 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001986 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001987 #if N_X64 || N_X86
1988 // special cases for x86 and shifting
1989 if (op == MP_BINARY_OP_LSHIFT
1990 || op == MP_BINARY_OP_INPLACE_LSHIFT
1991 || op == MP_BINARY_OP_RSHIFT
1992 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1993 #if N_X64
1994 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1995 #else
1996 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1997 #endif
1998 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1999 ASM_LSL_REG(emit->as, REG_RET);
2000 } else {
2001 ASM_ASR_REG(emit->as, REG_RET);
2002 }
2003 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
2004 return;
2005 }
2006 #endif
2007 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002008 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002009 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
2010 if (0) {
2011 // dummy
2012 #if !(N_X64 || N_X86)
2013 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
2014 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002015 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002016 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2017 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2018 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2019 #endif
Damien George1ef23482014-10-12 14:21:06 +01002020 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2021 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2022 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2023 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2024 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2025 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2026 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2027 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2028 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002029 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2030 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2031 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2032 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2033 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2034 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George567b3492015-06-04 14:00:29 +00002035 } else if (op == MP_BINARY_OP_MULTIPLY || op == MP_BINARY_OP_INPLACE_MULTIPLY) {
2036 ASM_MUL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2037 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002038 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2039 // comparison ops are (in enum order):
2040 // MP_BINARY_OP_LESS
2041 // MP_BINARY_OP_MORE
2042 // MP_BINARY_OP_EQUAL
2043 // MP_BINARY_OP_LESS_EQUAL
2044 // MP_BINARY_OP_MORE_EQUAL
2045 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002046 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002047 #if N_X64
2048 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2049 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2050 static byte ops[6] = {
2051 ASM_X64_CC_JL,
2052 ASM_X64_CC_JG,
2053 ASM_X64_CC_JE,
2054 ASM_X64_CC_JLE,
2055 ASM_X64_CC_JGE,
2056 ASM_X64_CC_JNE,
2057 };
2058 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2059 #elif N_X86
2060 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2061 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2062 static byte ops[6] = {
2063 ASM_X86_CC_JL,
2064 ASM_X86_CC_JG,
2065 ASM_X86_CC_JE,
2066 ASM_X86_CC_JLE,
2067 ASM_X86_CC_JGE,
2068 ASM_X86_CC_JNE,
2069 };
2070 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2071 #elif N_THUMB
2072 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2073 static uint16_t ops[6] = {
2074 ASM_THUMB_OP_ITE_GE,
2075 ASM_THUMB_OP_ITE_GT,
2076 ASM_THUMB_OP_ITE_EQ,
2077 ASM_THUMB_OP_ITE_GT,
2078 ASM_THUMB_OP_ITE_GE,
2079 ASM_THUMB_OP_ITE_EQ,
2080 };
2081 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2082 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2083 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2084 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2085 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002086 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2087 static uint ccs[6] = {
2088 ASM_ARM_CC_LT,
2089 ASM_ARM_CC_GT,
2090 ASM_ARM_CC_EQ,
2091 ASM_ARM_CC_LE,
2092 ASM_ARM_CC_GE,
2093 ASM_ARM_CC_NE,
2094 };
2095 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002096 #else
2097 #error not implemented
2098 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002099 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2100 } else {
2101 // TODO other ops not yet implemented
Damien George4d9cad12015-06-04 11:52:16 +01002102 adjust_stack(emit, 1);
2103 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2104 "binary op %q not implemented", mp_binary_op_method_name[op]);
Damien Georgebc1d3692014-01-11 09:47:06 +00002105 }
Damien13ed3a62013-10-08 09:05:10 +01002106 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002107 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002108 bool invert = false;
2109 if (op == MP_BINARY_OP_NOT_IN) {
2110 invert = true;
2111 op = MP_BINARY_OP_IN;
2112 } else if (op == MP_BINARY_OP_IS_NOT) {
2113 invert = true;
2114 op = MP_BINARY_OP_IS;
2115 }
Damien George7fe21912014-08-16 22:31:57 +01002116 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002117 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002118 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002119 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2120 }
Damien13ed3a62013-10-08 09:05:10 +01002121 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2122 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002123 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002124 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2125 "can't do binary op between '%q' and '%q'",
2126 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002127 }
2128}
2129
Damien George7ff996c2014-09-08 23:05:16 +01002130STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002131 // for viper: call runtime, with types of args
2132 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002133 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002134 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002135 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002136 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002137}
2138
Damien George7ff996c2014-09-08 23:05:16 +01002139STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002140 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002141 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002142 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002143 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2144}
2145
Damien George7ff996c2014-09-08 23:05:16 +01002146STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002147 // only used in list comprehension
2148 vtype_kind_t vtype_list, vtype_item;
2149 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2150 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2151 assert(vtype_list == VTYPE_PYOBJ);
2152 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002153 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002154 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002155}
2156
Damien George7ff996c2014-09-08 23:05:16 +01002157STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002158 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002159 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002160 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2161}
2162
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002163STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002164 vtype_kind_t vtype_key, vtype_value, vtype_map;
2165 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
2166 assert(vtype_key == VTYPE_PYOBJ);
2167 assert(vtype_value == VTYPE_PYOBJ);
2168 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002169 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002170 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2171}
2172
Damien George7ff996c2014-09-08 23:05:16 +01002173STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002174 // only used in list comprehension
2175 vtype_kind_t vtype_map, vtype_key, vtype_value;
2176 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2177 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2178 assert(vtype_map == VTYPE_PYOBJ);
2179 assert(vtype_key == VTYPE_PYOBJ);
2180 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002181 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002182 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002183}
2184
Damien Georgee37dcaa2014-12-27 17:07:16 +00002185#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002186STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002187 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002188 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002189 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002190 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2191}
2192
Damien George7ff996c2014-09-08 23:05:16 +01002193STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002194 // only used in set comprehension
2195 vtype_kind_t vtype_set, vtype_item;
2196 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2197 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2198 assert(vtype_set == VTYPE_PYOBJ);
2199 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002200 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002201 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002202}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002203#endif
Damiend2755ec2013-10-16 23:58:48 +01002204
Damien George83204f32014-12-27 17:20:41 +00002205#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002206STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002207 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002208 if (n_args == 2) {
2209 vtype_kind_t vtype_start, vtype_stop;
2210 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2211 assert(vtype_start == VTYPE_PYOBJ);
2212 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002213 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 +01002214 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2215 } else {
2216 assert(n_args == 3);
2217 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2218 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
2219 assert(vtype_start == VTYPE_PYOBJ);
2220 assert(vtype_stop == VTYPE_PYOBJ);
2221 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002222 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002223 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2224 }
Damien13ed3a62013-10-08 09:05:10 +01002225}
Damien George83204f32014-12-27 17:20:41 +00002226#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002227
Damien George7ff996c2014-09-08 23:05:16 +01002228STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002229 DEBUG_printf("unpack_sequence %d\n", n_args);
2230 vtype_kind_t vtype_base;
2231 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2232 assert(vtype_base == VTYPE_PYOBJ);
2233 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002234 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002235}
Damien Georgecdd96df2014-04-06 12:58:40 +01002236
Damien George7ff996c2014-09-08 23:05:16 +01002237STATIC 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 +01002238 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2239 vtype_kind_t vtype_base;
2240 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2241 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002242 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 +01002243 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 +01002244}
2245
Damien George7ff996c2014-09-08 23:05:16 +01002246STATIC 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 +01002247 // 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 +00002248 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002249 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002250 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 +01002251 } else {
2252 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2253 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2254 assert(vtype_def_tuple == VTYPE_PYOBJ);
2255 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002256 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 +01002257 }
Damien13ed3a62013-10-08 09:05:10 +01002258 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2259}
2260
Damien George7ff996c2014-09-08 23:05:16 +01002261STATIC 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 +00002262 emit_native_pre(emit);
2263 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2264 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2265 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2266 } else {
2267 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2268 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2269 }
2270 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2271 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2272 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002273}
2274
Damien George7ff996c2014-09-08 23:05:16 +01002275STATIC 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 +00002276 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2277
Damien Georgee9dac3b2014-09-29 22:10:41 +01002278 // TODO: in viper mode, call special runtime routine with type info for args,
2279 // and wanted type info for return, to remove need for boxing/unboxing
2280
Damien Georgee9dac3b2014-09-29 22:10:41 +01002281 emit_native_pre(emit);
2282 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2283 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2284 // casting operator
2285 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002286 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002287 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002288 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002289 switch (peek_vtype(emit, 0)) {
2290 case VTYPE_PYOBJ: {
2291 vtype_kind_t vtype;
2292 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2293 emit_pre_pop_discard(emit);
2294 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2295 emit_post_push_reg(emit, vtype_cast, REG_RET);
2296 break;
2297 }
2298 case VTYPE_BOOL:
2299 case VTYPE_INT:
2300 case VTYPE_UINT:
2301 case VTYPE_PTR:
2302 case VTYPE_PTR8:
2303 case VTYPE_PTR16:
2304 case VTYPE_PTR_NONE:
2305 emit_fold_stack_top(emit, REG_ARG_1);
2306 emit_post_top_set_vtype(emit, vtype_cast);
2307 break;
2308 default:
2309 assert(!"TODO: convert obj to int");
2310 }
2311 } else {
Damien13ed3a62013-10-08 09:05:10 +01002312 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002313 if (star_flags) {
2314 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2315 // load dummy entry for non-existent pos_seq
2316 emit_native_load_null(emit);
2317 emit_native_rot_two(emit);
2318 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2319 // load dummy entry for non-existent kw_dict
2320 emit_native_load_null(emit);
2321 }
2322 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2323 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);
2324 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2325 } else {
2326 if (n_positional != 0 || n_keyword != 0) {
2327 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2328 }
2329 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2330 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2331 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2332 }
Damien Georgecd82e022014-02-02 13:11:48 +00002333 }
Damien13ed3a62013-10-08 09:05:10 +01002334}
2335
Damien George7ff996c2014-09-08 23:05:16 +01002336STATIC 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 +01002337 if (star_flags) {
2338 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2339 // load dummy entry for non-existent pos_seq
2340 emit_native_load_null(emit);
2341 emit_native_rot_two(emit);
2342 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2343 // load dummy entry for non-existent kw_dict
2344 emit_native_load_null(emit);
2345 }
2346 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2347 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);
2348 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2349 } else {
2350 emit_native_pre(emit);
2351 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
2352 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2353 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2354 }
Damien13ed3a62013-10-08 09:05:10 +01002355}
2356
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002357STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002358 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002359 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002360 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2361 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002362 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002363 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002364 } else {
2365 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002366 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002367 } else {
2368 vtype_kind_t vtype;
2369 emit_pre_pop_reg(emit, &vtype, REG_RET);
2370 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002371 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2372 "return expected '%q' but got '%q'",
2373 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002374 }
Damien George2ac4af62014-08-15 16:45:41 +01002375 }
Damien13ed3a62013-10-08 09:05:10 +01002376 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002377 vtype_kind_t vtype;
2378 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002379 assert(vtype == VTYPE_PYOBJ);
2380 }
2381 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002382 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2383 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002384}
2385
Damien George7ff996c2014-09-08 23:05:16 +01002386STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002387 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002388 vtype_kind_t vtype_exc;
2389 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2390 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002391 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002392 }
2393 // 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 +01002394 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002395}
Damien Georgeb601d952014-06-30 05:17:25 +01002396
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002397STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002398 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002399 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002400 assert(0);
2401}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002402STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002403 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002404 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002405 assert(0);
2406}
2407
Damien Georgeb601d952014-06-30 05:17:25 +01002408STATIC void emit_native_start_except_handler(emit_t *emit) {
2409 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2410 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2411 // the first 2 elements, so we can get the thrown value.
2412 adjust_stack(emit, 2);
2413 vtype_kind_t vtype_nlr;
2414 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002415 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002416 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
2417}
2418
2419STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002420 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002421}
2422
Damien13ed3a62013-10-08 09:05:10 +01002423const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002424 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002425 emit_native_start_pass,
2426 emit_native_end_pass,
2427 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002428 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002429 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002430
Damien George542bd6b2015-03-26 14:42:40 +00002431 {
2432 emit_native_load_fast,
2433 emit_native_load_deref,
2434 emit_native_load_name,
2435 emit_native_load_global,
2436 },
2437 {
2438 emit_native_store_fast,
2439 emit_native_store_deref,
2440 emit_native_store_name,
2441 emit_native_store_global,
2442 },
2443 {
2444 emit_native_delete_fast,
2445 emit_native_delete_deref,
2446 emit_native_delete_name,
2447 emit_native_delete_global,
2448 },
Damien13ed3a62013-10-08 09:05:10 +01002449
2450 emit_native_label_assign,
2451 emit_native_import_name,
2452 emit_native_import_from,
2453 emit_native_import_star,
2454 emit_native_load_const_tok,
2455 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002456 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002457 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002458 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002459 emit_native_load_attr,
2460 emit_native_load_method,
2461 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002462 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002463 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002464 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002465 emit_native_delete_attr,
2466 emit_native_delete_subscr,
2467 emit_native_dup_top,
2468 emit_native_dup_top_two,
2469 emit_native_pop_top,
2470 emit_native_rot_two,
2471 emit_native_rot_three,
2472 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002473 emit_native_pop_jump_if,
2474 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002475 emit_native_break_loop,
2476 emit_native_continue_loop,
2477 emit_native_setup_with,
2478 emit_native_with_cleanup,
2479 emit_native_setup_except,
2480 emit_native_setup_finally,
2481 emit_native_end_finally,
2482 emit_native_get_iter,
2483 emit_native_for_iter,
2484 emit_native_for_iter_end,
2485 emit_native_pop_block,
2486 emit_native_pop_except,
2487 emit_native_unary_op,
2488 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002489 emit_native_build_tuple,
2490 emit_native_build_list,
2491 emit_native_list_append,
2492 emit_native_build_map,
2493 emit_native_store_map,
2494 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002495 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002496 emit_native_build_set,
2497 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002498 #endif
Damien George83204f32014-12-27 17:20:41 +00002499 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002500 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002501 #endif
Damien13ed3a62013-10-08 09:05:10 +01002502 emit_native_unpack_sequence,
2503 emit_native_unpack_ex,
2504 emit_native_make_function,
2505 emit_native_make_closure,
2506 emit_native_call_function,
2507 emit_native_call_method,
2508 emit_native_return_value,
2509 emit_native_raise_varargs,
2510 emit_native_yield_value,
2511 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002512
2513 emit_native_start_except_handler,
2514 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002515};
2516
Damien Georgec90f59e2014-09-06 23:06:36 +01002517#endif