blob: d16b919dc19201fe8ef3816926980b8f40a94c2c [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))
143#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
144#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
145
146#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
147#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
148#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 +0100149
Damien Georgec90f59e2014-09-06 23:06:36 +0100150#elif N_X86
151
152// x86 specific stuff
153
Damien George51dfcb42015-01-01 20:27:54 +0000154#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100155
156STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
157 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
158 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
Damien Georgec90f59e2014-09-06 23:06:36 +0100159 [MP_F_LOAD_CONST_STR] = 1,
160 [MP_F_LOAD_CONST_BYTES] = 1,
161 [MP_F_LOAD_NAME] = 1,
162 [MP_F_LOAD_GLOBAL] = 1,
163 [MP_F_LOAD_BUILD_CLASS] = 0,
164 [MP_F_LOAD_ATTR] = 2,
165 [MP_F_LOAD_METHOD] = 3,
166 [MP_F_STORE_NAME] = 2,
167 [MP_F_STORE_GLOBAL] = 2,
168 [MP_F_STORE_ATTR] = 3,
169 [MP_F_OBJ_SUBSCR] = 3,
170 [MP_F_OBJ_IS_TRUE] = 1,
171 [MP_F_UNARY_OP] = 2,
172 [MP_F_BINARY_OP] = 3,
173 [MP_F_BUILD_TUPLE] = 2,
174 [MP_F_BUILD_LIST] = 2,
175 [MP_F_LIST_APPEND] = 2,
176 [MP_F_BUILD_MAP] = 1,
177 [MP_F_STORE_MAP] = 3,
178#if MICROPY_PY_BUILTINS_SET
179 [MP_F_BUILD_SET] = 2,
180 [MP_F_STORE_SET] = 2,
181#endif
182 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
183 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
184 [MP_F_CALL_METHOD_N_KW] = 3,
185 [MP_F_GETITER] = 1,
186 [MP_F_ITERNEXT] = 1,
187 [MP_F_NLR_PUSH] = 1,
188 [MP_F_NLR_POP] = 0,
189 [MP_F_NATIVE_RAISE] = 1,
190 [MP_F_IMPORT_NAME] = 3,
191 [MP_F_IMPORT_FROM] = 2,
192 [MP_F_IMPORT_ALL] = 1,
193#if MICROPY_PY_BUILTINS_SLICE
194 [MP_F_NEW_SLICE] = 3,
195#endif
196 [MP_F_UNPACK_SEQUENCE] = 3,
197 [MP_F_UNPACK_EX] = 3,
198 [MP_F_DELETE_NAME] = 1,
199 [MP_F_DELETE_GLOBAL] = 1,
200};
201
202#define EXPORT_FUN(name) emit_native_x86_##name
203
Damien George0b610de2014-09-29 16:25:04 +0100204#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000205#define REG_ARG_1 ASM_X86_REG_ARG_1
206#define REG_ARG_2 ASM_X86_REG_ARG_2
207#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100208
Damien George25d90412014-09-06 23:24:32 +0000209// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100210#define REG_TEMP0 ASM_X86_REG_EAX
211#define REG_TEMP1 ASM_X86_REG_ECX
212#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100213
Damien George25d90412014-09-06 23:24:32 +0000214// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100215#define REG_LOCAL_1 ASM_X86_REG_EBX
216#define REG_LOCAL_2 ASM_X86_REG_ESI
217#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000218#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100219
220#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
221#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
222
223#define ASM_T asm_x86_t
224#define ASM_NEW asm_x86_new
225#define ASM_FREE asm_x86_free
226#define ASM_GET_CODE asm_x86_get_code
227#define ASM_GET_CODE_SIZE asm_x86_get_code_size
228#define ASM_START_PASS asm_x86_start_pass
229#define ASM_END_PASS asm_x86_end_pass
230#define ASM_ENTRY asm_x86_entry
231#define ASM_EXIT asm_x86_exit
232
233#define ASM_LABEL_ASSIGN asm_x86_label_assign
234#define ASM_JUMP asm_x86_jmp_label
235#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
236 do { \
237 asm_x86_test_r8_with_r8(as, reg, reg); \
238 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
239 } while (0)
240#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
241 do { \
242 asm_x86_test_r8_with_r8(as, reg, reg); \
243 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
244 } while (0)
245#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
246 do { \
247 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
248 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
249 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100250#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 +0100251
252#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
253#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
254#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
255#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
256 do { \
257 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
258 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
259 } while (false)
260#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100261#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 +0100262#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100263
Damien George3112cde2014-09-29 18:45:42 +0100264#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
265#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100266#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
267#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
268#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 +0100269#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
270#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
271
Damien George91cfd412014-10-12 16:59:29 +0100272#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem32_to_r32((as), (reg_base), 0, (reg_dest))
Damien George3c34d412014-10-12 16:10:25 +0000273#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
274#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 +0100275
276#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
277#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
278#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 +0100279
Damien3ef4abb2013-10-12 16:53:13 +0100280#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100281
282// thumb specific stuff
283
Damien George51dfcb42015-01-01 20:27:54 +0000284#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100285
Damien13ed3a62013-10-08 09:05:10 +0100286#define EXPORT_FUN(name) emit_native_thumb_##name
287
Damien George0b610de2014-09-29 16:25:04 +0100288#define REG_RET ASM_THUMB_REG_R0
289#define REG_ARG_1 ASM_THUMB_REG_R0
290#define REG_ARG_2 ASM_THUMB_REG_R1
291#define REG_ARG_3 ASM_THUMB_REG_R2
292#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100293
Damien George0b610de2014-09-29 16:25:04 +0100294#define REG_TEMP0 ASM_THUMB_REG_R0
295#define REG_TEMP1 ASM_THUMB_REG_R1
296#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100297
Damien George0b610de2014-09-29 16:25:04 +0100298#define REG_LOCAL_1 ASM_THUMB_REG_R4
299#define REG_LOCAL_2 ASM_THUMB_REG_R5
300#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100301#define REG_LOCAL_NUM (3)
302
303#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
304#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
305
306#define ASM_T asm_thumb_t
307#define ASM_NEW asm_thumb_new
308#define ASM_FREE asm_thumb_free
309#define ASM_GET_CODE asm_thumb_get_code
310#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
311#define ASM_START_PASS asm_thumb_start_pass
312#define ASM_END_PASS asm_thumb_end_pass
313#define ASM_ENTRY asm_thumb_entry
314#define ASM_EXIT asm_thumb_exit
315
316#define ASM_LABEL_ASSIGN asm_thumb_label_assign
317#define ASM_JUMP asm_thumb_b_label
318#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
319 do { \
320 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100321 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100322 } while (0)
323#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
324 do { \
325 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100326 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100327 } while (0)
328#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
329 do { \
330 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100331 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100332 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100333#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 +0100334
335#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
336#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
337#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
338#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
339 do { \
340 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
341 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
342 } while (false)
343#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 +0100344#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 +0100345#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 +0100346
Damien George3112cde2014-09-29 18:45:42 +0100347#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
348#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 +0100349#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
350#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
351#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 +0100352#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
353#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
354
Damien George91cfd412014-10-12 16:59:29 +0100355#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
356#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
357#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
358
Damien Georgee9dac3b2014-09-29 22:10:41 +0100359#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
360#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
361#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
362
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200363#elif N_ARM
364
365// ARM specific stuff
366
Damien George51dfcb42015-01-01 20:27:54 +0000367#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200368
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200369#define EXPORT_FUN(name) emit_native_arm_##name
370
Damien George0b610de2014-09-29 16:25:04 +0100371#define REG_RET ASM_ARM_REG_R0
372#define REG_ARG_1 ASM_ARM_REG_R0
373#define REG_ARG_2 ASM_ARM_REG_R1
374#define REG_ARG_3 ASM_ARM_REG_R2
375#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100376
Damien George0b610de2014-09-29 16:25:04 +0100377#define REG_TEMP0 ASM_ARM_REG_R0
378#define REG_TEMP1 ASM_ARM_REG_R1
379#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100380
Damien George0b610de2014-09-29 16:25:04 +0100381#define REG_LOCAL_1 ASM_ARM_REG_R4
382#define REG_LOCAL_2 ASM_ARM_REG_R5
383#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100384#define REG_LOCAL_NUM (3)
385
386#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
387#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
388
389#define ASM_T asm_arm_t
390#define ASM_NEW asm_arm_new
391#define ASM_FREE asm_arm_free
392#define ASM_GET_CODE asm_arm_get_code
393#define ASM_GET_CODE_SIZE asm_arm_get_code_size
394#define ASM_START_PASS asm_arm_start_pass
395#define ASM_END_PASS asm_arm_end_pass
396#define ASM_ENTRY asm_arm_entry
397#define ASM_EXIT asm_arm_exit
398
399#define ASM_LABEL_ASSIGN asm_arm_label_assign
400#define ASM_JUMP asm_arm_b_label
401#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
402 do { \
403 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100404 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100405 } while (0)
406#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
407 do { \
408 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100409 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100410 } while (0)
411#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
412 do { \
413 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100414 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100415 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100416#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 +0100417
418#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
419#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
420#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
421#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
422 do { \
423 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
424 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
425 } while (false)
426#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 +0100427#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 +0100428#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
429
Fabian Vogte5268962014-10-04 00:53:46 +0200430#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
431#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 +0100432#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
433#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
434#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 +0100435#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
436#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
437
Damien George91cfd412014-10-12 16:59:29 +0100438#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base))
439#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
440#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
441
Fabian Vogte5268962014-10-04 00:53:46 +0200442#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
443#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
444#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 +0100445
Damien Georgec90f59e2014-09-06 23:06:36 +0100446#else
447
448#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200449
Damien13ed3a62013-10-08 09:05:10 +0100450#endif
451
452typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100453 STACK_VALUE,
454 STACK_REG,
455 STACK_IMM,
456} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100457
Damien Georgee9dac3b2014-09-29 22:10:41 +0100458// these enums must be distinct and the bottom 2 bits
459// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100460typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100461 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
462 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
463 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
464 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
465
466 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
467 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
468 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
469 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
470
471 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
472 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100473} vtype_kind_t;
474
Damienff8ed772013-10-08 22:18:32 +0100475typedef struct _stack_info_t {
476 vtype_kind_t vtype;
477 stack_info_kind_t kind;
478 union {
479 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100480 mp_int_t u_imm;
Damien George32444b72015-01-24 23:14:12 +0000481 } data;
Damienff8ed772013-10-08 22:18:32 +0100482} stack_info_t;
483
Damien13ed3a62013-10-08 09:05:10 +0100484struct _emit_t {
485 int pass;
486
487 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100488
Damien George2ac4af62014-08-15 16:45:41 +0100489 vtype_kind_t return_vtype;
490
Damien George7ff996c2014-09-08 23:05:16 +0100491 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100492 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100493
Damien George7ff996c2014-09-08 23:05:16 +0100494 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100495 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100496 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100497
Damien13ed3a62013-10-08 09:05:10 +0100498 int stack_start;
499 int stack_size;
500
501 bool last_emit_was_return_value;
502
Damien13ed3a62013-10-08 09:05:10 +0100503 scope_t *scope;
504
Damien Georgec90f59e2014-09-06 23:06:36 +0100505 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100506};
507
Damien George7ff996c2014-09-08 23:05:16 +0100508emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100509 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100510 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100511 return emit;
512}
513
Damien George41d02b62014-01-24 22:42:28 +0000514void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100515 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100516 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
517 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200518 m_del_obj(emit_t, emit);
519}
520
Damien George2ac4af62014-08-15 16:45:41 +0100521STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
522 switch (op) {
523 case MP_EMIT_NATIVE_TYPE_ENABLE:
524 emit->do_viper_types = arg1;
525 break;
526
527 default: {
528 vtype_kind_t type;
529 switch (arg2) {
530 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
531 case MP_QSTR_bool: type = VTYPE_BOOL; break;
532 case MP_QSTR_int: type = VTYPE_INT; break;
533 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100534 case MP_QSTR_ptr: type = VTYPE_PTR; break;
535 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
536 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100537 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
538 }
539 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
540 emit->return_vtype = type;
541 } else {
542 assert(arg1 < emit->local_vtype_alloc);
543 emit->local_vtype[arg1] = type;
544 }
545 break;
546 }
547 }
Damien13ed3a62013-10-08 09:05:10 +0100548}
549
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200550STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000551 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
552
Damien13ed3a62013-10-08 09:05:10 +0100553 emit->pass = pass;
554 emit->stack_start = 0;
555 emit->stack_size = 0;
556 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100557 emit->scope = scope;
558
Damien George36db6bc2014-05-07 17:24:22 +0100559 // allocate memory for keeping track of the types of locals
560 if (emit->local_vtype_alloc < scope->num_locals) {
561 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
562 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100563 }
Damien George36db6bc2014-05-07 17:24:22 +0100564
565 // allocate memory for keeping track of the objects on the stack
566 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100567 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100568 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100569 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100570 }
571
Damien George2ac4af62014-08-15 16:45:41 +0100572 // set default type for return and arguments
573 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100574 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100575 emit->local_vtype[i] = VTYPE_PYOBJ;
576 }
Damien Georgea5190a72014-08-15 22:39:08 +0100577
578 // local variables begin unbound, and have unknown type
579 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
580 emit->local_vtype[i] = VTYPE_UNBOUND;
581 }
582
583 // values on stack begin unbound
584 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100585 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100586 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100587 }
588
Damien Georgec90f59e2014-09-06 23:06:36 +0100589 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100590
591 // entry to function
592 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100593 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100594 num_locals = scope->num_locals - REG_LOCAL_NUM;
595 if (num_locals < 0) {
596 num_locals = 0;
597 }
598 emit->stack_start = num_locals;
599 num_locals += scope->stack_size;
600 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100601 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100602
603 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100604#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100605 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100606 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100607 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100608 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100609 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100610 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100611 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100612 } else if (i == 3) {
613 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100614 } else {
615 // TODO not implemented
616 assert(0);
617 }
618 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100619#elif N_X86
620 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100621 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000622 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100623 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000624 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000625 } else if (i == 2) {
626 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100627 } else {
Damien George03281b32014-09-06 22:38:50 +0000628 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
629 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100630 }
631 }
Damien3ef4abb2013-10-12 16:53:13 +0100632#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100633 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100634 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100635 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100636 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100637 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100638 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100639 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100640 } else if (i == 3) {
641 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
642 } else {
643 // TODO not implemented
644 assert(0);
645 }
646 }
647
Damien Georgee9dac3b2014-09-29 22:10:41 +0100648 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100649 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200650#elif N_ARM
651 for (int i = 0; i < scope->num_pos_args; i++) {
652 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100653 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200654 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100655 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200656 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100657 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200658 } else if (i == 3) {
659 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
660 } else {
661 // TODO not implemented
662 assert(0);
663 }
664 }
665
Damien Georgee9dac3b2014-09-29 22:10:41 +0100666 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100667 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100668#else
669 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100670#endif
671}
672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200673STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100674 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100675 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100676 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100677 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100678
679 // check stack is back to zero size
680 if (emit->stack_size != 0) {
681 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
682 }
683
Damien George36db6bc2014-05-07 17:24:22 +0100684 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100685 void *f = ASM_GET_CODE(emit->as);
686 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100687
688 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100689 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100690 mp_uint_t type_sig = emit->return_vtype & 3;
691 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
692 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
693 }
694
695 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 +0100696 }
697}
698
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200699STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100700 return emit->last_emit_was_return_value;
701}
702
Damien Georged6230f62014-09-23 14:10:03 +0000703STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000704 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100705 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100706 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100707 emit->scope->stack_size = emit->stack_size;
708 }
Damien George21ca2d72014-10-19 19:00:51 +0100709#ifdef DEBUG_PRINT
710 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
711 for (int i = 0; i < emit->stack_size; i++) {
712 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000713 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100714 }
715 DEBUG_printf("\n");
716#endif
Damien13ed3a62013-10-08 09:05:10 +0100717}
718
Damien Georged6230f62014-09-23 14:10:03 +0000719STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100720 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000721 // If we are adjusting the stack in a positive direction (pushing) then we
722 // need to fill in values for the stack kind and vtype of the newly-pushed
723 // entries. These should be set to "value" (ie not reg or imm) because we
724 // should only need to adjust the stack due to a jump to this part in the
725 // code (and hence we have settled the stack before the jump).
726 for (mp_int_t i = 0; i < delta; i++) {
727 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
728 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100729 // TODO we don't know the vtype to use here. At the moment this is a
730 // hack to get the case of multi comparison working.
731 if (delta == 1) {
732 si->vtype = emit->saved_stack_vtype;
733 } else {
734 si->vtype = VTYPE_PYOBJ;
735 }
Damien Georged6230f62014-09-23 14:10:03 +0000736 }
737 adjust_stack(emit, delta);
738}
739
740STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000741 (void)emit;
742 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000743}
744
Damienff8ed772013-10-08 22:18:32 +0100745/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200746STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100747 adjust_stack(emit, stack_size_delta);
748 emit->last_emit_was_return_value = false;
749}
Damienff8ed772013-10-08 22:18:32 +0100750*/
Damien13ed3a62013-10-08 09:05:10 +0100751
Damienff8ed772013-10-08 22:18:32 +0100752// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000753STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100754 emit->last_emit_was_return_value = false;
755 // settle the stack
756 /*
757 if (regs_needed != 0) {
758 for (int i = 0; i < emit->stack_size; i++) {
759 switch (emit->stack_info[i].kind) {
760 case STACK_VALUE:
761 break;
762
763 case STACK_REG:
764 // TODO only push reg if in regs_needed
765 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000766 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100767 break;
768
769 case STACK_IMM:
770 // don't think we ever need to push imms for settling
771 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
772 break;
773 }
774 }
775 }
776 */
Damien13ed3a62013-10-08 09:05:10 +0100777}
778
Damien George3112cde2014-09-29 18:45:42 +0100779// depth==0 is top, depth==1 is before top, etc
780STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
781 return &emit->stack_info[emit->stack_size - 1 - depth];
782}
783
784// depth==0 is top, depth==1 is before top, etc
785STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
786 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100787}
Damien13ed3a62013-10-08 09:05:10 +0100788
Damiend2755ec2013-10-16 23:58:48 +0100789// pos=1 is TOS, pos=2 is next, etc
790// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200791STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100792 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100793 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100794 if (i != skip_stack_pos) {
795 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000796 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100797 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000798 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100799 }
Damienff8ed772013-10-08 22:18:32 +0100800 }
801 }
802}
Damien13ed3a62013-10-08 09:05:10 +0100803
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200804STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100805 for (int i = 0; i < emit->stack_size; i++) {
806 stack_info_t *si = &emit->stack_info[i];
807 if (si->kind == STACK_REG) {
808 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000809 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100810 }
Damien13ed3a62013-10-08 09:05:10 +0100811 }
812}
813
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200814STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000815 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100816 for (int i = 0; i < emit->stack_size; i++) {
817 stack_info_t *si = &emit->stack_info[i];
818 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000819 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100820 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000821 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100822 }
823 }
824 for (int i = 0; i < emit->stack_size; i++) {
825 stack_info_t *si = &emit->stack_info[i];
826 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000827 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100828 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000829 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100830 }
831 }
832}
833
834// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200835STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100836 need_reg_single(emit, reg_dest, pos);
837 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100838 *vtype = si->vtype;
839 switch (si->kind) {
840 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100841 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100842 break;
843
Damienff8ed772013-10-08 22:18:32 +0100844 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000845 if (si->data.u_reg != reg_dest) {
846 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100847 }
848 break;
849
Damienff8ed772013-10-08 22:18:32 +0100850 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +0000851 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100852 break;
853 }
Damien13ed3a62013-10-08 09:05:10 +0100854}
855
Damien Georgee9dac3b2014-09-29 22:10:41 +0100856// does an efficient X=pop(); discard(); push(X)
857// needs a (non-temp) register in case the poped element was stored in the stack
858STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
859 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
860 si[0] = si[1];
861 if (si->kind == STACK_VALUE) {
862 // if folded element was on the stack we need to put it in a register
863 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
864 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000865 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100866 }
867 adjust_stack(emit, -1);
868}
869
870// If stacked value is in a register and the register is not r1 or r2, then
871// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
872STATIC 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 +0100873 emit->last_emit_was_return_value = false;
874 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +0000875 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100876 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +0000877 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +0100878 need_reg_single(emit, *reg_dest, 1);
879 } else {
880 emit_access_stack(emit, 1, vtype, *reg_dest);
881 }
882 adjust_stack(emit, -1);
883}
884
Damien Georgee6ce10a2014-09-06 18:38:20 +0100885STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100886 emit->last_emit_was_return_value = false;
887 adjust_stack(emit, -1);
888}
889
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200890STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100891 emit->last_emit_was_return_value = false;
892 emit_access_stack(emit, 1, vtype, reg_dest);
893 adjust_stack(emit, -1);
894}
895
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200896STATIC 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 +0100897 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100898 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100899}
900
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200901STATIC 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 +0100902 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100903 emit_pre_pop_reg(emit, vtypeb, regb);
904 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100905}
906
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200907STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000908 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +0100909}
910
Damien Georgee9dac3b2014-09-29 22:10:41 +0100911STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
912 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
913 si->vtype = new_vtype;
914}
915
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200916STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100917 stack_info_t *si = &emit->stack_info[emit->stack_size];
918 si->vtype = vtype;
919 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000920 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100921 adjust_stack(emit, 1);
922}
923
Damien George40f3c022014-07-03 13:25:24 +0100924STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100925 stack_info_t *si = &emit->stack_info[emit->stack_size];
926 si->vtype = vtype;
927 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +0000928 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +0100929 adjust_stack(emit, 1);
930}
931
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200932STATIC 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 +0100933 emit_post_push_reg(emit, vtypea, rega);
934 emit_post_push_reg(emit, vtypeb, regb);
935}
936
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200937STATIC 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 +0100938 emit_post_push_reg(emit, vtypea, rega);
939 emit_post_push_reg(emit, vtypeb, regb);
940 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100941}
942
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200943STATIC 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 +0100944 emit_post_push_reg(emit, vtypea, rega);
945 emit_post_push_reg(emit, vtypeb, regb);
946 emit_post_push_reg(emit, vtypec, regc);
947 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100948}
949
Damien George7fe21912014-08-16 22:31:57 +0100950STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100951 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100952 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100953}
954
Damien George7fe21912014-08-16 22:31:57 +0100955STATIC 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 +0100956 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100957 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
958 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100959}
960
Damien George40f3c022014-07-03 13:25:24 +0100961// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100962STATIC 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 +0100963 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100964 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
965 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100966}
967
Damien George7fe21912014-08-16 22:31:57 +0100968STATIC 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 +0000969 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100970 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
971 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
972 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000973}
974
Damien George40f3c022014-07-03 13:25:24 +0100975// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100976STATIC 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 +0100977 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100978 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
979 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
980 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
981 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100982}
983
Damien George86de21b2014-08-16 22:06:11 +0100984// vtype of all n_pop objects is VTYPE_PYOBJ
985// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
986// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
987// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
988STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
989 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100990
Damien George86de21b2014-08-16 22:06:11 +0100991 // First, store any immediate values to their respective place on the stack.
992 for (mp_uint_t i = 0; i < n_pop; i++) {
993 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
994 // must push any imm's to stack
995 // must convert them to VTYPE_PYOBJ for viper code
996 if (si->kind == STACK_IMM) {
997 si->kind = STACK_VALUE;
998 switch (si->vtype) {
999 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001000 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 +01001001 break;
1002 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001003 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001004 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 +01001005 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001006 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 +01001007 }
1008 si->vtype = VTYPE_PYOBJ;
1009 break;
1010 case VTYPE_INT:
1011 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001012 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 +01001013 si->vtype = VTYPE_PYOBJ;
1014 break;
1015 default:
1016 // not handled
1017 assert(0);
1018 }
1019 }
1020
1021 // verify that this value is on the stack
1022 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001023 }
Damien George86de21b2014-08-16 22:06:11 +01001024
1025 // Second, convert any non-VTYPE_PYOBJ to that type.
1026 for (mp_uint_t i = 0; i < n_pop; i++) {
1027 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1028 if (si->vtype != VTYPE_PYOBJ) {
1029 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001030 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001031 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 +01001032 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001033 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001034 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001035 }
1036 }
1037
1038 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1039 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001040 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001041}
1042
1043// vtype of all n_push objects is VTYPE_PYOBJ
1044STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1045 need_reg_all(emit);
1046 for (mp_uint_t i = 0; i < n_push; i++) {
1047 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1048 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1049 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001050 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001051 adjust_stack(emit, n_push);
1052}
1053
Damien George7ff996c2014-09-08 23:05:16 +01001054STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001055 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001056 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001057 // need to commit stack because we can jump here from elsewhere
1058 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001059 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001060 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001061}
1062
Damien Georgecdd96df2014-04-06 12:58:40 +01001063STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1064 DEBUG_printf("import_name %s\n", qstr_str(qst));
1065 vtype_kind_t vtype_fromlist;
1066 vtype_kind_t vtype_level;
1067 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1068 assert(vtype_fromlist == VTYPE_PYOBJ);
1069 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001070 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001071 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001072}
1073
Damien Georgecdd96df2014-04-06 12:58:40 +01001074STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1075 DEBUG_printf("import_from %s\n", qstr_str(qst));
1076 emit_native_pre(emit);
1077 vtype_kind_t vtype_module;
1078 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1079 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001080 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001081 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001082}
1083
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001084STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001085 DEBUG_printf("import_star\n");
1086 vtype_kind_t vtype_module;
1087 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1088 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001089 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001090 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001091}
1092
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001093STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001094 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001095 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001096 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001097 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001098 if (emit->do_viper_types) {
1099 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001100 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1101 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1102 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001103 no_other_choice1:
1104 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1105 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001106 }
1107 } else {
1108 vtype = VTYPE_PYOBJ;
1109 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001110 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1111 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1112 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001113 no_other_choice2:
1114 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1115 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001116 }
1117 }
1118 emit_post_push_imm(emit, vtype, val);
1119}
1120
Damien George40f3c022014-07-03 13:25:24 +01001121STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001122 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001123 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001124 if (emit->do_viper_types) {
1125 emit_post_push_imm(emit, VTYPE_INT, arg);
1126 } else {
1127 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1128 }
1129}
1130
Damien George7ff996c2014-09-08 23:05:16 +01001131STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001132 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001133 // TODO: Eventually we want to be able to work with raw pointers in viper to
1134 // do native array access. For now we just load them as any other object.
1135 /*
Damien13ed3a62013-10-08 09:05:10 +01001136 if (emit->do_viper_types) {
1137 // not implemented properly
1138 // load a pointer to the asciiz string?
1139 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001140 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001141 } else
1142 */
1143 {
Damien Georgeb601d952014-06-30 05:17:25 +01001144 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001145 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001146 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001147 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001148 }
Damien13ed3a62013-10-08 09:05:10 +01001149 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1150 }
1151}
1152
Damien Georgedab13852015-01-13 15:55:54 +00001153STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1154 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001155 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001156 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1157 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1158}
1159
Damien George3558f622014-04-20 17:50:40 +01001160STATIC void emit_native_load_null(emit_t *emit) {
1161 emit_native_pre(emit);
1162 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1163}
1164
Damien George0abb5602015-01-16 12:24:49 +00001165STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1166 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001167 vtype_kind_t vtype = emit->local_vtype[local_num];
1168 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001169 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001170 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001171 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001172#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001173 if (local_num == 0) {
1174 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001175 } else if (local_num == 1) {
1176 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1177 } else if (local_num == 2) {
1178 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001179 } else {
Damien George0b610de2014-09-29 16:25:04 +01001180 need_reg_single(emit, REG_TEMP0, 0);
1181 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1182 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001183 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001184#elif N_X86
1185 if (local_num == 0) {
1186 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001187 } else if (local_num == 1) {
1188 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001189 } else if (local_num == 2) {
1190 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001191 } else {
Damien George0b610de2014-09-29 16:25:04 +01001192 need_reg_single(emit, REG_TEMP0, 0);
1193 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1194 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001195 }
Damien3ef4abb2013-10-12 16:53:13 +01001196#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001197 if (local_num == 0) {
1198 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1199 } else if (local_num == 1) {
1200 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1201 } else if (local_num == 2) {
1202 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1203 } else {
Damien George0b610de2014-09-29 16:25:04 +01001204 need_reg_single(emit, REG_TEMP0, 0);
1205 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1206 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001207 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001208#elif N_ARM
1209 if (local_num == 0) {
1210 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1211 } else if (local_num == 1) {
1212 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1213 } else if (local_num == 2) {
1214 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1215 } else {
Damien George0b610de2014-09-29 16:25:04 +01001216 need_reg_single(emit, REG_TEMP0, 0);
1217 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1218 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001219 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001220#else
1221 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001222#endif
1223}
1224
Damien George7ff996c2014-09-08 23:05:16 +01001225STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001226 // not implemented
1227 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001228 (void)emit;
1229 (void)qst;
1230 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001231 assert(0);
1232}
1233
Damien George7ff996c2014-09-08 23:05:16 +01001234STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001235 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001236 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001237 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001238 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1239}
1240
Damien George7ff996c2014-09-08 23:05:16 +01001241STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001242 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001243 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001244 // check for builtin casting operators
1245 if (emit->do_viper_types && qst == MP_QSTR_int) {
1246 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1247 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1248 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1249 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1250 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1251 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1252 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1253 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1254 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1255 } else {
1256 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1257 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1258 }
Damien13ed3a62013-10-08 09:05:10 +01001259}
1260
Damien George7ff996c2014-09-08 23:05:16 +01001261STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001262 // depends on type of subject:
1263 // - integer, function, pointer to integers: error
1264 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001265 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001266 vtype_kind_t vtype_base;
1267 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1268 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001269 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001270 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1271}
1272
Damien George7ff996c2014-09-08 23:05:16 +01001273STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001274 vtype_kind_t vtype_base;
1275 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1276 assert(vtype_base == VTYPE_PYOBJ);
1277 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001278 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001279}
1280
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001281STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001282 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001283 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001284 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001285}
1286
Damien George729f7b42014-04-17 22:10:53 +01001287STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001288 DEBUG_printf("load_subscr\n");
1289 // need to compile: base[index]
1290
1291 // pop: index, base
1292 // optimise case where index is an immediate
1293 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1294
1295 if (vtype_base == VTYPE_PYOBJ) {
1296 // standard Python call
1297 vtype_kind_t vtype_index;
1298 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1299 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001300 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 +01001301 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1302 } else {
Damien George91cfd412014-10-12 16:59:29 +01001303 // viper load
1304 // TODO The different machine architectures have very different
1305 // capabilities and requirements for loads, so probably best to
1306 // write a completely separate load-optimiser for each one.
1307 stack_info_t *top = peek_stack(emit, 0);
1308 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1309 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001310 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001311 emit_pre_pop_discard(emit); // discard index
1312 int reg_base = REG_ARG_1;
1313 int reg_index = REG_ARG_2;
1314 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1315 switch (vtype_base) {
1316 case VTYPE_PTR8: {
1317 // pointer to 8-bit memory
1318 // TODO optimise to use thumb ldrb r1, [r2, r3]
1319 if (index_value != 0) {
1320 // index is non-zero
1321 #if N_THUMB
1322 if (index_value > 0 && index_value < 32) {
1323 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1324 break;
1325 }
1326 #endif
1327 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1328 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1329 reg_base = reg_index;
1330 }
1331 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1332 break;
1333 }
1334 case VTYPE_PTR16: {
1335 // pointer to 16-bit memory
1336 if (index_value != 0) {
1337 // index is a non-zero immediate
1338 #if N_THUMB
1339 if (index_value > 0 && index_value < 32) {
1340 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1341 break;
1342 }
1343 #endif
1344 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1345 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1346 reg_base = reg_index;
1347 }
1348 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1349 break;
1350 }
1351 default:
1352 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1353 }
1354 } else {
1355 // index is not an immediate
1356 vtype_kind_t vtype_index;
1357 int reg_index = REG_ARG_2;
1358 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1359 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1360 switch (vtype_base) {
1361 case VTYPE_PTR8: {
1362 // pointer to 8-bit memory
1363 // TODO optimise to use thumb ldrb r1, [r2, r3]
1364 assert(vtype_index == VTYPE_INT);
1365 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1366 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1367 break;
1368 }
1369 case VTYPE_PTR16: {
1370 // pointer to 16-bit memory
1371 assert(vtype_index == VTYPE_INT);
1372 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1373 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1374 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1375 break;
1376 }
1377 default:
1378 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1379 }
1380 }
1381 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001382 }
1383}
1384
Damien George7ff996c2014-09-08 23:05:16 +01001385STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001386 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001387#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001388 if (local_num == 0) {
1389 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001390 } else if (local_num == 1) {
1391 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1392 } else if (local_num == 2) {
1393 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001394 } else {
Damien George0b610de2014-09-29 16:25:04 +01001395 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1396 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001397 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001398#elif N_X86
1399 if (local_num == 0) {
1400 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001401 } else if (local_num == 1) {
1402 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001403 } else if (local_num == 2) {
1404 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001405 } else {
Damien George0b610de2014-09-29 16:25:04 +01001406 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1407 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001408 }
Damien3ef4abb2013-10-12 16:53:13 +01001409#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001410 if (local_num == 0) {
1411 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1412 } else if (local_num == 1) {
1413 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1414 } else if (local_num == 2) {
1415 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1416 } else {
Damien George0b610de2014-09-29 16:25:04 +01001417 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1418 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001419 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001420#elif N_ARM
1421 if (local_num == 0) {
1422 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1423 } 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);
1427 } else {
Damien George0b610de2014-09-29 16:25:04 +01001428 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1429 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001430 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001431#else
1432 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001433#endif
1434
1435 emit_post(emit);
1436
1437 // check types
1438 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1439 // first time this local is assigned, so give it a type of the object stored in it
1440 emit->local_vtype[local_num] = vtype;
1441 } else if (emit->local_vtype[local_num] != vtype) {
1442 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001443 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 +01001444 }
1445}
1446
Damien George7ff996c2014-09-08 23:05:16 +01001447STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001448 // not implemented
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001449 (void)emit;
1450 (void)qst;
1451 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001452 assert(0);
1453}
1454
Damien George7ff996c2014-09-08 23:05:16 +01001455STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001456 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001457 vtype_kind_t vtype;
1458 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1459 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001460 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001461 emit_post(emit);
1462}
1463
Damien George7ff996c2014-09-08 23:05:16 +01001464STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001465 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001466 if (vtype == VTYPE_PYOBJ) {
1467 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1468 } else {
1469 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001470 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 +01001471 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001472 }
Damien George7ff996c2014-09-08 23:05:16 +01001473 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001474 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001475}
1476
Damien George7ff996c2014-09-08 23:05:16 +01001477STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001478 vtype_kind_t vtype_base, vtype_val;
1479 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1480 assert(vtype_base == VTYPE_PYOBJ);
1481 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001482 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001483 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001484}
1485
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001486STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001487 DEBUG_printf("store_subscr\n");
1488 // need to compile: base[index] = value
1489
1490 // pop: index, base, value
1491 // optimise case where index is an immediate
1492 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1493
1494 if (vtype_base == VTYPE_PYOBJ) {
1495 // standard Python call
1496 vtype_kind_t vtype_index, vtype_value;
1497 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1498 assert(vtype_index == VTYPE_PYOBJ);
1499 assert(vtype_value == VTYPE_PYOBJ);
1500 emit_call(emit, MP_F_OBJ_SUBSCR);
1501 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001502 // viper store
1503 // TODO The different machine architectures have very different
1504 // capabilities and requirements for stores, so probably best to
1505 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001506 stack_info_t *top = peek_stack(emit, 0);
1507 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1508 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001509 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001510 emit_pre_pop_discard(emit); // discard index
1511 vtype_kind_t vtype_value;
1512 int reg_base = REG_ARG_1;
1513 int reg_index = REG_ARG_2;
1514 int reg_value = REG_ARG_3;
1515 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001516 #if N_X86
1517 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1518 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1519 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001520 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001521 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001522 switch (vtype_base) {
1523 case VTYPE_PTR8: {
1524 // pointer to 8-bit memory
1525 // TODO optimise to use thumb strb r1, [r2, r3]
1526 if (index_value != 0) {
1527 // index is non-zero
1528 #if N_THUMB
1529 if (index_value > 0 && index_value < 32) {
1530 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1531 break;
1532 }
1533 #endif
1534 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001535 #if N_ARM
1536 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1537 return;
1538 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001539 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1540 reg_base = reg_index;
1541 }
1542 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1543 break;
1544 }
1545 case VTYPE_PTR16: {
1546 // pointer to 16-bit memory
1547 if (index_value != 0) {
1548 // index is a non-zero immediate
1549 #if N_THUMB
1550 if (index_value > 0 && index_value < 32) {
1551 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1552 break;
1553 }
1554 #endif
1555 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001556 #if N_ARM
1557 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1558 return;
1559 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001560 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1561 reg_base = reg_index;
1562 }
1563 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1564 break;
1565 }
1566 default:
1567 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1568 }
1569 } else {
1570 // index is not an immediate
1571 vtype_kind_t vtype_index, vtype_value;
1572 int reg_index = REG_ARG_2;
1573 int reg_value = REG_ARG_3;
1574 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1575 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001576 #if N_X86
1577 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1578 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1579 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001580 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001581 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001582 switch (vtype_base) {
1583 case VTYPE_PTR8: {
1584 // pointer to 8-bit memory
1585 // TODO optimise to use thumb strb r1, [r2, r3]
1586 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001587 #if N_ARM
1588 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1589 break;
1590 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001591 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1592 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1593 break;
1594 }
1595 case VTYPE_PTR16: {
1596 // pointer to 16-bit memory
1597 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001598 #if N_ARM
1599 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1600 break;
1601 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001602 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1603 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1604 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1605 break;
1606 }
1607 default:
1608 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1609 }
1610 }
1611
1612 }
Damien13ed3a62013-10-08 09:05:10 +01001613}
1614
Damien George7ff996c2014-09-08 23:05:16 +01001615STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001616 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001617 // could support for Python types, just set to None (so GC can reclaim it)
Paul Sokolovskydb1ac362015-01-01 09:43:42 +02001618 // local is automatically deleted for exception block "as" var, and the message
1619 // breaks tests.
1620 //mp_emitter_warning(emit->pass, "Native codegeneration doesn't support deleting local");
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001621 (void)emit;
1622 (void)qst;
1623 (void)local_num;
Damien13ed3a62013-10-08 09:05:10 +01001624}
1625
Damien George7ff996c2014-09-08 23:05:16 +01001626STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001627 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001628 (void)emit;
1629 (void)qst;
1630 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001631}
1632
Damien Georgee6ce10a2014-09-06 18:38:20 +01001633STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1634 emit_native_pre(emit);
1635 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1636 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001637}
1638
Damien Georgee6ce10a2014-09-06 18:38:20 +01001639STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1640 emit_native_pre(emit);
1641 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1642 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001643}
1644
Damien Georgee6ce10a2014-09-06 18:38:20 +01001645STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001646 vtype_kind_t vtype_base;
1647 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1648 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001649 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 +01001650 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001651}
1652
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001653STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001654 vtype_kind_t vtype_index, vtype_base;
1655 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1656 assert(vtype_index == VTYPE_PYOBJ);
1657 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001658 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 +01001659}
1660
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001661STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001662 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001663 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001664 int reg = REG_TEMP0;
1665 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1666 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001667}
1668
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001669STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001670 vtype_kind_t vtype0, vtype1;
1671 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1672 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1673}
1674
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001675STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001676 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001677 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001678 emit_post(emit);
1679}
1680
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001681STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001682 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001683 vtype_kind_t vtype0, vtype1;
1684 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1685 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001686}
1687
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001688STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001689 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001690 vtype_kind_t vtype0, vtype1, vtype2;
1691 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1692 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1693}
1694
Damien George7ff996c2014-09-08 23:05:16 +01001695STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001696 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001697 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001698 // need to commit stack because we are jumping elsewhere
1699 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001700 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001701 emit_post(emit);
1702}
1703
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001704STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001705 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001706 switch (vtype) {
1707 case VTYPE_PYOBJ:
1708 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1709 if (!pop) {
1710 adjust_stack(emit, 1);
1711 }
1712 emit_call(emit, MP_F_OBJ_IS_TRUE);
1713 break;
1714 case VTYPE_BOOL:
1715 case VTYPE_INT:
1716 case VTYPE_UINT:
1717 emit_pre_pop_reg(emit, &vtype, REG_RET);
1718 if (!pop) {
1719 adjust_stack(emit, 1);
1720 }
1721 break;
1722 default:
1723 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1724 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001725 }
Damien George21ca2d72014-10-19 19:00:51 +01001726 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1727 // can use it. This is a bit of a hack.
1728 if (!pop) {
1729 emit->saved_stack_vtype = vtype;
1730 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001731 // need to commit stack because we may jump elsewhere
1732 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001733}
1734
Damien George63f38322015-02-28 15:04:06 +00001735STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1736 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001737 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001738 if (cond) {
1739 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1740 } else {
1741 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1742 }
Damien1a6633a2013-11-03 13:58:19 +00001743 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001744}
Damien1a6633a2013-11-03 13:58:19 +00001745
Damien George63f38322015-02-28 15:04:06 +00001746STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1747 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001748 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001749 if (cond) {
1750 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1751 } else {
1752 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1753 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001754 adjust_stack(emit, -1);
1755 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001756}
1757
Damien George7ff996c2014-09-08 23:05:16 +01001758STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001759 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001760 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001761}
Damien Georgea32c1e42014-05-07 18:30:52 +01001762
Damien George7ff996c2014-09-08 23:05:16 +01001763STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001764 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001765 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001766}
Damien Georgea32c1e42014-05-07 18:30:52 +01001767
Damien George7ff996c2014-09-08 23:05:16 +01001768STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001769 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001770 (void)emit;
1771 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001772 assert(0);
1773}
Damien Georgeb601d952014-06-30 05:17:25 +01001774
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001775STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001776 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001777 assert(0);
1778}
Damien Georgeb601d952014-06-30 05:17:25 +01001779
Damien George7ff996c2014-09-08 23:05:16 +01001780STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001781 emit_native_pre(emit);
1782 // need to commit stack because we may jump elsewhere
1783 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001784 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 +01001785 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001786 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001787 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001788}
Damien Georgeb601d952014-06-30 05:17:25 +01001789
Damien George7ff996c2014-09-08 23:05:16 +01001790STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001791 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001792}
Damien Georgeb601d952014-06-30 05:17:25 +01001793
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001794STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001795 // logic:
1796 // exc = pop_stack
1797 // if exc == None: pass
1798 // else: raise exc
1799 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1800 vtype_kind_t vtype;
1801 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1802 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001803 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001804}
Damiend2755ec2013-10-16 23:58:48 +01001805
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001806STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001807 // perhaps the difficult one, as we want to rewrite for loops using native code
1808 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001809
1810 vtype_kind_t vtype;
1811 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1812 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001813 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001814 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001815}
Damiend2755ec2013-10-16 23:58:48 +01001816
Damien George7ff996c2014-09-08 23:05:16 +01001817STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001818 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001819 vtype_kind_t vtype;
1820 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1821 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001822 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001823 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1824 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001825 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1826}
1827
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001828STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001829 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001830 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001831 adjust_stack(emit, -1);
1832 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001833}
1834
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001835STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001836 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001837 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001838 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001839 emit_post(emit);
1840}
1841
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001842STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001843 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001844 /*
1845 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001846 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001847 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001848 emit_post(emit);
1849 */
Damien13ed3a62013-10-08 09:05:10 +01001850}
1851
Damien Georged17926d2014-03-30 13:35:08 +01001852STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001853 vtype_kind_t vtype;
1854 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1855 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001856 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001857 // we need to synthesise this operation by converting to bool first
1858 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001859 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001860 }
Damien Georged6230f62014-09-23 14:10:03 +00001861 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1862 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001863}
1864
Damien Georged17926d2014-03-30 13:35:08 +01001865STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001866 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001867 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1868 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001869 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001870 #if N_X64 || N_X86
1871 // special cases for x86 and shifting
1872 if (op == MP_BINARY_OP_LSHIFT
1873 || op == MP_BINARY_OP_INPLACE_LSHIFT
1874 || op == MP_BINARY_OP_RSHIFT
1875 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1876 #if N_X64
1877 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1878 #else
1879 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1880 #endif
1881 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1882 ASM_LSL_REG(emit->as, REG_RET);
1883 } else {
1884 ASM_ASR_REG(emit->as, REG_RET);
1885 }
1886 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1887 return;
1888 }
1889 #endif
1890 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001891 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001892 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1893 if (0) {
1894 // dummy
1895 #if !(N_X64 || N_X86)
1896 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1897 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001898 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001899 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1900 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1901 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1902 #endif
Damien George1ef23482014-10-12 14:21:06 +01001903 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1904 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1905 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1906 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1907 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1908 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1909 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1910 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1911 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001912 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1913 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1914 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1915 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1916 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1917 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1918 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1919 // comparison ops are (in enum order):
1920 // MP_BINARY_OP_LESS
1921 // MP_BINARY_OP_MORE
1922 // MP_BINARY_OP_EQUAL
1923 // MP_BINARY_OP_LESS_EQUAL
1924 // MP_BINARY_OP_MORE_EQUAL
1925 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001926 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001927 #if N_X64
1928 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1929 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1930 static byte ops[6] = {
1931 ASM_X64_CC_JL,
1932 ASM_X64_CC_JG,
1933 ASM_X64_CC_JE,
1934 ASM_X64_CC_JLE,
1935 ASM_X64_CC_JGE,
1936 ASM_X64_CC_JNE,
1937 };
1938 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1939 #elif N_X86
1940 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1941 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1942 static byte ops[6] = {
1943 ASM_X86_CC_JL,
1944 ASM_X86_CC_JG,
1945 ASM_X86_CC_JE,
1946 ASM_X86_CC_JLE,
1947 ASM_X86_CC_JGE,
1948 ASM_X86_CC_JNE,
1949 };
1950 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1951 #elif N_THUMB
1952 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1953 static uint16_t ops[6] = {
1954 ASM_THUMB_OP_ITE_GE,
1955 ASM_THUMB_OP_ITE_GT,
1956 ASM_THUMB_OP_ITE_EQ,
1957 ASM_THUMB_OP_ITE_GT,
1958 ASM_THUMB_OP_ITE_GE,
1959 ASM_THUMB_OP_ITE_EQ,
1960 };
1961 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1962 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1963 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1964 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1965 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001966 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1967 static uint ccs[6] = {
1968 ASM_ARM_CC_LT,
1969 ASM_ARM_CC_GT,
1970 ASM_ARM_CC_EQ,
1971 ASM_ARM_CC_LE,
1972 ASM_ARM_CC_GE,
1973 ASM_ARM_CC_NE,
1974 };
1975 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001976 #else
1977 #error not implemented
1978 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001979 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1980 } else {
1981 // TODO other ops not yet implemented
1982 assert(0);
1983 }
Damien13ed3a62013-10-08 09:05:10 +01001984 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001985 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001986 bool invert = false;
1987 if (op == MP_BINARY_OP_NOT_IN) {
1988 invert = true;
1989 op = MP_BINARY_OP_IN;
1990 } else if (op == MP_BINARY_OP_IS_NOT) {
1991 invert = true;
1992 op = MP_BINARY_OP_IS;
1993 }
Damien George7fe21912014-08-16 22:31:57 +01001994 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001995 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001996 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001997 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1998 }
Damien13ed3a62013-10-08 09:05:10 +01001999 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2000 } else {
2001 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002002 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002003 }
2004}
2005
Damien George7ff996c2014-09-08 23:05:16 +01002006STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002007 // for viper: call runtime, with types of args
2008 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002009 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002010 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002011 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002012 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002013}
2014
Damien George7ff996c2014-09-08 23:05:16 +01002015STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002016 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002017 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002018 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002019 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2020}
2021
Damien George7ff996c2014-09-08 23:05:16 +01002022STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002023 // only used in list comprehension
2024 vtype_kind_t vtype_list, vtype_item;
2025 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2026 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2027 assert(vtype_list == VTYPE_PYOBJ);
2028 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002029 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002030 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002031}
2032
Damien George7ff996c2014-09-08 23:05:16 +01002033STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002034 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002035 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002036 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2037}
2038
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002039STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002040 vtype_kind_t vtype_key, vtype_value, vtype_map;
2041 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
2042 assert(vtype_key == VTYPE_PYOBJ);
2043 assert(vtype_value == VTYPE_PYOBJ);
2044 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002045 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002046 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2047}
2048
Damien George7ff996c2014-09-08 23:05:16 +01002049STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002050 // only used in list comprehension
2051 vtype_kind_t vtype_map, vtype_key, vtype_value;
2052 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2053 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2054 assert(vtype_map == VTYPE_PYOBJ);
2055 assert(vtype_key == VTYPE_PYOBJ);
2056 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002057 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002058 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002059}
2060
Damien Georgee37dcaa2014-12-27 17:07:16 +00002061#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002062STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002063 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002064 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002065 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002066 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2067}
2068
Damien George7ff996c2014-09-08 23:05:16 +01002069STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002070 // only used in set comprehension
2071 vtype_kind_t vtype_set, vtype_item;
2072 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2073 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2074 assert(vtype_set == VTYPE_PYOBJ);
2075 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002076 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002077 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002078}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002079#endif
Damiend2755ec2013-10-16 23:58:48 +01002080
Damien George83204f32014-12-27 17:20:41 +00002081#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002082STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002083 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002084 if (n_args == 2) {
2085 vtype_kind_t vtype_start, vtype_stop;
2086 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2087 assert(vtype_start == VTYPE_PYOBJ);
2088 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002089 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 +01002090 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2091 } else {
2092 assert(n_args == 3);
2093 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2094 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
2095 assert(vtype_start == VTYPE_PYOBJ);
2096 assert(vtype_stop == VTYPE_PYOBJ);
2097 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002098 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002099 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2100 }
Damien13ed3a62013-10-08 09:05:10 +01002101}
Damien George83204f32014-12-27 17:20:41 +00002102#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002103
Damien George7ff996c2014-09-08 23:05:16 +01002104STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002105 DEBUG_printf("unpack_sequence %d\n", n_args);
2106 vtype_kind_t vtype_base;
2107 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2108 assert(vtype_base == VTYPE_PYOBJ);
2109 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002110 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002111}
Damien Georgecdd96df2014-04-06 12:58:40 +01002112
Damien George7ff996c2014-09-08 23:05:16 +01002113STATIC 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 +01002114 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2115 vtype_kind_t vtype_base;
2116 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2117 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002118 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 +01002119 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 +01002120}
2121
Damien George7ff996c2014-09-08 23:05:16 +01002122STATIC 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 +01002123 // 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 +00002124 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002125 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002126 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 +01002127 } else {
2128 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2129 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2130 assert(vtype_def_tuple == VTYPE_PYOBJ);
2131 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002132 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 +01002133 }
Damien13ed3a62013-10-08 09:05:10 +01002134 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2135}
2136
Damien George7ff996c2014-09-08 23:05:16 +01002137STATIC 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 Georgeff8dd3f2015-01-20 12:47:20 +00002138 (void)emit;
2139 (void)scope;
2140 (void)n_closed_over;
2141 (void)n_pos_defaults;
2142 (void)n_kw_defaults;
Damien13ed3a62013-10-08 09:05:10 +01002143 assert(0);
2144}
2145
Damien George7ff996c2014-09-08 23:05:16 +01002146STATIC 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 +00002147 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2148
Damien Georgee9dac3b2014-09-29 22:10:41 +01002149 // TODO: in viper mode, call special runtime routine with type info for args,
2150 // and wanted type info for return, to remove need for boxing/unboxing
2151
Damien George922ddd62014-04-09 12:43:17 +01002152 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002153
Damien Georgee9dac3b2014-09-29 22:10:41 +01002154 emit_native_pre(emit);
2155 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2156 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2157 // casting operator
2158 assert(n_positional == 1 && n_keyword == 0);
2159 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002160 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002161 switch (peek_vtype(emit, 0)) {
2162 case VTYPE_PYOBJ: {
2163 vtype_kind_t vtype;
2164 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2165 emit_pre_pop_discard(emit);
2166 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2167 emit_post_push_reg(emit, vtype_cast, REG_RET);
2168 break;
2169 }
2170 case VTYPE_BOOL:
2171 case VTYPE_INT:
2172 case VTYPE_UINT:
2173 case VTYPE_PTR:
2174 case VTYPE_PTR8:
2175 case VTYPE_PTR16:
2176 case VTYPE_PTR_NONE:
2177 emit_fold_stack_top(emit, REG_ARG_1);
2178 emit_post_top_set_vtype(emit, vtype_cast);
2179 break;
2180 default:
2181 assert(!"TODO: convert obj to int");
2182 }
2183 } else {
2184 if (n_positional != 0 || n_keyword != 0) {
2185 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2186 }
Damien13ed3a62013-10-08 09:05:10 +01002187 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2188 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002189 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2190 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002191 }
Damien13ed3a62013-10-08 09:05:10 +01002192}
2193
Damien George7ff996c2014-09-08 23:05:16 +01002194STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
Damien George922ddd62014-04-09 12:43:17 +01002195 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002196 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002197 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
Damien George7fe21912014-08-16 22:31:57 +01002198 emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW, n_positional, REG_ARG_1, n_keyword, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +01002199 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2200}
2201
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002202STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002203 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002204 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002205 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2206 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002207 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002208 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002209 } else {
2210 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002211 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002212 } else {
2213 vtype_kind_t vtype;
2214 emit_pre_pop_reg(emit, &vtype, REG_RET);
2215 if (vtype != emit->return_vtype) {
2216 printf("ViperTypeError: incompatible return type\n");
2217 }
Damien George2ac4af62014-08-15 16:45:41 +01002218 }
Damien13ed3a62013-10-08 09:05:10 +01002219 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002220 vtype_kind_t vtype;
2221 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002222 assert(vtype == VTYPE_PYOBJ);
2223 }
2224 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002225 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2226 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002227}
2228
Damien George7ff996c2014-09-08 23:05:16 +01002229STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002230 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002231 vtype_kind_t vtype_exc;
2232 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2233 if (vtype_exc != VTYPE_PYOBJ) {
2234 printf("ViperTypeError: must raise an object\n");
2235 }
2236 // 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 +01002237 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002238}
Damien Georgeb601d952014-06-30 05:17:25 +01002239
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002240STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002241 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002242 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002243 assert(0);
2244}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002245STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002246 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002247 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002248 assert(0);
2249}
2250
Damien Georgeb601d952014-06-30 05:17:25 +01002251STATIC void emit_native_start_except_handler(emit_t *emit) {
2252 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2253 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2254 // the first 2 elements, so we can get the thrown value.
2255 adjust_stack(emit, 2);
2256 vtype_kind_t vtype_nlr;
2257 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002258 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002259 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
2260}
2261
2262STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002263 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002264}
2265
Damien13ed3a62013-10-08 09:05:10 +01002266const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002267 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002268 emit_native_start_pass,
2269 emit_native_end_pass,
2270 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002271 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002272 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002273
Damien George542bd6b2015-03-26 14:42:40 +00002274 {
2275 emit_native_load_fast,
2276 emit_native_load_deref,
2277 emit_native_load_name,
2278 emit_native_load_global,
2279 },
2280 {
2281 emit_native_store_fast,
2282 emit_native_store_deref,
2283 emit_native_store_name,
2284 emit_native_store_global,
2285 },
2286 {
2287 emit_native_delete_fast,
2288 emit_native_delete_deref,
2289 emit_native_delete_name,
2290 emit_native_delete_global,
2291 },
Damien13ed3a62013-10-08 09:05:10 +01002292
2293 emit_native_label_assign,
2294 emit_native_import_name,
2295 emit_native_import_from,
2296 emit_native_import_star,
2297 emit_native_load_const_tok,
2298 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002299 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002300 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002301 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002302 emit_native_load_attr,
2303 emit_native_load_method,
2304 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002305 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002306 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002307 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002308 emit_native_delete_attr,
2309 emit_native_delete_subscr,
2310 emit_native_dup_top,
2311 emit_native_dup_top_two,
2312 emit_native_pop_top,
2313 emit_native_rot_two,
2314 emit_native_rot_three,
2315 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002316 emit_native_pop_jump_if,
2317 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002318 emit_native_break_loop,
2319 emit_native_continue_loop,
2320 emit_native_setup_with,
2321 emit_native_with_cleanup,
2322 emit_native_setup_except,
2323 emit_native_setup_finally,
2324 emit_native_end_finally,
2325 emit_native_get_iter,
2326 emit_native_for_iter,
2327 emit_native_for_iter_end,
2328 emit_native_pop_block,
2329 emit_native_pop_except,
2330 emit_native_unary_op,
2331 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002332 emit_native_build_tuple,
2333 emit_native_build_list,
2334 emit_native_list_append,
2335 emit_native_build_map,
2336 emit_native_store_map,
2337 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002338 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002339 emit_native_build_set,
2340 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002341 #endif
Damien George83204f32014-12-27 17:20:41 +00002342 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002343 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002344 #endif
Damien13ed3a62013-10-08 09:05:10 +01002345 emit_native_unpack_sequence,
2346 emit_native_unpack_ex,
2347 emit_native_make_function,
2348 emit_native_make_closure,
2349 emit_native_call_function,
2350 emit_native_call_method,
2351 emit_native_return_value,
2352 emit_native_raise_varargs,
2353 emit_native_yield_value,
2354 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002355
2356 emit_native_start_except_handler,
2357 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002358};
2359
Damien Georgec90f59e2014-09-06 23:06:36 +01002360#endif