blob: 072163618502359992db0d200e5617250b18db31 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien13ed3a62013-10-08 09:05:10 +010027// Essentially normal Python has 1 type: Python objects
28// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
29// If you declare everything in Viper as a Python object (ie omit type decls) then
30// it should in principle be exactly the same as Python native.
31// Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
32// In practice we won't have a VM but rather do this in asm which is actually very minimal.
33
34// Because it breaks strict Python equivalence it should be a completely separate
35// decorator. It breaks equivalence because overflow on integers wraps around.
36// It shouldn't break equivalence if you don't use the new types, but since the
37// type decls might be used in normal Python for other reasons, it's probably safest,
38// cleanest and clearest to make it a separate decorator.
39
40// Actually, it does break equivalence because integers default to native integers,
41// not Python objects.
42
43// for x in l[0:8]: can be compiled into a native loop if l has pointer type
44
Damien13ed3a62013-10-08 09:05:10 +010045#include <stdio.h>
46#include <string.h>
47#include <assert.h>
48
Damien George51dfcb42015-01-01 20:27:54 +000049#include "py/nlr.h"
50#include "py/emit.h"
Damien13ed3a62013-10-08 09:05:10 +010051
Damien Georgecdd96df2014-04-06 12:58:40 +010052#if 0 // print debugging info
53#define DEBUG_PRINT (1)
54#define DEBUG_printf DEBUG_printf
55#else // don't print debugging info
56#define DEBUG_printf(...) (void)0
57#endif
58
Damien13ed3a62013-10-08 09:05:10 +010059// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010060#if (MICROPY_EMIT_X64 && N_X64) \
61 || (MICROPY_EMIT_X86 && N_X86) \
62 || (MICROPY_EMIT_THUMB && N_THUMB) \
63 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010064
Damien3ef4abb2013-10-12 16:53:13 +010065#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010066
67// x64 specific stuff
68
Damien George51dfcb42015-01-01 20:27:54 +000069#include "py/asmx64.h"
Damien13ed3a62013-10-08 09:05:10 +010070
Damien13ed3a62013-10-08 09:05:10 +010071#define EXPORT_FUN(name) emit_native_x64_##name
72
Damien George0b610de2014-09-29 16:25:04 +010073#define REG_RET ASM_X64_REG_RAX
74#define REG_ARG_1 ASM_X64_REG_RDI
75#define REG_ARG_2 ASM_X64_REG_RSI
76#define REG_ARG_3 ASM_X64_REG_RDX
77#define REG_ARG_4 ASM_X64_REG_RCX
Damien Georgec90f59e2014-09-06 23:06:36 +010078
Damien George81057362014-09-07 01:06:19 +010079// caller-save
Damien George0b610de2014-09-29 16:25:04 +010080#define REG_TEMP0 ASM_X64_REG_RAX
81#define REG_TEMP1 ASM_X64_REG_RDI
82#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010083
84// callee-save
Damien George0b610de2014-09-29 16:25:04 +010085#define REG_LOCAL_1 ASM_X64_REG_RBX
86#define REG_LOCAL_2 ASM_X64_REG_R12
87#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +010088#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +010089
90#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
91#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
92
93#define ASM_T asm_x64_t
94#define ASM_NEW asm_x64_new
95#define ASM_FREE asm_x64_free
96#define ASM_GET_CODE asm_x64_get_code
97#define ASM_GET_CODE_SIZE asm_x64_get_code_size
98#define ASM_START_PASS asm_x64_start_pass
99#define ASM_END_PASS asm_x64_end_pass
100#define ASM_ENTRY asm_x64_entry
101#define ASM_EXIT asm_x64_exit
102
103#define ASM_LABEL_ASSIGN asm_x64_label_assign
104#define ASM_JUMP asm_x64_jmp_label
105#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
106 do { \
107 asm_x64_test_r8_with_r8(as, reg, reg); \
108 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
109 } while (0)
110#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
111 do { \
112 asm_x64_test_r8_with_r8(as, reg, reg); \
113 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
114 } while (0)
115#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
116 do { \
117 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
118 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
119 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100120#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100121
122#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
123#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
124#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
125#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
126 do { \
127 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
128 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
129 } while (false)
130#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100131#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x64_mov_r64_r64((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100132#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
133
Damien George3112cde2014-09-29 18:45:42 +0100134#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
135#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100136#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
137#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
138#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100139#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
140#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
141
Damien George91cfd412014-10-12 16:59:29 +0100142#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem64_to_r64((as), (reg_base), 0, (reg_dest))
143#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem8_to_r64zx((as), (reg_base), 0, (reg_dest))
144#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_x64_mov_mem16_to_r64zx((as), (reg_base), 0, (reg_dest))
145
146#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_mem64((as), (reg_src), (reg_base), 0)
147#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
148#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_mem16((as), (reg_src), (reg_base), 0)
Damien Georgee9dac3b2014-09-29 22:10:41 +0100149
Damien Georgec90f59e2014-09-06 23:06:36 +0100150#elif N_X86
151
152// x86 specific stuff
153
Damien George51dfcb42015-01-01 20:27:54 +0000154#include "py/asmx86.h"
Damien Georgec90f59e2014-09-06 23:06:36 +0100155
156STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
157 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
158 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
159 [MP_F_LOAD_CONST_INT] = 1,
160 [MP_F_LOAD_CONST_DEC] = 1,
161 [MP_F_LOAD_CONST_STR] = 1,
162 [MP_F_LOAD_CONST_BYTES] = 1,
163 [MP_F_LOAD_NAME] = 1,
164 [MP_F_LOAD_GLOBAL] = 1,
165 [MP_F_LOAD_BUILD_CLASS] = 0,
166 [MP_F_LOAD_ATTR] = 2,
167 [MP_F_LOAD_METHOD] = 3,
168 [MP_F_STORE_NAME] = 2,
169 [MP_F_STORE_GLOBAL] = 2,
170 [MP_F_STORE_ATTR] = 3,
171 [MP_F_OBJ_SUBSCR] = 3,
172 [MP_F_OBJ_IS_TRUE] = 1,
173 [MP_F_UNARY_OP] = 2,
174 [MP_F_BINARY_OP] = 3,
175 [MP_F_BUILD_TUPLE] = 2,
176 [MP_F_BUILD_LIST] = 2,
177 [MP_F_LIST_APPEND] = 2,
178 [MP_F_BUILD_MAP] = 1,
179 [MP_F_STORE_MAP] = 3,
180#if MICROPY_PY_BUILTINS_SET
181 [MP_F_BUILD_SET] = 2,
182 [MP_F_STORE_SET] = 2,
183#endif
184 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
185 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
186 [MP_F_CALL_METHOD_N_KW] = 3,
187 [MP_F_GETITER] = 1,
188 [MP_F_ITERNEXT] = 1,
189 [MP_F_NLR_PUSH] = 1,
190 [MP_F_NLR_POP] = 0,
191 [MP_F_NATIVE_RAISE] = 1,
192 [MP_F_IMPORT_NAME] = 3,
193 [MP_F_IMPORT_FROM] = 2,
194 [MP_F_IMPORT_ALL] = 1,
195#if MICROPY_PY_BUILTINS_SLICE
196 [MP_F_NEW_SLICE] = 3,
197#endif
198 [MP_F_UNPACK_SEQUENCE] = 3,
199 [MP_F_UNPACK_EX] = 3,
200 [MP_F_DELETE_NAME] = 1,
201 [MP_F_DELETE_GLOBAL] = 1,
202};
203
204#define EXPORT_FUN(name) emit_native_x86_##name
205
Damien George0b610de2014-09-29 16:25:04 +0100206#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000207#define REG_ARG_1 ASM_X86_REG_ARG_1
208#define REG_ARG_2 ASM_X86_REG_ARG_2
209#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100210
Damien George25d90412014-09-06 23:24:32 +0000211// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100212#define REG_TEMP0 ASM_X86_REG_EAX
213#define REG_TEMP1 ASM_X86_REG_ECX
214#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100215
Damien George25d90412014-09-06 23:24:32 +0000216// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100217#define REG_LOCAL_1 ASM_X86_REG_EBX
218#define REG_LOCAL_2 ASM_X86_REG_ESI
219#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000220#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100221
222#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
223#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
224
225#define ASM_T asm_x86_t
226#define ASM_NEW asm_x86_new
227#define ASM_FREE asm_x86_free
228#define ASM_GET_CODE asm_x86_get_code
229#define ASM_GET_CODE_SIZE asm_x86_get_code_size
230#define ASM_START_PASS asm_x86_start_pass
231#define ASM_END_PASS asm_x86_end_pass
232#define ASM_ENTRY asm_x86_entry
233#define ASM_EXIT asm_x86_exit
234
235#define ASM_LABEL_ASSIGN asm_x86_label_assign
236#define ASM_JUMP asm_x86_jmp_label
237#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
238 do { \
239 asm_x86_test_r8_with_r8(as, reg, reg); \
240 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
241 } while (0)
242#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
243 do { \
244 asm_x86_test_r8_with_r8(as, reg, reg); \
245 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
246 } while (0)
247#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
248 do { \
249 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
250 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
251 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100252#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 +0100253
254#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
255#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
256#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
257#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
258 do { \
259 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
260 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
261 } while (false)
262#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100263#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 +0100264#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100265
Damien George3112cde2014-09-29 18:45:42 +0100266#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
267#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100268#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
269#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
270#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 +0100271#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
272#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
273
Damien George91cfd412014-10-12 16:59:29 +0100274#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 +0000275#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_x86_mov_mem8_to_r32zx((as), (reg_base), 0, (reg_dest))
276#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 +0100277
278#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_mem32((as), (reg_src), (reg_base), 0)
279#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_mem8((as), (reg_src), (reg_base), 0)
280#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 +0100281
Damien3ef4abb2013-10-12 16:53:13 +0100282#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100283
284// thumb specific stuff
285
Damien George51dfcb42015-01-01 20:27:54 +0000286#include "py/asmthumb.h"
Damien13ed3a62013-10-08 09:05:10 +0100287
Damien13ed3a62013-10-08 09:05:10 +0100288#define EXPORT_FUN(name) emit_native_thumb_##name
289
Damien George0b610de2014-09-29 16:25:04 +0100290#define REG_RET ASM_THUMB_REG_R0
291#define REG_ARG_1 ASM_THUMB_REG_R0
292#define REG_ARG_2 ASM_THUMB_REG_R1
293#define REG_ARG_3 ASM_THUMB_REG_R2
294#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100295
Damien George0b610de2014-09-29 16:25:04 +0100296#define REG_TEMP0 ASM_THUMB_REG_R0
297#define REG_TEMP1 ASM_THUMB_REG_R1
298#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100299
Damien George0b610de2014-09-29 16:25:04 +0100300#define REG_LOCAL_1 ASM_THUMB_REG_R4
301#define REG_LOCAL_2 ASM_THUMB_REG_R5
302#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100303#define REG_LOCAL_NUM (3)
304
305#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
306#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
307
308#define ASM_T asm_thumb_t
309#define ASM_NEW asm_thumb_new
310#define ASM_FREE asm_thumb_free
311#define ASM_GET_CODE asm_thumb_get_code
312#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
313#define ASM_START_PASS asm_thumb_start_pass
314#define ASM_END_PASS asm_thumb_end_pass
315#define ASM_ENTRY asm_thumb_entry
316#define ASM_EXIT asm_thumb_exit
317
318#define ASM_LABEL_ASSIGN asm_thumb_label_assign
319#define ASM_JUMP asm_thumb_b_label
320#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
321 do { \
322 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100323 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100324 } while (0)
325#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
326 do { \
327 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100328 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100329 } while (0)
330#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
331 do { \
332 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100333 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100334 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100335#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 +0100336
337#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
338#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
339#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
340#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
341 do { \
342 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
343 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
344 } while (false)
345#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 +0100346#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 +0100347#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 +0100348
Damien George3112cde2014-09-29 18:45:42 +0100349#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
350#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 +0100351#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
352#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
353#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 +0100354#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
355#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
356
Damien George91cfd412014-10-12 16:59:29 +0100357#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_thumb_ldr_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
358#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrb_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
359#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_thumb_ldrh_rlo_rlo_i5((as), (reg_dest), (reg_base), 0)
360
Damien Georgee9dac3b2014-09-29 22:10:41 +0100361#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_thumb_str_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
362#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_thumb_strb_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
363#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_thumb_strh_rlo_rlo_i5((as), (reg_src), (reg_base), 0)
364
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200365#elif N_ARM
366
367// ARM specific stuff
368
Damien George51dfcb42015-01-01 20:27:54 +0000369#include "py/asmarm.h"
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200370
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200371#define EXPORT_FUN(name) emit_native_arm_##name
372
Damien George0b610de2014-09-29 16:25:04 +0100373#define REG_RET ASM_ARM_REG_R0
374#define REG_ARG_1 ASM_ARM_REG_R0
375#define REG_ARG_2 ASM_ARM_REG_R1
376#define REG_ARG_3 ASM_ARM_REG_R2
377#define REG_ARG_4 ASM_ARM_REG_R3
Damien George81057362014-09-07 01:06:19 +0100378
Damien George0b610de2014-09-29 16:25:04 +0100379#define REG_TEMP0 ASM_ARM_REG_R0
380#define REG_TEMP1 ASM_ARM_REG_R1
381#define REG_TEMP2 ASM_ARM_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100382
Damien George0b610de2014-09-29 16:25:04 +0100383#define REG_LOCAL_1 ASM_ARM_REG_R4
384#define REG_LOCAL_2 ASM_ARM_REG_R5
385#define REG_LOCAL_3 ASM_ARM_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100386#define REG_LOCAL_NUM (3)
387
388#define ASM_PASS_COMPUTE ASM_ARM_PASS_COMPUTE
389#define ASM_PASS_EMIT ASM_ARM_PASS_EMIT
390
391#define ASM_T asm_arm_t
392#define ASM_NEW asm_arm_new
393#define ASM_FREE asm_arm_free
394#define ASM_GET_CODE asm_arm_get_code
395#define ASM_GET_CODE_SIZE asm_arm_get_code_size
396#define ASM_START_PASS asm_arm_start_pass
397#define ASM_END_PASS asm_arm_end_pass
398#define ASM_ENTRY asm_arm_entry
399#define ASM_EXIT asm_arm_exit
400
401#define ASM_LABEL_ASSIGN asm_arm_label_assign
402#define ASM_JUMP asm_arm_b_label
403#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
404 do { \
405 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100406 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100407 } while (0)
408#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
409 do { \
410 asm_arm_cmp_reg_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100411 asm_arm_bcc_label(as, ASM_ARM_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100412 } while (0)
413#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
414 do { \
415 asm_arm_cmp_reg_reg(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100416 asm_arm_bcc_label(as, ASM_ARM_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100417 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100418#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 +0100419
420#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_arm_mov_local_reg(as, (local_num), (reg))
421#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
422#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_arm_mov_reg_i32(as, (reg), (imm))
423#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
424 do { \
425 asm_arm_mov_reg_i32(as, (reg_temp), (imm)); \
426 asm_arm_mov_local_reg(as, (local_num), (reg_temp)); \
427 } while (false)
428#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 +0100429#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 +0100430#define ASM_MOV_LOCAL_ADDR_TO_REG(as, local_num, reg) asm_arm_mov_reg_local_addr(as, (reg), (local_num))
431
Fabian Vogte5268962014-10-04 00:53:46 +0200432#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_arm_lsl_reg_reg((as), (reg_dest), (reg_shift))
433#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 +0100434#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_arm_orr_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
435#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_arm_eor_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
436#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 +0100437#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_arm_add_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
438#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_arm_sub_reg_reg_reg((as), (reg_dest), (reg_dest), (reg_src))
439
Damien George91cfd412014-10-12 16:59:29 +0100440#define ASM_LOAD_REG_REG(as, reg_dest, reg_base) asm_arm_ldr_reg_reg((as), (reg_dest), (reg_base))
441#define ASM_LOAD8_REG_REG(as, reg_dest, reg_base) asm_arm_ldrb_reg_reg((as), (reg_dest), (reg_base))
442#define ASM_LOAD16_REG_REG(as, reg_dest, reg_base) asm_arm_ldrh_reg_reg((as), (reg_dest), (reg_base))
443
Fabian Vogte5268962014-10-04 00:53:46 +0200444#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
445#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
446#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 +0100447
Damien Georgec90f59e2014-09-06 23:06:36 +0100448#else
449
450#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200451
Damien13ed3a62013-10-08 09:05:10 +0100452#endif
453
454typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100455 STACK_VALUE,
456 STACK_REG,
457 STACK_IMM,
458} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100459
Damien Georgee9dac3b2014-09-29 22:10:41 +0100460// these enums must be distinct and the bottom 2 bits
461// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100462typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100463 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
464 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
465 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
466 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
467
468 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
469 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
470 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
471 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
472
473 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
474 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100475} vtype_kind_t;
476
Damienff8ed772013-10-08 22:18:32 +0100477typedef struct _stack_info_t {
478 vtype_kind_t vtype;
479 stack_info_kind_t kind;
480 union {
481 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100482 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100483 };
484} stack_info_t;
485
Damien13ed3a62013-10-08 09:05:10 +0100486struct _emit_t {
487 int pass;
488
489 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100490
Damien George2ac4af62014-08-15 16:45:41 +0100491 vtype_kind_t return_vtype;
492
Damien George7ff996c2014-09-08 23:05:16 +0100493 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100494 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100495
Damien George7ff996c2014-09-08 23:05:16 +0100496 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100497 stack_info_t *stack_info;
Damien George21ca2d72014-10-19 19:00:51 +0100498 vtype_kind_t saved_stack_vtype;
Damienff8ed772013-10-08 22:18:32 +0100499
Damien13ed3a62013-10-08 09:05:10 +0100500 int stack_start;
501 int stack_size;
502
503 bool last_emit_was_return_value;
504
Damien13ed3a62013-10-08 09:05:10 +0100505 scope_t *scope;
506
Damien Georgec90f59e2014-09-06 23:06:36 +0100507 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100508};
509
Damien George7ff996c2014-09-08 23:05:16 +0100510emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100511 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100512 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100513 return emit;
514}
515
Damien George41d02b62014-01-24 22:42:28 +0000516void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100517 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100518 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
519 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200520 m_del_obj(emit_t, emit);
521}
522
Damien George2ac4af62014-08-15 16:45:41 +0100523STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
524 switch (op) {
525 case MP_EMIT_NATIVE_TYPE_ENABLE:
526 emit->do_viper_types = arg1;
527 break;
528
529 default: {
530 vtype_kind_t type;
531 switch (arg2) {
532 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
533 case MP_QSTR_bool: type = VTYPE_BOOL; break;
534 case MP_QSTR_int: type = VTYPE_INT; break;
535 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100536 case MP_QSTR_ptr: type = VTYPE_PTR; break;
537 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
538 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100539 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
540 }
541 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
542 emit->return_vtype = type;
543 } else {
544 assert(arg1 < emit->local_vtype_alloc);
545 emit->local_vtype[arg1] = type;
546 }
547 break;
548 }
549 }
Damien13ed3a62013-10-08 09:05:10 +0100550}
551
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200552STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000553 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
554
Damien13ed3a62013-10-08 09:05:10 +0100555 emit->pass = pass;
556 emit->stack_start = 0;
557 emit->stack_size = 0;
558 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100559 emit->scope = scope;
560
Damien George36db6bc2014-05-07 17:24:22 +0100561 // allocate memory for keeping track of the types of locals
562 if (emit->local_vtype_alloc < scope->num_locals) {
563 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
564 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100565 }
Damien George36db6bc2014-05-07 17:24:22 +0100566
567 // allocate memory for keeping track of the objects on the stack
568 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100569 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100570 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100571 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100572 }
573
Damien George2ac4af62014-08-15 16:45:41 +0100574 // set default type for return and arguments
575 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100576 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100577 emit->local_vtype[i] = VTYPE_PYOBJ;
578 }
Damien Georgea5190a72014-08-15 22:39:08 +0100579
580 // local variables begin unbound, and have unknown type
581 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
582 emit->local_vtype[i] = VTYPE_UNBOUND;
583 }
584
585 // values on stack begin unbound
586 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100587 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100588 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100589 }
590
Damien Georgec90f59e2014-09-06 23:06:36 +0100591 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100592
593 // entry to function
594 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100595 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100596 num_locals = scope->num_locals - REG_LOCAL_NUM;
597 if (num_locals < 0) {
598 num_locals = 0;
599 }
600 emit->stack_start = num_locals;
601 num_locals += scope->stack_size;
602 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100603 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100604
605 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100606#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100607 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100608 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100609 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100610 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100611 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100612 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100613 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100614 } else if (i == 3) {
615 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100616 } else {
617 // TODO not implemented
618 assert(0);
619 }
620 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100621#elif N_X86
622 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100623 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000624 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100625 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000626 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000627 } else if (i == 2) {
628 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100629 } else {
Damien George03281b32014-09-06 22:38:50 +0000630 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
631 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100632 }
633 }
Damien3ef4abb2013-10-12 16:53:13 +0100634#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100635 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100636 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100637 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100638 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100639 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100640 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100641 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100642 } else if (i == 3) {
643 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
644 } else {
645 // TODO not implemented
646 assert(0);
647 }
648 }
649
Damien Georgee9dac3b2014-09-29 22:10:41 +0100650 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100651 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200652#elif N_ARM
653 for (int i = 0; i < scope->num_pos_args; i++) {
654 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100655 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200656 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100657 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200658 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100659 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200660 } else if (i == 3) {
661 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
662 } else {
663 // TODO not implemented
664 assert(0);
665 }
666 }
667
Damien Georgee9dac3b2014-09-29 22:10:41 +0100668 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100669 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100670#else
671 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100672#endif
673}
674
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200675STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100676 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100677 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100678 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100679 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100680
681 // check stack is back to zero size
682 if (emit->stack_size != 0) {
683 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
684 }
685
Damien George36db6bc2014-05-07 17:24:22 +0100686 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100687 void *f = ASM_GET_CODE(emit->as);
688 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100689
690 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100691 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100692 mp_uint_t type_sig = emit->return_vtype & 3;
693 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
694 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
695 }
696
697 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 +0100698 }
699}
700
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200701STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100702 return emit->last_emit_was_return_value;
703}
704
Damien Georged6230f62014-09-23 14:10:03 +0000705STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
Damien Georged6230f62014-09-23 14:10:03 +0000706 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100707 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100708 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100709 emit->scope->stack_size = emit->stack_size;
710 }
Damien George21ca2d72014-10-19 19:00:51 +0100711#ifdef DEBUG_PRINT
712 DEBUG_printf(" adjust_stack; stack_size=%d+%d; stack now:", emit->stack_size - stack_size_delta, stack_size_delta);
713 for (int i = 0; i < emit->stack_size; i++) {
714 stack_info_t *si = &emit->stack_info[i];
715 DEBUG_printf(" (v=%d k=%d %d)", si->vtype, si->kind, si->u_reg);
716 }
717 DEBUG_printf("\n");
718#endif
Damien13ed3a62013-10-08 09:05:10 +0100719}
720
Damien Georged6230f62014-09-23 14:10:03 +0000721STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
Damien George21ca2d72014-10-19 19:00:51 +0100722 DEBUG_printf("adjust_stack_size(" INT_FMT ")\n", delta);
Damien Georged6230f62014-09-23 14:10:03 +0000723 // If we are adjusting the stack in a positive direction (pushing) then we
724 // need to fill in values for the stack kind and vtype of the newly-pushed
725 // entries. These should be set to "value" (ie not reg or imm) because we
726 // should only need to adjust the stack due to a jump to this part in the
727 // code (and hence we have settled the stack before the jump).
728 for (mp_int_t i = 0; i < delta; i++) {
729 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
730 si->kind = STACK_VALUE;
Damien George21ca2d72014-10-19 19:00:51 +0100731 // TODO we don't know the vtype to use here. At the moment this is a
732 // hack to get the case of multi comparison working.
733 if (delta == 1) {
734 si->vtype = emit->saved_stack_vtype;
735 } else {
736 si->vtype = VTYPE_PYOBJ;
737 }
Damien Georged6230f62014-09-23 14:10:03 +0000738 }
739 adjust_stack(emit, delta);
740}
741
742STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000743 (void)emit;
744 (void)source_line;
Damien Georged6230f62014-09-23 14:10:03 +0000745}
746
Damienff8ed772013-10-08 22:18:32 +0100747/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200748STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100749 adjust_stack(emit, stack_size_delta);
750 emit->last_emit_was_return_value = false;
751}
Damienff8ed772013-10-08 22:18:32 +0100752*/
Damien13ed3a62013-10-08 09:05:10 +0100753
Damienff8ed772013-10-08 22:18:32 +0100754// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000755STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100756 emit->last_emit_was_return_value = false;
757 // settle the stack
758 /*
759 if (regs_needed != 0) {
760 for (int i = 0; i < emit->stack_size; i++) {
761 switch (emit->stack_info[i].kind) {
762 case STACK_VALUE:
763 break;
764
765 case STACK_REG:
766 // TODO only push reg if in regs_needed
767 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100768 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100769 break;
770
771 case STACK_IMM:
772 // don't think we ever need to push imms for settling
773 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
774 break;
775 }
776 }
777 }
778 */
Damien13ed3a62013-10-08 09:05:10 +0100779}
780
Damien George3112cde2014-09-29 18:45:42 +0100781// depth==0 is top, depth==1 is before top, etc
782STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
783 return &emit->stack_info[emit->stack_size - 1 - depth];
784}
785
786// depth==0 is top, depth==1 is before top, etc
787STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
788 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100789}
Damien13ed3a62013-10-08 09:05:10 +0100790
Damiend2755ec2013-10-16 23:58:48 +0100791// pos=1 is TOS, pos=2 is next, etc
792// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200793STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100794 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100795 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100796 if (i != skip_stack_pos) {
797 stack_info_t *si = &emit->stack_info[i];
798 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
799 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100800 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100801 }
Damienff8ed772013-10-08 22:18:32 +0100802 }
803 }
804}
Damien13ed3a62013-10-08 09:05:10 +0100805
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200806STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100807 for (int i = 0; i < emit->stack_size; i++) {
808 stack_info_t *si = &emit->stack_info[i];
809 if (si->kind == STACK_REG) {
810 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100811 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100812 }
Damien13ed3a62013-10-08 09:05:10 +0100813 }
814}
815
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200816STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000817 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100818 for (int i = 0; i < emit->stack_size; i++) {
819 stack_info_t *si = &emit->stack_info[i];
820 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000821 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100822 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100823 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100824 }
825 }
826 for (int i = 0; i < emit->stack_size; i++) {
827 stack_info_t *si = &emit->stack_info[i];
828 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000829 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100830 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100831 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100832 }
833 }
834}
835
836// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200837STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100838 need_reg_single(emit, reg_dest, pos);
839 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100840 *vtype = si->vtype;
841 switch (si->kind) {
842 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100843 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100844 break;
845
Damienff8ed772013-10-08 22:18:32 +0100846 case STACK_REG:
847 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100848 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100849 }
850 break;
851
Damienff8ed772013-10-08 22:18:32 +0100852 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100853 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100854 break;
855 }
Damien13ed3a62013-10-08 09:05:10 +0100856}
857
Damien Georgee9dac3b2014-09-29 22:10:41 +0100858// does an efficient X=pop(); discard(); push(X)
859// needs a (non-temp) register in case the poped element was stored in the stack
860STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
861 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
862 si[0] = si[1];
863 if (si->kind == STACK_VALUE) {
864 // if folded element was on the stack we need to put it in a register
865 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
866 si->kind = STACK_REG;
867 si->u_reg = reg_dest;
868 }
869 adjust_stack(emit, -1);
870}
871
872// If stacked value is in a register and the register is not r1 or r2, then
873// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
874STATIC 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 +0100875 emit->last_emit_was_return_value = false;
876 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100877 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100878 *vtype = si->vtype;
879 *reg_dest = si->u_reg;
880 need_reg_single(emit, *reg_dest, 1);
881 } else {
882 emit_access_stack(emit, 1, vtype, *reg_dest);
883 }
884 adjust_stack(emit, -1);
885}
886
Damien Georgee6ce10a2014-09-06 18:38:20 +0100887STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100888 emit->last_emit_was_return_value = false;
889 adjust_stack(emit, -1);
890}
891
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200892STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100893 emit->last_emit_was_return_value = false;
894 emit_access_stack(emit, 1, vtype, reg_dest);
895 adjust_stack(emit, -1);
896}
897
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200898STATIC 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 +0100899 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100900 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100901}
902
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200903STATIC 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 +0100904 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100905 emit_pre_pop_reg(emit, vtypeb, regb);
906 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100907}
908
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200909STATIC void emit_post(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +0000910 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +0100911}
912
Damien Georgee9dac3b2014-09-29 22:10:41 +0100913STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
914 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
915 si->vtype = new_vtype;
916}
917
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200918STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100919 stack_info_t *si = &emit->stack_info[emit->stack_size];
920 si->vtype = vtype;
921 si->kind = STACK_REG;
922 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100923 adjust_stack(emit, 1);
924}
925
Damien George40f3c022014-07-03 13:25:24 +0100926STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100927 stack_info_t *si = &emit->stack_info[emit->stack_size];
928 si->vtype = vtype;
929 si->kind = STACK_IMM;
930 si->u_imm = imm;
931 adjust_stack(emit, 1);
932}
933
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200934STATIC 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 +0100935 emit_post_push_reg(emit, vtypea, rega);
936 emit_post_push_reg(emit, vtypeb, regb);
937}
938
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200939STATIC 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 +0100940 emit_post_push_reg(emit, vtypea, rega);
941 emit_post_push_reg(emit, vtypeb, regb);
942 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100943}
944
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200945STATIC 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 +0100946 emit_post_push_reg(emit, vtypea, rega);
947 emit_post_push_reg(emit, vtypeb, regb);
948 emit_post_push_reg(emit, vtypec, regc);
949 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100950}
951
Damien George7fe21912014-08-16 22:31:57 +0100952STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100953 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100954 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100955}
956
Damien George7fe21912014-08-16 22:31:57 +0100957STATIC 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 +0100958 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100959 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
960 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100961}
962
Damien George40f3c022014-07-03 13:25:24 +0100963// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100964STATIC 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 +0100965 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100966 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
967 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100968}
969
Damien George7fe21912014-08-16 22:31:57 +0100970STATIC 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 +0000971 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100972 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
973 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
974 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000975}
976
Damien George40f3c022014-07-03 13:25:24 +0100977// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100978STATIC 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 +0100979 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100980 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
981 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
982 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
983 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100984}
985
Damien George86de21b2014-08-16 22:06:11 +0100986// vtype of all n_pop objects is VTYPE_PYOBJ
987// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
988// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
989// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
990STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
991 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100992
Damien George86de21b2014-08-16 22:06:11 +0100993 // First, store any immediate values to their respective place on the stack.
994 for (mp_uint_t i = 0; i < n_pop; i++) {
995 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
996 // must push any imm's to stack
997 // must convert them to VTYPE_PYOBJ for viper code
998 if (si->kind == STACK_IMM) {
999 si->kind = STACK_VALUE;
1000 switch (si->vtype) {
1001 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +01001002 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 +01001003 break;
1004 case VTYPE_BOOL:
1005 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001006 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 +01001007 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001008 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 +01001009 }
1010 si->vtype = VTYPE_PYOBJ;
1011 break;
1012 case VTYPE_INT:
1013 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +01001014 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 +01001015 si->vtype = VTYPE_PYOBJ;
1016 break;
1017 default:
1018 // not handled
1019 assert(0);
1020 }
1021 }
1022
1023 // verify that this value is on the stack
1024 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001025 }
Damien George86de21b2014-08-16 22:06:11 +01001026
1027 // Second, convert any non-VTYPE_PYOBJ to that type.
1028 for (mp_uint_t i = 0; i < n_pop; i++) {
1029 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1030 if (si->vtype != VTYPE_PYOBJ) {
1031 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001032 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001033 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 +01001034 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001035 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001036 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001037 }
1038 }
1039
1040 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1041 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001042 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001043}
1044
1045// vtype of all n_push objects is VTYPE_PYOBJ
1046STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1047 need_reg_all(emit);
1048 for (mp_uint_t i = 0; i < n_push; i++) {
1049 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1050 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1051 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001052 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001053 adjust_stack(emit, n_push);
1054}
1055
Damien George7ff996c2014-09-08 23:05:16 +01001056STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1057 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001058}
1059
Damien George7ff996c2014-09-08 23:05:16 +01001060STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1061 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001062}
1063
Damien George7ff996c2014-09-08 23:05:16 +01001064STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1065 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001066}
1067
Damien George7ff996c2014-09-08 23:05:16 +01001068STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001069 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001070 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001071 // need to commit stack because we can jump here from elsewhere
1072 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001073 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001074 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001075}
1076
Damien Georgecdd96df2014-04-06 12:58:40 +01001077STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1078 DEBUG_printf("import_name %s\n", qstr_str(qst));
1079 vtype_kind_t vtype_fromlist;
1080 vtype_kind_t vtype_level;
1081 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1082 assert(vtype_fromlist == VTYPE_PYOBJ);
1083 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001084 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001085 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001086}
1087
Damien Georgecdd96df2014-04-06 12:58:40 +01001088STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1089 DEBUG_printf("import_from %s\n", qstr_str(qst));
1090 emit_native_pre(emit);
1091 vtype_kind_t vtype_module;
1092 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1093 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001094 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001095 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001096}
1097
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001098STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001099 DEBUG_printf("import_star\n");
1100 vtype_kind_t vtype_module;
1101 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1102 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001103 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001104 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001105}
1106
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001107STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001108 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001109 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001110 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001111 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001112 if (emit->do_viper_types) {
1113 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001114 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1115 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1116 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001117 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1118 }
1119 } else {
1120 vtype = VTYPE_PYOBJ;
1121 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001122 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1123 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1124 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001125 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1126 }
1127 }
1128 emit_post_push_imm(emit, vtype, val);
1129}
1130
Damien George40f3c022014-07-03 13:25:24 +01001131STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001132 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001133 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001134 if (emit->do_viper_types) {
1135 emit_post_push_imm(emit, VTYPE_INT, arg);
1136 } else {
1137 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1138 }
1139}
1140
Damien Georgecdd96df2014-04-06 12:58:40 +01001141STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001142 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001143 // for viper: load integer, check fits in 32 bits
1144 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001145 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001146 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001147}
1148
Damien George7ff996c2014-09-08 23:05:16 +01001149STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001150 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001151 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001152 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001153 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001154}
1155
Damien George7ff996c2014-09-08 23:05:16 +01001156STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001157 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001158 // TODO: Eventually we want to be able to work with raw pointers in viper to
1159 // do native array access. For now we just load them as any other object.
1160 /*
Damien13ed3a62013-10-08 09:05:10 +01001161 if (emit->do_viper_types) {
1162 // not implemented properly
1163 // load a pointer to the asciiz string?
1164 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001165 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001166 } else
1167 */
1168 {
Damien Georgeb601d952014-06-30 05:17:25 +01001169 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001170 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001171 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001172 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001173 }
Damien13ed3a62013-10-08 09:05:10 +01001174 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1175 }
1176}
1177
Damien Georgedab13852015-01-13 15:55:54 +00001178STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1179 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001180 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001181 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1182 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1183}
1184
Damien George3558f622014-04-20 17:50:40 +01001185STATIC void emit_native_load_null(emit_t *emit) {
1186 emit_native_pre(emit);
1187 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1188}
1189
Damien George0abb5602015-01-16 12:24:49 +00001190STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1191 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001192 vtype_kind_t vtype = emit->local_vtype[local_num];
1193 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001194 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001195 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001196 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001197#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001198 if (local_num == 0) {
1199 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001200 } else if (local_num == 1) {
1201 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1202 } else if (local_num == 2) {
1203 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001204 } else {
Damien George0b610de2014-09-29 16:25:04 +01001205 need_reg_single(emit, REG_TEMP0, 0);
1206 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1207 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001208 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001209#elif N_X86
1210 if (local_num == 0) {
1211 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001212 } else if (local_num == 1) {
1213 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001214 } else if (local_num == 2) {
1215 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001216 } else {
Damien George0b610de2014-09-29 16:25:04 +01001217 need_reg_single(emit, REG_TEMP0, 0);
1218 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1219 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001220 }
Damien3ef4abb2013-10-12 16:53:13 +01001221#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001222 if (local_num == 0) {
1223 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1224 } else if (local_num == 1) {
1225 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1226 } else if (local_num == 2) {
1227 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1228 } else {
Damien George0b610de2014-09-29 16:25:04 +01001229 need_reg_single(emit, REG_TEMP0, 0);
1230 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1231 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001232 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001233#elif N_ARM
1234 if (local_num == 0) {
1235 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1236 } else if (local_num == 1) {
1237 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1238 } else if (local_num == 2) {
1239 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1240 } else {
Damien George0b610de2014-09-29 16:25:04 +01001241 need_reg_single(emit, REG_TEMP0, 0);
1242 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1243 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001244 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001245#else
1246 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001247#endif
1248}
1249
Damien George7ff996c2014-09-08 23:05:16 +01001250STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001251 // not implemented
1252 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001253 (void)emit;
1254 (void)qst;
1255 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001256 assert(0);
1257}
1258
Damien George7ff996c2014-09-08 23:05:16 +01001259STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001260 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001261 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001262 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001263 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1264}
1265
Damien George7ff996c2014-09-08 23:05:16 +01001266STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001267 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001268 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001269 // check for builtin casting operators
1270 if (emit->do_viper_types && qst == MP_QSTR_int) {
1271 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1272 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1273 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1274 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1275 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1276 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1277 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1278 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1279 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1280 } else {
1281 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1282 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1283 }
Damien13ed3a62013-10-08 09:05:10 +01001284}
1285
Damien George7ff996c2014-09-08 23:05:16 +01001286STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001287 // depends on type of subject:
1288 // - integer, function, pointer to integers: error
1289 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001290 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001291 vtype_kind_t vtype_base;
1292 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1293 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001294 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001295 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1296}
1297
Damien George7ff996c2014-09-08 23:05:16 +01001298STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001299 vtype_kind_t vtype_base;
1300 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1301 assert(vtype_base == VTYPE_PYOBJ);
1302 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001303 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001304}
1305
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001306STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001307 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001308 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001309 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001310}
1311
Damien George729f7b42014-04-17 22:10:53 +01001312STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001313 DEBUG_printf("load_subscr\n");
1314 // need to compile: base[index]
1315
1316 // pop: index, base
1317 // optimise case where index is an immediate
1318 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1319
1320 if (vtype_base == VTYPE_PYOBJ) {
1321 // standard Python call
1322 vtype_kind_t vtype_index;
1323 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1324 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001325 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 +01001326 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1327 } else {
Damien George91cfd412014-10-12 16:59:29 +01001328 // viper load
1329 // TODO The different machine architectures have very different
1330 // capabilities and requirements for loads, so probably best to
1331 // write a completely separate load-optimiser for each one.
1332 stack_info_t *top = peek_stack(emit, 0);
1333 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1334 // index is an immediate
1335 mp_int_t index_value = top->u_imm;
1336 emit_pre_pop_discard(emit); // discard index
1337 int reg_base = REG_ARG_1;
1338 int reg_index = REG_ARG_2;
1339 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1340 switch (vtype_base) {
1341 case VTYPE_PTR8: {
1342 // pointer to 8-bit memory
1343 // TODO optimise to use thumb ldrb r1, [r2, r3]
1344 if (index_value != 0) {
1345 // index is non-zero
1346 #if N_THUMB
1347 if (index_value > 0 && index_value < 32) {
1348 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1349 break;
1350 }
1351 #endif
1352 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1353 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1354 reg_base = reg_index;
1355 }
1356 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1357 break;
1358 }
1359 case VTYPE_PTR16: {
1360 // pointer to 16-bit memory
1361 if (index_value != 0) {
1362 // index is a non-zero immediate
1363 #if N_THUMB
1364 if (index_value > 0 && index_value < 32) {
1365 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1366 break;
1367 }
1368 #endif
1369 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1370 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1371 reg_base = reg_index;
1372 }
1373 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1374 break;
1375 }
1376 default:
1377 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1378 }
1379 } else {
1380 // index is not an immediate
1381 vtype_kind_t vtype_index;
1382 int reg_index = REG_ARG_2;
1383 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1384 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1385 switch (vtype_base) {
1386 case VTYPE_PTR8: {
1387 // pointer to 8-bit memory
1388 // TODO optimise to use thumb ldrb r1, [r2, r3]
1389 assert(vtype_index == VTYPE_INT);
1390 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1391 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1392 break;
1393 }
1394 case VTYPE_PTR16: {
1395 // pointer to 16-bit memory
1396 assert(vtype_index == VTYPE_INT);
1397 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1398 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1399 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1400 break;
1401 }
1402 default:
1403 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1404 }
1405 }
1406 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001407 }
1408}
1409
Damien George7ff996c2014-09-08 23:05:16 +01001410STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001411 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001412#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001413 if (local_num == 0) {
1414 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001415 } else if (local_num == 1) {
1416 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1417 } else if (local_num == 2) {
1418 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001419 } else {
Damien George0b610de2014-09-29 16:25:04 +01001420 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1421 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001422 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001423#elif N_X86
1424 if (local_num == 0) {
1425 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001426 } else if (local_num == 1) {
1427 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001428 } else if (local_num == 2) {
1429 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001430 } else {
Damien George0b610de2014-09-29 16:25:04 +01001431 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1432 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001433 }
Damien3ef4abb2013-10-12 16:53:13 +01001434#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001435 if (local_num == 0) {
1436 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1437 } else if (local_num == 1) {
1438 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1439 } else if (local_num == 2) {
1440 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1441 } else {
Damien George0b610de2014-09-29 16:25:04 +01001442 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1443 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001444 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001445#elif N_ARM
1446 if (local_num == 0) {
1447 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1448 } else if (local_num == 1) {
1449 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1450 } else if (local_num == 2) {
1451 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1452 } else {
Damien George0b610de2014-09-29 16:25:04 +01001453 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1454 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001455 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001456#else
1457 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001458#endif
1459
1460 emit_post(emit);
1461
1462 // check types
1463 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1464 // first time this local is assigned, so give it a type of the object stored in it
1465 emit->local_vtype[local_num] = vtype;
1466 } else if (emit->local_vtype[local_num] != vtype) {
1467 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001468 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 +01001469 }
1470}
1471
Damien George7ff996c2014-09-08 23:05:16 +01001472STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001473 // not implemented
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001474 (void)emit;
1475 (void)qst;
1476 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001477 assert(0);
1478}
1479
Damien George7ff996c2014-09-08 23:05:16 +01001480STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001481 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001482 vtype_kind_t vtype;
1483 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1484 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001485 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001486 emit_post(emit);
1487}
1488
Damien George7ff996c2014-09-08 23:05:16 +01001489STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001490 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001491 if (vtype == VTYPE_PYOBJ) {
1492 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1493 } else {
1494 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001495 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 +01001496 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001497 }
Damien George7ff996c2014-09-08 23:05:16 +01001498 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001499 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001500}
1501
Damien George7ff996c2014-09-08 23:05:16 +01001502STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001503 vtype_kind_t vtype_base, vtype_val;
1504 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1505 assert(vtype_base == VTYPE_PYOBJ);
1506 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001507 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001508 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001509}
1510
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001511STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001512 DEBUG_printf("store_subscr\n");
1513 // need to compile: base[index] = value
1514
1515 // pop: index, base, value
1516 // optimise case where index is an immediate
1517 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1518
1519 if (vtype_base == VTYPE_PYOBJ) {
1520 // standard Python call
1521 vtype_kind_t vtype_index, vtype_value;
1522 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1523 assert(vtype_index == VTYPE_PYOBJ);
1524 assert(vtype_value == VTYPE_PYOBJ);
1525 emit_call(emit, MP_F_OBJ_SUBSCR);
1526 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001527 // viper store
1528 // TODO The different machine architectures have very different
1529 // capabilities and requirements for stores, so probably best to
1530 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001531 stack_info_t *top = peek_stack(emit, 0);
1532 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1533 // index is an immediate
1534 mp_int_t index_value = top->u_imm;
1535 emit_pre_pop_discard(emit); // discard index
1536 vtype_kind_t vtype_value;
1537 int reg_base = REG_ARG_1;
1538 int reg_index = REG_ARG_2;
1539 int reg_value = REG_ARG_3;
1540 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001541 #if N_X86
1542 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1543 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1544 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001545 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001546 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001547 switch (vtype_base) {
1548 case VTYPE_PTR8: {
1549 // pointer to 8-bit memory
1550 // TODO optimise to use thumb strb r1, [r2, r3]
1551 if (index_value != 0) {
1552 // index is non-zero
1553 #if N_THUMB
1554 if (index_value > 0 && index_value < 32) {
1555 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1556 break;
1557 }
1558 #endif
1559 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001560 #if N_ARM
1561 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1562 return;
1563 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001564 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1565 reg_base = reg_index;
1566 }
1567 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1568 break;
1569 }
1570 case VTYPE_PTR16: {
1571 // pointer to 16-bit memory
1572 if (index_value != 0) {
1573 // index is a non-zero immediate
1574 #if N_THUMB
1575 if (index_value > 0 && index_value < 32) {
1576 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1577 break;
1578 }
1579 #endif
1580 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001581 #if N_ARM
1582 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1583 return;
1584 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001585 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1586 reg_base = reg_index;
1587 }
1588 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1589 break;
1590 }
1591 default:
1592 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1593 }
1594 } else {
1595 // index is not an immediate
1596 vtype_kind_t vtype_index, vtype_value;
1597 int reg_index = REG_ARG_2;
1598 int reg_value = REG_ARG_3;
1599 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1600 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001601 #if N_X86
1602 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1603 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1604 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001605 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001606 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001607 switch (vtype_base) {
1608 case VTYPE_PTR8: {
1609 // pointer to 8-bit memory
1610 // TODO optimise to use thumb strb r1, [r2, r3]
1611 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001612 #if N_ARM
1613 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1614 break;
1615 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001616 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1617 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1618 break;
1619 }
1620 case VTYPE_PTR16: {
1621 // pointer to 16-bit memory
1622 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001623 #if N_ARM
1624 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1625 break;
1626 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001627 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1628 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1629 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1630 break;
1631 }
1632 default:
1633 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1634 }
1635 }
1636
1637 }
Damien13ed3a62013-10-08 09:05:10 +01001638}
1639
Damien George7ff996c2014-09-08 23:05:16 +01001640STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001641 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001642 // could support for Python types, just set to None (so GC can reclaim it)
Paul Sokolovskydb1ac362015-01-01 09:43:42 +02001643 // local is automatically deleted for exception block "as" var, and the message
1644 // breaks tests.
1645 //mp_emitter_warning(emit->pass, "Native codegeneration doesn't support deleting local");
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001646 (void)emit;
1647 (void)qst;
1648 (void)local_num;
Damien13ed3a62013-10-08 09:05:10 +01001649}
1650
Damien George7ff996c2014-09-08 23:05:16 +01001651STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001652 // TODO implement me!
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001653 (void)emit;
1654 (void)qst;
1655 (void)local_num;
Damien9ecbcff2013-12-11 00:41:43 +00001656}
1657
Damien Georgee6ce10a2014-09-06 18:38:20 +01001658STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1659 emit_native_pre(emit);
1660 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1661 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001662}
1663
Damien Georgee6ce10a2014-09-06 18:38:20 +01001664STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1665 emit_native_pre(emit);
1666 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1667 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001668}
1669
Damien Georgee6ce10a2014-09-06 18:38:20 +01001670STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001671 vtype_kind_t vtype_base;
1672 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1673 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001674 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 +01001675 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001676}
1677
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001678STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001679 vtype_kind_t vtype_index, vtype_base;
1680 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1681 assert(vtype_index == VTYPE_PYOBJ);
1682 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001683 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 +01001684}
1685
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001686STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001687 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001688 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001689 int reg = REG_TEMP0;
1690 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1691 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001692}
1693
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001694STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +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_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1698}
1699
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001700STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001701 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001702 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001703 emit_post(emit);
1704}
1705
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001706STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001707 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001708 vtype_kind_t vtype0, vtype1;
1709 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1710 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001711}
1712
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001713STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001714 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001715 vtype_kind_t vtype0, vtype1, vtype2;
1716 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1717 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1718}
1719
Damien George7ff996c2014-09-08 23:05:16 +01001720STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001721 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001722 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001723 // need to commit stack because we are jumping elsewhere
1724 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001725 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001726 emit_post(emit);
1727}
1728
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001729STATIC void emit_native_jump_helper(emit_t *emit, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001730 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001731 switch (vtype) {
1732 case VTYPE_PYOBJ:
1733 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1734 if (!pop) {
1735 adjust_stack(emit, 1);
1736 }
1737 emit_call(emit, MP_F_OBJ_IS_TRUE);
1738 break;
1739 case VTYPE_BOOL:
1740 case VTYPE_INT:
1741 case VTYPE_UINT:
1742 emit_pre_pop_reg(emit, &vtype, REG_RET);
1743 if (!pop) {
1744 adjust_stack(emit, 1);
1745 }
1746 break;
1747 default:
1748 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1749 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001750 }
Damien George21ca2d72014-10-19 19:00:51 +01001751 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1752 // can use it. This is a bit of a hack.
1753 if (!pop) {
1754 emit->saved_stack_vtype = vtype;
1755 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001756 // need to commit stack because we may jump elsewhere
1757 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001758}
1759
Damien George7ff996c2014-09-08 23:05:16 +01001760STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001761 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001762 emit_native_jump_helper(emit, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001763 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001764 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001765}
Damien1a6633a2013-11-03 13:58:19 +00001766
Damien George7ff996c2014-09-08 23:05:16 +01001767STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001768 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001769 emit_native_jump_helper(emit, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001770 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001771 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001772}
Damien Georgea32c1e42014-05-07 18:30:52 +01001773
Damien George7ff996c2014-09-08 23:05:16 +01001774STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001775 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001776 emit_native_jump_helper(emit, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001777 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001778 adjust_stack(emit, -1);
1779 emit_post(emit);
1780}
1781
Damien George7ff996c2014-09-08 23:05:16 +01001782STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001783 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001784 emit_native_jump_helper(emit, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001785 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001786 adjust_stack(emit, -1);
1787 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001788}
1789
Damien George7ff996c2014-09-08 23:05:16 +01001790STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001791 (void)except_depth;
Damien George25c84642014-05-30 15:20:41 +01001792 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001793}
Damien Georgea32c1e42014-05-07 18:30:52 +01001794
Damien George7ff996c2014-09-08 23:05:16 +01001795STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001796 (void)except_depth;
Damien Georgea32c1e42014-05-07 18:30:52 +01001797 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001798}
Damien Georgea32c1e42014-05-07 18:30:52 +01001799
Damien George7ff996c2014-09-08 23:05:16 +01001800STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001801 // not supported, or could be with runtime call
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001802 (void)emit;
1803 (void)label;
Damien13ed3a62013-10-08 09:05:10 +01001804 assert(0);
1805}
Damien Georgeb601d952014-06-30 05:17:25 +01001806
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001807STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001808 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01001809 assert(0);
1810}
Damien Georgeb601d952014-06-30 05:17:25 +01001811
Damien George7ff996c2014-09-08 23:05:16 +01001812STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001813 emit_native_pre(emit);
1814 // need to commit stack because we may jump elsewhere
1815 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001816 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 +01001817 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001818 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001819 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001820}
Damien Georgeb601d952014-06-30 05:17:25 +01001821
Damien George7ff996c2014-09-08 23:05:16 +01001822STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001823 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001824}
Damien Georgeb601d952014-06-30 05:17:25 +01001825
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001826STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001827 emit_pre_pop_discard(emit);
1828 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001829}
Damiend2755ec2013-10-16 23:58:48 +01001830
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001831STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001832 // perhaps the difficult one, as we want to rewrite for loops using native code
1833 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001834
1835 vtype_kind_t vtype;
1836 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1837 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001838 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001839 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001840}
Damiend2755ec2013-10-16 23:58:48 +01001841
Damien George7ff996c2014-09-08 23:05:16 +01001842STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001843 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001844 vtype_kind_t vtype;
1845 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1846 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001847 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001848 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1849 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001850 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1851}
1852
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001853STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001854 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001855 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001856 adjust_stack(emit, -1);
1857 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001858}
1859
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001860STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001861 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001862 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001863 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001864 emit_post(emit);
1865}
1866
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001867STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00001868 (void)emit;
Damien Georgeb601d952014-06-30 05:17:25 +01001869 /*
1870 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001871 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001872 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001873 emit_post(emit);
1874 */
Damien13ed3a62013-10-08 09:05:10 +01001875}
1876
Damien Georged17926d2014-03-30 13:35:08 +01001877STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001878 vtype_kind_t vtype;
1879 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1880 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001881 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001882 // we need to synthesise this operation by converting to bool first
1883 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001884 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001885 }
Damien Georged6230f62014-09-23 14:10:03 +00001886 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1887 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001888}
1889
Damien Georged17926d2014-03-30 13:35:08 +01001890STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001891 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001892 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1893 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001894 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001895 #if N_X64 || N_X86
1896 // special cases for x86 and shifting
1897 if (op == MP_BINARY_OP_LSHIFT
1898 || op == MP_BINARY_OP_INPLACE_LSHIFT
1899 || op == MP_BINARY_OP_RSHIFT
1900 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1901 #if N_X64
1902 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1903 #else
1904 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1905 #endif
1906 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1907 ASM_LSL_REG(emit->as, REG_RET);
1908 } else {
1909 ASM_ASR_REG(emit->as, REG_RET);
1910 }
1911 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1912 return;
1913 }
1914 #endif
1915 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001916 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001917 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1918 if (0) {
1919 // dummy
1920 #if !(N_X64 || N_X86)
1921 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1922 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001923 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001924 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1925 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1926 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1927 #endif
Damien George1ef23482014-10-12 14:21:06 +01001928 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1929 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1930 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1931 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1932 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1933 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1934 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1935 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1936 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001937 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1938 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1939 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1940 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1941 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1942 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1943 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1944 // comparison ops are (in enum order):
1945 // MP_BINARY_OP_LESS
1946 // MP_BINARY_OP_MORE
1947 // MP_BINARY_OP_EQUAL
1948 // MP_BINARY_OP_LESS_EQUAL
1949 // MP_BINARY_OP_MORE_EQUAL
1950 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001951 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001952 #if N_X64
1953 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1954 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1955 static byte ops[6] = {
1956 ASM_X64_CC_JL,
1957 ASM_X64_CC_JG,
1958 ASM_X64_CC_JE,
1959 ASM_X64_CC_JLE,
1960 ASM_X64_CC_JGE,
1961 ASM_X64_CC_JNE,
1962 };
1963 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1964 #elif N_X86
1965 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1966 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1967 static byte ops[6] = {
1968 ASM_X86_CC_JL,
1969 ASM_X86_CC_JG,
1970 ASM_X86_CC_JE,
1971 ASM_X86_CC_JLE,
1972 ASM_X86_CC_JGE,
1973 ASM_X86_CC_JNE,
1974 };
1975 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1976 #elif N_THUMB
1977 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1978 static uint16_t ops[6] = {
1979 ASM_THUMB_OP_ITE_GE,
1980 ASM_THUMB_OP_ITE_GT,
1981 ASM_THUMB_OP_ITE_EQ,
1982 ASM_THUMB_OP_ITE_GT,
1983 ASM_THUMB_OP_ITE_GE,
1984 ASM_THUMB_OP_ITE_EQ,
1985 };
1986 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1987 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1988 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1989 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1990 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001991 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1992 static uint ccs[6] = {
1993 ASM_ARM_CC_LT,
1994 ASM_ARM_CC_GT,
1995 ASM_ARM_CC_EQ,
1996 ASM_ARM_CC_LE,
1997 ASM_ARM_CC_GE,
1998 ASM_ARM_CC_NE,
1999 };
2000 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01002001 #else
2002 #error not implemented
2003 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00002004 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
2005 } else {
2006 // TODO other ops not yet implemented
2007 assert(0);
2008 }
Damien13ed3a62013-10-08 09:05:10 +01002009 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01002010 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00002011 bool invert = false;
2012 if (op == MP_BINARY_OP_NOT_IN) {
2013 invert = true;
2014 op = MP_BINARY_OP_IN;
2015 } else if (op == MP_BINARY_OP_IS_NOT) {
2016 invert = true;
2017 op = MP_BINARY_OP_IS;
2018 }
Damien George7fe21912014-08-16 22:31:57 +01002019 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00002020 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002021 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002022 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2023 }
Damien13ed3a62013-10-08 09:05:10 +01002024 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2025 } else {
2026 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002027 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002028 }
2029}
2030
Damien George7ff996c2014-09-08 23:05:16 +01002031STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002032 // for viper: call runtime, with types of args
2033 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002034 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002035 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002036 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002037 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002038}
2039
Damien George7ff996c2014-09-08 23:05:16 +01002040STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002041 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002042 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002043 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002044 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2045}
2046
Damien George7ff996c2014-09-08 23:05:16 +01002047STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002048 // only used in list comprehension
2049 vtype_kind_t vtype_list, vtype_item;
2050 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2051 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2052 assert(vtype_list == VTYPE_PYOBJ);
2053 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002054 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002055 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002056}
2057
Damien George7ff996c2014-09-08 23:05:16 +01002058STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002059 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002060 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002061 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2062}
2063
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002064STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002065 vtype_kind_t vtype_key, vtype_value, vtype_map;
2066 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
2067 assert(vtype_key == VTYPE_PYOBJ);
2068 assert(vtype_value == VTYPE_PYOBJ);
2069 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002070 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002071 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2072}
2073
Damien George7ff996c2014-09-08 23:05:16 +01002074STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002075 // only used in list comprehension
2076 vtype_kind_t vtype_map, vtype_key, vtype_value;
2077 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2078 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2079 assert(vtype_map == VTYPE_PYOBJ);
2080 assert(vtype_key == VTYPE_PYOBJ);
2081 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002082 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002083 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002084}
2085
Damien Georgee37dcaa2014-12-27 17:07:16 +00002086#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002087STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002088 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002089 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002090 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002091 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2092}
2093
Damien George7ff996c2014-09-08 23:05:16 +01002094STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002095 // only used in set comprehension
2096 vtype_kind_t vtype_set, vtype_item;
2097 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2098 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2099 assert(vtype_set == VTYPE_PYOBJ);
2100 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002101 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002102 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002103}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002104#endif
Damiend2755ec2013-10-16 23:58:48 +01002105
Damien George83204f32014-12-27 17:20:41 +00002106#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002107STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002108 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002109 if (n_args == 2) {
2110 vtype_kind_t vtype_start, vtype_stop;
2111 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2112 assert(vtype_start == VTYPE_PYOBJ);
2113 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002114 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 +01002115 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2116 } else {
2117 assert(n_args == 3);
2118 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2119 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
2120 assert(vtype_start == VTYPE_PYOBJ);
2121 assert(vtype_stop == VTYPE_PYOBJ);
2122 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002123 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002124 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2125 }
Damien13ed3a62013-10-08 09:05:10 +01002126}
Damien George83204f32014-12-27 17:20:41 +00002127#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002128
Damien George7ff996c2014-09-08 23:05:16 +01002129STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002130 DEBUG_printf("unpack_sequence %d\n", n_args);
2131 vtype_kind_t vtype_base;
2132 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2133 assert(vtype_base == VTYPE_PYOBJ);
2134 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002135 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002136}
Damien Georgecdd96df2014-04-06 12:58:40 +01002137
Damien George7ff996c2014-09-08 23:05:16 +01002138STATIC 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 +01002139 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2140 vtype_kind_t vtype_base;
2141 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2142 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002143 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 +01002144 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 +01002145}
2146
Damien George7ff996c2014-09-08 23:05:16 +01002147STATIC 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 +01002148 // 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 +00002149 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002150 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002151 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 +01002152 } else {
2153 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2154 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2155 assert(vtype_def_tuple == VTYPE_PYOBJ);
2156 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002157 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 +01002158 }
Damien13ed3a62013-10-08 09:05:10 +01002159 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2160}
2161
Damien George7ff996c2014-09-08 23:05:16 +01002162STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002163 (void)emit;
2164 (void)scope;
2165 (void)n_closed_over;
2166 (void)n_pos_defaults;
2167 (void)n_kw_defaults;
Damien13ed3a62013-10-08 09:05:10 +01002168 assert(0);
2169}
2170
Damien George7ff996c2014-09-08 23:05:16 +01002171STATIC 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 +00002172 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2173
Damien Georgee9dac3b2014-09-29 22:10:41 +01002174 // TODO: in viper mode, call special runtime routine with type info for args,
2175 // and wanted type info for return, to remove need for boxing/unboxing
2176
Damien George922ddd62014-04-09 12:43:17 +01002177 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002178
Damien Georgee9dac3b2014-09-29 22:10:41 +01002179 emit_native_pre(emit);
2180 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2181 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2182 // casting operator
2183 assert(n_positional == 1 && n_keyword == 0);
2184 DEBUG_printf(" cast to %d\n", vtype_fun);
2185 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2186 switch (peek_vtype(emit, 0)) {
2187 case VTYPE_PYOBJ: {
2188 vtype_kind_t vtype;
2189 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2190 emit_pre_pop_discard(emit);
2191 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2192 emit_post_push_reg(emit, vtype_cast, REG_RET);
2193 break;
2194 }
2195 case VTYPE_BOOL:
2196 case VTYPE_INT:
2197 case VTYPE_UINT:
2198 case VTYPE_PTR:
2199 case VTYPE_PTR8:
2200 case VTYPE_PTR16:
2201 case VTYPE_PTR_NONE:
2202 emit_fold_stack_top(emit, REG_ARG_1);
2203 emit_post_top_set_vtype(emit, vtype_cast);
2204 break;
2205 default:
2206 assert(!"TODO: convert obj to int");
2207 }
2208 } else {
2209 if (n_positional != 0 || n_keyword != 0) {
2210 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2211 }
Damien13ed3a62013-10-08 09:05:10 +01002212 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2213 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002214 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2215 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002216 }
Damien13ed3a62013-10-08 09:05:10 +01002217}
2218
Damien George7ff996c2014-09-08 23:05:16 +01002219STATIC 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 +01002220 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002221 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002222 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 +01002223 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 +01002224 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2225}
2226
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002227STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002228 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002229 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002230 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2231 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002232 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002233 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002234 } else {
2235 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002236 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002237 } else {
2238 vtype_kind_t vtype;
2239 emit_pre_pop_reg(emit, &vtype, REG_RET);
2240 if (vtype != emit->return_vtype) {
2241 printf("ViperTypeError: incompatible return type\n");
2242 }
Damien George2ac4af62014-08-15 16:45:41 +01002243 }
Damien13ed3a62013-10-08 09:05:10 +01002244 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002245 vtype_kind_t vtype;
2246 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002247 assert(vtype == VTYPE_PYOBJ);
2248 }
2249 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002250 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2251 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002252}
2253
Damien George7ff996c2014-09-08 23:05:16 +01002254STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002255 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002256 vtype_kind_t vtype_exc;
2257 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2258 if (vtype_exc != VTYPE_PYOBJ) {
2259 printf("ViperTypeError: must raise an object\n");
2260 }
2261 // 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 +01002262 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002263}
Damien Georgeb601d952014-06-30 05:17:25 +01002264
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002265STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002266 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002267 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002268 assert(0);
2269}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002270STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002271 // not supported (for now)
Damien Georgeff8dd3f2015-01-20 12:47:20 +00002272 (void)emit;
Damien13ed3a62013-10-08 09:05:10 +01002273 assert(0);
2274}
2275
Damien Georgeb601d952014-06-30 05:17:25 +01002276STATIC void emit_native_start_except_handler(emit_t *emit) {
2277 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2278 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2279 // the first 2 elements, so we can get the thrown value.
2280 adjust_stack(emit, 2);
2281 vtype_kind_t vtype_nlr;
2282 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002283 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002284 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
2285}
2286
2287STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002288 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002289}
2290
Damien13ed3a62013-10-08 09:05:10 +01002291const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002292 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002293 emit_native_start_pass,
2294 emit_native_end_pass,
2295 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002296 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002297 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002298
2299 emit_native_load_id,
2300 emit_native_store_id,
2301 emit_native_delete_id,
2302
2303 emit_native_label_assign,
2304 emit_native_import_name,
2305 emit_native_import_from,
2306 emit_native_import_star,
2307 emit_native_load_const_tok,
2308 emit_native_load_const_small_int,
2309 emit_native_load_const_int,
2310 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002311 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002312 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002313 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002314 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002315 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002316 emit_native_load_name,
2317 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002318 emit_native_load_attr,
2319 emit_native_load_method,
2320 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002321 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002322 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002323 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002324 emit_native_store_name,
2325 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002326 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002327 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002328 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002329 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002330 emit_native_delete_name,
2331 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002332 emit_native_delete_attr,
2333 emit_native_delete_subscr,
2334 emit_native_dup_top,
2335 emit_native_dup_top_two,
2336 emit_native_pop_top,
2337 emit_native_rot_two,
2338 emit_native_rot_three,
2339 emit_native_jump,
2340 emit_native_pop_jump_if_true,
2341 emit_native_pop_jump_if_false,
2342 emit_native_jump_if_true_or_pop,
2343 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002344 emit_native_break_loop,
2345 emit_native_continue_loop,
2346 emit_native_setup_with,
2347 emit_native_with_cleanup,
2348 emit_native_setup_except,
2349 emit_native_setup_finally,
2350 emit_native_end_finally,
2351 emit_native_get_iter,
2352 emit_native_for_iter,
2353 emit_native_for_iter_end,
2354 emit_native_pop_block,
2355 emit_native_pop_except,
2356 emit_native_unary_op,
2357 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002358 emit_native_build_tuple,
2359 emit_native_build_list,
2360 emit_native_list_append,
2361 emit_native_build_map,
2362 emit_native_store_map,
2363 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002364 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002365 emit_native_build_set,
2366 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002367 #endif
Damien George83204f32014-12-27 17:20:41 +00002368 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002369 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002370 #endif
Damien13ed3a62013-10-08 09:05:10 +01002371 emit_native_unpack_sequence,
2372 emit_native_unpack_ex,
2373 emit_native_make_function,
2374 emit_native_make_closure,
2375 emit_native_call_function,
2376 emit_native_call_method,
2377 emit_native_return_value,
2378 emit_native_raise_varargs,
2379 emit_native_yield_value,
2380 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002381
2382 emit_native_start_except_handler,
2383 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002384};
2385
Damien Georgec90f59e2014-09-06 23:06:36 +01002386#endif