blob: 423b1684b6405e6e2d153cdc1b6852299debb208 [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
Damien George51229af2015-03-26 17:54:12 +0000567 // XXX this is such a big hack and really needs to be fixed
Damienff8ed772013-10-08 22:18:32 +0100568 if (emit->stack_info == NULL) {
Damien George51229af2015-03-26 17:54:12 +0000569 emit->stack_info_alloc = scope->stack_size + 200;
Damienff8ed772013-10-08 22:18:32 +0100570 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100571 }
572
Damien George2ac4af62014-08-15 16:45:41 +0100573 // set default type for return and arguments
574 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100575 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100576 emit->local_vtype[i] = VTYPE_PYOBJ;
577 }
Damien Georgea5190a72014-08-15 22:39:08 +0100578
579 // local variables begin unbound, and have unknown type
580 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
581 emit->local_vtype[i] = VTYPE_UNBOUND;
582 }
583
584 // values on stack begin unbound
585 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100586 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100587 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100588 }
589
Damien Georgec90f59e2014-09-06 23:06:36 +0100590 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100591
592 // entry to function
593 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100594 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100595 num_locals = scope->num_locals - REG_LOCAL_NUM;
596 if (num_locals < 0) {
597 num_locals = 0;
598 }
599 emit->stack_start = num_locals;
600 num_locals += scope->stack_size;
601 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100602 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100603
604 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100605#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100606 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100607 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100608 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100609 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100610 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100611 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100612 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100613 } else if (i == 3) {
614 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100615 } else {
616 // TODO not implemented
617 assert(0);
618 }
619 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100620#elif N_X86
621 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100622 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000623 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100624 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000625 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000626 } else if (i == 2) {
627 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100628 } else {
Damien George03281b32014-09-06 22:38:50 +0000629 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
630 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100631 }
632 }
Damien3ef4abb2013-10-12 16:53:13 +0100633#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100634 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100635 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100636 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100637 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100638 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100639 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100640 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100641 } else if (i == 3) {
642 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
643 } else {
644 // TODO not implemented
645 assert(0);
646 }
647 }
648
Damien Georgee9dac3b2014-09-29 22:10:41 +0100649 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100650 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200651#elif N_ARM
652 for (int i = 0; i < scope->num_pos_args; i++) {
653 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100654 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200655 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100656 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200657 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100658 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200659 } else if (i == 3) {
660 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
661 } else {
662 // TODO not implemented
663 assert(0);
664 }
665 }
666
Damien Georgee9dac3b2014-09-29 22:10:41 +0100667 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100668 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100669#else
670 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100671#endif
672}
673
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200674STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100675 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100676 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100677 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100678 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100679
680 // check stack is back to zero size
681 if (emit->stack_size != 0) {
682 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
683 }
684
Damien George36db6bc2014-05-07 17:24:22 +0100685 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100686 void *f = ASM_GET_CODE(emit->as);
687 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100688
689 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100690 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100691 mp_uint_t type_sig = emit->return_vtype & 3;
692 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
693 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
694 }
695
696 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 +0100697 }
698}
699
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200700STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100701 return emit->last_emit_was_return_value;
702}
703
Damien Georged6230f62014-09-23 14:10:03 +0000704STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000705 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100706 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100707 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100708 emit->scope->stack_size = emit->stack_size;
709 }
Damien George21ca2d72014-10-19 19:00:51 +0100710#ifdef DEBUG_PRINT
711 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
712 for (int i = 0; i < emit->stack_size; i++) {
713 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000714 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->data.u_reg);
Damien George21ca2d72014-10-19 19:00:51 +0100715 }
716 DEBUG_printf("\n");
717#endif
Damien13ed3a62013-10-08 09:05:10 +0100718}
719
Damien Georged6230f62014-09-23 14:10:03 +0000720STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100721 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000722 // If we are adjusting the stack in a positive direction (pushing) then we
723 // need to fill in values for the stack kind and vtype of the newly-pushed
724 // entries. These should be set to "value" (ie not reg or imm) because we
725 // should only need to adjust the stack due to a jump to this part in the
726 // code (and hence we have settled the stack before the jump).
727 for (mp_int_t i = 0; i < delta; i++) {
728 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
729 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100730 // TODO we don't know the vtype to use here. At the moment this is a
731 // hack to get the case of multi comparison working.
732 if (delta == 1) {
733 si->vtype = emit->saved_stack_vtype;
734 } else {
735 si->vtype = VTYPE_PYOBJ;
736 }
Damien Georged6230f62014-09-23 14:10:03 +0000737 }
738 adjust_stack(emit, delta);
739}
740
741STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000742 (void)emit;
743 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000744}
745
Damienff8ed772013-10-08 22:18:32 +0100746/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200747STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100748 adjust_stack(emit, stack_size_delta);
749 emit->last_emit_was_return_value = false;
750}
Damienff8ed772013-10-08 22:18:32 +0100751*/
Damien13ed3a62013-10-08 09:05:10 +0100752
Damienff8ed772013-10-08 22:18:32 +0100753// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000754STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100755 emit->last_emit_was_return_value = false;
756 // settle the stack
757 /*
758 if (regs_needed != 0) {
759 for (int i = 0; i < emit->stack_size; i++) {
760 switch (emit->stack_info[i].kind) {
761 case STACK_VALUE:
762 break;
763
764 case STACK_REG:
765 // TODO only push reg if in regs_needed
766 emit->stack_info[i].kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000767 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100768 break;
769
770 case STACK_IMM:
771 // don't think we ever need to push imms for settling
772 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
773 break;
774 }
775 }
776 }
777 */
Damien13ed3a62013-10-08 09:05:10 +0100778}
779
Damien George3112cde2014-09-29 18:45:42 +0100780// depth==0 is top, depth==1 is before top, etc
781STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
782 return &emit->stack_info[emit->stack_size - 1 - depth];
783}
784
785// depth==0 is top, depth==1 is before top, etc
786STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
787 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100788}
Damien13ed3a62013-10-08 09:05:10 +0100789
Damiend2755ec2013-10-16 23:58:48 +0100790// pos=1 is TOS, pos=2 is next, etc
791// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200792STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100793 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100794 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100795 if (i != skip_stack_pos) {
796 stack_info_t *si = &emit->stack_info[i];
Damien George32444b72015-01-24 23:14:12 +0000797 if (si->kind == STACK_REG && si->data.u_reg == reg_needed) {
Damiend2755ec2013-10-16 23:58:48 +0100798 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000799 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100800 }
Damienff8ed772013-10-08 22:18:32 +0100801 }
802 }
803}
Damien13ed3a62013-10-08 09:05:10 +0100804
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200805STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100806 for (int i = 0; i < emit->stack_size; i++) {
807 stack_info_t *si = &emit->stack_info[i];
808 if (si->kind == STACK_REG) {
809 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000810 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100811 }
Damien13ed3a62013-10-08 09:05:10 +0100812 }
813}
814
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200815STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000816 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100817 for (int i = 0; i < emit->stack_size; i++) {
818 stack_info_t *si = &emit->stack_info[i];
819 if (si->kind == STACK_REG) {
Damien George32444b72015-01-24 23:14:12 +0000820 DEBUG_printf(" reg(%u) to local(%u)\n", si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100821 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000822 ASM_MOV_REG_TO_LOCAL(emit->as, si->data.u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100823 }
824 }
825 for (int i = 0; i < emit->stack_size; i++) {
826 stack_info_t *si = &emit->stack_info[i];
827 if (si->kind == STACK_IMM) {
Damien George32444b72015-01-24 23:14:12 +0000828 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->data.u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100829 si->kind = STACK_VALUE;
Damien George32444b72015-01-24 23:14:12 +0000830 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->data.u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100831 }
832 }
833}
834
835// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200836STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100837 need_reg_single(emit, reg_dest, pos);
838 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100839 *vtype = si->vtype;
840 switch (si->kind) {
841 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100842 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100843 break;
844
Damienff8ed772013-10-08 22:18:32 +0100845 case STACK_REG:
Damien George32444b72015-01-24 23:14:12 +0000846 if (si->data.u_reg != reg_dest) {
847 ASM_MOV_REG_REG(emit->as, reg_dest, si->data.u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100848 }
849 break;
850
Damienff8ed772013-10-08 22:18:32 +0100851 case STACK_IMM:
Damien George32444b72015-01-24 23:14:12 +0000852 ASM_MOV_IMM_TO_REG(emit->as, si->data.u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100853 break;
854 }
Damien13ed3a62013-10-08 09:05:10 +0100855}
856
Damien Georgee9dac3b2014-09-29 22:10:41 +0100857// does an efficient X=pop(); discard(); push(X)
858// needs a (non-temp) register in case the poped element was stored in the stack
859STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
860 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
861 si[0] = si[1];
862 if (si->kind == STACK_VALUE) {
863 // if folded element was on the stack we need to put it in a register
864 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
865 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000866 si->data.u_reg = reg_dest;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100867 }
868 adjust_stack(emit, -1);
869}
870
871// If stacked value is in a register and the register is not r1 or r2, then
872// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
873STATIC 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 +0100874 emit->last_emit_was_return_value = false;
875 stack_info_t *si = peek_stack(emit, 0);
Damien George32444b72015-01-24 23:14:12 +0000876 if (si->kind == STACK_REG && si->data.u_reg != not_r1 && si->data.u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100877 *vtype = si->vtype;
Damien George32444b72015-01-24 23:14:12 +0000878 *reg_dest = si->data.u_reg;
Damien George3112cde2014-09-29 18:45:42 +0100879 need_reg_single(emit, *reg_dest, 1);
880 } else {
881 emit_access_stack(emit, 1, vtype, *reg_dest);
882 }
883 adjust_stack(emit, -1);
884}
885
Damien Georgee6ce10a2014-09-06 18:38:20 +0100886STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100887 emit->last_emit_was_return_value = false;
888 adjust_stack(emit, -1);
889}
890
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200891STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100892 emit->last_emit_was_return_value = false;
893 emit_access_stack(emit, 1, vtype, reg_dest);
894 adjust_stack(emit, -1);
895}
896
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200897STATIC 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 +0100898 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100899 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100900}
901
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200902STATIC 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 +0100903 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100904 emit_pre_pop_reg(emit, vtypeb, regb);
905 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100906}
907
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200908STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000909 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +0100910}
911
Damien Georgee9dac3b2014-09-29 22:10:41 +0100912STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
913 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
914 si->vtype = new_vtype;
915}
916
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200917STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100918 stack_info_t *si = &emit->stack_info[emit->stack_size];
919 si->vtype = vtype;
920 si->kind = STACK_REG;
Damien George32444b72015-01-24 23:14:12 +0000921 si->data.u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100922 adjust_stack(emit, 1);
923}
924
Damien George40f3c022014-07-03 13:25:24 +0100925STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100926 stack_info_t *si = &emit->stack_info[emit->stack_size];
927 si->vtype = vtype;
928 si->kind = STACK_IMM;
Damien George32444b72015-01-24 23:14:12 +0000929 si->data.u_imm = imm;
Damienff8ed772013-10-08 22:18:32 +0100930 adjust_stack(emit, 1);
931}
932
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200933STATIC 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 +0100934 emit_post_push_reg(emit, vtypea, rega);
935 emit_post_push_reg(emit, vtypeb, regb);
936}
937
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200938STATIC 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 +0100939 emit_post_push_reg(emit, vtypea, rega);
940 emit_post_push_reg(emit, vtypeb, regb);
941 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100942}
943
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200944STATIC 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 +0100945 emit_post_push_reg(emit, vtypea, rega);
946 emit_post_push_reg(emit, vtypeb, regb);
947 emit_post_push_reg(emit, vtypec, regc);
948 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100949}
950
Damien George7fe21912014-08-16 22:31:57 +0100951STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100952 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100953 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100954}
955
Damien George7fe21912014-08-16 22:31:57 +0100956STATIC 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 +0100957 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100958 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
959 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100960}
961
Damien George40f3c022014-07-03 13:25:24 +0100962// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100963STATIC 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 +0100964 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100965 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
966 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100967}
968
Damien George7fe21912014-08-16 22:31:57 +0100969STATIC 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 +0000970 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100971 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
972 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
973 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000974}
975
Damien George40f3c022014-07-03 13:25:24 +0100976// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100977STATIC 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 +0100978 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100979 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
980 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
981 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
982 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100983}
984
Damien George86de21b2014-08-16 22:06:11 +0100985// vtype of all n_pop objects is VTYPE_PYOBJ
986// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
987// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
988// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
989STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
990 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100991
Damien George86de21b2014-08-16 22:06:11 +0100992 // First, store any immediate values to their respective place on the stack.
993 for (mp_uint_t i = 0; i < n_pop; i++) {
994 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
995 // must push any imm's to stack
996 // must convert them to VTYPE_PYOBJ for viper code
997 if (si->kind == STACK_IMM) {
998 si->kind = STACK_VALUE;
999 switch (si->vtype) {
1000 case VTYPE_PYOBJ:
Damien George32444b72015-01-24 23:14:12 +00001001 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 +01001002 break;
1003 case VTYPE_BOOL:
Damien George32444b72015-01-24 23:14:12 +00001004 if (si->data.u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001005 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 +01001006 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001007 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 +01001008 }
1009 si->vtype = VTYPE_PYOBJ;
1010 break;
1011 case VTYPE_INT:
1012 case VTYPE_UINT:
Damien George32444b72015-01-24 23:14:12 +00001013 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 +01001014 si->vtype = VTYPE_PYOBJ;
1015 break;
1016 default:
1017 // not handled
1018 assert(0);
1019 }
1020 }
1021
1022 // verify that this value is on the stack
1023 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001024 }
Damien George86de21b2014-08-16 22:06:11 +01001025
1026 // Second, convert any non-VTYPE_PYOBJ to that type.
1027 for (mp_uint_t i = 0; i < n_pop; i++) {
1028 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1029 if (si->vtype != VTYPE_PYOBJ) {
1030 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001031 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001032 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 +01001033 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001034 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001035 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001036 }
1037 }
1038
1039 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1040 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001041 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001042}
1043
1044// vtype of all n_push objects is VTYPE_PYOBJ
1045STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1046 need_reg_all(emit);
1047 for (mp_uint_t i = 0; i < n_push; i++) {
1048 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1049 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1050 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001051 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001052 adjust_stack(emit, n_push);
1053}
1054
Damien George7ff996c2014-09-08 23:05:16 +01001055STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001056 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001057 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001058 // need to commit stack because we can jump here from elsewhere
1059 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001060 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001061 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001062}
1063
Damien Georgecdd96df2014-04-06 12:58:40 +01001064STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1065 DEBUG_printf("import_name %s\n", qstr_str(qst));
1066 vtype_kind_t vtype_fromlist;
1067 vtype_kind_t vtype_level;
1068 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1069 assert(vtype_fromlist == VTYPE_PYOBJ);
1070 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001071 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001072 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001073}
1074
Damien Georgecdd96df2014-04-06 12:58:40 +01001075STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1076 DEBUG_printf("import_from %s\n", qstr_str(qst));
1077 emit_native_pre(emit);
1078 vtype_kind_t vtype_module;
1079 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1080 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001081 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001082 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001083}
1084
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001085STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001086 DEBUG_printf("import_star\n");
1087 vtype_kind_t vtype_module;
1088 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1089 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001090 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001091 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001092}
1093
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001094STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001095 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001096 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001097 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001098 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001099 if (emit->do_viper_types) {
1100 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001101 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1102 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1103 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien George3da677e2015-01-29 15:13:40 +00001104 no_other_choice1:
1105 case MP_TOKEN_ELLIPSIS: vtype = VTYPE_PYOBJ; val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1106 default: assert(0); goto no_other_choice1; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001107 }
1108 } else {
1109 vtype = VTYPE_PYOBJ;
1110 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001111 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1112 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1113 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien George3da677e2015-01-29 15:13:40 +00001114 no_other_choice2:
1115 case MP_TOKEN_ELLIPSIS: val = (mp_uint_t)&mp_const_ellipsis_obj; break;
1116 default: assert(0); goto no_other_choice2; // to help flow control analysis
Damien13ed3a62013-10-08 09:05:10 +01001117 }
1118 }
1119 emit_post_push_imm(emit, vtype, val);
1120}
1121
Damien George40f3c022014-07-03 13:25:24 +01001122STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001123 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001124 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001125 if (emit->do_viper_types) {
1126 emit_post_push_imm(emit, VTYPE_INT, arg);
1127 } else {
1128 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1129 }
1130}
1131
Damien George7ff996c2014-09-08 23:05:16 +01001132STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001133 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001134 // TODO: Eventually we want to be able to work with raw pointers in viper to
1135 // do native array access. For now we just load them as any other object.
1136 /*
Damien13ed3a62013-10-08 09:05:10 +01001137 if (emit->do_viper_types) {
1138 // not implemented properly
1139 // load a pointer to the asciiz string?
1140 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001141 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001142 } else
1143 */
1144 {
Damien Georgeb601d952014-06-30 05:17:25 +01001145 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001146 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001147 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001148 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001149 }
Damien13ed3a62013-10-08 09:05:10 +01001150 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1151 }
1152}
1153
Damien Georgedab13852015-01-13 15:55:54 +00001154STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1155 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001156 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001157 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1158 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1159}
1160
Damien George3558f622014-04-20 17:50:40 +01001161STATIC void emit_native_load_null(emit_t *emit) {
1162 emit_native_pre(emit);
1163 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1164}
1165
Damien George0abb5602015-01-16 12:24:49 +00001166STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1167 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001168 vtype_kind_t vtype = emit->local_vtype[local_num];
1169 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001170 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001171 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001172 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001173#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001174 if (local_num == 0) {
1175 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001176 } else if (local_num == 1) {
1177 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1178 } else if (local_num == 2) {
1179 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001180 } else {
Damien George0b610de2014-09-29 16:25:04 +01001181 need_reg_single(emit, REG_TEMP0, 0);
1182 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1183 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001184 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001185#elif N_X86
1186 if (local_num == 0) {
1187 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001188 } else if (local_num == 1) {
1189 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001190 } else if (local_num == 2) {
1191 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001192 } else {
Damien George0b610de2014-09-29 16:25:04 +01001193 need_reg_single(emit, REG_TEMP0, 0);
1194 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1195 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001196 }
Damien3ef4abb2013-10-12 16:53:13 +01001197#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001198 if (local_num == 0) {
1199 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1200 } else if (local_num == 1) {
1201 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1202 } else if (local_num == 2) {
1203 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1204 } else {
Damien George0b610de2014-09-29 16:25:04 +01001205 need_reg_single(emit, REG_TEMP0, 0);
1206 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1207 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001208 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001209#elif N_ARM
1210 if (local_num == 0) {
1211 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1212 } else if (local_num == 1) {
1213 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1214 } else if (local_num == 2) {
1215 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1216 } else {
Damien George0b610de2014-09-29 16:25:04 +01001217 need_reg_single(emit, REG_TEMP0, 0);
1218 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1219 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001220 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001221#else
1222 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001223#endif
1224}
1225
Damien George7ff996c2014-09-08 23:05:16 +01001226STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001227 // not implemented
1228 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001229 (void)emit;
1230 (void)qst;
1231 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001232 assert(0);
1233}
1234
Damien George7ff996c2014-09-08 23:05:16 +01001235STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001236 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001237 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001238 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001239 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1240}
1241
Damien George7ff996c2014-09-08 23:05:16 +01001242STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001243 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001244 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001245 // check for builtin casting operators
1246 if (emit->do_viper_types && qst == MP_QSTR_int) {
1247 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1248 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1249 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1250 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1251 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1252 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1253 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1254 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1255 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1256 } else {
1257 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1258 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1259 }
Damien13ed3a62013-10-08 09:05:10 +01001260}
1261
Damien George7ff996c2014-09-08 23:05:16 +01001262STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001263 // depends on type of subject:
1264 // - integer, function, pointer to integers: error
1265 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001266 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001267 vtype_kind_t vtype_base;
1268 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1269 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001270 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001271 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1272}
1273
Damien George7ff996c2014-09-08 23:05:16 +01001274STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001275 vtype_kind_t vtype_base;
1276 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1277 assert(vtype_base == VTYPE_PYOBJ);
1278 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001279 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001280}
1281
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001282STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001283 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001284 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001285 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001286}
1287
Damien George729f7b42014-04-17 22:10:53 +01001288STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001289 DEBUG_printf("load_subscr\n");
1290 // need to compile: base[index]
1291
1292 // pop: index, base
1293 // optimise case where index is an immediate
1294 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1295
1296 if (vtype_base == VTYPE_PYOBJ) {
1297 // standard Python call
1298 vtype_kind_t vtype_index;
1299 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1300 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001301 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 +01001302 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1303 } else {
Damien George91cfd412014-10-12 16:59:29 +01001304 // viper load
1305 // TODO The different machine architectures have very different
1306 // capabilities and requirements for loads, so probably best to
1307 // write a completely separate load-optimiser for each one.
1308 stack_info_t *top = peek_stack(emit, 0);
1309 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1310 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001311 mp_int_t index_value = top->data.u_imm;
Damien George91cfd412014-10-12 16:59:29 +01001312 emit_pre_pop_discard(emit); // discard index
1313 int reg_base = REG_ARG_1;
1314 int reg_index = REG_ARG_2;
1315 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1316 switch (vtype_base) {
1317 case VTYPE_PTR8: {
1318 // pointer to 8-bit memory
1319 // TODO optimise to use thumb ldrb r1, [r2, r3]
1320 if (index_value != 0) {
1321 // index is non-zero
1322 #if N_THUMB
1323 if (index_value > 0 && index_value < 32) {
1324 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1325 break;
1326 }
1327 #endif
1328 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1329 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1330 reg_base = reg_index;
1331 }
1332 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1333 break;
1334 }
1335 case VTYPE_PTR16: {
1336 // pointer to 16-bit memory
1337 if (index_value != 0) {
1338 // index is a non-zero immediate
1339 #if N_THUMB
1340 if (index_value > 0 && index_value < 32) {
1341 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1342 break;
1343 }
1344 #endif
1345 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1346 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1347 reg_base = reg_index;
1348 }
1349 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1350 break;
1351 }
1352 default:
1353 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1354 }
1355 } else {
1356 // index is not an immediate
1357 vtype_kind_t vtype_index;
1358 int reg_index = REG_ARG_2;
1359 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1360 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1361 switch (vtype_base) {
1362 case VTYPE_PTR8: {
1363 // pointer to 8-bit memory
1364 // TODO optimise to use thumb ldrb r1, [r2, r3]
1365 assert(vtype_index == VTYPE_INT);
1366 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1367 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1368 break;
1369 }
1370 case VTYPE_PTR16: {
1371 // pointer to 16-bit memory
1372 assert(vtype_index == VTYPE_INT);
1373 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1374 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1375 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1376 break;
1377 }
1378 default:
1379 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1380 }
1381 }
1382 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001383 }
1384}
1385
Damien George7ff996c2014-09-08 23:05:16 +01001386STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001387 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001388#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001389 if (local_num == 0) {
1390 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001391 } else if (local_num == 1) {
1392 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1393 } else if (local_num == 2) {
1394 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001395 } else {
Damien George0b610de2014-09-29 16:25:04 +01001396 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1397 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001398 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001399#elif N_X86
1400 if (local_num == 0) {
1401 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001402 } else if (local_num == 1) {
1403 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001404 } else if (local_num == 2) {
1405 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001406 } else {
Damien George0b610de2014-09-29 16:25:04 +01001407 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1408 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001409 }
Damien3ef4abb2013-10-12 16:53:13 +01001410#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001411 if (local_num == 0) {
1412 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1413 } else if (local_num == 1) {
1414 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1415 } else if (local_num == 2) {
1416 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1417 } else {
Damien George0b610de2014-09-29 16:25:04 +01001418 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1419 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001420 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001421#elif N_ARM
1422 if (local_num == 0) {
1423 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1424 } else if (local_num == 1) {
1425 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1426 } else if (local_num == 2) {
1427 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1428 } else {
Damien George0b610de2014-09-29 16:25:04 +01001429 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1430 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001431 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001432#else
1433 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001434#endif
1435
1436 emit_post(emit);
1437
1438 // check types
1439 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1440 // first time this local is assigned, so give it a type of the object stored in it
1441 emit->local_vtype[local_num] = vtype;
1442 } else if (emit->local_vtype[local_num] != vtype) {
1443 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001444 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 +01001445 }
1446}
1447
Damien George7ff996c2014-09-08 23:05:16 +01001448STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001449 // not implemented
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001450 (void)emit;
1451 (void)qst;
1452 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001453 assert(0);
1454}
1455
Damien George7ff996c2014-09-08 23:05:16 +01001456STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001457 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001458 vtype_kind_t vtype;
1459 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1460 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001461 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001462 emit_post(emit);
1463}
1464
Damien George7ff996c2014-09-08 23:05:16 +01001465STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001466 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001467 if (vtype == VTYPE_PYOBJ) {
1468 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1469 } else {
1470 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001471 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 +01001472 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001473 }
Damien George7ff996c2014-09-08 23:05:16 +01001474 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001475 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001476}
1477
Damien George7ff996c2014-09-08 23:05:16 +01001478STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001479 vtype_kind_t vtype_base, vtype_val;
1480 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1481 assert(vtype_base == VTYPE_PYOBJ);
1482 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001483 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001484 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001485}
1486
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001487STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001488 DEBUG_printf("store_subscr\n");
1489 // need to compile: base[index] = value
1490
1491 // pop: index, base, value
1492 // optimise case where index is an immediate
1493 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1494
1495 if (vtype_base == VTYPE_PYOBJ) {
1496 // standard Python call
1497 vtype_kind_t vtype_index, vtype_value;
1498 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1499 assert(vtype_index == VTYPE_PYOBJ);
1500 assert(vtype_value == VTYPE_PYOBJ);
1501 emit_call(emit, MP_F_OBJ_SUBSCR);
1502 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001503 // viper store
1504 // TODO The different machine architectures have very different
1505 // capabilities and requirements for stores, so probably best to
1506 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001507 stack_info_t *top = peek_stack(emit, 0);
1508 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1509 // index is an immediate
Damien George32444b72015-01-24 23:14:12 +00001510 mp_int_t index_value = top->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001511 emit_pre_pop_discard(emit); // discard index
1512 vtype_kind_t vtype_value;
1513 int reg_base = REG_ARG_1;
1514 int reg_index = REG_ARG_2;
1515 int reg_value = REG_ARG_3;
1516 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001517 #if N_X86
1518 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1519 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1520 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001521 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001522 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001523 switch (vtype_base) {
1524 case VTYPE_PTR8: {
1525 // pointer to 8-bit memory
1526 // TODO optimise to use thumb strb r1, [r2, r3]
1527 if (index_value != 0) {
1528 // index is non-zero
1529 #if N_THUMB
1530 if (index_value > 0 && index_value < 32) {
1531 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1532 break;
1533 }
1534 #endif
1535 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001536 #if N_ARM
1537 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1538 return;
1539 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001540 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1541 reg_base = reg_index;
1542 }
1543 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1544 break;
1545 }
1546 case VTYPE_PTR16: {
1547 // pointer to 16-bit memory
1548 if (index_value != 0) {
1549 // index is a non-zero immediate
1550 #if N_THUMB
1551 if (index_value > 0 && index_value < 32) {
1552 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1553 break;
1554 }
1555 #endif
1556 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001557 #if N_ARM
1558 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1559 return;
1560 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001561 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1562 reg_base = reg_index;
1563 }
1564 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1565 break;
1566 }
1567 default:
1568 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1569 }
1570 } else {
1571 // index is not an immediate
1572 vtype_kind_t vtype_index, vtype_value;
1573 int reg_index = REG_ARG_2;
1574 int reg_value = REG_ARG_3;
1575 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1576 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001577 #if N_X86
1578 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1579 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1580 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001581 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001582 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001583 switch (vtype_base) {
1584 case VTYPE_PTR8: {
1585 // pointer to 8-bit memory
1586 // TODO optimise to use thumb strb r1, [r2, r3]
1587 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001588 #if N_ARM
1589 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1590 break;
1591 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001592 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1593 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1594 break;
1595 }
1596 case VTYPE_PTR16: {
1597 // pointer to 16-bit memory
1598 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001599 #if N_ARM
1600 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1601 break;
1602 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001603 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1604 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1605 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1606 break;
1607 }
1608 default:
1609 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1610 }
1611 }
1612
1613 }
Damien13ed3a62013-10-08 09:05:10 +01001614}
1615
Damien George7ff996c2014-09-08 23:05:16 +01001616STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001617 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001618 // could support for Python types, just set to None (so GC can reclaim it)
Paul Sokolovskydb1ac362015-01-01 09:43:42 +02001619 // local is automatically deleted for exception block "as" var, and the message
1620 // breaks tests.
1621 //mp_emitter_warning(emit->pass, "Native codegeneration doesn't support deleting local");
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001622 (void)emit;
1623 (void)qst;
1624 (void)local_num;
Damien13ed3a62013-10-08 09:05:10 +01001625}
1626
Damien George7ff996c2014-09-08 23:05:16 +01001627STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001628 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001629 (void)emit;
1630 (void)qst;
1631 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001632}
1633
Damien Georgee6ce10a2014-09-06 18:38:20 +01001634STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1635 emit_native_pre(emit);
1636 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1637 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001638}
1639
Damien Georgee6ce10a2014-09-06 18:38:20 +01001640STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1641 emit_native_pre(emit);
1642 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1643 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001644}
1645
Damien Georgee6ce10a2014-09-06 18:38:20 +01001646STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001647 vtype_kind_t vtype_base;
1648 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1649 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001650 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 +01001651 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001652}
1653
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001654STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001655 vtype_kind_t vtype_index, vtype_base;
1656 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1657 assert(vtype_index == VTYPE_PYOBJ);
1658 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001659 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 +01001660}
1661
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001662STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001663 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001664 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001665 int reg = REG_TEMP0;
1666 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1667 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001668}
1669
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001670STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001671 vtype_kind_t vtype0, vtype1;
1672 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1673 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1674}
1675
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001676STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001677 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001678 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001679 emit_post(emit);
1680}
1681
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001682STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001683 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001684 vtype_kind_t vtype0, vtype1;
1685 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1686 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001687}
1688
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001689STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001690 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001691 vtype_kind_t vtype0, vtype1, vtype2;
1692 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1693 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1694}
1695
Damien George7ff996c2014-09-08 23:05:16 +01001696STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001697 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001698 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001699 // need to commit stack because we are jumping elsewhere
1700 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001701 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001702 emit_post(emit);
1703}
1704
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001705STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001706 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001707 switch (vtype) {
1708 case VTYPE_PYOBJ:
1709 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1710 if (!pop) {
1711 adjust_stack(emit, 1);
1712 }
1713 emit_call(emit, MP_F_OBJ_IS_TRUE);
1714 break;
1715 case VTYPE_BOOL:
1716 case VTYPE_INT:
1717 case VTYPE_UINT:
1718 emit_pre_pop_reg(emit, &vtype, REG_RET);
1719 if (!pop) {
1720 adjust_stack(emit, 1);
1721 }
1722 break;
1723 default:
1724 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1725 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001726 }
Damien George21ca2d72014-10-19 19:00:51 +01001727 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1728 // can use it. This is a bit of a hack.
1729 if (!pop) {
1730 emit->saved_stack_vtype = vtype;
1731 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001732 // need to commit stack because we may jump elsewhere
1733 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001734}
1735
Damien George63f38322015-02-28 15:04:06 +00001736STATIC void emit_native_pop_jump_if(emit_t *emit, bool cond, mp_uint_t label) {
1737 DEBUG_printf("pop_jump_if(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001738 emit_native_jump_helper(emit, true);
Damien George63f38322015-02-28 15:04:06 +00001739 if (cond) {
1740 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1741 } else {
1742 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1743 }
Damien1a6633a2013-11-03 13:58:19 +00001744 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001745}
Damien1a6633a2013-11-03 13:58:19 +00001746
Damien George63f38322015-02-28 15:04:06 +00001747STATIC void emit_native_jump_if_or_pop(emit_t *emit, bool cond, mp_uint_t label) {
1748 DEBUG_printf("jump_if_or_pop(cond=%u, label=" UINT_FMT ")\n", cond, label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001749 emit_native_jump_helper(emit, false);
Damien George63f38322015-02-28 15:04:06 +00001750 if (cond) {
1751 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
1752 } else {
1753 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
1754 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001755 adjust_stack(emit, -1);
1756 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001757}
1758
Damien George7ff996c2014-09-08 23:05:16 +01001759STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001760 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001761 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001762}
Damien Georgea32c1e42014-05-07 18:30:52 +01001763
Damien George7ff996c2014-09-08 23:05:16 +01001764STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001765 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001766 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001767}
Damien Georgea32c1e42014-05-07 18:30:52 +01001768
Damien George7ff996c2014-09-08 23:05:16 +01001769STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001770 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001771 (void)emit;
1772 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001773 assert(0);
1774}
Damien Georgeb601d952014-06-30 05:17:25 +01001775
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001776STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001777 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001778 assert(0);
1779}
Damien Georgeb601d952014-06-30 05:17:25 +01001780
Damien George7ff996c2014-09-08 23:05:16 +01001781STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001782 emit_native_pre(emit);
1783 // need to commit stack because we may jump elsewhere
1784 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001785 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 +01001786 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001787 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001788 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001789}
Damien Georgeb601d952014-06-30 05:17:25 +01001790
Damien George7ff996c2014-09-08 23:05:16 +01001791STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001792 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001793}
Damien Georgeb601d952014-06-30 05:17:25 +01001794
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001795STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgeb6e6b522015-01-21 17:00:01 +00001796 // logic:
1797 // exc = pop_stack
1798 // if exc == None: pass
1799 // else: raise exc
1800 // the check if exc is None is done in the MP_F_NATIVE_RAISE stub
1801 vtype_kind_t vtype;
1802 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1803 emit_call(emit, MP_F_NATIVE_RAISE);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001804 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001805}
Damiend2755ec2013-10-16 23:58:48 +01001806
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001807STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001808 // perhaps the difficult one, as we want to rewrite for loops using native code
1809 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001810
1811 vtype_kind_t vtype;
1812 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1813 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001814 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001815 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001816}
Damiend2755ec2013-10-16 23:58:48 +01001817
Damien George7ff996c2014-09-08 23:05:16 +01001818STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001819 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001820 vtype_kind_t vtype;
1821 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1822 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001823 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001824 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1825 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001826 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1827}
1828
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001829STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001830 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001831 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001832 adjust_stack(emit, -1);
1833 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001834}
1835
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001836STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001837 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001838 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001839 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001840 emit_post(emit);
1841}
1842
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001843STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001844 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001845 /*
1846 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001847 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001848 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001849 emit_post(emit);
1850 */
Damien13ed3a62013-10-08 09:05:10 +01001851}
1852
Damien Georged17926d2014-03-30 13:35:08 +01001853STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001854 vtype_kind_t vtype;
1855 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1856 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001857 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001858 // we need to synthesise this operation by converting to bool first
1859 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001860 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001861 }
Damien Georged6230f62014-09-23 14:10:03 +00001862 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1863 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001864}
1865
Damien Georged17926d2014-03-30 13:35:08 +01001866STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001867 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001868 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1869 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001870 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001871 #if N_X64 || N_X86
1872 // special cases for x86 and shifting
1873 if (op == MP_BINARY_OP_LSHIFT
1874 || op == MP_BINARY_OP_INPLACE_LSHIFT
1875 || op == MP_BINARY_OP_RSHIFT
1876 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1877 #if N_X64
1878 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1879 #else
1880 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1881 #endif
1882 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1883 ASM_LSL_REG(emit->as, REG_RET);
1884 } else {
1885 ASM_ASR_REG(emit->as, REG_RET);
1886 }
1887 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1888 return;
1889 }
1890 #endif
1891 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001892 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001893 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1894 if (0) {
1895 // dummy
1896 #if !(N_X64 || N_X86)
1897 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1898 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001899 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001900 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1901 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1902 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1903 #endif
Damien George1ef23482014-10-12 14:21:06 +01001904 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1905 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1906 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1907 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1908 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1909 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1910 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1911 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1912 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001913 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1914 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1915 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1916 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1917 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1918 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1919 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1920 // comparison ops are (in enum order):
1921 // MP_BINARY_OP_LESS
1922 // MP_BINARY_OP_MORE
1923 // MP_BINARY_OP_EQUAL
1924 // MP_BINARY_OP_LESS_EQUAL
1925 // MP_BINARY_OP_MORE_EQUAL
1926 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001927 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001928 #if N_X64
1929 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1930 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1931 static byte ops[6] = {
1932 ASM_X64_CC_JL,
1933 ASM_X64_CC_JG,
1934 ASM_X64_CC_JE,
1935 ASM_X64_CC_JLE,
1936 ASM_X64_CC_JGE,
1937 ASM_X64_CC_JNE,
1938 };
1939 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1940 #elif N_X86
1941 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1942 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1943 static byte ops[6] = {
1944 ASM_X86_CC_JL,
1945 ASM_X86_CC_JG,
1946 ASM_X86_CC_JE,
1947 ASM_X86_CC_JLE,
1948 ASM_X86_CC_JGE,
1949 ASM_X86_CC_JNE,
1950 };
1951 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1952 #elif N_THUMB
1953 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1954 static uint16_t ops[6] = {
1955 ASM_THUMB_OP_ITE_GE,
1956 ASM_THUMB_OP_ITE_GT,
1957 ASM_THUMB_OP_ITE_EQ,
1958 ASM_THUMB_OP_ITE_GT,
1959 ASM_THUMB_OP_ITE_GE,
1960 ASM_THUMB_OP_ITE_EQ,
1961 };
1962 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1963 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1964 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1965 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1966 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001967 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1968 static uint ccs[6] = {
1969 ASM_ARM_CC_LT,
1970 ASM_ARM_CC_GT,
1971 ASM_ARM_CC_EQ,
1972 ASM_ARM_CC_LE,
1973 ASM_ARM_CC_GE,
1974 ASM_ARM_CC_NE,
1975 };
1976 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001977 #else
1978 #error not implemented
1979 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001980 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1981 } else {
1982 // TODO other ops not yet implemented
1983 assert(0);
1984 }
Damien13ed3a62013-10-08 09:05:10 +01001985 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001986 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001987 bool invert = false;
1988 if (op == MP_BINARY_OP_NOT_IN) {
1989 invert = true;
1990 op = MP_BINARY_OP_IN;
1991 } else if (op == MP_BINARY_OP_IS_NOT) {
1992 invert = true;
1993 op = MP_BINARY_OP_IS;
1994 }
Damien George7fe21912014-08-16 22:31:57 +01001995 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001996 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001997 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001998 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1999 }
Damien13ed3a62013-10-08 09:05:10 +01002000 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2001 } else {
2002 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002003 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002004 }
2005}
2006
Damien George7ff996c2014-09-08 23:05:16 +01002007STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002008 // for viper: call runtime, with types of args
2009 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002010 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002011 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002012 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002013 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002014}
2015
Damien George7ff996c2014-09-08 23:05:16 +01002016STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002017 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002018 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002019 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002020 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2021}
2022
Damien George7ff996c2014-09-08 23:05:16 +01002023STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002024 // only used in list comprehension
2025 vtype_kind_t vtype_list, vtype_item;
2026 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2027 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2028 assert(vtype_list == VTYPE_PYOBJ);
2029 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002030 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002031 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002032}
2033
Damien George7ff996c2014-09-08 23:05:16 +01002034STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002035 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002036 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002037 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2038}
2039
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002040STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002041 vtype_kind_t vtype_key, vtype_value, vtype_map;
2042 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
2043 assert(vtype_key == VTYPE_PYOBJ);
2044 assert(vtype_value == VTYPE_PYOBJ);
2045 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002046 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002047 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2048}
2049
Damien George7ff996c2014-09-08 23:05:16 +01002050STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002051 // only used in list comprehension
2052 vtype_kind_t vtype_map, vtype_key, vtype_value;
2053 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2054 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2055 assert(vtype_map == VTYPE_PYOBJ);
2056 assert(vtype_key == VTYPE_PYOBJ);
2057 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002058 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002059 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002060}
2061
Damien Georgee37dcaa2014-12-27 17:07:16 +00002062#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002063STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002064 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002065 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002066 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002067 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2068}
2069
Damien George7ff996c2014-09-08 23:05:16 +01002070STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002071 // only used in set comprehension
2072 vtype_kind_t vtype_set, vtype_item;
2073 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2074 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2075 assert(vtype_set == VTYPE_PYOBJ);
2076 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002077 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002078 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002079}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002080#endif
Damiend2755ec2013-10-16 23:58:48 +01002081
Damien George83204f32014-12-27 17:20:41 +00002082#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002083STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002084 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002085 if (n_args == 2) {
2086 vtype_kind_t vtype_start, vtype_stop;
2087 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2088 assert(vtype_start == VTYPE_PYOBJ);
2089 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002090 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 +01002091 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2092 } else {
2093 assert(n_args == 3);
2094 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2095 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
2096 assert(vtype_start == VTYPE_PYOBJ);
2097 assert(vtype_stop == VTYPE_PYOBJ);
2098 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002099 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002100 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2101 }
Damien13ed3a62013-10-08 09:05:10 +01002102}
Damien George83204f32014-12-27 17:20:41 +00002103#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002104
Damien George7ff996c2014-09-08 23:05:16 +01002105STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002106 DEBUG_printf("unpack_sequence %d\n", n_args);
2107 vtype_kind_t vtype_base;
2108 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2109 assert(vtype_base == VTYPE_PYOBJ);
2110 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002111 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002112}
Damien Georgecdd96df2014-04-06 12:58:40 +01002113
Damien George7ff996c2014-09-08 23:05:16 +01002114STATIC 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 +01002115 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2116 vtype_kind_t vtype_base;
2117 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2118 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002119 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 +01002120 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 +01002121}
2122
Damien George7ff996c2014-09-08 23:05:16 +01002123STATIC 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 +01002124 // 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 +00002125 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002126 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002127 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 +01002128 } else {
2129 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2130 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2131 assert(vtype_def_tuple == VTYPE_PYOBJ);
2132 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002133 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 +01002134 }
Damien13ed3a62013-10-08 09:05:10 +01002135 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2136}
2137
Damien George7ff996c2014-09-08 23:05:16 +01002138STATIC 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 +00002139 (void)emit;
2140 (void)scope;
2141 (void)n_closed_over;
2142 (void)n_pos_defaults;
2143 (void)n_kw_defaults;
Damien13ed3a62013-10-08 09:05:10 +01002144 assert(0);
2145}
2146
Damien George7ff996c2014-09-08 23:05:16 +01002147STATIC 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 +00002148 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2149
Damien Georgee9dac3b2014-09-29 22:10:41 +01002150 // TODO: in viper mode, call special runtime routine with type info for args,
2151 // and wanted type info for return, to remove need for boxing/unboxing
2152
Damien George922ddd62014-04-09 12:43:17 +01002153 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002154
Damien Georgee9dac3b2014-09-29 22:10:41 +01002155 emit_native_pre(emit);
2156 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2157 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2158 // casting operator
2159 assert(n_positional == 1 && n_keyword == 0);
2160 DEBUG_printf(" cast to %d\n", vtype_fun);
Damien George32444b72015-01-24 23:14:12 +00002161 vtype_kind_t vtype_cast = peek_stack(emit, 1)->data.u_imm;
Damien Georgee9dac3b2014-09-29 22:10:41 +01002162 switch (peek_vtype(emit, 0)) {
2163 case VTYPE_PYOBJ: {
2164 vtype_kind_t vtype;
2165 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2166 emit_pre_pop_discard(emit);
2167 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2168 emit_post_push_reg(emit, vtype_cast, REG_RET);
2169 break;
2170 }
2171 case VTYPE_BOOL:
2172 case VTYPE_INT:
2173 case VTYPE_UINT:
2174 case VTYPE_PTR:
2175 case VTYPE_PTR8:
2176 case VTYPE_PTR16:
2177 case VTYPE_PTR_NONE:
2178 emit_fold_stack_top(emit, REG_ARG_1);
2179 emit_post_top_set_vtype(emit, vtype_cast);
2180 break;
2181 default:
2182 assert(!"TODO: convert obj to int");
2183 }
2184 } else {
2185 if (n_positional != 0 || n_keyword != 0) {
2186 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2187 }
Damien13ed3a62013-10-08 09:05:10 +01002188 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2189 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002190 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2191 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002192 }
Damien13ed3a62013-10-08 09:05:10 +01002193}
2194
Damien George7ff996c2014-09-08 23:05:16 +01002195STATIC 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 +01002196 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002197 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002198 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 +01002199 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 +01002200 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2201}
2202
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002203STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002204 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002205 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002206 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2207 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002208 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002209 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002210 } else {
2211 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002212 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002213 } else {
2214 vtype_kind_t vtype;
2215 emit_pre_pop_reg(emit, &vtype, REG_RET);
2216 if (vtype != emit->return_vtype) {
2217 printf("ViperTypeError: incompatible return type\n");
2218 }
Damien George2ac4af62014-08-15 16:45:41 +01002219 }
Damien13ed3a62013-10-08 09:05:10 +01002220 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002221 vtype_kind_t vtype;
2222 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002223 assert(vtype == VTYPE_PYOBJ);
2224 }
2225 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002226 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2227 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002228}
2229
Damien George7ff996c2014-09-08 23:05:16 +01002230STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002231 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002232 vtype_kind_t vtype_exc;
2233 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2234 if (vtype_exc != VTYPE_PYOBJ) {
2235 printf("ViperTypeError: must raise an object\n");
2236 }
2237 // 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 +01002238 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002239}
Damien Georgeb601d952014-06-30 05:17:25 +01002240
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002241STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002242 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002243 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002244 assert(0);
2245}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002246STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002247 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002248 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002249 assert(0);
2250}
2251
Damien Georgeb601d952014-06-30 05:17:25 +01002252STATIC void emit_native_start_except_handler(emit_t *emit) {
2253 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2254 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2255 // the first 2 elements, so we can get the thrown value.
2256 adjust_stack(emit, 2);
2257 vtype_kind_t vtype_nlr;
2258 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002259 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002260 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
2261}
2262
2263STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002264 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002265}
2266
Damien13ed3a62013-10-08 09:05:10 +01002267const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002268 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002269 emit_native_start_pass,
2270 emit_native_end_pass,
2271 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002272 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002273 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002274
Damien George542bd6b2015-03-26 14:42:40 +00002275 {
2276 emit_native_load_fast,
2277 emit_native_load_deref,
2278 emit_native_load_name,
2279 emit_native_load_global,
2280 },
2281 {
2282 emit_native_store_fast,
2283 emit_native_store_deref,
2284 emit_native_store_name,
2285 emit_native_store_global,
2286 },
2287 {
2288 emit_native_delete_fast,
2289 emit_native_delete_deref,
2290 emit_native_delete_name,
2291 emit_native_delete_global,
2292 },
Damien13ed3a62013-10-08 09:05:10 +01002293
2294 emit_native_label_assign,
2295 emit_native_import_name,
2296 emit_native_import_from,
2297 emit_native_import_star,
2298 emit_native_load_const_tok,
2299 emit_native_load_const_small_int,
Damien13ed3a62013-10-08 09:05:10 +01002300 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002301 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002302 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002303 emit_native_load_attr,
2304 emit_native_load_method,
2305 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002306 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002307 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002308 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002309 emit_native_delete_attr,
2310 emit_native_delete_subscr,
2311 emit_native_dup_top,
2312 emit_native_dup_top_two,
2313 emit_native_pop_top,
2314 emit_native_rot_two,
2315 emit_native_rot_three,
2316 emit_native_jump,
Damien George63f38322015-02-28 15:04:06 +00002317 emit_native_pop_jump_if,
2318 emit_native_jump_if_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002319 emit_native_break_loop,
2320 emit_native_continue_loop,
2321 emit_native_setup_with,
2322 emit_native_with_cleanup,
2323 emit_native_setup_except,
2324 emit_native_setup_finally,
2325 emit_native_end_finally,
2326 emit_native_get_iter,
2327 emit_native_for_iter,
2328 emit_native_for_iter_end,
2329 emit_native_pop_block,
2330 emit_native_pop_except,
2331 emit_native_unary_op,
2332 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002333 emit_native_build_tuple,
2334 emit_native_build_list,
2335 emit_native_list_append,
2336 emit_native_build_map,
2337 emit_native_store_map,
2338 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002339 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002340 emit_native_build_set,
2341 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002342 #endif
Damien George83204f32014-12-27 17:20:41 +00002343 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002344 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002345 #endif
Damien13ed3a62013-10-08 09:05:10 +01002346 emit_native_unpack_sequence,
2347 emit_native_unpack_ex,
2348 emit_native_make_function,
2349 emit_native_make_closure,
2350 emit_native_call_function,
2351 emit_native_call_method,
2352 emit_native_return_value,
2353 emit_native_raise_varargs,
2354 emit_native_yield_value,
2355 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002356
2357 emit_native_start_except_handler,
2358 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002359};
2360
Damien Georgec90f59e2014-09-06 23:06:36 +01002361#endif