blob: 74c5b98ef894501970016f64f539ba25d8b08623 [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))
149
Damien George91cfd412014-10-12 16:59:29 +0100150#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 +0000151#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 +0100152#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
153#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
154
155#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 +0000156#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 +0100157#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
158#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 +0100159
Damien Georgec90f59e2014-09-06 23:06:36 +0100160#elif N_X86
161
162// x86 specific stuff
163
Damien George51dfcb42015-01-01 20:27:54 +0000164#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100165
166STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
167 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
168 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100169 [MP_F_LOAD_CONST_STR] = 1,
170 [MP_F_LOAD_CONST_BYTES] = 1,
171 [MP_F_LOAD_NAME] = 1,
172 [MP_F_LOAD_GLOBAL] = 1,
173 [MP_F_LOAD_BUILD_CLASS] = 0,
174 [MP_F_LOAD_ATTR] = 2,
175 [MP_F_LOAD_METHOD] = 3,
176 [MP_F_STORE_NAME] = 2,
177 [MP_F_STORE_GLOBAL] = 2,
178 [MP_F_STORE_ATTR] = 3,
179 [MP_F_OBJ_SUBSCR] = 3,
180 [MP_F_OBJ_IS_TRUE] = 1,
181 [MP_F_UNARY_OP] = 2,
182 [MP_F_BINARY_OP] = 3,
183 [MP_F_BUILD_TUPLE] = 2,
184 [MP_F_BUILD_LIST] = 2,
185 [MP_F_LIST_APPEND] = 2,
186 [MP_F_BUILD_MAP] = 1,
187 [MP_F_STORE_MAP] = 3,
188#if MICROPY_PY_BUILTINS_SET
189 [MP_F_BUILD_SET] = 2,
190 [MP_F_STORE_SET] = 2,
191#endif
192 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
193 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
194 [MP_F_CALL_METHOD_N_KW] = 3,
Damien George78772ad2015-04-06 22:48:21 +0100195 [MP_F_CALL_METHOD_N_KW_VAR] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100196 [MP_F_GETITER] = 1,
197 [MP_F_ITERNEXT] = 1,
198 [MP_F_NLR_PUSH] = 1,
199 [MP_F_NLR_POP] = 0,
200 [MP_F_NATIVE_RAISE] = 1,
201 [MP_F_IMPORT_NAME] = 3,
202 [MP_F_IMPORT_FROM] = 2,
203 [MP_F_IMPORT_ALL] = 1,
204#if MICROPY_PY_BUILTINS_SLICE
205 [MP_F_NEW_SLICE] = 3,
206#endif
207 [MP_F_UNPACK_SEQUENCE] = 3,
208 [MP_F_UNPACK_EX] = 3,
209 [MP_F_DELETE_NAME] = 1,
210 [MP_F_DELETE_GLOBAL] = 1,
Damien George99957382015-04-03 14:38:41 +0000211 [MP_F_NEW_CELL] = 1,
212 [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
Damien George99886182015-04-06 22:38:53 +0100213 [MP_F_SETUP_CODE_STATE] = 5,
Damien Georgec90f59e2014-09-06 23:06:36 +0100214};
215
216#define EXPORT_FUN(name) emit_native_x86_##name
217
Damien George99886182015-04-06 22:38:53 +0100218#define ASM_WORD_SIZE (4)
219
Damien George0b610de2014-09-29 16:25:04 +0100220#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000221#define REG_ARG_1 ASM_X86_REG_ARG_1
222#define REG_ARG_2 ASM_X86_REG_ARG_2
223#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George99886182015-04-06 22:38:53 +0100224#define REG_ARG_4 ASM_X86_REG_ARG_4
225#define REG_ARG_5 ASM_X86_REG_ARG_5
Damien George81057362014-09-07 01:06:19 +0100226
Damien George25d90412014-09-06 23:24:32 +0000227// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100228#define REG_TEMP0 ASM_X86_REG_EAX
229#define REG_TEMP1 ASM_X86_REG_ECX
230#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100231
Damien George25d90412014-09-06 23:24:32 +0000232// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100233#define REG_LOCAL_1 ASM_X86_REG_EBX
234#define REG_LOCAL_2 ASM_X86_REG_ESI
235#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000236#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100237
238#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
239#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
240
241#define ASM_T asm_x86_t
242#define ASM_NEW asm_x86_new
243#define ASM_FREE asm_x86_free
244#define ASM_GET_CODE asm_x86_get_code
Damien George99886182015-04-06 22:38:53 +0100245#define ASM_GET_CODE_POS asm_x86_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100246#define ASM_GET_CODE_SIZE asm_x86_get_code_size
247#define ASM_START_PASS asm_x86_start_pass
248#define ASM_END_PASS asm_x86_end_pass
249#define ASM_ENTRY asm_x86_entry
250#define ASM_EXIT asm_x86_exit
251
Damien George99886182015-04-06 22:38:53 +0100252#define ASM_ALIGN asm_x86_align
253#define ASM_DATA asm_x86_data
254
Damien Georgec90f59e2014-09-06 23:06:36 +0100255#define ASM_LABEL_ASSIGN asm_x86_label_assign
256#define ASM_JUMP asm_x86_jmp_label
257#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
258 do { \
259 asm_x86_test_r8_with_r8(as, reg, reg); \
260 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
261 } while (0)
262#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
263 do { \
264 asm_x86_test_r8_with_r8(as, reg, reg); \
265 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
266 } while (0)
267#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
268 do { \
269 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
270 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
271 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100272#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 +0100273
274#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
275#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
276#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
277#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
278 do { \
279 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
280 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
281 } while (false)
282#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100283#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 +0100284#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100285
Damien George3112cde2014-09-29 18:45:42 +0100286#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
287#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100288#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
289#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
290#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 +0100291#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
292#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
293
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))
385
Damien George91cfd412014-10-12 16:59:29 +0100386#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 +0000387#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 +0100388#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
389#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
390
Damien Georgee9dac3b2014-09-29 22:10:41 +0100391#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 +0000392#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 +0100393#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
394#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
395
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200396#elif N_ARM
397
398// ARM specific stuff
399
Damien George51dfcb42015-01-01 20:27:54 +0000400#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200401
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300402#define ASM_WORD_SIZE (4)
403
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200404#define EXPORT_FUN(name) emit_native_arm_##name
405
Damien George0b610de2014-09-29 16:25:04 +0100406#define REG_RET ASM_ARM_REG_R0
407#define REG_ARG_1 ASM_ARM_REG_R0
408#define REG_ARG_2 ASM_ARM_REG_R1
409#define REG_ARG_3 ASM_ARM_REG_R2
410#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100411
Damien George0b610de2014-09-29 16:25:04 +0100412#define REG_TEMP0 ASM_ARM_REG_R0
413#define REG_TEMP1 ASM_ARM_REG_R1
414#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100415
Damien George0b610de2014-09-29 16:25:04 +0100416#define REG_LOCAL_1 ASM_ARM_REG_R4
417#define REG_LOCAL_2 ASM_ARM_REG_R5
418#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100419#define REG_LOCAL_NUM (3)
420
421#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
422#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
423
424#define ASM_T asm_arm_t
425#define ASM_NEW asm_arm_new
426#define ASM_FREE asm_arm_free
427#define ASM_GET_CODE asm_arm_get_code
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300428#define ASM_GET_CODE_POS asm_arm_get_code_pos
Damien Georgec90f59e2014-09-06 23:06:36 +0100429#define ASM_GET_CODE_SIZE asm_arm_get_code_size
430#define ASM_START_PASS asm_arm_start_pass
431#define ASM_END_PASS asm_arm_end_pass
432#define ASM_ENTRY asm_arm_entry
433#define ASM_EXIT asm_arm_exit
434
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300435#define ASM_ALIGN asm_arm_align
436#define ASM_DATA asm_arm_data
437
Damien Georgec90f59e2014-09-06 23:06:36 +0100438#define ASM_LABEL_ASSIGN asm_arm_label_assign
439#define ASM_JUMP asm_arm_b_label
440#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
441 do { \
442 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100443 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100444 } while (0)
445#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
446 do { \
447 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100448 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100449 } while (0)
450#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
451 do { \
452 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100453 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100454 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100455#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 +0100456
457#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
458#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
459#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
460#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
461 do { \
462 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
463 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
464 } while (false)
465#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 +0100466#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 +0100467#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
468
Fabian Vogte5268962014-10-04 00:53:46 +0200469#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
470#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 +0100471#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
472#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
473#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 +0100474#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
475#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
476
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300477#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base), 0)
478#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 +0100479#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
480#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
481
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300482#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base), 0)
483#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 +0200484#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
485#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 +0100486
Damien Georgec90f59e2014-09-06 23:06:36 +0100487#else
488
489#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200490
Damien13ed3a62013-10-08 09:05:10 +0100491#endif
492
Damien Georgec8b60f02015-04-20 13:29:31 +0000493#define EMIT_NATIVE_VIPER_TYPE_ERROR(emit, ...) do { \
494 *emit->error_slot = mp_obj_new_exception_msg_varg(&mp_type_ViperTypeError, __VA_ARGS__); \
495 } while (0)
496
Damien13ed3a62013-10-08 09:05:10 +0100497typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100498 STACK_VALUE,
499 STACK_REG,
500 STACK_IMM,
501} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100502
Damien Georgee9dac3b2014-09-29 22:10:41 +0100503// these enums must be distinct and the bottom 2 bits
504// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100505typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100506 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
507 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
508 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
509 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
510
511 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
512 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
513 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
514 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
515
516 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
517 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100518} vtype_kind_t;
519
Damien Georgec8b60f02015-04-20 13:29:31 +0000520STATIC qstr vtype_to_qstr(vtype_kind_t vtype) {
521 switch (vtype) {
522 case VTYPE_PYOBJ: return MP_QSTR_object;
523 case VTYPE_BOOL: return MP_QSTR_bool;
524 case VTYPE_INT: return MP_QSTR_int;
525 case VTYPE_UINT: return MP_QSTR_uint;
526 case VTYPE_PTR: return MP_QSTR_ptr;
527 case VTYPE_PTR8: return MP_QSTR_ptr8;
528 case VTYPE_PTR16: return MP_QSTR_ptr16;
529 case VTYPE_PTR_NONE: default: return MP_QSTR_None;
530 }
531}
532
Damienff8ed772013-10-08 22:18:32 +0100533typedef struct _stack_info_t {
534 vtype_kind_t vtype;
535 stack_info_kind_t kind;
536 union {
537 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100538 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000539 } data;
Damienff8ed772013-10-08 22:18:32 +0100540} stack_info_t;
541
Damien13ed3a62013-10-08 09:05:10 +0100542struct _emit_t {
Damien Georgec8b60f02015-04-20 13:29:31 +0000543 mp_obj_t *error_slot;
Damien13ed3a62013-10-08 09:05:10 +0100544 int pass;
545
546 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100547
Damien George2ac4af62014-08-15 16:45:41 +0100548 vtype_kind_t return_vtype;
549
Damien George7ff996c2014-09-08 23:05:16 +0100550 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100551 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100552
Damien George7ff996c2014-09-08 23:05:16 +0100553 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100554 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100555 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100556
Damien George99886182015-04-06 22:38:53 +0100557 int code_info_size;
558 int code_info_offset;
559 int prelude_offset;
560 int n_state;
Damien13ed3a62013-10-08 09:05:10 +0100561 int stack_start;
562 int stack_size;
563
564 bool last_emit_was_return_value;
565
Damien13ed3a62013-10-08 09:05:10 +0100566 scope_t *scope;
567
Damien Georgec90f59e2014-09-06 23:06:36 +0100568 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100569};
570
Damien Georgec8b60f02015-04-20 13:29:31 +0000571emit_t *EXPORT_FUN(new)(mp_obj_t *error_slot, mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100572 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec8b60f02015-04-20 13:29:31 +0000573 emit->error_slot = error_slot;
Damien Georgec90f59e2014-09-06 23:06:36 +0100574 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100575 return emit;
576}
577
Damien George41d02b62014-01-24 22:42:28 +0000578void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100579 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100580 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
581 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200582 m_del_obj(emit_t, emit);
583}
584
Damien George2ac4af62014-08-15 16:45:41 +0100585STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
586 switch (op) {
587 case MP_EMIT_NATIVE_TYPE_ENABLE:
588 emit->do_viper_types = arg1;
589 break;
590
591 default: {
592 vtype_kind_t type;
593 switch (arg2) {
594 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
595 case MP_QSTR_bool: type = VTYPE_BOOL; break;
596 case MP_QSTR_int: type = VTYPE_INT; break;
597 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100598 case MP_QSTR_ptr: type = VTYPE_PTR; break;
599 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
600 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien Georgec8b60f02015-04-20 13:29:31 +0000601 default: EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "unknown type '%q'", arg2); return;
Damien George2ac4af62014-08-15 16:45:41 +0100602 }
603 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
604 emit->return_vtype = type;
605 } else {
606 assert(arg1 < emit->local_vtype_alloc);
607 emit->local_vtype[arg1] = type;
608 }
609 break;
610 }
611 }
Damien13ed3a62013-10-08 09:05:10 +0100612}
613
Damien Georgefa5950e2015-04-03 15:03:24 +0000614STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000615STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000616STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000617STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000618
Damien George99886182015-04-06 22:38:53 +0100619#define STATE_START (sizeof(mp_code_state) / sizeof(mp_uint_t))
620
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200621STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000622 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
623
Damien13ed3a62013-10-08 09:05:10 +0100624 emit->pass = pass;
625 emit->stack_start = 0;
626 emit->stack_size = 0;
627 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100628 emit->scope = scope;
629
Damien George36db6bc2014-05-07 17:24:22 +0100630 // allocate memory for keeping track of the types of locals
631 if (emit->local_vtype_alloc < scope->num_locals) {
632 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
633 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100634 }
Damien George36db6bc2014-05-07 17:24:22 +0100635
636 // allocate memory for keeping track of the objects on the stack
637 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000638 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100639 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000640 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100641 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100642 }
643
Damien George99886182015-04-06 22:38:53 +0100644 // set default type for return
Damien George2ac4af62014-08-15 16:45:41 +0100645 emit->return_vtype = VTYPE_PYOBJ;
Damien George99886182015-04-06 22:38:53 +0100646
647 // set default type for arguments
648 mp_uint_t num_args = emit->scope->num_pos_args + emit->scope->num_kwonly_args;
649 if (scope->scope_flags & MP_SCOPE_FLAG_VARARGS) {
650 num_args += 1;
651 }
652 if (scope->scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) {
653 num_args += 1;
654 }
655 for (mp_uint_t i = 0; i < num_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100656 emit->local_vtype[i] = VTYPE_PYOBJ;
657 }
Damien Georgea5190a72014-08-15 22:39:08 +0100658
659 // local variables begin unbound, and have unknown type
Damien George99886182015-04-06 22:38:53 +0100660 for (mp_uint_t i = num_args; i < emit->local_vtype_alloc; i++) {
Damien Georgea5190a72014-08-15 22:39:08 +0100661 emit->local_vtype[i] = VTYPE_UNBOUND;
662 }
663
664 // values on stack begin unbound
665 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100666 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100667 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100668 }
669
Damien Georgec90f59e2014-09-06 23:06:36 +0100670 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100671
Damien George99886182015-04-06 22:38:53 +0100672 // generate code for entry to function
Damien13ed3a62013-10-08 09:05:10 +0100673
Damien George99886182015-04-06 22:38:53 +0100674 if (emit->do_viper_types) {
675
676 // entry to function
677 int num_locals = 0;
678 if (pass > MP_PASS_SCOPE) {
679 num_locals = scope->num_locals - REG_LOCAL_NUM;
680 if (num_locals < 0) {
681 num_locals = 0;
682 }
683 emit->stack_start = num_locals;
684 num_locals += scope->stack_size;
Damien13ed3a62013-10-08 09:05:10 +0100685 }
Damien George99886182015-04-06 22:38:53 +0100686 ASM_ENTRY(emit->as, num_locals);
687
688 #if N_X86
689 for (int i = 0; i < scope->num_pos_args; i++) {
690 if (i == 0) {
691 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
692 } else if (i == 1) {
693 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
694 } else if (i == 2) {
695 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
696 } else {
697 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
698 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
699 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100700 }
Damien George99886182015-04-06 22:38:53 +0100701 #else
702 for (int i = 0; i < scope->num_pos_args; i++) {
703 if (i == 0) {
704 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
705 } else if (i == 1) {
706 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
707 } else if (i == 2) {
708 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
709 } else if (i == 3) {
710 ASM_MOV_REG_TO_LOCAL(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
711 } else {
712 // TODO not implemented
713 assert(0);
714 }
715 }
716 #endif
717
718 } else {
719 // work out size of state (locals plus stack)
720 emit->n_state = scope->num_locals + scope->stack_size;
721
722 // allocate space on C-stack for code_state structure, which includes state
723 ASM_ENTRY(emit->as, STATE_START + emit->n_state);
724
725 // prepare incoming arguments for call to mp_setup_code_state
726 #if N_X86
727 asm_x86_mov_arg_to_r32(emit->as, 0, REG_ARG_2);
728 asm_x86_mov_arg_to_r32(emit->as, 1, REG_ARG_3);
729 asm_x86_mov_arg_to_r32(emit->as, 2, REG_ARG_4);
730 asm_x86_mov_arg_to_r32(emit->as, 3, REG_ARG_5);
731 #else
732 #if N_THUMB
733 ASM_MOV_REG_REG(emit->as, ASM_THUMB_REG_R4, REG_ARG_4);
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300734 #elif N_ARM
735 ASM_MOV_REG_REG(emit->as, ASM_ARM_REG_R4, REG_ARG_4);
Damien George99886182015-04-06 22:38:53 +0100736 #else
737 ASM_MOV_REG_REG(emit->as, REG_ARG_5, REG_ARG_4);
738 #endif
739 ASM_MOV_REG_REG(emit->as, REG_ARG_4, REG_ARG_3);
740 ASM_MOV_REG_REG(emit->as, REG_ARG_3, REG_ARG_2);
741 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_ARG_1);
742 #endif
743
744 // set code_state.code_info (offset from start of this function to code_info data)
745 // XXX this encoding may change size
746 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);
747
748 // set code_state.ip (offset from start of this function to prelude info)
749 // XXX this encoding may change size
750 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->prelude_offset, offsetof(mp_code_state, ip) / sizeof(mp_uint_t), REG_ARG_1);
751
752 // set code_state.n_state
753 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, emit->n_state, offsetof(mp_code_state, n_state) / sizeof(mp_uint_t), REG_ARG_1);
754
755 // put address of code_state into first arg
756 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, 0, REG_ARG_1);
757
758 // call mp_setup_code_state to prepare code_state structure
759 #if N_THUMB
760 asm_thumb_op16(emit->as, 0xb400 | (1 << ASM_THUMB_REG_R4)); // push 5th arg
761 asm_thumb_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_THUMB_REG_R4);
762 asm_thumb_op16(emit->as, 0xbc00 | (1 << REG_RET)); // pop dummy (was 5th arg)
Paul Sokolovsky351424e2015-05-07 23:08:09 +0300763 #elif N_ARM
764 asm_arm_push(emit->as, 1 << ASM_ARM_REG_R4); // push 5th arg
765 asm_arm_bl_ind(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE, ASM_ARM_REG_R4);
766 asm_arm_pop(emit->as, 1 << REG_RET); // pop dummy (was 5th arg)
Damien George99886182015-04-06 22:38:53 +0100767 #else
768 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_SETUP_CODE_STATE], MP_F_SETUP_CODE_STATE);
769 #endif
770
771 // cache some locals in registers
772 if (scope->num_locals > 0) {
773 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 0, REG_LOCAL_1);
774 if (scope->num_locals > 1) {
775 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 1, REG_LOCAL_2);
776 if (scope->num_locals > 2) {
777 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - 2, REG_LOCAL_3);
778 }
779 }
780 }
781
782 // set the type of closed over variables
783 for (mp_uint_t i = 0; i < scope->id_info_len; i++) {
784 id_info_t *id = &scope->id_info[i];
785 if (id->kind == ID_INFO_KIND_CELL) {
786 emit->local_vtype[id->local_num] = VTYPE_PYOBJ;
787 }
Damien13ed3a62013-10-08 09:05:10 +0100788 }
789 }
790
Damien George99886182015-04-06 22:38:53 +0100791 #if N_THUMB
Damien Georgee9dac3b2014-09-29 22:10:41 +0100792 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100793 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100794 #endif
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200795
Damien George99886182015-04-06 22:38:53 +0100796 #if N_ARM
Damien Georgee9dac3b2014-09-29 22:10:41 +0100797 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100798 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien George99886182015-04-06 22:38:53 +0100799 #endif
Damien13ed3a62013-10-08 09:05:10 +0100800}
801
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200802STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100803 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100804 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100805 }
Damien George99886182015-04-06 22:38:53 +0100806
807 if (!emit->do_viper_types) {
808 // write dummy code info (for mp_setup_code_state to parse) and arg names
809 emit->code_info_offset = ASM_GET_CODE_POS(emit->as);
810 ASM_DATA(emit->as, 1, emit->code_info_size);
811 ASM_ALIGN(emit->as, ASM_WORD_SIZE);
812 emit->code_info_size = ASM_GET_CODE_POS(emit->as) - emit->code_info_offset;
Damien George9a42eb52015-05-06 13:55:33 +0100813 // see comment in corresponding part of emitbc.c about the logic here
Damien George99886182015-04-06 22:38:53 +0100814 for (int i = 0; i < emit->scope->num_pos_args + emit->scope->num_kwonly_args; i++) {
Damien George9a42eb52015-05-06 13:55:33 +0100815 qstr qst = MP_QSTR__star_;
816 for (int j = 0; j < emit->scope->id_info_len; ++j) {
817 id_info_t *id = &emit->scope->id_info[j];
818 if ((id->flags & ID_FLAG_IS_PARAM) && id->local_num == i) {
819 qst = id->qst;
820 break;
821 }
822 }
823 ASM_DATA(emit->as, ASM_WORD_SIZE, (mp_uint_t)MP_OBJ_NEW_QSTR(qst));
Damien George99886182015-04-06 22:38:53 +0100824 }
825
826 // bytecode prelude: initialise closed over variables
827 emit->prelude_offset = ASM_GET_CODE_POS(emit->as);
828 for (int i = 0; i < emit->scope->id_info_len; i++) {
829 id_info_t *id = &emit->scope->id_info[i];
830 if (id->kind == ID_INFO_KIND_CELL) {
831 assert(id->local_num < 255);
832 ASM_DATA(emit->as, 1, id->local_num); // write the local which should be converted to a cell
833 }
834 }
835 ASM_DATA(emit->as, 1, 255); // end of list sentinel
836 }
837
Damien Georgec90f59e2014-09-06 23:06:36 +0100838 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100839
840 // check stack is back to zero size
841 if (emit->stack_size != 0) {
Damien Georgee72cda92015-04-11 12:15:47 +0100842 mp_printf(&mp_plat_print, "ERROR: stack size not back to zero; got %d\n", emit->stack_size);
Damien13ed3a62013-10-08 09:05:10 +0100843 }
844
Damien George36db6bc2014-05-07 17:24:22 +0100845 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100846 void *f = ASM_GET_CODE(emit->as);
847 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100848
849 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100850 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100851 mp_uint_t type_sig = emit->return_vtype & 3;
852 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
853 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
854 }
855
Damien George99886182015-04-06 22:38:53 +0100856 mp_emit_glue_assign_native(emit->scope->raw_code,
857 emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY,
858 f, f_len, emit->scope->num_pos_args, emit->scope->num_kwonly_args,
859 emit->scope->scope_flags, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100860 }
861}
862
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200863STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100864 return emit->last_emit_was_return_value;
865}
866
Damien Georged6230f62014-09-23 14:10:03 +0000867STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000868 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100869 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100870 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100871 emit->scope->stack_size = emit->stack_size;
872 }
Damien George21ca2d72014-10-19 19:00:51 +0100873#ifdef DEBUG_PRINT
874 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
875 for (int i = 0; i < emit->stack_size; i++) {
876 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000877 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100878 }
879 DEBUG_printf("\n");
880#endif
Damien13ed3a62013-10-08 09:05:10 +0100881}
882
Damien Georged6230f62014-09-23 14:10:03 +0000883STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100884 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000885 // If we are adjusting the stack in a positive direction (pushing) then we
886 // need to fill in values for the stack kind and vtype of the newly-pushed
887 // entries. These should be set to "value" (ie not reg or imm) because we
888 // should only need to adjust the stack due to a jump to this part in the
889 // code (and hence we have settled the stack before the jump).
890 for (mp_int_t i = 0; i < delta; i++) {
891 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
892 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100893 // TODO we don't know the vtype to use here. At the moment this is a
894 // hack to get the case of multi comparison working.
895 if (delta == 1) {
896 si->vtype = emit->saved_stack_vtype;
897 } else {
898 si->vtype = VTYPE_PYOBJ;
899 }
Damien Georged6230f62014-09-23 14:10:03 +0000900 }
901 adjust_stack(emit, delta);
902}
903
904STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000905 (void)emit;
906 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000907}
908
Damienff8ed772013-10-08 22:18:32 +0100909/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200910STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100911 adjust_stack(emit, stack_size_delta);
912 emit->last_emit_was_return_value = false;
913}
Damienff8ed772013-10-08 22:18:32 +0100914*/
Damien13ed3a62013-10-08 09:05:10 +0100915
Damienff8ed772013-10-08 22:18:32 +0100916// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000917STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100918 emit->last_emit_was_return_value = false;
919 // settle the stack
920 /*
921 if (regs_needed != 0) {
922 for (int i = 0; i < emit->stack_size; i++) {
923 switch (emit->stack_info[i].kind) {
924 case STACK_VALUE:
925 break;
926
927 case STACK_REG:
928 // TODO only push reg if in regs_needed
929 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000930 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100931 break;
932
933 case STACK_IMM:
934 // don't think we ever need to push imms for settling
935 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
936 break;
937 }
938 }
939 }
940 */
Damien13ed3a62013-10-08 09:05:10 +0100941}
942
Damien George3112cde2014-09-29 18:45:42 +0100943// depth==0 is top, depth==1 is before top, etc
944STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
945 return &emit->stack_info[emit->stack_size - 1 - depth];
946}
947
948// depth==0 is top, depth==1 is before top, etc
949STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
950 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100951}
Damien13ed3a62013-10-08 09:05:10 +0100952
Damiend2755ec2013-10-16 23:58:48 +0100953// pos=1 is TOS, pos=2 is next, etc
954// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200955STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100956 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100957 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100958 if (i != skip_stack_pos) {
959 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000960 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100961 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000962 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100963 }
Damienff8ed772013-10-08 22:18:32 +0100964 }
965 }
966}
Damien13ed3a62013-10-08 09:05:10 +0100967
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200968STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100969 for (int i = 0; i < emit->stack_size; i++) {
970 stack_info_t *si = &emit->stack_info[i];
971 if (si->kind == STACK_REG) {
972 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000973 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100974 }
Damien13ed3a62013-10-08 09:05:10 +0100975 }
976}
977
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200978STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000979 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100980 for (int i = 0; i < emit->stack_size; i++) {
981 stack_info_t *si = &emit->stack_info[i];
982 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000983 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100984 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000985 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100986 }
987 }
988 for (int i = 0; i < emit->stack_size; i++) {
989 stack_info_t *si = &emit->stack_info[i];
990 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000991 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100992 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000993 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100994 }
995 }
996}
997
998// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200999STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001000 need_reg_single(emit, reg_dest, pos);
1001 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +01001002 *vtype = si->vtype;
1003 switch (si->kind) {
1004 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +01001005 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001006 break;
1007
Damienff8ed772013-10-08 22:18:32 +01001008 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +00001009 if (si->data.u_reg != reg_dest) {
1010 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +01001011 }
1012 break;
1013
Damienff8ed772013-10-08 22:18:32 +01001014 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +00001015 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +01001016 break;
1017 }
Damien13ed3a62013-10-08 09:05:10 +01001018}
1019
Damien Georgee9dac3b2014-09-29 22:10:41 +01001020// does an efficient X=pop(); discard(); push(X)
1021// needs a (non-temp) register in case the poped element was stored in the stack
1022STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
1023 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
1024 si[0] = si[1];
1025 if (si->kind == STACK_VALUE) {
1026 // if folded element was on the stack we need to put it in a register
1027 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
1028 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001029 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001030 }
1031 adjust_stack(emit, -1);
1032}
1033
1034// If stacked value is in a register and the register is not r1 or r2, then
1035// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
1036STATIC 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 +01001037 emit->last_emit_was_return_value = false;
1038 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +00001039 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +01001040 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +00001041 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +01001042 need_reg_single(emit, *reg_dest, 1);
1043 } else {
1044 emit_access_stack(emit, 1, vtype, *reg_dest);
1045 }
1046 adjust_stack(emit, -1);
1047}
1048
Damien Georgee6ce10a2014-09-06 18:38:20 +01001049STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +01001050 emit->last_emit_was_return_value = false;
1051 adjust_stack(emit, -1);
1052}
1053
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001054STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +01001055 emit->last_emit_was_return_value = false;
1056 emit_access_stack(emit, 1, vtype, reg_dest);
1057 adjust_stack(emit, -1);
1058}
1059
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001060STATIC 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 +01001061 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001062 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +01001063}
1064
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001065STATIC 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 +01001066 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +01001067 emit_pre_pop_reg(emit, vtypeb, regb);
1068 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001069}
1070
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001071STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001072 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001073}
1074
Damien Georgee9dac3b2014-09-29 22:10:41 +01001075STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
1076 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
1077 si->vtype = new_vtype;
1078}
1079
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001080STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +01001081 stack_info_t *si = &emit->stack_info[emit->stack_size];
1082 si->vtype = vtype;
1083 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +00001084 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +01001085 adjust_stack(emit, 1);
1086}
1087
Damien George40f3c022014-07-03 13:25:24 +01001088STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
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_IMM;
Damien George32444b72015-01-24 23:14:12 +00001092 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +01001093 adjust_stack(emit, 1);
1094}
1095
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001096STATIC 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 +01001097 emit_post_push_reg(emit, vtypea, rega);
1098 emit_post_push_reg(emit, vtypeb, regb);
1099}
1100
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001101STATIC 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 +01001102 emit_post_push_reg(emit, vtypea, rega);
1103 emit_post_push_reg(emit, vtypeb, regb);
1104 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +01001105}
1106
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001107STATIC 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 +01001108 emit_post_push_reg(emit, vtypea, rega);
1109 emit_post_push_reg(emit, vtypeb, regb);
1110 emit_post_push_reg(emit, vtypec, regc);
1111 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +01001112}
1113
Damien George7fe21912014-08-16 22:31:57 +01001114STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +01001115 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001116 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001117}
1118
Damien George7fe21912014-08-16 22:31:57 +01001119STATIC 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 +01001120 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001121 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
1122 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +01001123}
1124
Damien George40f3c022014-07-03 13:25:24 +01001125// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001126STATIC 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 +01001127 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001128 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
1129 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +01001130}
1131
Damien George7fe21912014-08-16 22:31:57 +01001132STATIC 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 +00001133 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001134 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1135 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1136 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001137}
1138
Damien George40f3c022014-07-03 13:25:24 +01001139// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001140STATIC 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 +01001141 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001142 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1143 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1144 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1145 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001146}
1147
Damien George86de21b2014-08-16 22:06:11 +01001148// vtype of all n_pop objects is VTYPE_PYOBJ
1149// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1150// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1151// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1152STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1153 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001154
Damien George86de21b2014-08-16 22:06:11 +01001155 // First, store any immediate values to their respective place on the stack.
1156 for (mp_uint_t i = 0; i < n_pop; i++) {
1157 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1158 // must push any imm's to stack
1159 // must convert them to VTYPE_PYOBJ for viper code
1160 if (si->kind == STACK_IMM) {
1161 si->kind = STACK_VALUE;
1162 switch (si->vtype) {
1163 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001164 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 +01001165 break;
1166 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001167 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001168 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 +01001169 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001170 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 +01001171 }
1172 si->vtype = VTYPE_PYOBJ;
1173 break;
1174 case VTYPE_INT:
1175 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001176 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 +01001177 si->vtype = VTYPE_PYOBJ;
1178 break;
1179 default:
1180 // not handled
1181 assert(0);
1182 }
1183 }
1184
1185 // verify that this value is on the stack
1186 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001187 }
Damien George86de21b2014-08-16 22:06:11 +01001188
1189 // Second, convert any non-VTYPE_PYOBJ to that type.
1190 for (mp_uint_t i = 0; i < n_pop; i++) {
1191 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1192 if (si->vtype != VTYPE_PYOBJ) {
1193 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001194 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001195 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 +01001196 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001197 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001198 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001199 }
1200 }
1201
1202 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1203 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001204 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001205}
1206
1207// vtype of all n_push objects is VTYPE_PYOBJ
1208STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1209 need_reg_all(emit);
1210 for (mp_uint_t i = 0; i < n_push; i++) {
1211 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1212 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1213 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001214 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001215 adjust_stack(emit, n_push);
1216}
1217
Damien George7ff996c2014-09-08 23:05:16 +01001218STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001219 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001220 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001221 // need to commit stack because we can jump here from elsewhere
1222 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001223 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001224 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001225}
1226
Damien Georgecdd96df2014-04-06 12:58:40 +01001227STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1228 DEBUG_printf("import_name %s\n", qstr_str(qst));
1229 vtype_kind_t vtype_fromlist;
1230 vtype_kind_t vtype_level;
1231 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1232 assert(vtype_fromlist == VTYPE_PYOBJ);
1233 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001234 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001235 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001236}
1237
Damien Georgecdd96df2014-04-06 12:58:40 +01001238STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1239 DEBUG_printf("import_from %s\n", qstr_str(qst));
1240 emit_native_pre(emit);
1241 vtype_kind_t vtype_module;
1242 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1243 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001244 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001245 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001246}
1247
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001248STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001249 DEBUG_printf("import_star\n");
1250 vtype_kind_t vtype_module;
1251 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1252 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001253 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001254 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001255}
1256
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001257STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001258 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001259 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001260 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001261 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001262 if (emit->do_viper_types) {
1263 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001264 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1265 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1266 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001267 no_other_choice1:
1268 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1269 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001270 }
1271 } else {
1272 vtype = VTYPE_PYOBJ;
1273 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001274 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1275 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1276 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001277 no_other_choice2:
1278 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1279 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001280 }
1281 }
1282 emit_post_push_imm(emit, vtype, val);
1283}
1284
Damien George40f3c022014-07-03 13:25:24 +01001285STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001286 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001287 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001288 if (emit->do_viper_types) {
1289 emit_post_push_imm(emit, VTYPE_INT, arg);
1290 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001291 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001292 }
1293}
1294
Damien George7ff996c2014-09-08 23:05:16 +01001295STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001296 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001297 // TODO: Eventually we want to be able to work with raw pointers in viper to
1298 // do native array access. For now we just load them as any other object.
1299 /*
Damien13ed3a62013-10-08 09:05:10 +01001300 if (emit->do_viper_types) {
1301 // not implemented properly
1302 // load a pointer to the asciiz string?
1303 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001304 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001305 } else
1306 */
1307 {
Damien Georgeb601d952014-06-30 05:17:25 +01001308 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001309 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001310 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001311 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001312 }
Damien13ed3a62013-10-08 09:05:10 +01001313 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1314 }
1315}
1316
Damien Georgedab13852015-01-13 15:55:54 +00001317STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1318 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001319 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001320 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1321 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1322}
1323
Damien George3558f622014-04-20 17:50:40 +01001324STATIC void emit_native_load_null(emit_t *emit) {
1325 emit_native_pre(emit);
1326 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1327}
1328
Damien George0abb5602015-01-16 12:24:49 +00001329STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1330 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001331 vtype_kind_t vtype = emit->local_vtype[local_num];
1332 if (vtype == VTYPE_UNBOUND) {
Damien Georgec8b60f02015-04-20 13:29:31 +00001333 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "local '%q' used before type known", qst);
Damien13ed3a62013-10-08 09:05:10 +01001334 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001335 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001336 if (local_num == 0) {
1337 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001338 } else if (local_num == 1) {
1339 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1340 } else if (local_num == 2) {
1341 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001342 } else {
Damien George0b610de2014-09-29 16:25:04 +01001343 need_reg_single(emit, REG_TEMP0, 0);
Damien George99886182015-04-06 22:38:53 +01001344 if (emit->do_viper_types) {
1345 ASM_MOV_LOCAL_TO_REG(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1346 } else {
1347 ASM_MOV_LOCAL_TO_REG(emit->as, STATE_START + emit->n_state - 1 - local_num, REG_TEMP0);
1348 }
Damien George0b610de2014-09-29 16:25:04 +01001349 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001350 }
Damien13ed3a62013-10-08 09:05:10 +01001351}
1352
Damien George7ff996c2014-09-08 23:05:16 +01001353STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001354 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1355 need_reg_single(emit, REG_RET, 0);
1356 emit_native_load_fast(emit, qst, local_num);
1357 vtype_kind_t vtype;
1358 int reg_base = REG_RET;
1359 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1360 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1361 // closed over vars are always Python objects
1362 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001363}
1364
Damien George7ff996c2014-09-08 23:05:16 +01001365STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001366 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001367 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001368 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001369 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1370}
1371
Damien George7ff996c2014-09-08 23:05:16 +01001372STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001373 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001374 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001375 // check for builtin casting operators
1376 if (emit->do_viper_types && qst == MP_QSTR_int) {
1377 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1378 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1379 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1380 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1381 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1382 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1383 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1384 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1385 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1386 } else {
1387 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1388 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1389 }
Damien13ed3a62013-10-08 09:05:10 +01001390}
1391
Damien George7ff996c2014-09-08 23:05:16 +01001392STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001393 // depends on type of subject:
1394 // - integer, function, pointer to integers: error
1395 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001396 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001397 vtype_kind_t vtype_base;
1398 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1399 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001400 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001401 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1402}
1403
Damien George7ff996c2014-09-08 23:05:16 +01001404STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001405 vtype_kind_t vtype_base;
1406 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1407 assert(vtype_base == VTYPE_PYOBJ);
1408 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001409 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001410}
1411
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001412STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001413 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001414 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001415 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001416}
1417
Damien George729f7b42014-04-17 22:10:53 +01001418STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001419 DEBUG_printf("load_subscr\n");
1420 // need to compile: base[index]
1421
1422 // pop: index, base
1423 // optimise case where index is an immediate
1424 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1425
1426 if (vtype_base == VTYPE_PYOBJ) {
1427 // standard Python call
1428 vtype_kind_t vtype_index;
1429 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1430 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001431 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 +01001432 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1433 } else {
Damien George91cfd412014-10-12 16:59:29 +01001434 // viper load
1435 // TODO The different machine architectures have very different
1436 // capabilities and requirements for loads, so probably best to
1437 // write a completely separate load-optimiser for each one.
1438 stack_info_t *top = peek_stack(emit, 0);
1439 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1440 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001441 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001442 emit_pre_pop_discard(emit); // discard index
1443 int reg_base = REG_ARG_1;
1444 int reg_index = REG_ARG_2;
1445 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1446 switch (vtype_base) {
1447 case VTYPE_PTR8: {
1448 // pointer to 8-bit memory
1449 // TODO optimise to use thumb ldrb r1, [r2, r3]
1450 if (index_value != 0) {
1451 // index is non-zero
1452 #if N_THUMB
1453 if (index_value > 0 && index_value < 32) {
1454 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1455 break;
1456 }
1457 #endif
1458 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1459 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1460 reg_base = reg_index;
1461 }
1462 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1463 break;
1464 }
1465 case VTYPE_PTR16: {
1466 // pointer to 16-bit memory
1467 if (index_value != 0) {
1468 // index is a non-zero immediate
1469 #if N_THUMB
1470 if (index_value > 0 && index_value < 32) {
1471 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1472 break;
1473 }
1474 #endif
1475 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1476 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1477 reg_base = reg_index;
1478 }
1479 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1480 break;
1481 }
1482 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001483 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1484 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001485 }
1486 } else {
1487 // index is not an immediate
1488 vtype_kind_t vtype_index;
1489 int reg_index = REG_ARG_2;
1490 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1491 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1492 switch (vtype_base) {
1493 case VTYPE_PTR8: {
1494 // pointer to 8-bit memory
1495 // TODO optimise to use thumb ldrb r1, [r2, r3]
1496 assert(vtype_index == VTYPE_INT);
1497 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1498 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1499 break;
1500 }
1501 case VTYPE_PTR16: {
1502 // pointer to 16-bit memory
1503 assert(vtype_index == VTYPE_INT);
1504 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1505 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1506 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1507 break;
1508 }
1509 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001510 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1511 "can't load from '%q'", vtype_to_qstr(vtype_base));
Damien George91cfd412014-10-12 16:59:29 +01001512 }
1513 }
1514 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001515 }
1516}
1517
Damien George7ff996c2014-09-08 23:05:16 +01001518STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001519 vtype_kind_t vtype;
Damien13ed3a62013-10-08 09:05:10 +01001520 if (local_num == 0) {
1521 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001522 } else if (local_num == 1) {
1523 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1524 } else if (local_num == 2) {
1525 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001526 } else {
Damien George0b610de2014-09-29 16:25:04 +01001527 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
Damien George99886182015-04-06 22:38:53 +01001528 if (emit->do_viper_types) {
1529 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1530 } else {
1531 ASM_MOV_REG_TO_LOCAL(emit->as, REG_TEMP0, STATE_START + emit->n_state - 1 - local_num);
1532 }
Damien13ed3a62013-10-08 09:05:10 +01001533 }
Damien13ed3a62013-10-08 09:05:10 +01001534 emit_post(emit);
1535
1536 // check types
1537 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1538 // first time this local is assigned, so give it a type of the object stored in it
1539 emit->local_vtype[local_num] = vtype;
1540 } else if (emit->local_vtype[local_num] != vtype) {
1541 // type of local is not the same as object stored in it
Damien Georgec8b60f02015-04-20 13:29:31 +00001542 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1543 "local '%q' has type '%q' but source is '%q'",
1544 qst, vtype_to_qstr(emit->local_vtype[local_num]), vtype_to_qstr(vtype));
Damien13ed3a62013-10-08 09:05:10 +01001545 }
1546}
1547
Damien George7ff996c2014-09-08 23:05:16 +01001548STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001549 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1550 need_reg_single(emit, REG_TEMP0, 0);
1551 need_reg_single(emit, REG_TEMP1, 0);
1552 emit_native_load_fast(emit, qst, local_num);
1553 vtype_kind_t vtype;
1554 int reg_base = REG_TEMP0;
1555 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1556 int reg_src = REG_TEMP1;
1557 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1558 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1559 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001560}
1561
Damien George7ff996c2014-09-08 23:05:16 +01001562STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001563 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001564 vtype_kind_t vtype;
1565 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1566 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001567 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001568 emit_post(emit);
1569}
1570
Damien George7ff996c2014-09-08 23:05:16 +01001571STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001572 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001573 if (vtype == VTYPE_PYOBJ) {
1574 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1575 } else {
1576 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001577 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 +01001578 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001579 }
Damien George7ff996c2014-09-08 23:05:16 +01001580 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001581 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001582}
1583
Damien George7ff996c2014-09-08 23:05:16 +01001584STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001585 vtype_kind_t vtype_base, vtype_val;
1586 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1587 assert(vtype_base == VTYPE_PYOBJ);
1588 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001589 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001590 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001591}
1592
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001593STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001594 DEBUG_printf("store_subscr\n");
1595 // need to compile: base[index] = value
1596
1597 // pop: index, base, value
1598 // optimise case where index is an immediate
1599 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1600
1601 if (vtype_base == VTYPE_PYOBJ) {
1602 // standard Python call
1603 vtype_kind_t vtype_index, vtype_value;
1604 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1605 assert(vtype_index == VTYPE_PYOBJ);
1606 assert(vtype_value == VTYPE_PYOBJ);
1607 emit_call(emit, MP_F_OBJ_SUBSCR);
1608 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001609 // viper store
1610 // TODO The different machine architectures have very different
1611 // capabilities and requirements for stores, so probably best to
1612 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001613 stack_info_t *top = peek_stack(emit, 0);
1614 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1615 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001616 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001617 emit_pre_pop_discard(emit); // discard index
1618 vtype_kind_t vtype_value;
1619 int reg_base = REG_ARG_1;
1620 int reg_index = REG_ARG_2;
1621 int reg_value = REG_ARG_3;
1622 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001623 #if N_X86
1624 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1625 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1626 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001627 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001628 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001629 switch (vtype_base) {
1630 case VTYPE_PTR8: {
1631 // pointer to 8-bit memory
1632 // TODO optimise to use thumb strb r1, [r2, r3]
1633 if (index_value != 0) {
1634 // index is non-zero
1635 #if N_THUMB
1636 if (index_value > 0 && index_value < 32) {
1637 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1638 break;
1639 }
1640 #endif
1641 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001642 #if N_ARM
1643 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1644 return;
1645 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001646 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1647 reg_base = reg_index;
1648 }
1649 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1650 break;
1651 }
1652 case VTYPE_PTR16: {
1653 // pointer to 16-bit memory
1654 if (index_value != 0) {
1655 // index is a non-zero immediate
1656 #if N_THUMB
1657 if (index_value > 0 && index_value < 32) {
1658 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1659 break;
1660 }
1661 #endif
1662 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001663 #if N_ARM
1664 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1665 return;
1666 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001667 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1668 reg_base = reg_index;
1669 }
1670 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1671 break;
1672 }
1673 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001674 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1675 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001676 }
1677 } else {
1678 // index is not an immediate
1679 vtype_kind_t vtype_index, vtype_value;
1680 int reg_index = REG_ARG_2;
1681 int reg_value = REG_ARG_3;
1682 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1683 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001684 #if N_X86
1685 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1686 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1687 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001688 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001689 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001690 switch (vtype_base) {
1691 case VTYPE_PTR8: {
1692 // pointer to 8-bit memory
1693 // TODO optimise to use thumb strb r1, [r2, r3]
1694 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001695 #if N_ARM
1696 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1697 break;
1698 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001699 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1700 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1701 break;
1702 }
1703 case VTYPE_PTR16: {
1704 // pointer to 16-bit memory
1705 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001706 #if N_ARM
1707 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1708 break;
1709 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001710 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1711 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1712 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1713 break;
1714 }
1715 default:
Damien Georgec8b60f02015-04-20 13:29:31 +00001716 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1717 "can't store to '%q'", vtype_to_qstr(vtype_base));
Damien Georgee9dac3b2014-09-29 22:10:41 +01001718 }
1719 }
1720
1721 }
Damien13ed3a62013-10-08 09:05:10 +01001722}
1723
Damien George7ff996c2014-09-08 23:05:16 +01001724STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001725 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1726 // to mark deleted vars but then every var would need to be checked on
1727 // each access. Very inefficient, so just set value to None to enable GC.
1728 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1729 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001730}
1731
Damien George7ff996c2014-09-08 23:05:16 +01001732STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001733 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001734 (void)emit;
1735 (void)qst;
1736 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001737}
1738
Damien Georgee6ce10a2014-09-06 18:38:20 +01001739STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1740 emit_native_pre(emit);
1741 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1742 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001743}
1744
Damien Georgee6ce10a2014-09-06 18:38:20 +01001745STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1746 emit_native_pre(emit);
1747 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1748 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001749}
1750
Damien Georgee6ce10a2014-09-06 18:38:20 +01001751STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001752 vtype_kind_t vtype_base;
1753 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1754 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001755 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 +01001756 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001757}
1758
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001759STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001760 vtype_kind_t vtype_index, vtype_base;
1761 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1762 assert(vtype_index == VTYPE_PYOBJ);
1763 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001764 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 +01001765}
1766
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001767STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001768 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001769 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001770 int reg = REG_TEMP0;
1771 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1772 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001773}
1774
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001775STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001776 vtype_kind_t vtype0, vtype1;
1777 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1778 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1779}
1780
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001781STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001782 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001783 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001784 emit_post(emit);
1785}
1786
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001787STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001788 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001789 vtype_kind_t vtype0, vtype1;
1790 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1791 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001792}
1793
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001794STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001795 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001796 vtype_kind_t vtype0, vtype1, vtype2;
1797 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1798 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1799}
1800
Damien George7ff996c2014-09-08 23:05:16 +01001801STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001802 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001803 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001804 // need to commit stack because we are jumping elsewhere
1805 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001806 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001807 emit_post(emit);
1808}
1809
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001810STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001811 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgec8b60f02015-04-20 13:29:31 +00001812 if (vtype == VTYPE_PYOBJ) {
1813 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1814 if (!pop) {
1815 adjust_stack(emit, 1);
1816 }
1817 emit_call(emit, MP_F_OBJ_IS_TRUE);
1818 } else {
1819 emit_pre_pop_reg(emit, &vtype, REG_RET);
1820 if (!pop) {
1821 adjust_stack(emit, 1);
1822 }
1823 if (!(vtype == VTYPE_BOOL || vtype == VTYPE_INT || vtype == VTYPE_UINT)) {
1824 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
1825 "can't implicitly convert '%q' to 'bool'", vtype_to_qstr(vtype));
1826 }
Damien13ed3a62013-10-08 09:05:10 +01001827 }
Damien George21ca2d72014-10-19 19:00:51 +01001828 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1829 // can use it. This is a bit of a hack.
1830 if (!pop) {
1831 emit->saved_stack_vtype = vtype;
1832 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001833 // need to commit stack because we may jump elsewhere
1834 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001835}
1836
Damien George63f38322015-02-28 15:04:06 +00001837STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1838 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001839 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001840 if (cond) {
1841 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1842 } else {
1843 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1844 }
Damien1a6633a2013-11-03 13:58:19 +00001845 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001846}
Damien1a6633a2013-11-03 13:58:19 +00001847
Damien George63f38322015-02-28 15:04:06 +00001848STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1849 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001850 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001851 if (cond) {
1852 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1853 } else {
1854 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1855 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001856 adjust_stack(emit, -1);
1857 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001858}
1859
Damien George7ff996c2014-09-08 23:05:16 +01001860STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001861 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001862 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001863}
Damien Georgea32c1e42014-05-07 18:30:52 +01001864
Damien George7ff996c2014-09-08 23:05:16 +01001865STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001866 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001867 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001868}
Damien Georgea32c1e42014-05-07 18:30:52 +01001869
Damien George7ff996c2014-09-08 23:05:16 +01001870STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001871 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001872 (void)emit;
1873 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001874 assert(0);
1875}
Damien Georgeb601d952014-06-30 05:17:25 +01001876
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001877STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001878 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001879 assert(0);
1880}
Damien Georgeb601d952014-06-30 05:17:25 +01001881
Damien George7ff996c2014-09-08 23:05:16 +01001882STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001883 emit_native_pre(emit);
1884 // need to commit stack because we may jump elsewhere
1885 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001886 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 +01001887 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001888 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001889 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001890}
Damien Georgeb601d952014-06-30 05:17:25 +01001891
Damien George7ff996c2014-09-08 23:05:16 +01001892STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001893 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001894}
Damien Georgeb601d952014-06-30 05:17:25 +01001895
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001896STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001897 // logic:
1898 // exc = pop_stack
1899 // if exc == None: pass
1900 // else: raise exc
1901 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1902 vtype_kind_t vtype;
1903 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1904 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001905 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001906}
Damiend2755ec2013-10-16 23:58:48 +01001907
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001908STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001909 // perhaps the difficult one, as we want to rewrite for loops using native code
1910 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001911
1912 vtype_kind_t vtype;
1913 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1914 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001915 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001916 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001917}
Damiend2755ec2013-10-16 23:58:48 +01001918
Damien George7ff996c2014-09-08 23:05:16 +01001919STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001920 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001921 vtype_kind_t vtype;
1922 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1923 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001924 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001925 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1926 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001927 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1928}
1929
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001930STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001931 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001932 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001933 adjust_stack(emit, -1);
1934 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001935}
1936
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001937STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001938 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001939 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001940 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001941 emit_post(emit);
1942}
1943
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001944STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001945 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001946 /*
1947 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001948 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001949 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001950 emit_post(emit);
1951 */
Damien13ed3a62013-10-08 09:05:10 +01001952}
1953
Damien Georged17926d2014-03-30 13:35:08 +01001954STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001955 vtype_kind_t vtype;
1956 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1957 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001958 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001959 // we need to synthesise this operation by converting to bool first
1960 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001961 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001962 }
Damien Georged6230f62014-09-23 14:10:03 +00001963 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1964 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001965}
1966
Damien Georged17926d2014-03-30 13:35:08 +01001967STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001968 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001969 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1970 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001971 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001972 #if N_X64 || N_X86
1973 // special cases for x86 and shifting
1974 if (op == MP_BINARY_OP_LSHIFT
1975 || op == MP_BINARY_OP_INPLACE_LSHIFT
1976 || op == MP_BINARY_OP_RSHIFT
1977 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1978 #if N_X64
1979 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1980 #else
1981 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1982 #endif
1983 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1984 ASM_LSL_REG(emit->as, REG_RET);
1985 } else {
1986 ASM_ASR_REG(emit->as, REG_RET);
1987 }
1988 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1989 return;
1990 }
1991 #endif
1992 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001993 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001994 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1995 if (0) {
1996 // dummy
1997 #if !(N_X64 || N_X86)
1998 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1999 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00002000 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002001 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
2002 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2003 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2004 #endif
Damien George1ef23482014-10-12 14:21:06 +01002005 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
2006 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2007 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2008 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
2009 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2010 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2011 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
2012 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2013 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01002014 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
2015 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2016 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2017 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
2018 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
2019 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
2020 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
2021 // comparison ops are (in enum order):
2022 // MP_BINARY_OP_LESS
2023 // MP_BINARY_OP_MORE
2024 // MP_BINARY_OP_EQUAL
2025 // MP_BINARY_OP_LESS_EQUAL
2026 // MP_BINARY_OP_MORE_EQUAL
2027 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01002028 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01002029 #if N_X64
2030 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
2031 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
2032 static byte ops[6] = {
2033 ASM_X64_CC_JL,
2034 ASM_X64_CC_JG,
2035 ASM_X64_CC_JE,
2036 ASM_X64_CC_JLE,
2037 ASM_X64_CC_JGE,
2038 ASM_X64_CC_JNE,
2039 };
2040 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2041 #elif N_X86
2042 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
2043 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
2044 static byte ops[6] = {
2045 ASM_X86_CC_JL,
2046 ASM_X86_CC_JG,
2047 ASM_X86_CC_JE,
2048 ASM_X86_CC_JLE,
2049 ASM_X86_CC_JGE,
2050 ASM_X86_CC_JNE,
2051 };
2052 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
2053 #elif N_THUMB
2054 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
2055 static uint16_t ops[6] = {
2056 ASM_THUMB_OP_ITE_GE,
2057 ASM_THUMB_OP_ITE_GT,
2058 ASM_THUMB_OP_ITE_EQ,
2059 ASM_THUMB_OP_ITE_GT,
2060 ASM_THUMB_OP_ITE_GE,
2061 ASM_THUMB_OP_ITE_EQ,
2062 };
2063 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
2064 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
2065 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2066 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2067 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002068 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2069 static uint ccs[6] = {
2070 ASM_ARM_CC_LT,
2071 ASM_ARM_CC_GT,
2072 ASM_ARM_CC_EQ,
2073 ASM_ARM_CC_LE,
2074 ASM_ARM_CC_GE,
2075 ASM_ARM_CC_NE,
2076 };
2077 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002078 #else
2079 #error not implemented
2080 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002081 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2082 } else {
2083 // TODO other ops not yet implemented
2084 assert(0);
2085 }
Damien13ed3a62013-10-08 09:05:10 +01002086 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002087 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002088 bool invert = false;
2089 if (op == MP_BINARY_OP_NOT_IN) {
2090 invert = true;
2091 op = MP_BINARY_OP_IN;
2092 } else if (op == MP_BINARY_OP_IS_NOT) {
2093 invert = true;
2094 op = MP_BINARY_OP_IS;
2095 }
Damien George7fe21912014-08-16 22:31:57 +01002096 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002097 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002098 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002099 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2100 }
Damien13ed3a62013-10-08 09:05:10 +01002101 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2102 } else {
Damien George8f6aad22015-04-22 23:16:03 +01002103 adjust_stack(emit, -1);
Damien Georgec8b60f02015-04-20 13:29:31 +00002104 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2105 "can't do binary op between '%q' and '%q'",
2106 vtype_to_qstr(vtype_lhs), vtype_to_qstr(vtype_rhs));
Damien13ed3a62013-10-08 09:05:10 +01002107 }
2108}
2109
Damien George7ff996c2014-09-08 23:05:16 +01002110STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002111 // for viper: call runtime, with types of args
2112 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002113 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002114 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002115 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002116 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002117}
2118
Damien George7ff996c2014-09-08 23:05:16 +01002119STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002120 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002121 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002122 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002123 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2124}
2125
Damien George7ff996c2014-09-08 23:05:16 +01002126STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002127 // only used in list comprehension
2128 vtype_kind_t vtype_list, vtype_item;
2129 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2130 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2131 assert(vtype_list == VTYPE_PYOBJ);
2132 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002133 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002134 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002135}
2136
Damien George7ff996c2014-09-08 23:05:16 +01002137STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002138 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002139 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002140 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2141}
2142
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002143STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002144 vtype_kind_t vtype_key, vtype_value, vtype_map;
2145 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
2146 assert(vtype_key == VTYPE_PYOBJ);
2147 assert(vtype_value == VTYPE_PYOBJ);
2148 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002149 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002150 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2151}
2152
Damien George7ff996c2014-09-08 23:05:16 +01002153STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002154 // only used in list comprehension
2155 vtype_kind_t vtype_map, vtype_key, vtype_value;
2156 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2157 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2158 assert(vtype_map == VTYPE_PYOBJ);
2159 assert(vtype_key == VTYPE_PYOBJ);
2160 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002161 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002162 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002163}
2164
Damien Georgee37dcaa2014-12-27 17:07:16 +00002165#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002166STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002167 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002168 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002169 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002170 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2171}
2172
Damien George7ff996c2014-09-08 23:05:16 +01002173STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002174 // only used in set comprehension
2175 vtype_kind_t vtype_set, vtype_item;
2176 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2177 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2178 assert(vtype_set == VTYPE_PYOBJ);
2179 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002180 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002181 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002182}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002183#endif
Damiend2755ec2013-10-16 23:58:48 +01002184
Damien George83204f32014-12-27 17:20:41 +00002185#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002186STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002187 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002188 if (n_args == 2) {
2189 vtype_kind_t vtype_start, vtype_stop;
2190 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2191 assert(vtype_start == VTYPE_PYOBJ);
2192 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002193 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 +01002194 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2195 } else {
2196 assert(n_args == 3);
2197 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2198 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
2199 assert(vtype_start == VTYPE_PYOBJ);
2200 assert(vtype_stop == VTYPE_PYOBJ);
2201 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002202 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002203 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2204 }
Damien13ed3a62013-10-08 09:05:10 +01002205}
Damien George83204f32014-12-27 17:20:41 +00002206#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002207
Damien George7ff996c2014-09-08 23:05:16 +01002208STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002209 DEBUG_printf("unpack_sequence %d\n", n_args);
2210 vtype_kind_t vtype_base;
2211 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2212 assert(vtype_base == VTYPE_PYOBJ);
2213 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002214 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002215}
Damien Georgecdd96df2014-04-06 12:58:40 +01002216
Damien George7ff996c2014-09-08 23:05:16 +01002217STATIC 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 +01002218 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2219 vtype_kind_t vtype_base;
2220 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2221 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002222 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 +01002223 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 +01002224}
2225
Damien George7ff996c2014-09-08 23:05:16 +01002226STATIC 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 +01002227 // 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 +00002228 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002229 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002230 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 +01002231 } else {
2232 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2233 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2234 assert(vtype_def_tuple == VTYPE_PYOBJ);
2235 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002236 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 +01002237 }
Damien13ed3a62013-10-08 09:05:10 +01002238 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2239}
2240
Damien George7ff996c2014-09-08 23:05:16 +01002241STATIC 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 +00002242 emit_native_pre(emit);
2243 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2244 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2245 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2246 } else {
2247 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2248 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2249 }
2250 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2251 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2252 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002253}
2254
Damien George7ff996c2014-09-08 23:05:16 +01002255STATIC 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 +00002256 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2257
Damien Georgee9dac3b2014-09-29 22:10:41 +01002258 // TODO: in viper mode, call special runtime routine with type info for args,
2259 // and wanted type info for return, to remove need for boxing/unboxing
2260
Damien Georgee9dac3b2014-09-29 22:10:41 +01002261 emit_native_pre(emit);
2262 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2263 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2264 // casting operator
2265 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002266 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002267 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002268 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002269 switch (peek_vtype(emit, 0)) {
2270 case VTYPE_PYOBJ: {
2271 vtype_kind_t vtype;
2272 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2273 emit_pre_pop_discard(emit);
2274 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2275 emit_post_push_reg(emit, vtype_cast, REG_RET);
2276 break;
2277 }
2278 case VTYPE_BOOL:
2279 case VTYPE_INT:
2280 case VTYPE_UINT:
2281 case VTYPE_PTR:
2282 case VTYPE_PTR8:
2283 case VTYPE_PTR16:
2284 case VTYPE_PTR_NONE:
2285 emit_fold_stack_top(emit, REG_ARG_1);
2286 emit_post_top_set_vtype(emit, vtype_cast);
2287 break;
2288 default:
2289 assert(!"TODO: convert obj to int");
2290 }
2291 } else {
Damien13ed3a62013-10-08 09:05:10 +01002292 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002293 if (star_flags) {
2294 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2295 // load dummy entry for non-existent pos_seq
2296 emit_native_load_null(emit);
2297 emit_native_rot_two(emit);
2298 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2299 // load dummy entry for non-existent kw_dict
2300 emit_native_load_null(emit);
2301 }
2302 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2303 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);
2304 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2305 } else {
2306 if (n_positional != 0 || n_keyword != 0) {
2307 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2308 }
2309 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2310 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2311 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2312 }
Damien Georgecd82e022014-02-02 13:11:48 +00002313 }
Damien13ed3a62013-10-08 09:05:10 +01002314}
2315
Damien George7ff996c2014-09-08 23:05:16 +01002316STATIC 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 +01002317 if (star_flags) {
2318 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2319 // load dummy entry for non-existent pos_seq
2320 emit_native_load_null(emit);
2321 emit_native_rot_two(emit);
2322 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2323 // load dummy entry for non-existent kw_dict
2324 emit_native_load_null(emit);
2325 }
2326 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2327 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);
2328 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2329 } else {
2330 emit_native_pre(emit);
2331 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
2332 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2333 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2334 }
Damien13ed3a62013-10-08 09:05:10 +01002335}
2336
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002337STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002338 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002339 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002340 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2341 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002342 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002343 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002344 } else {
2345 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002346 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002347 } else {
2348 vtype_kind_t vtype;
2349 emit_pre_pop_reg(emit, &vtype, REG_RET);
2350 if (vtype != emit->return_vtype) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002351 EMIT_NATIVE_VIPER_TYPE_ERROR(emit,
2352 "return expected '%q' but got '%q'",
2353 vtype_to_qstr(emit->return_vtype), vtype_to_qstr(vtype));
Damien Georgee9dac3b2014-09-29 22:10:41 +01002354 }
Damien George2ac4af62014-08-15 16:45:41 +01002355 }
Damien13ed3a62013-10-08 09:05:10 +01002356 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002357 vtype_kind_t vtype;
2358 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002359 assert(vtype == VTYPE_PYOBJ);
2360 }
2361 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002362 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2363 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002364}
2365
Damien George7ff996c2014-09-08 23:05:16 +01002366STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002367 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002368 vtype_kind_t vtype_exc;
2369 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2370 if (vtype_exc != VTYPE_PYOBJ) {
Damien Georgec8b60f02015-04-20 13:29:31 +00002371 EMIT_NATIVE_VIPER_TYPE_ERROR(emit, "must raise an object");
Damien George86de21b2014-08-16 22:06:11 +01002372 }
2373 // 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 +01002374 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002375}
Damien Georgeb601d952014-06-30 05:17:25 +01002376
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002377STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002378 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002379 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002380 assert(0);
2381}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002382STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002383 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002384 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002385 assert(0);
2386}
2387
Damien Georgeb601d952014-06-30 05:17:25 +01002388STATIC void emit_native_start_except_handler(emit_t *emit) {
2389 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2390 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2391 // the first 2 elements, so we can get the thrown value.
2392 adjust_stack(emit, 2);
2393 vtype_kind_t vtype_nlr;
2394 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002395 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002396 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
2397}
2398
2399STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002400 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002401}
2402
Damien13ed3a62013-10-08 09:05:10 +01002403const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002404 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002405 emit_native_start_pass,
2406 emit_native_end_pass,
2407 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002408 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002409 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002410
Damien George542bd6b2015-03-26 14:42:40 +00002411 {
2412 emit_native_load_fast,
2413 emit_native_load_deref,
2414 emit_native_load_name,
2415 emit_native_load_global,
2416 },
2417 {
2418 emit_native_store_fast,
2419 emit_native_store_deref,
2420 emit_native_store_name,
2421 emit_native_store_global,
2422 },
2423 {
2424 emit_native_delete_fast,
2425 emit_native_delete_deref,
2426 emit_native_delete_name,
2427 emit_native_delete_global,
2428 },
Damien13ed3a62013-10-08 09:05:10 +01002429
2430 emit_native_label_assign,
2431 emit_native_import_name,
2432 emit_native_import_from,
2433 emit_native_import_star,
2434 emit_native_load_const_tok,
2435 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002436 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002437 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002438 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002439 emit_native_load_attr,
2440 emit_native_load_method,
2441 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002442 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002443 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002444 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002445 emit_native_delete_attr,
2446 emit_native_delete_subscr,
2447 emit_native_dup_top,
2448 emit_native_dup_top_two,
2449 emit_native_pop_top,
2450 emit_native_rot_two,
2451 emit_native_rot_three,
2452 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002453 emit_native_pop_jump_if,
2454 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002455 emit_native_break_loop,
2456 emit_native_continue_loop,
2457 emit_native_setup_with,
2458 emit_native_with_cleanup,
2459 emit_native_setup_except,
2460 emit_native_setup_finally,
2461 emit_native_end_finally,
2462 emit_native_get_iter,
2463 emit_native_for_iter,
2464 emit_native_for_iter_end,
2465 emit_native_pop_block,
2466 emit_native_pop_except,
2467 emit_native_unary_op,
2468 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002469 emit_native_build_tuple,
2470 emit_native_build_list,
2471 emit_native_list_append,
2472 emit_native_build_map,
2473 emit_native_store_map,
2474 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002475 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002476 emit_native_build_set,
2477 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002478 #endif
Damien George83204f32014-12-27 17:20:41 +00002479 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002480 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002481 #endif
Damien13ed3a62013-10-08 09:05:10 +01002482 emit_native_unpack_sequence,
2483 emit_native_unpack_ex,
2484 emit_native_make_function,
2485 emit_native_make_closure,
2486 emit_native_call_function,
2487 emit_native_call_method,
2488 emit_native_return_value,
2489 emit_native_raise_varargs,
2490 emit_native_yield_value,
2491 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002492
2493 emit_native_start_except_handler,
2494 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002495};
2496
Damien Georgec90f59e2014-09-06 23:06:36 +01002497#endif