blob: c2fe951be879e9014ead3c21c4b54f76aa10287a [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
xbeefe34222014-03-16 00:14:26 -070045#include <stdbool.h>
Damien13ed3a62013-10-08 09:05:10 +010046#include <stdint.h>
47#include <stdio.h>
48#include <string.h>
49#include <assert.h>
50
Damiend99b0522013-12-21 18:17:45 +000051#include "mpconfig.h"
Damien Georgeb601d952014-06-30 05:17:25 +010052#include "nlr.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030053#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000054#include "qstr.h"
Damien13ed3a62013-10-08 09:05:10 +010055#include "lexer.h"
Damien13ed3a62013-10-08 09:05:10 +010056#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010057#include "obj.h"
58#include "emitglue.h"
Damien13ed3a62013-10-08 09:05:10 +010059#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000060#include "runtime0.h"
Damien13ed3a62013-10-08 09:05:10 +010061#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000062#include "runtime.h"
Damien13ed3a62013-10-08 09:05:10 +010063
Damien Georgecdd96df2014-04-06 12:58:40 +010064#if 0 // print debugging info
65#define DEBUG_PRINT (1)
66#define DEBUG_printf DEBUG_printf
67#else // don't print debugging info
68#define DEBUG_printf(...) (void)0
69#endif
70
Damien13ed3a62013-10-08 09:05:10 +010071// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010072#if (MICROPY_EMIT_X64 && N_X64) \
73 || (MICROPY_EMIT_X86 && N_X86) \
74 || (MICROPY_EMIT_THUMB && N_THUMB) \
75 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010076
Damien3ef4abb2013-10-12 16:53:13 +010077#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010078
79// x64 specific stuff
80
81#include "asmx64.h"
82
Damien13ed3a62013-10-08 09:05:10 +010083#define EXPORT_FUN(name) emit_native_x64_##name
84
Damien George0b610de2014-09-29 16:25:04 +010085#define REG_RET ASM_X64_REG_RAX
86#define REG_ARG_1 ASM_X64_REG_RDI
87#define REG_ARG_2 ASM_X64_REG_RSI
88#define REG_ARG_3 ASM_X64_REG_RDX
89#define REG_ARG_4 ASM_X64_REG_RCX
Damien Georgec90f59e2014-09-06 23:06:36 +010090
Damien George81057362014-09-07 01:06:19 +010091// caller-save
Damien George0b610de2014-09-29 16:25:04 +010092#define REG_TEMP0 ASM_X64_REG_RAX
93#define REG_TEMP1 ASM_X64_REG_RDI
94#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010095
96// callee-save
Damien George0b610de2014-09-29 16:25:04 +010097#define REG_LOCAL_1 ASM_X64_REG_RBX
98#define REG_LOCAL_2 ASM_X64_REG_R12
99#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +0100100#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100101
102#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
103#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
104
105#define ASM_T asm_x64_t
106#define ASM_NEW asm_x64_new
107#define ASM_FREE asm_x64_free
108#define ASM_GET_CODE asm_x64_get_code
109#define ASM_GET_CODE_SIZE asm_x64_get_code_size
110#define ASM_START_PASS asm_x64_start_pass
111#define ASM_END_PASS asm_x64_end_pass
112#define ASM_ENTRY asm_x64_entry
113#define ASM_EXIT asm_x64_exit
114
115#define ASM_LABEL_ASSIGN asm_x64_label_assign
116#define ASM_JUMP asm_x64_jmp_label
117#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
118 do { \
119 asm_x64_test_r8_with_r8(as, reg, reg); \
120 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
121 } while (0)
122#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
123 do { \
124 asm_x64_test_r8_with_r8(as, reg, reg); \
125 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
126 } while (0)
127#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
128 do { \
129 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
130 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
131 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100132#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100133
134#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
135#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
136#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
137#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
138 do { \
139 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
140 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
141 } while (false)
142#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100143#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 +0100144#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
145
Damien George3112cde2014-09-29 18:45:42 +0100146#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
147#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100148#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
149#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
150#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 +0100151#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
152#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
153
Damien George91cfd412014-10-12 16:59:29 +0100154#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
155#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
156#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
157
158#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
159#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
160#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 +0100161
Damien Georgec90f59e2014-09-06 23:06:36 +0100162#elif N_X86
163
164// x86 specific stuff
165
166#include "asmx86.h"
167
168STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
169 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
170 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
171 [MP_F_LOAD_CONST_INT] = 1,
172 [MP_F_LOAD_CONST_DEC] = 1,
173 [MP_F_LOAD_CONST_STR] = 1,
174 [MP_F_LOAD_CONST_BYTES] = 1,
175 [MP_F_LOAD_NAME] = 1,
176 [MP_F_LOAD_GLOBAL] = 1,
177 [MP_F_LOAD_BUILD_CLASS] = 0,
178 [MP_F_LOAD_ATTR] = 2,
179 [MP_F_LOAD_METHOD] = 3,
180 [MP_F_STORE_NAME] = 2,
181 [MP_F_STORE_GLOBAL] = 2,
182 [MP_F_STORE_ATTR] = 3,
183 [MP_F_OBJ_SUBSCR] = 3,
184 [MP_F_OBJ_IS_TRUE] = 1,
185 [MP_F_UNARY_OP] = 2,
186 [MP_F_BINARY_OP] = 3,
187 [MP_F_BUILD_TUPLE] = 2,
188 [MP_F_BUILD_LIST] = 2,
189 [MP_F_LIST_APPEND] = 2,
190 [MP_F_BUILD_MAP] = 1,
191 [MP_F_STORE_MAP] = 3,
192#if MICROPY_PY_BUILTINS_SET
193 [MP_F_BUILD_SET] = 2,
194 [MP_F_STORE_SET] = 2,
195#endif
196 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
197 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
198 [MP_F_CALL_METHOD_N_KW] = 3,
199 [MP_F_GETITER] = 1,
200 [MP_F_ITERNEXT] = 1,
201 [MP_F_NLR_PUSH] = 1,
202 [MP_F_NLR_POP] = 0,
203 [MP_F_NATIVE_RAISE] = 1,
204 [MP_F_IMPORT_NAME] = 3,
205 [MP_F_IMPORT_FROM] = 2,
206 [MP_F_IMPORT_ALL] = 1,
207#if MICROPY_PY_BUILTINS_SLICE
208 [MP_F_NEW_SLICE] = 3,
209#endif
210 [MP_F_UNPACK_SEQUENCE] = 3,
211 [MP_F_UNPACK_EX] = 3,
212 [MP_F_DELETE_NAME] = 1,
213 [MP_F_DELETE_GLOBAL] = 1,
214};
215
216#define EXPORT_FUN(name) emit_native_x86_##name
217
Damien George0b610de2014-09-29 16:25:04 +0100218#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000219#define REG_ARG_1 ASM_X86_REG_ARG_1
220#define REG_ARG_2 ASM_X86_REG_ARG_2
221#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100222
Damien George25d90412014-09-06 23:24:32 +0000223// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100224#define REG_TEMP0 ASM_X86_REG_EAX
225#define REG_TEMP1 ASM_X86_REG_ECX
226#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100227
Damien George25d90412014-09-06 23:24:32 +0000228// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100229#define REG_LOCAL_1 ASM_X86_REG_EBX
230#define REG_LOCAL_2 ASM_X86_REG_ESI
231#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000232#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100233
234#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
235#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
236
237#define ASM_T asm_x86_t
238#define ASM_NEW asm_x86_new
239#define ASM_FREE asm_x86_free
240#define ASM_GET_CODE asm_x86_get_code
241#define ASM_GET_CODE_SIZE asm_x86_get_code_size
242#define ASM_START_PASS asm_x86_start_pass
243#define ASM_END_PASS asm_x86_end_pass
244#define ASM_ENTRY asm_x86_entry
245#define ASM_EXIT asm_x86_exit
246
247#define ASM_LABEL_ASSIGN asm_x86_label_assign
248#define ASM_JUMP asm_x86_jmp_label
249#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
250 do { \
251 asm_x86_test_r8_with_r8(as, reg, reg); \
252 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
253 } while (0)
254#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
255 do { \
256 asm_x86_test_r8_with_r8(as, reg, reg); \
257 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
258 } while (0)
259#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
260 do { \
261 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
262 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
263 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100264#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 +0100265
266#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
267#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
268#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
269#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
270 do { \
271 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
272 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
273 } while (false)
274#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100275#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 +0100276#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100277
Damien George3112cde2014-09-29 18:45:42 +0100278#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
279#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100280#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
281#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
282#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 +0100283#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
284#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
285
Damien George91cfd412014-10-12 16:59:29 +0100286#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 +0000287#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
288#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 +0100289
290#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
291#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
292#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 +0100293
Damien3ef4abb2013-10-12 16:53:13 +0100294#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100295
296// thumb specific stuff
297
298#include "asmthumb.h"
299
Damien13ed3a62013-10-08 09:05:10 +0100300#define EXPORT_FUN(name) emit_native_thumb_##name
301
Damien George0b610de2014-09-29 16:25:04 +0100302#define REG_RET ASM_THUMB_REG_R0
303#define REG_ARG_1 ASM_THUMB_REG_R0
304#define REG_ARG_2 ASM_THUMB_REG_R1
305#define REG_ARG_3 ASM_THUMB_REG_R2
306#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100307
Damien George0b610de2014-09-29 16:25:04 +0100308#define REG_TEMP0 ASM_THUMB_REG_R0
309#define REG_TEMP1 ASM_THUMB_REG_R1
310#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100311
Damien George0b610de2014-09-29 16:25:04 +0100312#define REG_LOCAL_1 ASM_THUMB_REG_R4
313#define REG_LOCAL_2 ASM_THUMB_REG_R5
314#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100315#define REG_LOCAL_NUM (3)
316
317#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
318#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
319
320#define ASM_T asm_thumb_t
321#define ASM_NEW asm_thumb_new
322#define ASM_FREE asm_thumb_free
323#define ASM_GET_CODE asm_thumb_get_code
324#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
325#define ASM_START_PASS asm_thumb_start_pass
326#define ASM_END_PASS asm_thumb_end_pass
327#define ASM_ENTRY asm_thumb_entry
328#define ASM_EXIT asm_thumb_exit
329
330#define ASM_LABEL_ASSIGN asm_thumb_label_assign
331#define ASM_JUMP asm_thumb_b_label
332#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
333 do { \
334 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100335 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100336 } while (0)
337#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
338 do { \
339 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100340 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100341 } while (0)
342#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
343 do { \
344 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100345 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100346 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100347#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 +0100348
349#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
350#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
351#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
352#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
353 do { \
354 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
355 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
356 } while (false)
357#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 +0100358#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 +0100359#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 +0100360
Damien George3112cde2014-09-29 18:45:42 +0100361#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
362#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 +0100363#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
364#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
365#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 +0100366#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
367#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
368
Damien George91cfd412014-10-12 16:59:29 +0100369#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
370#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
371#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
372
Damien Georgee9dac3b2014-09-29 22:10:41 +0100373#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
374#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
375#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
376
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200377#elif N_ARM
378
379// ARM specific stuff
380
381#include "asmarm.h"
382
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200383#define EXPORT_FUN(name) emit_native_arm_##name
384
Damien George0b610de2014-09-29 16:25:04 +0100385#define REG_RET ASM_ARM_REG_R0
386#define REG_ARG_1 ASM_ARM_REG_R0
387#define REG_ARG_2 ASM_ARM_REG_R1
388#define REG_ARG_3 ASM_ARM_REG_R2
389#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100390
Damien George0b610de2014-09-29 16:25:04 +0100391#define REG_TEMP0 ASM_ARM_REG_R0
392#define REG_TEMP1 ASM_ARM_REG_R1
393#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100394
Damien George0b610de2014-09-29 16:25:04 +0100395#define REG_LOCAL_1 ASM_ARM_REG_R4
396#define REG_LOCAL_2 ASM_ARM_REG_R5
397#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100398#define REG_LOCAL_NUM (3)
399
400#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
401#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
402
403#define ASM_T asm_arm_t
404#define ASM_NEW asm_arm_new
405#define ASM_FREE asm_arm_free
406#define ASM_GET_CODE asm_arm_get_code
407#define ASM_GET_CODE_SIZE asm_arm_get_code_size
408#define ASM_START_PASS asm_arm_start_pass
409#define ASM_END_PASS asm_arm_end_pass
410#define ASM_ENTRY asm_arm_entry
411#define ASM_EXIT asm_arm_exit
412
413#define ASM_LABEL_ASSIGN asm_arm_label_assign
414#define ASM_JUMP asm_arm_b_label
415#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
416 do { \
417 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100418 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100419 } while (0)
420#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
421 do { \
422 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100423 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100424 } while (0)
425#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
426 do { \
427 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100428 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100429 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100430#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 +0100431
432#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
433#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
434#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
435#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
436 do { \
437 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
438 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
439 } while (false)
440#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 +0100441#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 +0100442#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
443
Fabian Vogte5268962014-10-04 00:53:46 +0200444#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
445#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 +0100446#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
447#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
448#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 +0100449#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
450#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
451
Damien George91cfd412014-10-12 16:59:29 +0100452#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base))
453#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
454#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
455
Fabian Vogte5268962014-10-04 00:53:46 +0200456#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
457#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
458#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 +0100459
Damien Georgec90f59e2014-09-06 23:06:36 +0100460#else
461
462#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200463
Damien13ed3a62013-10-08 09:05:10 +0100464#endif
465
466typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100467 STACK_VALUE,
468 STACK_REG,
469 STACK_IMM,
470} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100471
Damien Georgee9dac3b2014-09-29 22:10:41 +0100472// these enums must be distinct and the bottom 2 bits
473// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100474typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100475 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
476 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
477 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
478 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
479
480 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
481 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
482 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
483 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
484
485 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
486 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100487} vtype_kind_t;
488
Damienff8ed772013-10-08 22:18:32 +0100489typedef struct _stack_info_t {
490 vtype_kind_t vtype;
491 stack_info_kind_t kind;
492 union {
493 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100494 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100495 };
496} stack_info_t;
497
Damien13ed3a62013-10-08 09:05:10 +0100498struct _emit_t {
499 int pass;
500
501 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100502
Damien George2ac4af62014-08-15 16:45:41 +0100503 vtype_kind_t return_vtype;
504
Damien George7ff996c2014-09-08 23:05:16 +0100505 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100506 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100507
Damien George7ff996c2014-09-08 23:05:16 +0100508 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100509 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100510 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100511
Damien13ed3a62013-10-08 09:05:10 +0100512 int stack_start;
513 int stack_size;
514
515 bool last_emit_was_return_value;
516
Damien13ed3a62013-10-08 09:05:10 +0100517 scope_t *scope;
518
Damien Georgec90f59e2014-09-06 23:06:36 +0100519 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100520};
521
Damien George7ff996c2014-09-08 23:05:16 +0100522emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100523 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100524 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100525 return emit;
526}
527
Damien George41d02b62014-01-24 22:42:28 +0000528void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100529 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100530 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
531 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200532 m_del_obj(emit_t, emit);
533}
534
Damien George2ac4af62014-08-15 16:45:41 +0100535STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
536 switch (op) {
537 case MP_EMIT_NATIVE_TYPE_ENABLE:
538 emit->do_viper_types = arg1;
539 break;
540
541 default: {
542 vtype_kind_t type;
543 switch (arg2) {
544 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
545 case MP_QSTR_bool: type = VTYPE_BOOL; break;
546 case MP_QSTR_int: type = VTYPE_INT; break;
547 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100548 case MP_QSTR_ptr: type = VTYPE_PTR; break;
549 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
550 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100551 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
552 }
553 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
554 emit->return_vtype = type;
555 } else {
556 assert(arg1 < emit->local_vtype_alloc);
557 emit->local_vtype[arg1] = type;
558 }
559 break;
560 }
561 }
Damien13ed3a62013-10-08 09:05:10 +0100562}
563
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200564STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000565 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
566
Damien13ed3a62013-10-08 09:05:10 +0100567 emit->pass = pass;
568 emit->stack_start = 0;
569 emit->stack_size = 0;
570 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100571 emit->scope = scope;
572
Damien George36db6bc2014-05-07 17:24:22 +0100573 // allocate memory for keeping track of the types of locals
574 if (emit->local_vtype_alloc < scope->num_locals) {
575 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
576 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100577 }
Damien George36db6bc2014-05-07 17:24:22 +0100578
579 // allocate memory for keeping track of the objects on the stack
580 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100581 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100582 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100583 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100584 }
585
Damien George2ac4af62014-08-15 16:45:41 +0100586 // set default type for return and arguments
587 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100588 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100589 emit->local_vtype[i] = VTYPE_PYOBJ;
590 }
Damien Georgea5190a72014-08-15 22:39:08 +0100591
592 // local variables begin unbound, and have unknown type
593 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
594 emit->local_vtype[i] = VTYPE_UNBOUND;
595 }
596
597 // values on stack begin unbound
598 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100599 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100600 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100601 }
602
Damien Georgec90f59e2014-09-06 23:06:36 +0100603 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100604
605 // entry to function
606 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100607 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100608 num_locals = scope->num_locals - REG_LOCAL_NUM;
609 if (num_locals < 0) {
610 num_locals = 0;
611 }
612 emit->stack_start = num_locals;
613 num_locals += scope->stack_size;
614 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100615 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100616
617 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100618#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100619 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100620 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100621 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100622 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100623 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100624 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100625 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100626 } else if (i == 3) {
627 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100628 } else {
629 // TODO not implemented
630 assert(0);
631 }
632 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100633#elif N_X86
634 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100635 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000636 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100637 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000638 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000639 } else if (i == 2) {
640 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100641 } else {
Damien George03281b32014-09-06 22:38:50 +0000642 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
643 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100644 }
645 }
Damien3ef4abb2013-10-12 16:53:13 +0100646#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100647 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100648 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100649 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100650 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100651 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100652 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100653 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100654 } else if (i == 3) {
655 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
656 } else {
657 // TODO not implemented
658 assert(0);
659 }
660 }
661
Damien Georgee9dac3b2014-09-29 22:10:41 +0100662 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100663 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200664#elif N_ARM
665 for (int i = 0; i < scope->num_pos_args; i++) {
666 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100667 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200668 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100669 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200670 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100671 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200672 } else if (i == 3) {
673 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
674 } else {
675 // TODO not implemented
676 assert(0);
677 }
678 }
679
Damien Georgee9dac3b2014-09-29 22:10:41 +0100680 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100681 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100682#else
683 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100684#endif
685}
686
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200687STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100688 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100689 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100690 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100691 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100692
693 // check stack is back to zero size
694 if (emit->stack_size != 0) {
695 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
696 }
697
Damien George36db6bc2014-05-07 17:24:22 +0100698 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100699 void *f = ASM_GET_CODE(emit->as);
700 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100701
702 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100703 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100704 mp_uint_t type_sig = emit->return_vtype & 3;
705 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
706 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
707 }
708
709 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 +0100710 }
711}
712
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200713STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100714 return emit->last_emit_was_return_value;
715}
716
Damien Georged6230f62014-09-23 14:10:03 +0000717STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000718 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100719 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100720 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100721 emit->scope->stack_size = emit->stack_size;
722 }
Damien George21ca2d72014-10-19 19:00:51 +0100723#ifdef DEBUG_PRINT
724 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
725 for (int i = 0; i < emit->stack_size; i++) {
726 stack_info_t *si = &emit->stack_info[i];
727 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->u_reg);
728 }
729 DEBUG_printf("\n");
730#endif
Damien13ed3a62013-10-08 09:05:10 +0100731}
732
Damien Georged6230f62014-09-23 14:10:03 +0000733STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100734 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000735 // If we are adjusting the stack in a positive direction (pushing) then we
736 // need to fill in values for the stack kind and vtype of the newly-pushed
737 // entries. These should be set to "value" (ie not reg or imm) because we
738 // should only need to adjust the stack due to a jump to this part in the
739 // code (and hence we have settled the stack before the jump).
740 for (mp_int_t i = 0; i < delta; i++) {
741 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
742 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100743 // TODO we don't know the vtype to use here. At the moment this is a
744 // hack to get the case of multi comparison working.
745 if (delta == 1) {
746 si->vtype = emit->saved_stack_vtype;
747 } else {
748 si->vtype = VTYPE_PYOBJ;
749 }
Damien Georged6230f62014-09-23 14:10:03 +0000750 }
751 adjust_stack(emit, delta);
752}
753
754STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
755}
756
Damienff8ed772013-10-08 22:18:32 +0100757/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200758STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100759 adjust_stack(emit, stack_size_delta);
760 emit->last_emit_was_return_value = false;
761}
Damienff8ed772013-10-08 22:18:32 +0100762*/
Damien13ed3a62013-10-08 09:05:10 +0100763
Damienff8ed772013-10-08 22:18:32 +0100764// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000765STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100766 emit->last_emit_was_return_value = false;
767 // settle the stack
768 /*
769 if (regs_needed != 0) {
770 for (int i = 0; i < emit->stack_size; i++) {
771 switch (emit->stack_info[i].kind) {
772 case STACK_VALUE:
773 break;
774
775 case STACK_REG:
776 // TODO only push reg if in regs_needed
777 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100778 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100779 break;
780
781 case STACK_IMM:
782 // don't think we ever need to push imms for settling
783 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
784 break;
785 }
786 }
787 }
788 */
Damien13ed3a62013-10-08 09:05:10 +0100789}
790
Damien George3112cde2014-09-29 18:45:42 +0100791// depth==0 is top, depth==1 is before top, etc
792STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
793 return &emit->stack_info[emit->stack_size - 1 - depth];
794}
795
796// depth==0 is top, depth==1 is before top, etc
797STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
798 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100799}
Damien13ed3a62013-10-08 09:05:10 +0100800
Damiend2755ec2013-10-16 23:58:48 +0100801// pos=1 is TOS, pos=2 is next, etc
802// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200803STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100804 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100805 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100806 if (i != skip_stack_pos) {
807 stack_info_t *si = &emit->stack_info[i];
808 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
809 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100810 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100811 }
Damienff8ed772013-10-08 22:18:32 +0100812 }
813 }
814}
Damien13ed3a62013-10-08 09:05:10 +0100815
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200816STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +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) {
820 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100821 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100822 }
Damien13ed3a62013-10-08 09:05:10 +0100823 }
824}
825
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200826STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000827 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100828 for (int i = 0; i < emit->stack_size; i++) {
829 stack_info_t *si = &emit->stack_info[i];
830 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000831 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100832 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100833 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100834 }
835 }
836 for (int i = 0; i < emit->stack_size; i++) {
837 stack_info_t *si = &emit->stack_info[i];
838 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000839 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100840 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100841 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100842 }
843 }
844}
845
846// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200847STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100848 need_reg_single(emit, reg_dest, pos);
849 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100850 *vtype = si->vtype;
851 switch (si->kind) {
852 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100853 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100854 break;
855
Damienff8ed772013-10-08 22:18:32 +0100856 case STACK_REG:
857 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100858 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100859 }
860 break;
861
Damienff8ed772013-10-08 22:18:32 +0100862 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100863 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100864 break;
865 }
Damien13ed3a62013-10-08 09:05:10 +0100866}
867
Damien Georgee9dac3b2014-09-29 22:10:41 +0100868// does an efficient X=pop(); discard(); push(X)
869// needs a (non-temp) register in case the poped element was stored in the stack
870STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
871 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
872 si[0] = si[1];
873 if (si->kind == STACK_VALUE) {
874 // if folded element was on the stack we need to put it in a register
875 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
876 si->kind = STACK_REG;
877 si->u_reg = reg_dest;
878 }
879 adjust_stack(emit, -1);
880}
881
882// If stacked value is in a register and the register is not r1 or r2, then
883// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
884STATIC 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 +0100885 emit->last_emit_was_return_value = false;
886 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100887 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100888 *vtype = si->vtype;
889 *reg_dest = si->u_reg;
890 need_reg_single(emit, *reg_dest, 1);
891 } else {
892 emit_access_stack(emit, 1, vtype, *reg_dest);
893 }
894 adjust_stack(emit, -1);
895}
896
Damien Georgee6ce10a2014-09-06 18:38:20 +0100897STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100898 emit->last_emit_was_return_value = false;
899 adjust_stack(emit, -1);
900}
901
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200902STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100903 emit->last_emit_was_return_value = false;
904 emit_access_stack(emit, 1, vtype, reg_dest);
905 adjust_stack(emit, -1);
906}
907
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200908STATIC 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 +0100909 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100910 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100911}
912
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200913STATIC 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 +0100914 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100915 emit_pre_pop_reg(emit, vtypeb, regb);
916 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100917}
918
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200919STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100920}
921
Damien Georgee9dac3b2014-09-29 22:10:41 +0100922STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
923 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
924 si->vtype = new_vtype;
925}
926
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200927STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100928 stack_info_t *si = &emit->stack_info[emit->stack_size];
929 si->vtype = vtype;
930 si->kind = STACK_REG;
931 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100932 adjust_stack(emit, 1);
933}
934
Damien George40f3c022014-07-03 13:25:24 +0100935STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100936 stack_info_t *si = &emit->stack_info[emit->stack_size];
937 si->vtype = vtype;
938 si->kind = STACK_IMM;
939 si->u_imm = imm;
940 adjust_stack(emit, 1);
941}
942
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200943STATIC 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 +0100944 emit_post_push_reg(emit, vtypea, rega);
945 emit_post_push_reg(emit, vtypeb, regb);
946}
947
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200948STATIC 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 +0100949 emit_post_push_reg(emit, vtypea, rega);
950 emit_post_push_reg(emit, vtypeb, regb);
951 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100952}
953
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200954STATIC 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 +0100955 emit_post_push_reg(emit, vtypea, rega);
956 emit_post_push_reg(emit, vtypeb, regb);
957 emit_post_push_reg(emit, vtypec, regc);
958 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100959}
960
Damien George7fe21912014-08-16 22:31:57 +0100961STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100962 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100963 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100964}
965
Damien George7fe21912014-08-16 22:31:57 +0100966STATIC 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 +0100967 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100968 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
969 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100970}
971
Damien George40f3c022014-07-03 13:25:24 +0100972// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100973STATIC 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 +0100974 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100975 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
976 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100977}
978
Damien George7fe21912014-08-16 22:31:57 +0100979STATIC 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 +0000980 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100981 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
982 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
983 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000984}
985
Damien George40f3c022014-07-03 13:25:24 +0100986// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100987STATIC 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 +0100988 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100989 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
990 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
991 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
992 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100993}
994
Damien George86de21b2014-08-16 22:06:11 +0100995// vtype of all n_pop objects is VTYPE_PYOBJ
996// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
997// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
998// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
999STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
1000 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +01001001
Damien George86de21b2014-08-16 22:06:11 +01001002 // First, store any immediate values to their respective place on the stack.
1003 for (mp_uint_t i = 0; i < n_pop; i++) {
1004 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1005 // must push any imm's to stack
1006 // must convert them to VTYPE_PYOBJ for viper code
1007 if (si->kind == STACK_IMM) {
1008 si->kind = STACK_VALUE;
1009 switch (si->vtype) {
1010 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +01001011 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001012 break;
1013 case VTYPE_BOOL:
1014 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001015 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 +01001016 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001017 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 +01001018 }
1019 si->vtype = VTYPE_PYOBJ;
1020 break;
1021 case VTYPE_INT:
1022 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +01001023 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, (si->u_imm << 1) | 1, emit->stack_start + emit->stack_size - 1 - i, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001024 si->vtype = VTYPE_PYOBJ;
1025 break;
1026 default:
1027 // not handled
1028 assert(0);
1029 }
1030 }
1031
1032 // verify that this value is on the stack
1033 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001034 }
Damien George86de21b2014-08-16 22:06:11 +01001035
1036 // Second, convert any non-VTYPE_PYOBJ to that type.
1037 for (mp_uint_t i = 0; i < n_pop; i++) {
1038 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1039 if (si->vtype != VTYPE_PYOBJ) {
1040 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001041 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001042 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 +01001043 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001044 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001045 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001046 }
1047 }
1048
1049 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1050 adjust_stack(emit, -n_pop);
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}
1053
1054// vtype of all n_push objects is VTYPE_PYOBJ
1055STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1056 need_reg_all(emit);
1057 for (mp_uint_t i = 0; i < n_push; i++) {
1058 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1059 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1060 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001061 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001062 adjust_stack(emit, n_push);
1063}
1064
Damien George7ff996c2014-09-08 23:05:16 +01001065STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1066 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001067}
1068
Damien George7ff996c2014-09-08 23:05:16 +01001069STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1070 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001071}
1072
Damien George7ff996c2014-09-08 23:05:16 +01001073STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1074 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001075}
1076
Damien George7ff996c2014-09-08 23:05:16 +01001077STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001078 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001079 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001080 // need to commit stack because we can jump here from elsewhere
1081 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001082 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001083 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001084}
1085
Damien Georgecdd96df2014-04-06 12:58:40 +01001086STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1087 DEBUG_printf("import_name %s\n", qstr_str(qst));
1088 vtype_kind_t vtype_fromlist;
1089 vtype_kind_t vtype_level;
1090 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1091 assert(vtype_fromlist == VTYPE_PYOBJ);
1092 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001093 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001094 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001095}
1096
Damien Georgecdd96df2014-04-06 12:58:40 +01001097STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1098 DEBUG_printf("import_from %s\n", qstr_str(qst));
1099 emit_native_pre(emit);
1100 vtype_kind_t vtype_module;
1101 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1102 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001103 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001104 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001105}
1106
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001107STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001108 DEBUG_printf("import_star\n");
1109 vtype_kind_t vtype_module;
1110 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1111 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001112 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001113 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001114}
1115
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001116STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001117 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001118 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001119 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001120 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001121 if (emit->do_viper_types) {
1122 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001123 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1124 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1125 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001126 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1127 }
1128 } else {
1129 vtype = VTYPE_PYOBJ;
1130 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001131 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1132 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1133 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001134 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1135 }
1136 }
1137 emit_post_push_imm(emit, vtype, val);
1138}
1139
Damien George40f3c022014-07-03 13:25:24 +01001140STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001141 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001142 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001143 if (emit->do_viper_types) {
1144 emit_post_push_imm(emit, VTYPE_INT, arg);
1145 } else {
1146 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1147 }
1148}
1149
Damien Georgecdd96df2014-04-06 12:58:40 +01001150STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001151 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001152 // for viper: load integer, check fits in 32 bits
1153 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001154 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001155 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001156}
1157
Damien George7ff996c2014-09-08 23:05:16 +01001158STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001159 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001160 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001161 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001162 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001163}
1164
Damien George7ff996c2014-09-08 23:05:16 +01001165STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001166 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001167 // TODO: Eventually we want to be able to work with raw pointers in viper to
1168 // do native array access. For now we just load them as any other object.
1169 /*
Damien13ed3a62013-10-08 09:05:10 +01001170 if (emit->do_viper_types) {
1171 // not implemented properly
1172 // load a pointer to the asciiz string?
1173 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001174 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001175 } else
1176 */
1177 {
Damien Georgeb601d952014-06-30 05:17:25 +01001178 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001179 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001180 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001181 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001182 }
Damien13ed3a62013-10-08 09:05:10 +01001183 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1184 }
1185}
1186
Damien George3558f622014-04-20 17:50:40 +01001187STATIC void emit_native_load_null(emit_t *emit) {
1188 emit_native_pre(emit);
1189 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1190}
1191
Damien George7ff996c2014-09-08 23:05:16 +01001192STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien George21ca2d72014-10-19 19:00:51 +01001193 DEBUG_printf("load_fast(%s, " UINT_FMT ", " UINT_FMT ")\n", qstr_str(qst), id_flags, local_num);
Damien13ed3a62013-10-08 09:05:10 +01001194 vtype_kind_t vtype = emit->local_vtype[local_num];
1195 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001196 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001197 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001198 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001199#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001200 if (local_num == 0) {
1201 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001202 } else if (local_num == 1) {
1203 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1204 } else if (local_num == 2) {
1205 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001206 } else {
Damien George0b610de2014-09-29 16:25:04 +01001207 need_reg_single(emit, REG_TEMP0, 0);
1208 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1209 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001210 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001211#elif N_X86
1212 if (local_num == 0) {
1213 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001214 } else if (local_num == 1) {
1215 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001216 } else if (local_num == 2) {
1217 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001218 } else {
Damien George0b610de2014-09-29 16:25:04 +01001219 need_reg_single(emit, REG_TEMP0, 0);
1220 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1221 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001222 }
Damien3ef4abb2013-10-12 16:53:13 +01001223#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001224 if (local_num == 0) {
1225 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1226 } else if (local_num == 1) {
1227 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1228 } else if (local_num == 2) {
1229 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1230 } else {
Damien George0b610de2014-09-29 16:25:04 +01001231 need_reg_single(emit, REG_TEMP0, 0);
1232 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1233 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001234 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001235#elif N_ARM
1236 if (local_num == 0) {
1237 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1238 } else if (local_num == 1) {
1239 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1240 } else if (local_num == 2) {
1241 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1242 } else {
Damien George0b610de2014-09-29 16:25:04 +01001243 need_reg_single(emit, REG_TEMP0, 0);
1244 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1245 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001246 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001247#else
1248 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001249#endif
1250}
1251
Damien George7ff996c2014-09-08 23:05:16 +01001252STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001253 // not implemented
1254 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1255 assert(0);
1256}
1257
Damien George7ff996c2014-09-08 23:05:16 +01001258STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001259 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001260 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001261 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001262 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1263}
1264
Damien George7ff996c2014-09-08 23:05:16 +01001265STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001266 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001267 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001268 // check for builtin casting operators
1269 if (emit->do_viper_types && qst == MP_QSTR_int) {
1270 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1271 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1272 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1273 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1274 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1275 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1276 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1277 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1278 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1279 } else {
1280 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1281 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1282 }
Damien13ed3a62013-10-08 09:05:10 +01001283}
1284
Damien George7ff996c2014-09-08 23:05:16 +01001285STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001286 // depends on type of subject:
1287 // - integer, function, pointer to integers: error
1288 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001289 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001290 vtype_kind_t vtype_base;
1291 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1292 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001293 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001294 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1295}
1296
Damien George7ff996c2014-09-08 23:05:16 +01001297STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001298 vtype_kind_t vtype_base;
1299 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1300 assert(vtype_base == VTYPE_PYOBJ);
1301 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001302 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001303}
1304
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001305STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001306 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001307 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001308 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001309}
1310
Damien George729f7b42014-04-17 22:10:53 +01001311STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001312 DEBUG_printf("load_subscr\n");
1313 // need to compile: base[index]
1314
1315 // pop: index, base
1316 // optimise case where index is an immediate
1317 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1318
1319 if (vtype_base == VTYPE_PYOBJ) {
1320 // standard Python call
1321 vtype_kind_t vtype_index;
1322 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1323 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001324 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 +01001325 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1326 } else {
Damien George91cfd412014-10-12 16:59:29 +01001327 // viper load
1328 // TODO The different machine architectures have very different
1329 // capabilities and requirements for loads, so probably best to
1330 // write a completely separate load-optimiser for each one.
1331 stack_info_t *top = peek_stack(emit, 0);
1332 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1333 // index is an immediate
1334 mp_int_t index_value = top->u_imm;
1335 emit_pre_pop_discard(emit); // discard index
1336 int reg_base = REG_ARG_1;
1337 int reg_index = REG_ARG_2;
1338 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1339 switch (vtype_base) {
1340 case VTYPE_PTR8: {
1341 // pointer to 8-bit memory
1342 // TODO optimise to use thumb ldrb r1, [r2, r3]
1343 if (index_value != 0) {
1344 // index is non-zero
1345 #if N_THUMB
1346 if (index_value > 0 && index_value < 32) {
1347 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1348 break;
1349 }
1350 #endif
1351 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1352 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1353 reg_base = reg_index;
1354 }
1355 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1356 break;
1357 }
1358 case VTYPE_PTR16: {
1359 // pointer to 16-bit memory
1360 if (index_value != 0) {
1361 // index is a non-zero immediate
1362 #if N_THUMB
1363 if (index_value > 0 && index_value < 32) {
1364 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1365 break;
1366 }
1367 #endif
1368 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1369 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1370 reg_base = reg_index;
1371 }
1372 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1373 break;
1374 }
1375 default:
1376 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1377 }
1378 } else {
1379 // index is not an immediate
1380 vtype_kind_t vtype_index;
1381 int reg_index = REG_ARG_2;
1382 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1383 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1384 switch (vtype_base) {
1385 case VTYPE_PTR8: {
1386 // pointer to 8-bit memory
1387 // TODO optimise to use thumb ldrb r1, [r2, r3]
1388 assert(vtype_index == VTYPE_INT);
1389 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1390 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1391 break;
1392 }
1393 case VTYPE_PTR16: {
1394 // pointer to 16-bit memory
1395 assert(vtype_index == VTYPE_INT);
1396 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1397 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1398 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1399 break;
1400 }
1401 default:
1402 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1403 }
1404 }
1405 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001406 }
1407}
1408
Damien George7ff996c2014-09-08 23:05:16 +01001409STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001410 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001411#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001412 if (local_num == 0) {
1413 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001414 } else if (local_num == 1) {
1415 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1416 } else if (local_num == 2) {
1417 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001418 } else {
Damien George0b610de2014-09-29 16:25:04 +01001419 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1420 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001421 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001422#elif N_X86
1423 if (local_num == 0) {
1424 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001425 } else if (local_num == 1) {
1426 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001427 } else if (local_num == 2) {
1428 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001429 } else {
Damien George0b610de2014-09-29 16:25:04 +01001430 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1431 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001432 }
Damien3ef4abb2013-10-12 16:53:13 +01001433#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001434 if (local_num == 0) {
1435 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1436 } else if (local_num == 1) {
1437 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1438 } else if (local_num == 2) {
1439 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1440 } else {
Damien George0b610de2014-09-29 16:25:04 +01001441 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1442 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001443 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001444#elif N_ARM
1445 if (local_num == 0) {
1446 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1447 } else if (local_num == 1) {
1448 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1449 } else if (local_num == 2) {
1450 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1451 } else {
Damien George0b610de2014-09-29 16:25:04 +01001452 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1453 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001454 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001455#else
1456 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001457#endif
1458
1459 emit_post(emit);
1460
1461 // check types
1462 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1463 // first time this local is assigned, so give it a type of the object stored in it
1464 emit->local_vtype[local_num] = vtype;
1465 } else if (emit->local_vtype[local_num] != vtype) {
1466 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001467 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 +01001468 }
1469}
1470
Damien George7ff996c2014-09-08 23:05:16 +01001471STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001472 // not implemented
1473 assert(0);
1474}
1475
Damien George7ff996c2014-09-08 23:05:16 +01001476STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001477 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001478 vtype_kind_t vtype;
1479 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1480 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001481 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001482 emit_post(emit);
1483}
1484
Damien George7ff996c2014-09-08 23:05:16 +01001485STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001486 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001487 if (vtype == VTYPE_PYOBJ) {
1488 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1489 } else {
1490 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001491 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 +01001492 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001493 }
Damien George7ff996c2014-09-08 23:05:16 +01001494 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001495 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001496}
1497
Damien George7ff996c2014-09-08 23:05:16 +01001498STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001499 vtype_kind_t vtype_base, vtype_val;
1500 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1501 assert(vtype_base == VTYPE_PYOBJ);
1502 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001503 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001504 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001505}
1506
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001507STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001508 DEBUG_printf("store_subscr\n");
1509 // need to compile: base[index] = value
1510
1511 // pop: index, base, value
1512 // optimise case where index is an immediate
1513 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1514
1515 if (vtype_base == VTYPE_PYOBJ) {
1516 // standard Python call
1517 vtype_kind_t vtype_index, vtype_value;
1518 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1519 assert(vtype_index == VTYPE_PYOBJ);
1520 assert(vtype_value == VTYPE_PYOBJ);
1521 emit_call(emit, MP_F_OBJ_SUBSCR);
1522 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001523 // viper store
1524 // TODO The different machine architectures have very different
1525 // capabilities and requirements for stores, so probably best to
1526 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001527 stack_info_t *top = peek_stack(emit, 0);
1528 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1529 // index is an immediate
1530 mp_int_t index_value = top->u_imm;
1531 emit_pre_pop_discard(emit); // discard index
1532 vtype_kind_t vtype_value;
1533 int reg_base = REG_ARG_1;
1534 int reg_index = REG_ARG_2;
1535 int reg_value = REG_ARG_3;
1536 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001537 #if N_X86
1538 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1539 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1540 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001541 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001542 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001543 switch (vtype_base) {
1544 case VTYPE_PTR8: {
1545 // pointer to 8-bit memory
1546 // TODO optimise to use thumb strb r1, [r2, r3]
1547 if (index_value != 0) {
1548 // index is non-zero
1549 #if N_THUMB
1550 if (index_value > 0 && index_value < 32) {
1551 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1552 break;
1553 }
1554 #endif
1555 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001556 #if N_ARM
1557 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1558 return;
1559 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001560 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1561 reg_base = reg_index;
1562 }
1563 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1564 break;
1565 }
1566 case VTYPE_PTR16: {
1567 // pointer to 16-bit memory
1568 if (index_value != 0) {
1569 // index is a non-zero immediate
1570 #if N_THUMB
1571 if (index_value > 0 && index_value < 32) {
1572 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1573 break;
1574 }
1575 #endif
1576 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001577 #if N_ARM
1578 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1579 return;
1580 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001581 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1582 reg_base = reg_index;
1583 }
1584 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1585 break;
1586 }
1587 default:
1588 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1589 }
1590 } else {
1591 // index is not an immediate
1592 vtype_kind_t vtype_index, vtype_value;
1593 int reg_index = REG_ARG_2;
1594 int reg_value = REG_ARG_3;
1595 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1596 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001597 #if N_X86
1598 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1599 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1600 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001601 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001602 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001603 switch (vtype_base) {
1604 case VTYPE_PTR8: {
1605 // pointer to 8-bit memory
1606 // TODO optimise to use thumb strb r1, [r2, r3]
1607 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001608 #if N_ARM
1609 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1610 break;
1611 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001612 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1613 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1614 break;
1615 }
1616 case VTYPE_PTR16: {
1617 // pointer to 16-bit memory
1618 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001619 #if N_ARM
1620 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1621 break;
1622 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001623 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1624 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1625 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1626 break;
1627 }
1628 default:
1629 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1630 }
1631 }
1632
1633 }
Damien13ed3a62013-10-08 09:05:10 +01001634}
1635
Damien George7ff996c2014-09-08 23:05:16 +01001636STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001637 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001638 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001639}
1640
Damien George7ff996c2014-09-08 23:05:16 +01001641STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001642 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001643}
1644
Damien Georgee6ce10a2014-09-06 18:38:20 +01001645STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1646 emit_native_pre(emit);
1647 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1648 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001649}
1650
Damien Georgee6ce10a2014-09-06 18:38:20 +01001651STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1652 emit_native_pre(emit);
1653 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1654 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001655}
1656
Damien Georgee6ce10a2014-09-06 18:38:20 +01001657STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001658 vtype_kind_t vtype_base;
1659 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1660 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001661 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 +01001662 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001663}
1664
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001665STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001666 vtype_kind_t vtype_index, vtype_base;
1667 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1668 assert(vtype_index == VTYPE_PYOBJ);
1669 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001670 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 +01001671}
1672
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001673STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001674 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001675 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001676 int reg = REG_TEMP0;
1677 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1678 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001679}
1680
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001681STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001682 vtype_kind_t vtype0, vtype1;
1683 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1684 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1685}
1686
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001687STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001688 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001689 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001690 emit_post(emit);
1691}
1692
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001693STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001694 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001695 vtype_kind_t vtype0, vtype1;
1696 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1697 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001698}
1699
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001700STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001701 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001702 vtype_kind_t vtype0, vtype1, vtype2;
1703 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1704 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1705}
1706
Damien George7ff996c2014-09-08 23:05:16 +01001707STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001708 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001709 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001710 // need to commit stack because we are jumping elsewhere
1711 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001712 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001713 emit_post(emit);
1714}
1715
Damien George7ff996c2014-09-08 23:05:16 +01001716STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001717 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001718 switch (vtype) {
1719 case VTYPE_PYOBJ:
1720 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1721 if (!pop) {
1722 adjust_stack(emit, 1);
1723 }
1724 emit_call(emit, MP_F_OBJ_IS_TRUE);
1725 break;
1726 case VTYPE_BOOL:
1727 case VTYPE_INT:
1728 case VTYPE_UINT:
1729 emit_pre_pop_reg(emit, &vtype, REG_RET);
1730 if (!pop) {
1731 adjust_stack(emit, 1);
1732 }
1733 break;
1734 default:
1735 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1736 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001737 }
Damien George21ca2d72014-10-19 19:00:51 +01001738 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1739 // can use it. This is a bit of a hack.
1740 if (!pop) {
1741 emit->saved_stack_vtype = vtype;
1742 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001743 // need to commit stack because we may jump elsewhere
1744 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001745}
1746
Damien George7ff996c2014-09-08 23:05:16 +01001747STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001748 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001749 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001750 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001751 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001752}
Damien1a6633a2013-11-03 13:58:19 +00001753
Damien George7ff996c2014-09-08 23:05:16 +01001754STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001755 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001756 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001757 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001758 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001759}
Damien Georgea32c1e42014-05-07 18:30:52 +01001760
Damien George7ff996c2014-09-08 23:05:16 +01001761STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001762 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001763 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001764 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001765 adjust_stack(emit, -1);
1766 emit_post(emit);
1767}
1768
Damien George7ff996c2014-09-08 23:05:16 +01001769STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001770 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001771 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001772 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001773 adjust_stack(emit, -1);
1774 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001775}
1776
Damien George7ff996c2014-09-08 23:05:16 +01001777STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001778 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001779}
Damien Georgea32c1e42014-05-07 18:30:52 +01001780
Damien George7ff996c2014-09-08 23:05:16 +01001781STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001782 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001783}
Damien Georgea32c1e42014-05-07 18:30:52 +01001784
Damien George7ff996c2014-09-08 23:05:16 +01001785STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001786 // not supported, or could be with runtime call
1787 assert(0);
1788}
Damien Georgeb601d952014-06-30 05:17:25 +01001789
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001790STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001791 assert(0);
1792}
Damien Georgeb601d952014-06-30 05:17:25 +01001793
Damien George7ff996c2014-09-08 23:05:16 +01001794STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001795 emit_native_pre(emit);
1796 // need to commit stack because we may jump elsewhere
1797 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001798 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 +01001799 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001800 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001801 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001802}
Damien Georgeb601d952014-06-30 05:17:25 +01001803
Damien George7ff996c2014-09-08 23:05:16 +01001804STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001805 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001806}
Damien Georgeb601d952014-06-30 05:17:25 +01001807
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001808STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001809 emit_pre_pop_discard(emit);
1810 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001811}
Damiend2755ec2013-10-16 23:58:48 +01001812
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001813STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001814 // perhaps the difficult one, as we want to rewrite for loops using native code
1815 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001816
1817 vtype_kind_t vtype;
1818 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1819 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001820 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001821 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001822}
Damiend2755ec2013-10-16 23:58:48 +01001823
Damien George7ff996c2014-09-08 23:05:16 +01001824STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001825 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001826 vtype_kind_t vtype;
1827 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1828 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001829 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001830 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1831 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001832 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1833}
1834
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001835STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001836 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001837 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001838 adjust_stack(emit, -1);
1839 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001840}
1841
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001842STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001843 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001844 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001845 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001846 emit_post(emit);
1847}
1848
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001849STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001850 /*
1851 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001852 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001853 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001854 emit_post(emit);
1855 */
Damien13ed3a62013-10-08 09:05:10 +01001856}
1857
Damien Georged17926d2014-03-30 13:35:08 +01001858STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001859 vtype_kind_t vtype;
1860 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1861 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001862 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001863 // we need to synthesise this operation by converting to bool first
1864 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001865 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001866 }
Damien Georged6230f62014-09-23 14:10:03 +00001867 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1868 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001869}
1870
Damien Georged17926d2014-03-30 13:35:08 +01001871STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001872 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001873 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1874 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001875 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001876 #if N_X64 || N_X86
1877 // special cases for x86 and shifting
1878 if (op == MP_BINARY_OP_LSHIFT
1879 || op == MP_BINARY_OP_INPLACE_LSHIFT
1880 || op == MP_BINARY_OP_RSHIFT
1881 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1882 #if N_X64
1883 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1884 #else
1885 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1886 #endif
1887 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1888 ASM_LSL_REG(emit->as, REG_RET);
1889 } else {
1890 ASM_ASR_REG(emit->as, REG_RET);
1891 }
1892 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1893 return;
1894 }
1895 #endif
1896 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001897 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001898 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1899 if (0) {
1900 // dummy
1901 #if !(N_X64 || N_X86)
1902 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1903 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001904 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001905 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1906 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1907 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1908 #endif
Damien George1ef23482014-10-12 14:21:06 +01001909 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1910 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1911 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1912 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1913 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1914 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1915 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1916 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1917 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001918 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1919 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1920 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1921 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1922 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1923 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1924 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1925 // comparison ops are (in enum order):
1926 // MP_BINARY_OP_LESS
1927 // MP_BINARY_OP_MORE
1928 // MP_BINARY_OP_EQUAL
1929 // MP_BINARY_OP_LESS_EQUAL
1930 // MP_BINARY_OP_MORE_EQUAL
1931 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001932 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001933 #if N_X64
1934 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1935 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1936 static byte ops[6] = {
1937 ASM_X64_CC_JL,
1938 ASM_X64_CC_JG,
1939 ASM_X64_CC_JE,
1940 ASM_X64_CC_JLE,
1941 ASM_X64_CC_JGE,
1942 ASM_X64_CC_JNE,
1943 };
1944 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1945 #elif N_X86
1946 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1947 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1948 static byte ops[6] = {
1949 ASM_X86_CC_JL,
1950 ASM_X86_CC_JG,
1951 ASM_X86_CC_JE,
1952 ASM_X86_CC_JLE,
1953 ASM_X86_CC_JGE,
1954 ASM_X86_CC_JNE,
1955 };
1956 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1957 #elif N_THUMB
1958 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1959 static uint16_t ops[6] = {
1960 ASM_THUMB_OP_ITE_GE,
1961 ASM_THUMB_OP_ITE_GT,
1962 ASM_THUMB_OP_ITE_EQ,
1963 ASM_THUMB_OP_ITE_GT,
1964 ASM_THUMB_OP_ITE_GE,
1965 ASM_THUMB_OP_ITE_EQ,
1966 };
1967 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1968 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1969 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1970 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1971 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001972 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1973 static uint ccs[6] = {
1974 ASM_ARM_CC_LT,
1975 ASM_ARM_CC_GT,
1976 ASM_ARM_CC_EQ,
1977 ASM_ARM_CC_LE,
1978 ASM_ARM_CC_GE,
1979 ASM_ARM_CC_NE,
1980 };
1981 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001982 #else
1983 #error not implemented
1984 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001985 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1986 } else {
1987 // TODO other ops not yet implemented
1988 assert(0);
1989 }
Damien13ed3a62013-10-08 09:05:10 +01001990 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001991 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001992 bool invert = false;
1993 if (op == MP_BINARY_OP_NOT_IN) {
1994 invert = true;
1995 op = MP_BINARY_OP_IN;
1996 } else if (op == MP_BINARY_OP_IS_NOT) {
1997 invert = true;
1998 op = MP_BINARY_OP_IS;
1999 }
Damien George7fe21912014-08-16 22:31:57 +01002000 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002001 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002002 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002003 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2004 }
Damien13ed3a62013-10-08 09:05:10 +01002005 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2006 } else {
2007 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002008 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002009 }
2010}
2011
Damien George7ff996c2014-09-08 23:05:16 +01002012STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002013 // for viper: call runtime, with types of args
2014 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002015 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002016 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002017 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002018 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002019}
2020
Damien George7ff996c2014-09-08 23:05:16 +01002021STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002022 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002023 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002024 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002025 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2026}
2027
Damien George7ff996c2014-09-08 23:05:16 +01002028STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002029 // only used in list comprehension
2030 vtype_kind_t vtype_list, vtype_item;
2031 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2032 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2033 assert(vtype_list == VTYPE_PYOBJ);
2034 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002035 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002036 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002037}
2038
Damien George7ff996c2014-09-08 23:05:16 +01002039STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002040 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002041 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002042 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2043}
2044
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002045STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002046 vtype_kind_t vtype_key, vtype_value, vtype_map;
2047 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
2048 assert(vtype_key == VTYPE_PYOBJ);
2049 assert(vtype_value == VTYPE_PYOBJ);
2050 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002051 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002052 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2053}
2054
Damien George7ff996c2014-09-08 23:05:16 +01002055STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002056 // only used in list comprehension
2057 vtype_kind_t vtype_map, vtype_key, vtype_value;
2058 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2059 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2060 assert(vtype_map == VTYPE_PYOBJ);
2061 assert(vtype_key == VTYPE_PYOBJ);
2062 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002063 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002064 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002065}
2066
Damien Georgee37dcaa2014-12-27 17:07:16 +00002067#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002068STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002069 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002070 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002071 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002072 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2073}
2074
Damien George7ff996c2014-09-08 23:05:16 +01002075STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002076 // only used in set comprehension
2077 vtype_kind_t vtype_set, vtype_item;
2078 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2079 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2080 assert(vtype_set == VTYPE_PYOBJ);
2081 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002082 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002083 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002084}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002085#endif
Damiend2755ec2013-10-16 23:58:48 +01002086
Damien George83204f32014-12-27 17:20:41 +00002087#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002088STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002089 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002090 if (n_args == 2) {
2091 vtype_kind_t vtype_start, vtype_stop;
2092 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2093 assert(vtype_start == VTYPE_PYOBJ);
2094 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002095 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 +01002096 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2097 } else {
2098 assert(n_args == 3);
2099 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2100 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
2101 assert(vtype_start == VTYPE_PYOBJ);
2102 assert(vtype_stop == VTYPE_PYOBJ);
2103 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002104 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002105 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2106 }
Damien13ed3a62013-10-08 09:05:10 +01002107}
Damien George83204f32014-12-27 17:20:41 +00002108#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002109
Damien George7ff996c2014-09-08 23:05:16 +01002110STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002111 DEBUG_printf("unpack_sequence %d\n", n_args);
2112 vtype_kind_t vtype_base;
2113 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2114 assert(vtype_base == VTYPE_PYOBJ);
2115 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002116 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002117}
Damien Georgecdd96df2014-04-06 12:58:40 +01002118
Damien George7ff996c2014-09-08 23:05:16 +01002119STATIC 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 +01002120 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2121 vtype_kind_t vtype_base;
2122 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2123 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002124 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 +01002125 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 +01002126}
2127
Damien George7ff996c2014-09-08 23:05:16 +01002128STATIC 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 +01002129 // 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 +00002130 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002131 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002132 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 +01002133 } else {
2134 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2135 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2136 assert(vtype_def_tuple == VTYPE_PYOBJ);
2137 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002138 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 +01002139 }
Damien13ed3a62013-10-08 09:05:10 +01002140 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2141}
2142
Damien George7ff996c2014-09-08 23:05:16 +01002143STATIC 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) {
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);
2161 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2162 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)
2243 assert(0);
2244}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002245STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002246 // not supported (for now)
2247 assert(0);
2248}
2249
Damien Georgeb601d952014-06-30 05:17:25 +01002250STATIC void emit_native_start_except_handler(emit_t *emit) {
2251 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2252 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2253 // the first 2 elements, so we can get the thrown value.
2254 adjust_stack(emit, 2);
2255 vtype_kind_t vtype_nlr;
2256 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002257 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002258 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
2259}
2260
2261STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002262 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002263}
2264
Damien13ed3a62013-10-08 09:05:10 +01002265const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002266 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002267 emit_native_start_pass,
2268 emit_native_end_pass,
2269 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002270 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002271 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002272
2273 emit_native_load_id,
2274 emit_native_store_id,
2275 emit_native_delete_id,
2276
2277 emit_native_label_assign,
2278 emit_native_import_name,
2279 emit_native_import_from,
2280 emit_native_import_star,
2281 emit_native_load_const_tok,
2282 emit_native_load_const_small_int,
2283 emit_native_load_const_int,
2284 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002285 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01002286 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002287 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002288 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002289 emit_native_load_name,
2290 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002291 emit_native_load_attr,
2292 emit_native_load_method,
2293 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002294 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002295 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002296 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002297 emit_native_store_name,
2298 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002299 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002300 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002301 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002302 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002303 emit_native_delete_name,
2304 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002305 emit_native_delete_attr,
2306 emit_native_delete_subscr,
2307 emit_native_dup_top,
2308 emit_native_dup_top_two,
2309 emit_native_pop_top,
2310 emit_native_rot_two,
2311 emit_native_rot_three,
2312 emit_native_jump,
2313 emit_native_pop_jump_if_true,
2314 emit_native_pop_jump_if_false,
2315 emit_native_jump_if_true_or_pop,
2316 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002317 emit_native_break_loop,
2318 emit_native_continue_loop,
2319 emit_native_setup_with,
2320 emit_native_with_cleanup,
2321 emit_native_setup_except,
2322 emit_native_setup_finally,
2323 emit_native_end_finally,
2324 emit_native_get_iter,
2325 emit_native_for_iter,
2326 emit_native_for_iter_end,
2327 emit_native_pop_block,
2328 emit_native_pop_except,
2329 emit_native_unary_op,
2330 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002331 emit_native_build_tuple,
2332 emit_native_build_list,
2333 emit_native_list_append,
2334 emit_native_build_map,
2335 emit_native_store_map,
2336 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002337 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002338 emit_native_build_set,
2339 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002340 #endif
Damien George83204f32014-12-27 17:20:41 +00002341 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002342 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002343 #endif
Damien13ed3a62013-10-08 09:05:10 +01002344 emit_native_unpack_sequence,
2345 emit_native_unpack_ex,
2346 emit_native_make_function,
2347 emit_native_make_closure,
2348 emit_native_call_function,
2349 emit_native_call_method,
2350 emit_native_return_value,
2351 emit_native_raise_varargs,
2352 emit_native_yield_value,
2353 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002354
2355 emit_native_start_except_handler,
2356 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002357};
2358
Damien Georgec90f59e2014-09-06 23:06:36 +01002359#endif