blob: f72112ba07f180a43e7249221340af0e149c87ec [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"
Damien13ed3a62013-10-08 09:05:10 +010051
Damien Georgecdd96df2014-04-06 12:58:40 +010052#if 0 // print debugging info
53#define DEBUG_PRINT (1)
54#define DEBUG_printf DEBUG_printf
55#else // don't print debugging info
56#define DEBUG_printf(...) (void)0
57#endif
58
Damien13ed3a62013-10-08 09:05:10 +010059// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010060#if (MICROPY_EMIT_X64 && N_X64) \
61 || (MICROPY_EMIT_X86 && N_X86) \
62 || (MICROPY_EMIT_THUMB && N_THUMB) \
63 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010064
Damien3ef4abb2013-10-12 16:53:13 +010065#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010066
67// x64 specific stuff
68
Damien George51dfcb42015-01-01 20:27:54 +000069#include "py/asmx64.h"
Damien13ed3a62013-10-08 09:05:10 +010070
Damien13ed3a62013-10-08 09:05:10 +010071#define EXPORT_FUN(name) emit_native_x64_##name
72
Damien George0b610de2014-09-29 16:25:04 +010073#define REG_RET ASM_X64_REG_RAX
74#define REG_ARG_1 ASM_X64_REG_RDI
75#define REG_ARG_2 ASM_X64_REG_RSI
76#define REG_ARG_3 ASM_X64_REG_RDX
77#define REG_ARG_4 ASM_X64_REG_RCX
Damien Georgec90f59e2014-09-06 23:06:36 +010078
Damien George81057362014-09-07 01:06:19 +010079// caller-save
Damien George0b610de2014-09-29 16:25:04 +010080#define REG_TEMP0 ASM_X64_REG_RAX
81#define REG_TEMP1 ASM_X64_REG_RDI
82#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010083
84// callee-save
Damien George0b610de2014-09-29 16:25:04 +010085#define REG_LOCAL_1 ASM_X64_REG_RBX
86#define REG_LOCAL_2 ASM_X64_REG_R12
87#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +010088#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +010089
90#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
91#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
92
93#define ASM_T asm_x64_t
94#define ASM_NEW asm_x64_new
95#define ASM_FREE asm_x64_free
96#define ASM_GET_CODE asm_x64_get_code
97#define ASM_GET_CODE_SIZE asm_x64_get_code_size
98#define ASM_START_PASS asm_x64_start_pass
99#define ASM_END_PASS asm_x64_end_pass
100#define ASM_ENTRY asm_x64_entry
101#define ASM_EXIT asm_x64_exit
102
103#define ASM_LABEL_ASSIGN asm_x64_label_assign
104#define ASM_JUMP asm_x64_jmp_label
105#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
106 do { \
107 asm_x64_test_r8_with_r8(as, reg, reg); \
108 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
109 } while (0)
110#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
111 do { \
112 asm_x64_test_r8_with_r8(as, reg, reg); \
113 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
114 } while (0)
115#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
116 do { \
117 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
118 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
119 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100120#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100121
122#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
123#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
124#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
125#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
126 do { \
127 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
128 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
129 } while (false)
130#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100131#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 +0100132#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
133
Damien George3112cde2014-09-29 18:45:42 +0100134#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
135#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100136#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
137#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
138#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 +0100139#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
140#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
141
Damien George91cfd412014-10-12 16:59:29 +0100142#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 +0000143#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 +0100144#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
145#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
146
147#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 +0000148#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 +0100149#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
150#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 +0100151
Damien Georgec90f59e2014-09-06 23:06:36 +0100152#elif N_X86
153
154// x86 specific stuff
155
Damien George51dfcb42015-01-01 20:27:54 +0000156#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100157
158STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
159 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
160 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100161 [MP_F_LOAD_CONST_STR] = 1,
162 [MP_F_LOAD_CONST_BYTES] = 1,
163 [MP_F_LOAD_NAME] = 1,
164 [MP_F_LOAD_GLOBAL] = 1,
165 [MP_F_LOAD_BUILD_CLASS] = 0,
166 [MP_F_LOAD_ATTR] = 2,
167 [MP_F_LOAD_METHOD] = 3,
168 [MP_F_STORE_NAME] = 2,
169 [MP_F_STORE_GLOBAL] = 2,
170 [MP_F_STORE_ATTR] = 3,
171 [MP_F_OBJ_SUBSCR] = 3,
172 [MP_F_OBJ_IS_TRUE] = 1,
173 [MP_F_UNARY_OP] = 2,
174 [MP_F_BINARY_OP] = 3,
175 [MP_F_BUILD_TUPLE] = 2,
176 [MP_F_BUILD_LIST] = 2,
177 [MP_F_LIST_APPEND] = 2,
178 [MP_F_BUILD_MAP] = 1,
179 [MP_F_STORE_MAP] = 3,
180#if MICROPY_PY_BUILTINS_SET
181 [MP_F_BUILD_SET] = 2,
182 [MP_F_STORE_SET] = 2,
183#endif
184 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
185 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
186 [MP_F_CALL_METHOD_N_KW] = 3,
Damien George78772ad2015-04-06 22:48:21 +0100187 [MP_F_CALL_METHOD_N_KW_VAR] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100188 [MP_F_GETITER] = 1,
189 [MP_F_ITERNEXT] = 1,
190 [MP_F_NLR_PUSH] = 1,
191 [MP_F_NLR_POP] = 0,
192 [MP_F_NATIVE_RAISE] = 1,
193 [MP_F_IMPORT_NAME] = 3,
194 [MP_F_IMPORT_FROM] = 2,
195 [MP_F_IMPORT_ALL] = 1,
196#if MICROPY_PY_BUILTINS_SLICE
197 [MP_F_NEW_SLICE] = 3,
198#endif
199 [MP_F_UNPACK_SEQUENCE] = 3,
200 [MP_F_UNPACK_EX] = 3,
201 [MP_F_DELETE_NAME] = 1,
202 [MP_F_DELETE_GLOBAL] = 1,
Damien George99957382015-04-03 14:38:41 +0000203 [MP_F_NEW_CELL] = 1,
204 [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3,
Damien Georgec90f59e2014-09-06 23:06:36 +0100205};
206
207#define EXPORT_FUN(name) emit_native_x86_##name
208
Damien George0b610de2014-09-29 16:25:04 +0100209#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000210#define REG_ARG_1 ASM_X86_REG_ARG_1
211#define REG_ARG_2 ASM_X86_REG_ARG_2
212#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100213
Damien George25d90412014-09-06 23:24:32 +0000214// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100215#define REG_TEMP0 ASM_X86_REG_EAX
216#define REG_TEMP1 ASM_X86_REG_ECX
217#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100218
Damien George25d90412014-09-06 23:24:32 +0000219// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100220#define REG_LOCAL_1 ASM_X86_REG_EBX
221#define REG_LOCAL_2 ASM_X86_REG_ESI
222#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000223#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100224
225#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
226#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
227
228#define ASM_T asm_x86_t
229#define ASM_NEW asm_x86_new
230#define ASM_FREE asm_x86_free
231#define ASM_GET_CODE asm_x86_get_code
232#define ASM_GET_CODE_SIZE asm_x86_get_code_size
233#define ASM_START_PASS asm_x86_start_pass
234#define ASM_END_PASS asm_x86_end_pass
235#define ASM_ENTRY asm_x86_entry
236#define ASM_EXIT asm_x86_exit
237
238#define ASM_LABEL_ASSIGN asm_x86_label_assign
239#define ASM_JUMP asm_x86_jmp_label
240#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
241 do { \
242 asm_x86_test_r8_with_r8(as, reg, reg); \
243 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
244 } while (0)
245#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
246 do { \
247 asm_x86_test_r8_with_r8(as, reg, reg); \
248 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
249 } while (0)
250#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
251 do { \
252 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
253 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
254 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100255#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 +0100256
257#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
258#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
259#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
260#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
261 do { \
262 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
263 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
264 } while (false)
265#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100266#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 +0100267#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100268
Damien George3112cde2014-09-29 18:45:42 +0100269#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
270#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100271#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
272#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
273#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 +0100274#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
275#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
276
Damien George91cfd412014-10-12 16:59:29 +0100277#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 +0000278#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 +0000279#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
280#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 +0100281
282#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 +0000283#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 +0100284#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
285#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 +0100286
Damien3ef4abb2013-10-12 16:53:13 +0100287#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100288
289// thumb specific stuff
290
Damien George51dfcb42015-01-01 20:27:54 +0000291#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100292
Damien13ed3a62013-10-08 09:05:10 +0100293#define EXPORT_FUN(name) emit_native_thumb_##name
294
Damien George0b610de2014-09-29 16:25:04 +0100295#define REG_RET ASM_THUMB_REG_R0
296#define REG_ARG_1 ASM_THUMB_REG_R0
297#define REG_ARG_2 ASM_THUMB_REG_R1
298#define REG_ARG_3 ASM_THUMB_REG_R2
299#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100300
Damien George0b610de2014-09-29 16:25:04 +0100301#define REG_TEMP0 ASM_THUMB_REG_R0
302#define REG_TEMP1 ASM_THUMB_REG_R1
303#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100304
Damien George0b610de2014-09-29 16:25:04 +0100305#define REG_LOCAL_1 ASM_THUMB_REG_R4
306#define REG_LOCAL_2 ASM_THUMB_REG_R5
307#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100308#define REG_LOCAL_NUM (3)
309
310#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
311#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
312
313#define ASM_T asm_thumb_t
314#define ASM_NEW asm_thumb_new
315#define ASM_FREE asm_thumb_free
316#define ASM_GET_CODE asm_thumb_get_code
317#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
318#define ASM_START_PASS asm_thumb_start_pass
319#define ASM_END_PASS asm_thumb_end_pass
320#define ASM_ENTRY asm_thumb_entry
321#define ASM_EXIT asm_thumb_exit
322
323#define ASM_LABEL_ASSIGN asm_thumb_label_assign
324#define ASM_JUMP asm_thumb_b_label
325#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
326 do { \
327 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100328 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100329 } while (0)
330#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
331 do { \
332 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100333 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100334 } while (0)
335#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
336 do { \
337 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100338 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100339 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100340#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 +0100341
342#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
343#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
344#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
345#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
346 do { \
347 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
348 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
349 } while (false)
350#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 +0100351#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 +0100352#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 +0100353
Damien George3112cde2014-09-29 18:45:42 +0100354#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
355#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 +0100356#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
357#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
358#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 +0100359#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
360#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
361
Damien George91cfd412014-10-12 16:59:29 +0100362#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 +0000363#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 +0100364#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
365#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
366
Damien Georgee9dac3b2014-09-29 22:10:41 +0100367#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 +0000368#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 +0100369#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
370#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
371
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200372#elif N_ARM
373
374// ARM specific stuff
375
Damien George51dfcb42015-01-01 20:27:54 +0000376#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200377
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200378#define EXPORT_FUN(name) emit_native_arm_##name
379
Damien George0b610de2014-09-29 16:25:04 +0100380#define REG_RET ASM_ARM_REG_R0
381#define REG_ARG_1 ASM_ARM_REG_R0
382#define REG_ARG_2 ASM_ARM_REG_R1
383#define REG_ARG_3 ASM_ARM_REG_R2
384#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100385
Damien George0b610de2014-09-29 16:25:04 +0100386#define REG_TEMP0 ASM_ARM_REG_R0
387#define REG_TEMP1 ASM_ARM_REG_R1
388#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100389
Damien George0b610de2014-09-29 16:25:04 +0100390#define REG_LOCAL_1 ASM_ARM_REG_R4
391#define REG_LOCAL_2 ASM_ARM_REG_R5
392#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100393#define REG_LOCAL_NUM (3)
394
395#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
396#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
397
398#define ASM_T asm_arm_t
399#define ASM_NEW asm_arm_new
400#define ASM_FREE asm_arm_free
401#define ASM_GET_CODE asm_arm_get_code
402#define ASM_GET_CODE_SIZE asm_arm_get_code_size
403#define ASM_START_PASS asm_arm_start_pass
404#define ASM_END_PASS asm_arm_end_pass
405#define ASM_ENTRY asm_arm_entry
406#define ASM_EXIT asm_arm_exit
407
408#define ASM_LABEL_ASSIGN asm_arm_label_assign
409#define ASM_JUMP asm_arm_b_label
410#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
411 do { \
412 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100413 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100414 } while (0)
415#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
416 do { \
417 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100418 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100419 } while (0)
420#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
421 do { \
422 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100423 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100424 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100425#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 +0100426
427#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
428#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
429#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
430#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
431 do { \
432 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
433 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
434 } while (false)
435#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 +0100436#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 +0100437#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
438
Fabian Vogte5268962014-10-04 00:53:46 +0200439#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
440#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 +0100441#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
442#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
443#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 +0100444#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
445#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
446
Damien George91cfd412014-10-12 16:59:29 +0100447#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base))
448#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
449#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
450
Fabian Vogte5268962014-10-04 00:53:46 +0200451#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
452#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
453#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 +0100454
Damien Georgec90f59e2014-09-06 23:06:36 +0100455#else
456
457#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200458
Damien13ed3a62013-10-08 09:05:10 +0100459#endif
460
461typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100462 STACK_VALUE,
463 STACK_REG,
464 STACK_IMM,
465} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100466
Damien Georgee9dac3b2014-09-29 22:10:41 +0100467// these enums must be distinct and the bottom 2 bits
468// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100469typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100470 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
471 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
472 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
473 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
474
475 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
476 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
477 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
478 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
479
480 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
481 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100482} vtype_kind_t;
483
Damienff8ed772013-10-08 22:18:32 +0100484typedef struct _stack_info_t {
485 vtype_kind_t vtype;
486 stack_info_kind_t kind;
487 union {
488 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100489 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000490 } data;
Damienff8ed772013-10-08 22:18:32 +0100491} stack_info_t;
492
Damien13ed3a62013-10-08 09:05:10 +0100493struct _emit_t {
494 int pass;
495
496 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100497
Damien George2ac4af62014-08-15 16:45:41 +0100498 vtype_kind_t return_vtype;
499
Damien George7ff996c2014-09-08 23:05:16 +0100500 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100501 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100502
Damien George7ff996c2014-09-08 23:05:16 +0100503 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100504 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100505 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100506
Damien13ed3a62013-10-08 09:05:10 +0100507 int stack_start;
508 int stack_size;
509
510 bool last_emit_was_return_value;
511
Damien13ed3a62013-10-08 09:05:10 +0100512 scope_t *scope;
513
Damien Georgec90f59e2014-09-06 23:06:36 +0100514 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100515};
516
Damien George7ff996c2014-09-08 23:05:16 +0100517emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100518 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100519 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100520 return emit;
521}
522
Damien George41d02b62014-01-24 22:42:28 +0000523void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100524 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100525 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
526 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200527 m_del_obj(emit_t, emit);
528}
529
Damien George2ac4af62014-08-15 16:45:41 +0100530STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
531 switch (op) {
532 case MP_EMIT_NATIVE_TYPE_ENABLE:
533 emit->do_viper_types = arg1;
534 break;
535
536 default: {
537 vtype_kind_t type;
538 switch (arg2) {
539 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
540 case MP_QSTR_bool: type = VTYPE_BOOL; break;
541 case MP_QSTR_int: type = VTYPE_INT; break;
542 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100543 case MP_QSTR_ptr: type = VTYPE_PTR; break;
544 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
545 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100546 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
547 }
548 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
549 emit->return_vtype = type;
550 } else {
551 assert(arg1 < emit->local_vtype_alloc);
552 emit->local_vtype[arg1] = type;
553 }
554 break;
555 }
556 }
Damien13ed3a62013-10-08 09:05:10 +0100557}
558
Damien Georgefa5950e2015-04-03 15:03:24 +0000559STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest);
Damien George4cd9ced2015-01-15 14:41:41 +0000560STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg);
Damien Georgefa5950e2015-04-03 15:03:24 +0000561STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien George4cd9ced2015-01-15 14:41:41 +0000562STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num);
Damien Georgefa5950e2015-04-03 15:03:24 +0000563
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200564STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000565 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
566
Damien13ed3a62013-10-08 09:05:10 +0100567 emit->pass = pass;
568 emit->stack_start = 0;
569 emit->stack_size = 0;
570 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100571 emit->scope = scope;
572
Damien George36db6bc2014-05-07 17:24:22 +0100573 // allocate memory for keeping track of the types of locals
574 if (emit->local_vtype_alloc < scope->num_locals) {
575 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
576 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100577 }
Damien George36db6bc2014-05-07 17:24:22 +0100578
579 // allocate memory for keeping track of the objects on the stack
580 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damien George51229af2015-03-26 17:54:12 +0000581 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100582 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000583 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100584 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100585 }
586
Damien George2ac4af62014-08-15 16:45:41 +0100587 // set default type for return and arguments
588 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100589 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100590 emit->local_vtype[i] = VTYPE_PYOBJ;
591 }
Damien Georgea5190a72014-08-15 22:39:08 +0100592
593 // local variables begin unbound, and have unknown type
594 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
595 emit->local_vtype[i] = VTYPE_UNBOUND;
596 }
597
598 // values on stack begin unbound
599 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100600 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100601 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100602 }
603
Damien Georgec90f59e2014-09-06 23:06:36 +0100604 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100605
606 // entry to function
607 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100608 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100609 num_locals = scope->num_locals - REG_LOCAL_NUM;
610 if (num_locals < 0) {
611 num_locals = 0;
612 }
613 emit->stack_start = num_locals;
614 num_locals += scope->stack_size;
615 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100616 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100617
618 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100619#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100620 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100621 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100622 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100623 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100624 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100625 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100626 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100627 } else if (i == 3) {
628 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100629 } else {
630 // TODO not implemented
631 assert(0);
632 }
633 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100634#elif N_X86
635 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100636 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000637 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100638 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000639 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000640 } else if (i == 2) {
641 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100642 } else {
Damien George03281b32014-09-06 22:38:50 +0000643 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
644 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100645 }
646 }
Damien3ef4abb2013-10-12 16:53:13 +0100647#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100648 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100649 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100650 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100651 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100652 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100653 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100654 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100655 } else if (i == 3) {
656 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
657 } else {
658 // TODO not implemented
659 assert(0);
660 }
661 }
662
Damien Georgee9dac3b2014-09-29 22:10:41 +0100663 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100664 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200665#elif N_ARM
666 for (int i = 0; i < scope->num_pos_args; i++) {
667 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100668 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200669 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100670 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200671 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100672 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200673 } else if (i == 3) {
674 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
675 } else {
676 // TODO not implemented
677 assert(0);
678 }
679 }
680
Damien Georgee9dac3b2014-09-29 22:10:41 +0100681 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100682 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100683#else
684 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100685#endif
Damien George4cd9ced2015-01-15 14:41:41 +0000686
687 // initialise closed over variables
688 for (int i = 0; i < scope->id_info_len; i++) {
689 id_info_t *id = &scope->id_info[i];
690 if (id->kind == ID_INFO_KIND_CELL) {
Damien Georgefa5950e2015-04-03 15:03:24 +0000691 if (emit->local_vtype[id->local_num] != VTYPE_UNBOUND) {
692 emit_native_load_fast(emit, id->qst, id->local_num);
693 vtype_kind_t vtype;
694 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
695 }
Damien George4cd9ced2015-01-15 14:41:41 +0000696 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_NEW_CELL], MP_F_NEW_CELL);
697 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
698 emit_native_store_fast(emit, id->qst, id->local_num);
699 }
700 }
Damien13ed3a62013-10-08 09:05:10 +0100701}
702
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200703STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100704 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100705 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100706 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100707 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100708
709 // check stack is back to zero size
710 if (emit->stack_size != 0) {
711 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
712 }
713
Damien George36db6bc2014-05-07 17:24:22 +0100714 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100715 void *f = ASM_GET_CODE(emit->as);
716 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100717
718 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100719 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100720 mp_uint_t type_sig = emit->return_vtype & 3;
721 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
722 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
723 }
724
725 mp_emit_glue_assign_native(emit->scope->raw_code, emit->do_viper_types ? MP_CODE_NATIVE_VIPER : MP_CODE_NATIVE_PY, f, f_len, emit->scope->num_pos_args, type_sig);
Damien13ed3a62013-10-08 09:05:10 +0100726 }
727}
728
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200729STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100730 return emit->last_emit_was_return_value;
731}
732
Damien Georged6230f62014-09-23 14:10:03 +0000733STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000734 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100735 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100736 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100737 emit->scope->stack_size = emit->stack_size;
738 }
Damien George21ca2d72014-10-19 19:00:51 +0100739#ifdef DEBUG_PRINT
740 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
741 for (int i = 0; i < emit->stack_size; i++) {
742 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000743 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100744 }
745 DEBUG_printf("\n");
746#endif
Damien13ed3a62013-10-08 09:05:10 +0100747}
748
Damien Georged6230f62014-09-23 14:10:03 +0000749STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100750 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000751 // If we are adjusting the stack in a positive direction (pushing) then we
752 // need to fill in values for the stack kind and vtype of the newly-pushed
753 // entries. These should be set to "value" (ie not reg or imm) because we
754 // should only need to adjust the stack due to a jump to this part in the
755 // code (and hence we have settled the stack before the jump).
756 for (mp_int_t i = 0; i < delta; i++) {
757 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
758 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100759 // TODO we don't know the vtype to use here. At the moment this is a
760 // hack to get the case of multi comparison working.
761 if (delta == 1) {
762 si->vtype = emit->saved_stack_vtype;
763 } else {
764 si->vtype = VTYPE_PYOBJ;
765 }
Damien Georged6230f62014-09-23 14:10:03 +0000766 }
767 adjust_stack(emit, delta);
768}
769
770STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000771 (void)emit;
772 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000773}
774
Damienff8ed772013-10-08 22:18:32 +0100775/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200776STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100777 adjust_stack(emit, stack_size_delta);
778 emit->last_emit_was_return_value = false;
779}
Damienff8ed772013-10-08 22:18:32 +0100780*/
Damien13ed3a62013-10-08 09:05:10 +0100781
Damienff8ed772013-10-08 22:18:32 +0100782// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000783STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100784 emit->last_emit_was_return_value = false;
785 // settle the stack
786 /*
787 if (regs_needed != 0) {
788 for (int i = 0; i < emit->stack_size; i++) {
789 switch (emit->stack_info[i].kind) {
790 case STACK_VALUE:
791 break;
792
793 case STACK_REG:
794 // TODO only push reg if in regs_needed
795 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000796 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100797 break;
798
799 case STACK_IMM:
800 // don't think we ever need to push imms for settling
801 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
802 break;
803 }
804 }
805 }
806 */
Damien13ed3a62013-10-08 09:05:10 +0100807}
808
Damien George3112cde2014-09-29 18:45:42 +0100809// depth==0 is top, depth==1 is before top, etc
810STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
811 return &emit->stack_info[emit->stack_size - 1 - depth];
812}
813
814// depth==0 is top, depth==1 is before top, etc
815STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
816 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100817}
Damien13ed3a62013-10-08 09:05:10 +0100818
Damiend2755ec2013-10-16 23:58:48 +0100819// pos=1 is TOS, pos=2 is next, etc
820// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200821STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100822 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100823 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100824 if (i != skip_stack_pos) {
825 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000826 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100827 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000828 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100829 }
Damienff8ed772013-10-08 22:18:32 +0100830 }
831 }
832}
Damien13ed3a62013-10-08 09:05:10 +0100833
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200834STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100835 for (int i = 0; i < emit->stack_size; i++) {
836 stack_info_t *si = &emit->stack_info[i];
837 if (si->kind == STACK_REG) {
838 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000839 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100840 }
Damien13ed3a62013-10-08 09:05:10 +0100841 }
842}
843
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200844STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000845 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100846 for (int i = 0; i < emit->stack_size; i++) {
847 stack_info_t *si = &emit->stack_info[i];
848 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000849 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100850 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000851 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100852 }
853 }
854 for (int i = 0; i < emit->stack_size; i++) {
855 stack_info_t *si = &emit->stack_info[i];
856 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000857 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100858 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000859 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100860 }
861 }
862}
863
864// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200865STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100866 need_reg_single(emit, reg_dest, pos);
867 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100868 *vtype = si->vtype;
869 switch (si->kind) {
870 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100871 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100872 break;
873
Damienff8ed772013-10-08 22:18:32 +0100874 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000875 if (si->data.u_reg != reg_dest) {
876 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100877 }
878 break;
879
Damienff8ed772013-10-08 22:18:32 +0100880 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +0000881 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100882 break;
883 }
Damien13ed3a62013-10-08 09:05:10 +0100884}
885
Damien Georgee9dac3b2014-09-29 22:10:41 +0100886// does an efficient X=pop(); discard(); push(X)
887// needs a (non-temp) register in case the poped element was stored in the stack
888STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
889 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
890 si[0] = si[1];
891 if (si->kind == STACK_VALUE) {
892 // if folded element was on the stack we need to put it in a register
893 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
894 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000895 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100896 }
897 adjust_stack(emit, -1);
898}
899
900// If stacked value is in a register and the register is not r1 or r2, then
901// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
902STATIC 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 +0100903 emit->last_emit_was_return_value = false;
904 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +0000905 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100906 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +0000907 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +0100908 need_reg_single(emit, *reg_dest, 1);
909 } else {
910 emit_access_stack(emit, 1, vtype, *reg_dest);
911 }
912 adjust_stack(emit, -1);
913}
914
Damien Georgee6ce10a2014-09-06 18:38:20 +0100915STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100916 emit->last_emit_was_return_value = false;
917 adjust_stack(emit, -1);
918}
919
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200920STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100921 emit->last_emit_was_return_value = false;
922 emit_access_stack(emit, 1, vtype, reg_dest);
923 adjust_stack(emit, -1);
924}
925
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200926STATIC 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 +0100927 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100928 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100929}
930
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200931STATIC 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 +0100932 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100933 emit_pre_pop_reg(emit, vtypeb, regb);
934 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100935}
936
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200937STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000938 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +0100939}
940
Damien Georgee9dac3b2014-09-29 22:10:41 +0100941STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
942 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
943 si->vtype = new_vtype;
944}
945
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200946STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100947 stack_info_t *si = &emit->stack_info[emit->stack_size];
948 si->vtype = vtype;
949 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000950 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100951 adjust_stack(emit, 1);
952}
953
Damien George40f3c022014-07-03 13:25:24 +0100954STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100955 stack_info_t *si = &emit->stack_info[emit->stack_size];
956 si->vtype = vtype;
957 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +0000958 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +0100959 adjust_stack(emit, 1);
960}
961
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200962STATIC 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 +0100963 emit_post_push_reg(emit, vtypea, rega);
964 emit_post_push_reg(emit, vtypeb, regb);
965}
966
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200967STATIC 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 +0100968 emit_post_push_reg(emit, vtypea, rega);
969 emit_post_push_reg(emit, vtypeb, regb);
970 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100971}
972
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200973STATIC 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 +0100974 emit_post_push_reg(emit, vtypea, rega);
975 emit_post_push_reg(emit, vtypeb, regb);
976 emit_post_push_reg(emit, vtypec, regc);
977 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100978}
979
Damien George7fe21912014-08-16 22:31:57 +0100980STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100981 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100982 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100983}
984
Damien George7fe21912014-08-16 22:31:57 +0100985STATIC 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 +0100986 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100987 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
988 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100989}
990
Damien George40f3c022014-07-03 13:25:24 +0100991// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100992STATIC 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 +0100993 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100994 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
995 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100996}
997
Damien George7fe21912014-08-16 22:31:57 +0100998STATIC 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 +0000999 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001000 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1001 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1002 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +00001003}
1004
Damien George40f3c022014-07-03 13:25:24 +01001005// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +01001006STATIC 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 +01001007 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001008 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
1009 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
1010 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
1011 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +01001012}
1013
Damien George86de21b2014-08-16 22:06:11 +01001014// vtype of all n_pop objects is VTYPE_PYOBJ
1015// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
1016// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
1017// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
1018STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1019 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001020
Damien George86de21b2014-08-16 22:06:11 +01001021 // First, store any immediate values to their respective place on the stack.
1022 for (mp_uint_t i = 0; i < n_pop; i++) {
1023 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1024 // must push any imm's to stack
1025 // must convert them to VTYPE_PYOBJ for viper code
1026 if (si->kind == STACK_IMM) {
1027 si->kind = STACK_VALUE;
1028 switch (si->vtype) {
1029 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001030 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 +01001031 break;
1032 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001033 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001034 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 +01001035 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001036 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 +01001037 }
1038 si->vtype = VTYPE_PYOBJ;
1039 break;
1040 case VTYPE_INT:
1041 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001042 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 +01001043 si->vtype = VTYPE_PYOBJ;
1044 break;
1045 default:
1046 // not handled
1047 assert(0);
1048 }
1049 }
1050
1051 // verify that this value is on the stack
1052 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001053 }
Damien George86de21b2014-08-16 22:06:11 +01001054
1055 // Second, convert any non-VTYPE_PYOBJ to that type.
1056 for (mp_uint_t i = 0; i < n_pop; i++) {
1057 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1058 if (si->vtype != VTYPE_PYOBJ) {
1059 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001060 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001061 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 +01001062 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001063 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001064 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001065 }
1066 }
1067
1068 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1069 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001070 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001071}
1072
1073// vtype of all n_push objects is VTYPE_PYOBJ
1074STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1075 need_reg_all(emit);
1076 for (mp_uint_t i = 0; i < n_push; i++) {
1077 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1078 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1079 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001080 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001081 adjust_stack(emit, n_push);
1082}
1083
Damien George7ff996c2014-09-08 23:05:16 +01001084STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001085 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001086 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001087 // need to commit stack because we can jump here from elsewhere
1088 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001089 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001090 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001091}
1092
Damien Georgecdd96df2014-04-06 12:58:40 +01001093STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1094 DEBUG_printf("import_name %s\n", qstr_str(qst));
1095 vtype_kind_t vtype_fromlist;
1096 vtype_kind_t vtype_level;
1097 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1098 assert(vtype_fromlist == VTYPE_PYOBJ);
1099 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001100 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001101 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001102}
1103
Damien Georgecdd96df2014-04-06 12:58:40 +01001104STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1105 DEBUG_printf("import_from %s\n", qstr_str(qst));
1106 emit_native_pre(emit);
1107 vtype_kind_t vtype_module;
1108 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1109 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001110 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001111 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001112}
1113
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001114STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001115 DEBUG_printf("import_star\n");
1116 vtype_kind_t vtype_module;
1117 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1118 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001119 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001120 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001121}
1122
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001123STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001124 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001125 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001126 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001127 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001128 if (emit->do_viper_types) {
1129 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001130 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1131 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1132 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001133 no_other_choice1:
1134 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1135 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001136 }
1137 } else {
1138 vtype = VTYPE_PYOBJ;
1139 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001140 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1141 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1142 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001143 no_other_choice2:
1144 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1145 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001146 }
1147 }
1148 emit_post_push_imm(emit, vtype, val);
1149}
1150
Damien George40f3c022014-07-03 13:25:24 +01001151STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001152 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001153 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001154 if (emit->do_viper_types) {
1155 emit_post_push_imm(emit, VTYPE_INT, arg);
1156 } else {
Damien George2686f9b2015-04-01 00:12:43 +01001157 emit_post_push_imm(emit, VTYPE_PYOBJ, (mp_uint_t)MP_OBJ_NEW_SMALL_INT(arg));
Damien13ed3a62013-10-08 09:05:10 +01001158 }
1159}
1160
Damien George7ff996c2014-09-08 23:05:16 +01001161STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001162 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001163 // TODO: Eventually we want to be able to work with raw pointers in viper to
1164 // do native array access. For now we just load them as any other object.
1165 /*
Damien13ed3a62013-10-08 09:05:10 +01001166 if (emit->do_viper_types) {
1167 // not implemented properly
1168 // load a pointer to the asciiz string?
1169 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001170 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001171 } else
1172 */
1173 {
Damien Georgeb601d952014-06-30 05:17:25 +01001174 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001175 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001176 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001177 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001178 }
Damien13ed3a62013-10-08 09:05:10 +01001179 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1180 }
1181}
1182
Damien Georgedab13852015-01-13 15:55:54 +00001183STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1184 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001185 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001186 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1187 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1188}
1189
Damien George3558f622014-04-20 17:50:40 +01001190STATIC void emit_native_load_null(emit_t *emit) {
1191 emit_native_pre(emit);
1192 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1193}
1194
Damien George0abb5602015-01-16 12:24:49 +00001195STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1196 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001197 vtype_kind_t vtype = emit->local_vtype[local_num];
1198 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001199 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001200 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001201 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001202#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001203 if (local_num == 0) {
1204 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001205 } else if (local_num == 1) {
1206 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1207 } else if (local_num == 2) {
1208 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001209 } else {
Damien George0b610de2014-09-29 16:25:04 +01001210 need_reg_single(emit, REG_TEMP0, 0);
1211 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1212 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001213 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001214#elif N_X86
1215 if (local_num == 0) {
1216 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001217 } else if (local_num == 1) {
1218 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001219 } else if (local_num == 2) {
1220 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001221 } else {
Damien George0b610de2014-09-29 16:25:04 +01001222 need_reg_single(emit, REG_TEMP0, 0);
1223 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1224 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001225 }
Damien3ef4abb2013-10-12 16:53:13 +01001226#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001227 if (local_num == 0) {
1228 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1229 } else if (local_num == 1) {
1230 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1231 } else if (local_num == 2) {
1232 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1233 } else {
Damien George0b610de2014-09-29 16:25:04 +01001234 need_reg_single(emit, REG_TEMP0, 0);
1235 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1236 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001237 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001238#elif N_ARM
1239 if (local_num == 0) {
1240 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1241 } else if (local_num == 1) {
1242 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1243 } else if (local_num == 2) {
1244 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1245 } else {
Damien George0b610de2014-09-29 16:25:04 +01001246 need_reg_single(emit, REG_TEMP0, 0);
1247 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1248 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001249 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001250#else
1251 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001252#endif
1253}
1254
Damien George7ff996c2014-09-08 23:05:16 +01001255STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001256 DEBUG_printf("load_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1257 need_reg_single(emit, REG_RET, 0);
1258 emit_native_load_fast(emit, qst, local_num);
1259 vtype_kind_t vtype;
1260 int reg_base = REG_RET;
1261 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1262 ASM_LOAD_REG_REG_OFFSET(emit->as, REG_RET, reg_base, 1);
1263 // closed over vars are always Python objects
1264 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien9ecbcff2013-12-11 00:41:43 +00001265}
1266
Damien George7ff996c2014-09-08 23:05:16 +01001267STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001268 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001269 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001270 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001271 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1272}
1273
Damien George7ff996c2014-09-08 23:05:16 +01001274STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001275 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001276 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001277 // check for builtin casting operators
1278 if (emit->do_viper_types && qst == MP_QSTR_int) {
1279 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1280 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1281 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1282 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1283 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1284 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1285 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1286 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1287 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1288 } else {
1289 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1290 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1291 }
Damien13ed3a62013-10-08 09:05:10 +01001292}
1293
Damien George7ff996c2014-09-08 23:05:16 +01001294STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001295 // depends on type of subject:
1296 // - integer, function, pointer to integers: error
1297 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001298 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001299 vtype_kind_t vtype_base;
1300 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1301 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001302 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001303 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1304}
1305
Damien George7ff996c2014-09-08 23:05:16 +01001306STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001307 vtype_kind_t vtype_base;
1308 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1309 assert(vtype_base == VTYPE_PYOBJ);
1310 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001311 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001312}
1313
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001314STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001315 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001316 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001317 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001318}
1319
Damien George729f7b42014-04-17 22:10:53 +01001320STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001321 DEBUG_printf("load_subscr\n");
1322 // need to compile: base[index]
1323
1324 // pop: index, base
1325 // optimise case where index is an immediate
1326 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1327
1328 if (vtype_base == VTYPE_PYOBJ) {
1329 // standard Python call
1330 vtype_kind_t vtype_index;
1331 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1332 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001333 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 +01001334 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1335 } else {
Damien George91cfd412014-10-12 16:59:29 +01001336 // viper load
1337 // TODO The different machine architectures have very different
1338 // capabilities and requirements for loads, so probably best to
1339 // write a completely separate load-optimiser for each one.
1340 stack_info_t *top = peek_stack(emit, 0);
1341 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1342 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001343 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001344 emit_pre_pop_discard(emit); // discard index
1345 int reg_base = REG_ARG_1;
1346 int reg_index = REG_ARG_2;
1347 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1348 switch (vtype_base) {
1349 case VTYPE_PTR8: {
1350 // pointer to 8-bit memory
1351 // TODO optimise to use thumb ldrb r1, [r2, r3]
1352 if (index_value != 0) {
1353 // index is non-zero
1354 #if N_THUMB
1355 if (index_value > 0 && index_value < 32) {
1356 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1357 break;
1358 }
1359 #endif
1360 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1361 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1362 reg_base = reg_index;
1363 }
1364 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1365 break;
1366 }
1367 case VTYPE_PTR16: {
1368 // pointer to 16-bit memory
1369 if (index_value != 0) {
1370 // index is a non-zero immediate
1371 #if N_THUMB
1372 if (index_value > 0 && index_value < 32) {
1373 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1374 break;
1375 }
1376 #endif
1377 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1378 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1379 reg_base = reg_index;
1380 }
1381 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1382 break;
1383 }
1384 default:
1385 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1386 }
1387 } else {
1388 // index is not an immediate
1389 vtype_kind_t vtype_index;
1390 int reg_index = REG_ARG_2;
1391 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1392 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1393 switch (vtype_base) {
1394 case VTYPE_PTR8: {
1395 // pointer to 8-bit memory
1396 // TODO optimise to use thumb ldrb r1, [r2, r3]
1397 assert(vtype_index == VTYPE_INT);
1398 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1399 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1400 break;
1401 }
1402 case VTYPE_PTR16: {
1403 // pointer to 16-bit memory
1404 assert(vtype_index == VTYPE_INT);
1405 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1406 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1407 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1408 break;
1409 }
1410 default:
1411 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1412 }
1413 }
1414 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001415 }
1416}
1417
Damien George7ff996c2014-09-08 23:05:16 +01001418STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001419 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001420#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001421 if (local_num == 0) {
1422 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001423 } else if (local_num == 1) {
1424 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1425 } else if (local_num == 2) {
1426 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001427 } else {
Damien George0b610de2014-09-29 16:25:04 +01001428 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1429 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001430 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001431#elif N_X86
1432 if (local_num == 0) {
1433 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001434 } else if (local_num == 1) {
1435 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001436 } else if (local_num == 2) {
1437 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001438 } else {
Damien George0b610de2014-09-29 16:25:04 +01001439 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1440 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001441 }
Damien3ef4abb2013-10-12 16:53:13 +01001442#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001443 if (local_num == 0) {
1444 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1445 } else if (local_num == 1) {
1446 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1447 } else if (local_num == 2) {
1448 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1449 } else {
Damien George0b610de2014-09-29 16:25:04 +01001450 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1451 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001452 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001453#elif N_ARM
1454 if (local_num == 0) {
1455 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1456 } else if (local_num == 1) {
1457 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1458 } else if (local_num == 2) {
1459 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1460 } else {
Damien George0b610de2014-09-29 16:25:04 +01001461 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1462 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001463 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001464#else
1465 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001466#endif
1467
1468 emit_post(emit);
1469
1470 // check types
1471 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1472 // first time this local is assigned, so give it a type of the object stored in it
1473 emit->local_vtype[local_num] = vtype;
1474 } else if (emit->local_vtype[local_num] != vtype) {
1475 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001476 printf("ViperTypeError: type mismatch, local %s has type %d but source object has type %d\n", qstr_str(qst), emit->local_vtype[local_num], vtype);
Damien13ed3a62013-10-08 09:05:10 +01001477 }
1478}
1479
Damien George7ff996c2014-09-08 23:05:16 +01001480STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George4cd9ced2015-01-15 14:41:41 +00001481 DEBUG_printf("store_deref(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
1482 need_reg_single(emit, REG_TEMP0, 0);
1483 need_reg_single(emit, REG_TEMP1, 0);
1484 emit_native_load_fast(emit, qst, local_num);
1485 vtype_kind_t vtype;
1486 int reg_base = REG_TEMP0;
1487 emit_pre_pop_reg_flexible(emit, &vtype, &reg_base, -1, -1);
1488 int reg_src = REG_TEMP1;
1489 emit_pre_pop_reg_flexible(emit, &vtype, &reg_src, reg_base, reg_base);
1490 ASM_STORE_REG_REG_OFFSET(emit->as, reg_src, reg_base, 1);
1491 emit_post(emit);
Damien9ecbcff2013-12-11 00:41:43 +00001492}
1493
Damien George7ff996c2014-09-08 23:05:16 +01001494STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001495 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001496 vtype_kind_t vtype;
1497 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1498 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001499 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001500 emit_post(emit);
1501}
1502
Damien George7ff996c2014-09-08 23:05:16 +01001503STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001504 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001505 if (vtype == VTYPE_PYOBJ) {
1506 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1507 } else {
1508 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001509 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 +01001510 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001511 }
Damien George7ff996c2014-09-08 23:05:16 +01001512 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001513 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001514}
1515
Damien George7ff996c2014-09-08 23:05:16 +01001516STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001517 vtype_kind_t vtype_base, vtype_val;
1518 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1519 assert(vtype_base == VTYPE_PYOBJ);
1520 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001521 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001522 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001523}
1524
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001525STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001526 DEBUG_printf("store_subscr\n");
1527 // need to compile: base[index] = value
1528
1529 // pop: index, base, value
1530 // optimise case where index is an immediate
1531 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1532
1533 if (vtype_base == VTYPE_PYOBJ) {
1534 // standard Python call
1535 vtype_kind_t vtype_index, vtype_value;
1536 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1537 assert(vtype_index == VTYPE_PYOBJ);
1538 assert(vtype_value == VTYPE_PYOBJ);
1539 emit_call(emit, MP_F_OBJ_SUBSCR);
1540 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001541 // viper store
1542 // TODO The different machine architectures have very different
1543 // capabilities and requirements for stores, so probably best to
1544 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001545 stack_info_t *top = peek_stack(emit, 0);
1546 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1547 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001548 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001549 emit_pre_pop_discard(emit); // discard index
1550 vtype_kind_t vtype_value;
1551 int reg_base = REG_ARG_1;
1552 int reg_index = REG_ARG_2;
1553 int reg_value = REG_ARG_3;
1554 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001555 #if N_X86
1556 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1557 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1558 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001559 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001560 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001561 switch (vtype_base) {
1562 case VTYPE_PTR8: {
1563 // pointer to 8-bit memory
1564 // TODO optimise to use thumb strb r1, [r2, r3]
1565 if (index_value != 0) {
1566 // index is non-zero
1567 #if N_THUMB
1568 if (index_value > 0 && index_value < 32) {
1569 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1570 break;
1571 }
1572 #endif
1573 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001574 #if N_ARM
1575 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1576 return;
1577 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001578 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1579 reg_base = reg_index;
1580 }
1581 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1582 break;
1583 }
1584 case VTYPE_PTR16: {
1585 // pointer to 16-bit memory
1586 if (index_value != 0) {
1587 // index is a non-zero immediate
1588 #if N_THUMB
1589 if (index_value > 0 && index_value < 32) {
1590 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1591 break;
1592 }
1593 #endif
1594 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001595 #if N_ARM
1596 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1597 return;
1598 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001599 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1600 reg_base = reg_index;
1601 }
1602 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1603 break;
1604 }
1605 default:
1606 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1607 }
1608 } else {
1609 // index is not an immediate
1610 vtype_kind_t vtype_index, vtype_value;
1611 int reg_index = REG_ARG_2;
1612 int reg_value = REG_ARG_3;
1613 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1614 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001615 #if N_X86
1616 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1617 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1618 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001619 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001620 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001621 switch (vtype_base) {
1622 case VTYPE_PTR8: {
1623 // pointer to 8-bit memory
1624 // TODO optimise to use thumb strb r1, [r2, r3]
1625 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001626 #if N_ARM
1627 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1628 break;
1629 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001630 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1631 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1632 break;
1633 }
1634 case VTYPE_PTR16: {
1635 // pointer to 16-bit memory
1636 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001637 #if N_ARM
1638 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1639 break;
1640 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001641 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1642 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1643 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1644 break;
1645 }
1646 default:
1647 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1648 }
1649 }
1650
1651 }
Damien13ed3a62013-10-08 09:05:10 +01001652}
1653
Damien George7ff996c2014-09-08 23:05:16 +01001654STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien George2cc54732015-04-03 14:29:30 +01001655 // TODO: This is not compliant implementation. We could use MP_OBJ_SENTINEL
1656 // to mark deleted vars but then every var would need to be checked on
1657 // each access. Very inefficient, so just set value to None to enable GC.
1658 emit_native_load_const_tok(emit, MP_TOKEN_KW_NONE);
1659 emit_native_store_fast(emit, qst, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001660}
1661
Damien George7ff996c2014-09-08 23:05:16 +01001662STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001663 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001664 (void)emit;
1665 (void)qst;
1666 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001667}
1668
Damien Georgee6ce10a2014-09-06 18:38:20 +01001669STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1670 emit_native_pre(emit);
1671 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1672 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001673}
1674
Damien Georgee6ce10a2014-09-06 18:38:20 +01001675STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1676 emit_native_pre(emit);
1677 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1678 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001679}
1680
Damien Georgee6ce10a2014-09-06 18:38:20 +01001681STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001682 vtype_kind_t vtype_base;
1683 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1684 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001685 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 +01001686 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001687}
1688
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001689STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001690 vtype_kind_t vtype_index, vtype_base;
1691 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1692 assert(vtype_index == VTYPE_PYOBJ);
1693 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001694 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 +01001695}
1696
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001697STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001698 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001699 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001700 int reg = REG_TEMP0;
1701 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1702 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001703}
1704
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001705STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001706 vtype_kind_t vtype0, vtype1;
1707 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1708 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1709}
1710
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001711STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001712 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001713 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001714 emit_post(emit);
1715}
1716
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001717STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001718 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001719 vtype_kind_t vtype0, vtype1;
1720 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1721 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001722}
1723
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001724STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001725 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001726 vtype_kind_t vtype0, vtype1, vtype2;
1727 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1728 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1729}
1730
Damien George7ff996c2014-09-08 23:05:16 +01001731STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001732 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001733 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001734 // need to commit stack because we are jumping elsewhere
1735 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001736 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001737 emit_post(emit);
1738}
1739
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001740STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001741 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001742 switch (vtype) {
1743 case VTYPE_PYOBJ:
1744 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1745 if (!pop) {
1746 adjust_stack(emit, 1);
1747 }
1748 emit_call(emit, MP_F_OBJ_IS_TRUE);
1749 break;
1750 case VTYPE_BOOL:
1751 case VTYPE_INT:
1752 case VTYPE_UINT:
1753 emit_pre_pop_reg(emit, &vtype, REG_RET);
1754 if (!pop) {
1755 adjust_stack(emit, 1);
1756 }
1757 break;
1758 default:
1759 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1760 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001761 }
Damien George21ca2d72014-10-19 19:00:51 +01001762 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1763 // can use it. This is a bit of a hack.
1764 if (!pop) {
1765 emit->saved_stack_vtype = vtype;
1766 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001767 // need to commit stack because we may jump elsewhere
1768 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001769}
1770
Damien George63f38322015-02-28 15:04:06 +00001771STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1772 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001773 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001774 if (cond) {
1775 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1776 } else {
1777 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1778 }
Damien1a6633a2013-11-03 13:58:19 +00001779 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001780}
Damien1a6633a2013-11-03 13:58:19 +00001781
Damien George63f38322015-02-28 15:04:06 +00001782STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1783 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001784 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001785 if (cond) {
1786 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1787 } else {
1788 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1789 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001790 adjust_stack(emit, -1);
1791 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001792}
1793
Damien George7ff996c2014-09-08 23:05:16 +01001794STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001795 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001796 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001797}
Damien Georgea32c1e42014-05-07 18:30:52 +01001798
Damien George7ff996c2014-09-08 23:05:16 +01001799STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001800 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001801 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001802}
Damien Georgea32c1e42014-05-07 18:30:52 +01001803
Damien George7ff996c2014-09-08 23:05:16 +01001804STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001805 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001806 (void)emit;
1807 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001808 assert(0);
1809}
Damien Georgeb601d952014-06-30 05:17:25 +01001810
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001811STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001812 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001813 assert(0);
1814}
Damien Georgeb601d952014-06-30 05:17:25 +01001815
Damien George7ff996c2014-09-08 23:05:16 +01001816STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001817 emit_native_pre(emit);
1818 // need to commit stack because we may jump elsewhere
1819 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001820 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 +01001821 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001822 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001823 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001824}
Damien Georgeb601d952014-06-30 05:17:25 +01001825
Damien George7ff996c2014-09-08 23:05:16 +01001826STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001827 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001828}
Damien Georgeb601d952014-06-30 05:17:25 +01001829
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001830STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001831 // logic:
1832 // exc = pop_stack
1833 // if exc == None: pass
1834 // else: raise exc
1835 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1836 vtype_kind_t vtype;
1837 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1838 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001839 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001840}
Damiend2755ec2013-10-16 23:58:48 +01001841
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001842STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001843 // perhaps the difficult one, as we want to rewrite for loops using native code
1844 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001845
1846 vtype_kind_t vtype;
1847 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1848 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001849 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001850 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001851}
Damiend2755ec2013-10-16 23:58:48 +01001852
Damien George7ff996c2014-09-08 23:05:16 +01001853STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001854 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001855 vtype_kind_t vtype;
1856 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1857 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001858 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001859 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1860 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001861 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1862}
1863
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001864STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001865 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001866 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001867 adjust_stack(emit, -1);
1868 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001869}
1870
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001871STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001872 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001873 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001874 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001875 emit_post(emit);
1876}
1877
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001878STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001879 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001880 /*
1881 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001882 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001883 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001884 emit_post(emit);
1885 */
Damien13ed3a62013-10-08 09:05:10 +01001886}
1887
Damien Georged17926d2014-03-30 13:35:08 +01001888STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001889 vtype_kind_t vtype;
1890 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1891 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001892 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001893 // we need to synthesise this operation by converting to bool first
1894 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001895 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001896 }
Damien Georged6230f62014-09-23 14:10:03 +00001897 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1898 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001899}
1900
Damien Georged17926d2014-03-30 13:35:08 +01001901STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001902 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001903 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1904 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001905 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001906 #if N_X64 || N_X86
1907 // special cases for x86 and shifting
1908 if (op == MP_BINARY_OP_LSHIFT
1909 || op == MP_BINARY_OP_INPLACE_LSHIFT
1910 || op == MP_BINARY_OP_RSHIFT
1911 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1912 #if N_X64
1913 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1914 #else
1915 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1916 #endif
1917 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1918 ASM_LSL_REG(emit->as, REG_RET);
1919 } else {
1920 ASM_ASR_REG(emit->as, REG_RET);
1921 }
1922 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1923 return;
1924 }
1925 #endif
1926 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001927 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001928 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1929 if (0) {
1930 // dummy
1931 #if !(N_X64 || N_X86)
1932 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1933 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001934 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001935 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1936 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1937 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1938 #endif
Damien George1ef23482014-10-12 14:21:06 +01001939 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1940 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1941 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1942 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1943 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1944 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1945 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1946 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1947 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001948 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1949 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1950 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1951 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1952 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1953 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1954 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1955 // comparison ops are (in enum order):
1956 // MP_BINARY_OP_LESS
1957 // MP_BINARY_OP_MORE
1958 // MP_BINARY_OP_EQUAL
1959 // MP_BINARY_OP_LESS_EQUAL
1960 // MP_BINARY_OP_MORE_EQUAL
1961 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001962 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001963 #if N_X64
1964 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1965 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1966 static byte ops[6] = {
1967 ASM_X64_CC_JL,
1968 ASM_X64_CC_JG,
1969 ASM_X64_CC_JE,
1970 ASM_X64_CC_JLE,
1971 ASM_X64_CC_JGE,
1972 ASM_X64_CC_JNE,
1973 };
1974 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1975 #elif N_X86
1976 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1977 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1978 static byte ops[6] = {
1979 ASM_X86_CC_JL,
1980 ASM_X86_CC_JG,
1981 ASM_X86_CC_JE,
1982 ASM_X86_CC_JLE,
1983 ASM_X86_CC_JGE,
1984 ASM_X86_CC_JNE,
1985 };
1986 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1987 #elif N_THUMB
1988 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1989 static uint16_t ops[6] = {
1990 ASM_THUMB_OP_ITE_GE,
1991 ASM_THUMB_OP_ITE_GT,
1992 ASM_THUMB_OP_ITE_EQ,
1993 ASM_THUMB_OP_ITE_GT,
1994 ASM_THUMB_OP_ITE_GE,
1995 ASM_THUMB_OP_ITE_EQ,
1996 };
1997 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1998 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1999 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
2000 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
2001 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02002002 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
2003 static uint ccs[6] = {
2004 ASM_ARM_CC_LT,
2005 ASM_ARM_CC_GT,
2006 ASM_ARM_CC_EQ,
2007 ASM_ARM_CC_LE,
2008 ASM_ARM_CC_GE,
2009 ASM_ARM_CC_NE,
2010 };
2011 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002012 #else
2013 #error not implemented
2014 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002015 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2016 } else {
2017 // TODO other ops not yet implemented
2018 assert(0);
2019 }
Damien13ed3a62013-10-08 09:05:10 +01002020 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002021 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002022 bool invert = false;
2023 if (op == MP_BINARY_OP_NOT_IN) {
2024 invert = true;
2025 op = MP_BINARY_OP_IN;
2026 } else if (op == MP_BINARY_OP_IS_NOT) {
2027 invert = true;
2028 op = MP_BINARY_OP_IS;
2029 }
Damien George7fe21912014-08-16 22:31:57 +01002030 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002031 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002032 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002033 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2034 }
Damien13ed3a62013-10-08 09:05:10 +01002035 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2036 } else {
2037 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002038 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002039 }
2040}
2041
Damien George7ff996c2014-09-08 23:05:16 +01002042STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002043 // for viper: call runtime, with types of args
2044 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002045 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002046 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002047 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002048 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002049}
2050
Damien George7ff996c2014-09-08 23:05:16 +01002051STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002052 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002053 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002054 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002055 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2056}
2057
Damien George7ff996c2014-09-08 23:05:16 +01002058STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002059 // only used in list comprehension
2060 vtype_kind_t vtype_list, vtype_item;
2061 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2062 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2063 assert(vtype_list == VTYPE_PYOBJ);
2064 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002065 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002066 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002067}
2068
Damien George7ff996c2014-09-08 23:05:16 +01002069STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002070 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002071 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002072 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2073}
2074
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002075STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002076 vtype_kind_t vtype_key, vtype_value, vtype_map;
2077 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
2078 assert(vtype_key == VTYPE_PYOBJ);
2079 assert(vtype_value == VTYPE_PYOBJ);
2080 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002081 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002082 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2083}
2084
Damien George7ff996c2014-09-08 23:05:16 +01002085STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002086 // only used in list comprehension
2087 vtype_kind_t vtype_map, vtype_key, vtype_value;
2088 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2089 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2090 assert(vtype_map == VTYPE_PYOBJ);
2091 assert(vtype_key == VTYPE_PYOBJ);
2092 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002093 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002094 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002095}
2096
Damien Georgee37dcaa2014-12-27 17:07:16 +00002097#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002098STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002099 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002100 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002101 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002102 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2103}
2104
Damien George7ff996c2014-09-08 23:05:16 +01002105STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002106 // only used in set comprehension
2107 vtype_kind_t vtype_set, vtype_item;
2108 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2109 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2110 assert(vtype_set == VTYPE_PYOBJ);
2111 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002112 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002113 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002114}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002115#endif
Damiend2755ec2013-10-16 23:58:48 +01002116
Damien George83204f32014-12-27 17:20:41 +00002117#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002118STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002119 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002120 if (n_args == 2) {
2121 vtype_kind_t vtype_start, vtype_stop;
2122 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2123 assert(vtype_start == VTYPE_PYOBJ);
2124 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002125 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 +01002126 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2127 } else {
2128 assert(n_args == 3);
2129 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2130 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
2131 assert(vtype_start == VTYPE_PYOBJ);
2132 assert(vtype_stop == VTYPE_PYOBJ);
2133 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002134 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002135 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2136 }
Damien13ed3a62013-10-08 09:05:10 +01002137}
Damien George83204f32014-12-27 17:20:41 +00002138#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002139
Damien George7ff996c2014-09-08 23:05:16 +01002140STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002141 DEBUG_printf("unpack_sequence %d\n", n_args);
2142 vtype_kind_t vtype_base;
2143 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2144 assert(vtype_base == VTYPE_PYOBJ);
2145 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002146 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002147}
Damien Georgecdd96df2014-04-06 12:58:40 +01002148
Damien George7ff996c2014-09-08 23:05:16 +01002149STATIC 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 +01002150 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2151 vtype_kind_t vtype_base;
2152 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2153 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002154 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 +01002155 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 +01002156}
2157
Damien George7ff996c2014-09-08 23:05:16 +01002158STATIC 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 +01002159 // 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 +00002160 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002161 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002162 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 +01002163 } else {
2164 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2165 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2166 assert(vtype_def_tuple == VTYPE_PYOBJ);
2167 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002168 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 +01002169 }
Damien13ed3a62013-10-08 09:05:10 +01002170 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2171}
2172
Damien George7ff996c2014-09-08 23:05:16 +01002173STATIC 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 +00002174 emit_native_pre(emit);
2175 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
2176 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over);
2177 ASM_MOV_IMM_TO_REG(emit->as, n_closed_over, REG_ARG_2);
2178 } else {
2179 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_closed_over + 2);
2180 ASM_MOV_IMM_TO_REG(emit->as, 0x100 | n_closed_over, REG_ARG_2);
2181 }
2182 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)scope->raw_code, REG_ARG_1);
2183 ASM_CALL_IND(emit->as, mp_fun_table[MP_F_MAKE_CLOSURE_FROM_RAW_CODE], MP_F_MAKE_CLOSURE_FROM_RAW_CODE);
2184 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002185}
2186
Damien George7ff996c2014-09-08 23:05:16 +01002187STATIC 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 +00002188 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2189
Damien Georgee9dac3b2014-09-29 22:10:41 +01002190 // TODO: in viper mode, call special runtime routine with type info for args,
2191 // and wanted type info for return, to remove need for boxing/unboxing
2192
Damien Georgee9dac3b2014-09-29 22:10:41 +01002193 emit_native_pre(emit);
2194 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2195 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2196 // casting operator
2197 assert(n_positional == 1 && n_keyword == 0);
Damien George78772ad2015-04-06 22:48:21 +01002198 assert(!star_flags);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002199 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002200 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002201 switch (peek_vtype(emit, 0)) {
2202 case VTYPE_PYOBJ: {
2203 vtype_kind_t vtype;
2204 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2205 emit_pre_pop_discard(emit);
2206 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2207 emit_post_push_reg(emit, vtype_cast, REG_RET);
2208 break;
2209 }
2210 case VTYPE_BOOL:
2211 case VTYPE_INT:
2212 case VTYPE_UINT:
2213 case VTYPE_PTR:
2214 case VTYPE_PTR8:
2215 case VTYPE_PTR16:
2216 case VTYPE_PTR_NONE:
2217 emit_fold_stack_top(emit, REG_ARG_1);
2218 emit_post_top_set_vtype(emit, vtype_cast);
2219 break;
2220 default:
2221 assert(!"TODO: convert obj to int");
2222 }
2223 } else {
Damien13ed3a62013-10-08 09:05:10 +01002224 assert(vtype_fun == VTYPE_PYOBJ);
Damien George78772ad2015-04-06 22:48:21 +01002225 if (star_flags) {
2226 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2227 // load dummy entry for non-existent pos_seq
2228 emit_native_load_null(emit);
2229 emit_native_rot_two(emit);
2230 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2231 // load dummy entry for non-existent kw_dict
2232 emit_native_load_null(emit);
2233 }
2234 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
2235 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);
2236 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2237 } else {
2238 if (n_positional != 0 || n_keyword != 0) {
2239 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2240 }
2241 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2242 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2243 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2244 }
Damien Georgecd82e022014-02-02 13:11:48 +00002245 }
Damien13ed3a62013-10-08 09:05:10 +01002246}
2247
Damien George7ff996c2014-09-08 23:05:16 +01002248STATIC 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 +01002249 if (star_flags) {
2250 if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2251 // load dummy entry for non-existent pos_seq
2252 emit_native_load_null(emit);
2253 emit_native_rot_two(emit);
2254 } else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2255 // load dummy entry for non-existent kw_dict
2256 emit_native_load_null(emit);
2257 }
2258 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
2259 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);
2260 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2261 } else {
2262 emit_native_pre(emit);
2263 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
2264 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
2265 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2266 }
Damien13ed3a62013-10-08 09:05:10 +01002267}
2268
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002269STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002270 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002271 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002272 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2273 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002274 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002275 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002276 } else {
2277 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002278 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002279 } else {
2280 vtype_kind_t vtype;
2281 emit_pre_pop_reg(emit, &vtype, REG_RET);
2282 if (vtype != emit->return_vtype) {
2283 printf("ViperTypeError: incompatible return type\n");
2284 }
Damien George2ac4af62014-08-15 16:45:41 +01002285 }
Damien13ed3a62013-10-08 09:05:10 +01002286 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002287 vtype_kind_t vtype;
2288 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002289 assert(vtype == VTYPE_PYOBJ);
2290 }
2291 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002292 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2293 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002294}
2295
Damien George7ff996c2014-09-08 23:05:16 +01002296STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002297 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002298 vtype_kind_t vtype_exc;
2299 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2300 if (vtype_exc != VTYPE_PYOBJ) {
2301 printf("ViperTypeError: must raise an object\n");
2302 }
2303 // 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 +01002304 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002305}
Damien Georgeb601d952014-06-30 05:17:25 +01002306
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002307STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002308 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002309 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002310 assert(0);
2311}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002312STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002313 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002314 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002315 assert(0);
2316}
2317
Damien Georgeb601d952014-06-30 05:17:25 +01002318STATIC void emit_native_start_except_handler(emit_t *emit) {
2319 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2320 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2321 // the first 2 elements, so we can get the thrown value.
2322 adjust_stack(emit, 2);
2323 vtype_kind_t vtype_nlr;
2324 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002325 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002326 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
2327}
2328
2329STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002330 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002331}
2332
Damien13ed3a62013-10-08 09:05:10 +01002333const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002334 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002335 emit_native_start_pass,
2336 emit_native_end_pass,
2337 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002338 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002339 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002340
Damien George542bd6b2015-03-26 14:42:40 +00002341 {
2342 emit_native_load_fast,
2343 emit_native_load_deref,
2344 emit_native_load_name,
2345 emit_native_load_global,
2346 },
2347 {
2348 emit_native_store_fast,
2349 emit_native_store_deref,
2350 emit_native_store_name,
2351 emit_native_store_global,
2352 },
2353 {
2354 emit_native_delete_fast,
2355 emit_native_delete_deref,
2356 emit_native_delete_name,
2357 emit_native_delete_global,
2358 },
Damien13ed3a62013-10-08 09:05:10 +01002359
2360 emit_native_label_assign,
2361 emit_native_import_name,
2362 emit_native_import_from,
2363 emit_native_import_star,
2364 emit_native_load_const_tok,
2365 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002366 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002367 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002368 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002369 emit_native_load_attr,
2370 emit_native_load_method,
2371 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002372 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002373 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002374 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002375 emit_native_delete_attr,
2376 emit_native_delete_subscr,
2377 emit_native_dup_top,
2378 emit_native_dup_top_two,
2379 emit_native_pop_top,
2380 emit_native_rot_two,
2381 emit_native_rot_three,
2382 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002383 emit_native_pop_jump_if,
2384 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002385 emit_native_break_loop,
2386 emit_native_continue_loop,
2387 emit_native_setup_with,
2388 emit_native_with_cleanup,
2389 emit_native_setup_except,
2390 emit_native_setup_finally,
2391 emit_native_end_finally,
2392 emit_native_get_iter,
2393 emit_native_for_iter,
2394 emit_native_for_iter_end,
2395 emit_native_pop_block,
2396 emit_native_pop_except,
2397 emit_native_unary_op,
2398 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002399 emit_native_build_tuple,
2400 emit_native_build_list,
2401 emit_native_list_append,
2402 emit_native_build_map,
2403 emit_native_store_map,
2404 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002405 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002406 emit_native_build_set,
2407 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002408 #endif
Damien George83204f32014-12-27 17:20:41 +00002409 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002410 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002411 #endif
Damien13ed3a62013-10-08 09:05:10 +01002412 emit_native_unpack_sequence,
2413 emit_native_unpack_ex,
2414 emit_native_make_function,
2415 emit_native_make_closure,
2416 emit_native_call_function,
2417 emit_native_call_method,
2418 emit_native_return_value,
2419 emit_native_raise_varargs,
2420 emit_native_yield_value,
2421 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002422
2423 emit_native_start_except_handler,
2424 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002425};
2426
Damien Georgec90f59e2014-09-06 23:06:36 +01002427#endif