blob: a8e547f0d3eee057e408c454069ea69f23b42843 [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) {
743}
744
Damienff8ed772013-10-08 22:18:32 +0100745/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200746STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100747 adjust_stack(emit, stack_size_delta);
748 emit->last_emit_was_return_value = false;
749}
Damienff8ed772013-10-08 22:18:32 +0100750*/
Damien13ed3a62013-10-08 09:05:10 +0100751
Damienff8ed772013-10-08 22:18:32 +0100752// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000753STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100754 emit->last_emit_was_return_value = false;
755 // settle the stack
756 /*
757 if (regs_needed != 0) {
758 for (int i = 0; i < emit->stack_size; i++) {
759 switch (emit->stack_info[i].kind) {
760 case STACK_VALUE:
761 break;
762
763 case STACK_REG:
764 // TODO only push reg if in regs_needed
765 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100766 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100767 break;
768
769 case STACK_IMM:
770 // don't think we ever need to push imms for settling
771 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
772 break;
773 }
774 }
775 }
776 */
Damien13ed3a62013-10-08 09:05:10 +0100777}
778
Damien George3112cde2014-09-29 18:45:42 +0100779// depth==0 is top, depth==1 is before top, etc
780STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
781 return &emit->stack_info[emit->stack_size - 1 - depth];
782}
783
784// depth==0 is top, depth==1 is before top, etc
785STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
786 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100787}
Damien13ed3a62013-10-08 09:05:10 +0100788
Damiend2755ec2013-10-16 23:58:48 +0100789// pos=1 is TOS, pos=2 is next, etc
790// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200791STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100792 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100793 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100794 if (i != skip_stack_pos) {
795 stack_info_t *si = &emit->stack_info[i];
796 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
797 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100798 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100799 }
Damienff8ed772013-10-08 22:18:32 +0100800 }
801 }
802}
Damien13ed3a62013-10-08 09:05:10 +0100803
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200804STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100805 for (int i = 0; i < emit->stack_size; i++) {
806 stack_info_t *si = &emit->stack_info[i];
807 if (si->kind == STACK_REG) {
808 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100809 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100810 }
Damien13ed3a62013-10-08 09:05:10 +0100811 }
812}
813
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200814STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000815 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100816 for (int i = 0; i < emit->stack_size; i++) {
817 stack_info_t *si = &emit->stack_info[i];
818 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000819 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100820 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100821 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100822 }
823 }
824 for (int i = 0; i < emit->stack_size; i++) {
825 stack_info_t *si = &emit->stack_info[i];
826 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000827 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100828 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100829 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100830 }
831 }
832}
833
834// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200835STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100836 need_reg_single(emit, reg_dest, pos);
837 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100838 *vtype = si->vtype;
839 switch (si->kind) {
840 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100841 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100842 break;
843
Damienff8ed772013-10-08 22:18:32 +0100844 case STACK_REG:
845 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100846 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100847 }
848 break;
849
Damienff8ed772013-10-08 22:18:32 +0100850 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100851 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100852 break;
853 }
Damien13ed3a62013-10-08 09:05:10 +0100854}
855
Damien Georgee9dac3b2014-09-29 22:10:41 +0100856// does an efficient X=pop(); discard(); push(X)
857// needs a (non-temp) register in case the poped element was stored in the stack
858STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
859 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
860 si[0] = si[1];
861 if (si->kind == STACK_VALUE) {
862 // if folded element was on the stack we need to put it in a register
863 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
864 si->kind = STACK_REG;
865 si->u_reg = reg_dest;
866 }
867 adjust_stack(emit, -1);
868}
869
870// If stacked value is in a register and the register is not r1 or r2, then
871// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
872STATIC void emit_pre_pop_reg_flexible(emit_t *emit, vtype_kind_t *vtype, int *reg_dest, int not_r1, int not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100873 emit->last_emit_was_return_value = false;
874 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100875 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100876 *vtype = si->vtype;
877 *reg_dest = si->u_reg;
878 need_reg_single(emit, *reg_dest, 1);
879 } else {
880 emit_access_stack(emit, 1, vtype, *reg_dest);
881 }
882 adjust_stack(emit, -1);
883}
884
Damien Georgee6ce10a2014-09-06 18:38:20 +0100885STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100886 emit->last_emit_was_return_value = false;
887 adjust_stack(emit, -1);
888}
889
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200890STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100891 emit->last_emit_was_return_value = false;
892 emit_access_stack(emit, 1, vtype, reg_dest);
893 adjust_stack(emit, -1);
894}
895
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200896STATIC void emit_pre_pop_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb) {
Damien13ed3a62013-10-08 09:05:10 +0100897 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100898 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100899}
900
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200901STATIC void emit_pre_pop_reg_reg_reg(emit_t *emit, vtype_kind_t *vtypea, int rega, vtype_kind_t *vtypeb, int regb, vtype_kind_t *vtypec, int regc) {
Damien13ed3a62013-10-08 09:05:10 +0100902 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100903 emit_pre_pop_reg(emit, vtypeb, regb);
904 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100905}
906
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200907STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100908}
909
Damien Georgee9dac3b2014-09-29 22:10:41 +0100910STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
911 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
912 si->vtype = new_vtype;
913}
914
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200915STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100916 stack_info_t *si = &emit->stack_info[emit->stack_size];
917 si->vtype = vtype;
918 si->kind = STACK_REG;
919 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100920 adjust_stack(emit, 1);
921}
922
Damien George40f3c022014-07-03 13:25:24 +0100923STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100924 stack_info_t *si = &emit->stack_info[emit->stack_size];
925 si->vtype = vtype;
926 si->kind = STACK_IMM;
927 si->u_imm = imm;
928 adjust_stack(emit, 1);
929}
930
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200931STATIC 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 +0100932 emit_post_push_reg(emit, vtypea, rega);
933 emit_post_push_reg(emit, vtypeb, regb);
934}
935
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200936STATIC 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 +0100937 emit_post_push_reg(emit, vtypea, rega);
938 emit_post_push_reg(emit, vtypeb, regb);
939 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100940}
941
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200942STATIC 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 +0100943 emit_post_push_reg(emit, vtypea, rega);
944 emit_post_push_reg(emit, vtypeb, regb);
945 emit_post_push_reg(emit, vtypec, regc);
946 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100947}
948
Damien George7fe21912014-08-16 22:31:57 +0100949STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100950 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100951 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100952}
953
Damien George7fe21912014-08-16 22:31:57 +0100954STATIC 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 +0100955 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100956 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
957 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100958}
959
Damien George40f3c022014-07-03 13:25:24 +0100960// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100961STATIC 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 +0100962 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100963 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
964 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100965}
966
Damien George7fe21912014-08-16 22:31:57 +0100967STATIC 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 +0000968 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100969 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
970 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
971 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000972}
973
Damien George40f3c022014-07-03 13:25:24 +0100974// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100975STATIC 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 +0100976 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100977 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
978 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
979 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
980 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100981}
982
Damien George86de21b2014-08-16 22:06:11 +0100983// vtype of all n_pop objects is VTYPE_PYOBJ
984// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
985// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
986// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
987STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
988 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100989
Damien George86de21b2014-08-16 22:06:11 +0100990 // First, store any immediate values to their respective place on the stack.
991 for (mp_uint_t i = 0; i < n_pop; i++) {
992 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
993 // must push any imm's to stack
994 // must convert them to VTYPE_PYOBJ for viper code
995 if (si->kind == STACK_IMM) {
996 si->kind = STACK_VALUE;
997 switch (si->vtype) {
998 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100999 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 +01001000 break;
1001 case VTYPE_BOOL:
1002 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +01001003 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 +01001004 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +01001005 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 +01001006 }
1007 si->vtype = VTYPE_PYOBJ;
1008 break;
1009 case VTYPE_INT:
1010 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +01001011 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 +01001012 si->vtype = VTYPE_PYOBJ;
1013 break;
1014 default:
1015 // not handled
1016 assert(0);
1017 }
1018 }
1019
1020 // verify that this value is on the stack
1021 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001022 }
Damien George86de21b2014-08-16 22:06:11 +01001023
1024 // Second, convert any non-VTYPE_PYOBJ to that type.
1025 for (mp_uint_t i = 0; i < n_pop; i++) {
1026 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1027 if (si->vtype != VTYPE_PYOBJ) {
1028 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001029 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001030 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 +01001031 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001032 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001033 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001034 }
1035 }
1036
1037 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1038 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001039 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001040}
1041
1042// vtype of all n_push objects is VTYPE_PYOBJ
1043STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1044 need_reg_all(emit);
1045 for (mp_uint_t i = 0; i < n_push; i++) {
1046 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1047 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1048 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001049 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001050 adjust_stack(emit, n_push);
1051}
1052
Damien George7ff996c2014-09-08 23:05:16 +01001053STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1054 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001055}
1056
Damien George7ff996c2014-09-08 23:05:16 +01001057STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1058 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001059}
1060
Damien George7ff996c2014-09-08 23:05:16 +01001061STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1062 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001063}
1064
Damien George7ff996c2014-09-08 23:05:16 +01001065STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001066 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001067 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001068 // need to commit stack because we can jump here from elsewhere
1069 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001070 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001071 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001072}
1073
Damien Georgecdd96df2014-04-06 12:58:40 +01001074STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1075 DEBUG_printf("import_name %s\n", qstr_str(qst));
1076 vtype_kind_t vtype_fromlist;
1077 vtype_kind_t vtype_level;
1078 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1079 assert(vtype_fromlist == VTYPE_PYOBJ);
1080 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001081 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001082 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001083}
1084
Damien Georgecdd96df2014-04-06 12:58:40 +01001085STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1086 DEBUG_printf("import_from %s\n", qstr_str(qst));
1087 emit_native_pre(emit);
1088 vtype_kind_t vtype_module;
1089 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1090 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001091 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001092 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001093}
1094
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001095STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001096 DEBUG_printf("import_star\n");
1097 vtype_kind_t vtype_module;
1098 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1099 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001100 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001101 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001102}
1103
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001104STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001105 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001106 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001107 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001108 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001109 if (emit->do_viper_types) {
1110 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001111 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1112 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1113 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001114 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1115 }
1116 } else {
1117 vtype = VTYPE_PYOBJ;
1118 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001119 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1120 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1121 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001122 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1123 }
1124 }
1125 emit_post_push_imm(emit, vtype, val);
1126}
1127
Damien George40f3c022014-07-03 13:25:24 +01001128STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001129 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001130 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001131 if (emit->do_viper_types) {
1132 emit_post_push_imm(emit, VTYPE_INT, arg);
1133 } else {
1134 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1135 }
1136}
1137
Damien Georgecdd96df2014-04-06 12:58:40 +01001138STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001139 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001140 // for viper: load integer, check fits in 32 bits
1141 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001142 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001143 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001144}
1145
Damien George7ff996c2014-09-08 23:05:16 +01001146STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001147 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001148 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001149 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001150 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001151}
1152
Damien George7ff996c2014-09-08 23:05:16 +01001153STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001154 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001155 // TODO: Eventually we want to be able to work with raw pointers in viper to
1156 // do native array access. For now we just load them as any other object.
1157 /*
Damien13ed3a62013-10-08 09:05:10 +01001158 if (emit->do_viper_types) {
1159 // not implemented properly
1160 // load a pointer to the asciiz string?
1161 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001162 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001163 } else
1164 */
1165 {
Damien Georgeb601d952014-06-30 05:17:25 +01001166 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001167 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001168 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001169 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001170 }
Damien13ed3a62013-10-08 09:05:10 +01001171 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1172 }
1173}
1174
Damien Georgedab13852015-01-13 15:55:54 +00001175STATIC void emit_native_load_const_obj(emit_t *emit, void *obj) {
1176 emit_native_pre(emit);
Damien George2127e9a2015-01-14 00:11:09 +00001177 need_reg_single(emit, REG_RET, 0);
Damien Georgedab13852015-01-13 15:55:54 +00001178 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, (mp_uint_t)obj, REG_RET);
1179 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1180}
1181
Damien George3558f622014-04-20 17:50:40 +01001182STATIC void emit_native_load_null(emit_t *emit) {
1183 emit_native_pre(emit);
1184 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1185}
1186
Damien George0abb5602015-01-16 12:24:49 +00001187STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
1188 DEBUG_printf("load_fast(%s, " UINT_FMT ")\n", qstr_str(qst), local_num);
Damien13ed3a62013-10-08 09:05:10 +01001189 vtype_kind_t vtype = emit->local_vtype[local_num];
1190 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001191 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001192 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001193 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001194#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001195 if (local_num == 0) {
1196 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001197 } else if (local_num == 1) {
1198 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1199 } else if (local_num == 2) {
1200 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001201 } else {
Damien George0b610de2014-09-29 16:25:04 +01001202 need_reg_single(emit, REG_TEMP0, 0);
1203 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1204 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001205 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001206#elif N_X86
1207 if (local_num == 0) {
1208 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001209 } else if (local_num == 1) {
1210 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001211 } else if (local_num == 2) {
1212 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001213 } else {
Damien George0b610de2014-09-29 16:25:04 +01001214 need_reg_single(emit, REG_TEMP0, 0);
1215 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1216 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001217 }
Damien3ef4abb2013-10-12 16:53:13 +01001218#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001219 if (local_num == 0) {
1220 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1221 } else if (local_num == 1) {
1222 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1223 } else if (local_num == 2) {
1224 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1225 } else {
Damien George0b610de2014-09-29 16:25:04 +01001226 need_reg_single(emit, REG_TEMP0, 0);
1227 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1228 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001229 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001230#elif N_ARM
1231 if (local_num == 0) {
1232 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1233 } else if (local_num == 1) {
1234 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1235 } else if (local_num == 2) {
1236 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1237 } else {
Damien George0b610de2014-09-29 16:25:04 +01001238 need_reg_single(emit, REG_TEMP0, 0);
1239 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1240 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001241 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001242#else
1243 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001244#endif
1245}
1246
Damien George7ff996c2014-09-08 23:05:16 +01001247STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001248 // not implemented
1249 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1250 assert(0);
1251}
1252
Damien George7ff996c2014-09-08 23:05:16 +01001253STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001254 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001255 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001256 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001257 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1258}
1259
Damien George7ff996c2014-09-08 23:05:16 +01001260STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001261 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001262 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001263 // check for builtin casting operators
1264 if (emit->do_viper_types && qst == MP_QSTR_int) {
1265 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1266 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1267 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1268 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1269 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1270 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1271 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1272 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1273 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1274 } else {
1275 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1276 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1277 }
Damien13ed3a62013-10-08 09:05:10 +01001278}
1279
Damien George7ff996c2014-09-08 23:05:16 +01001280STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001281 // depends on type of subject:
1282 // - integer, function, pointer to integers: error
1283 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001284 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001285 vtype_kind_t vtype_base;
1286 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1287 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001288 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001289 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1290}
1291
Damien George7ff996c2014-09-08 23:05:16 +01001292STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001293 vtype_kind_t vtype_base;
1294 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1295 assert(vtype_base == VTYPE_PYOBJ);
1296 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001297 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001298}
1299
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001300STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001301 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001302 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001303 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001304}
1305
Damien George729f7b42014-04-17 22:10:53 +01001306STATIC void emit_native_load_subscr(emit_t *emit) {
Damien George91cfd412014-10-12 16:59:29 +01001307 DEBUG_printf("load_subscr\n");
1308 // need to compile: base[index]
1309
1310 // pop: index, base
1311 // optimise case where index is an immediate
1312 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1313
1314 if (vtype_base == VTYPE_PYOBJ) {
1315 // standard Python call
1316 vtype_kind_t vtype_index;
1317 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1);
1318 assert(vtype_index == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001319 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 +01001320 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1321 } else {
Damien George91cfd412014-10-12 16:59:29 +01001322 // viper load
1323 // TODO The different machine architectures have very different
1324 // capabilities and requirements for loads, so probably best to
1325 // write a completely separate load-optimiser for each one.
1326 stack_info_t *top = peek_stack(emit, 0);
1327 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1328 // index is an immediate
1329 mp_int_t index_value = top->u_imm;
1330 emit_pre_pop_discard(emit); // discard index
1331 int reg_base = REG_ARG_1;
1332 int reg_index = REG_ARG_2;
1333 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_index);
1334 switch (vtype_base) {
1335 case VTYPE_PTR8: {
1336 // pointer to 8-bit memory
1337 // TODO optimise to use thumb ldrb r1, [r2, r3]
1338 if (index_value != 0) {
1339 // index is non-zero
1340 #if N_THUMB
1341 if (index_value > 0 && index_value < 32) {
1342 asm_thumb_ldrb_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1343 break;
1344 }
1345 #endif
1346 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
1347 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1348 reg_base = reg_index;
1349 }
1350 ASM_LOAD8_REG_REG(emit->as, REG_RET, reg_base); // load from (base+index)
1351 break;
1352 }
1353 case VTYPE_PTR16: {
1354 // pointer to 16-bit memory
1355 if (index_value != 0) {
1356 // index is a non-zero immediate
1357 #if N_THUMB
1358 if (index_value > 0 && index_value < 32) {
1359 asm_thumb_ldrh_rlo_rlo_i5(emit->as, REG_RET, reg_base, index_value);
1360 break;
1361 }
1362 #endif
1363 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
1364 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1365 reg_base = reg_index;
1366 }
1367 ASM_LOAD16_REG_REG(emit->as, REG_RET, reg_base); // load from (base+2*index)
1368 break;
1369 }
1370 default:
1371 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1372 }
1373 } else {
1374 // index is not an immediate
1375 vtype_kind_t vtype_index;
1376 int reg_index = REG_ARG_2;
1377 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, REG_ARG_1);
1378 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
1379 switch (vtype_base) {
1380 case VTYPE_PTR8: {
1381 // pointer to 8-bit memory
1382 // TODO optimise to use thumb ldrb r1, [r2, r3]
1383 assert(vtype_index == VTYPE_INT);
1384 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1385 ASM_LOAD8_REG_REG(emit->as, REG_RET, REG_ARG_1); // store value to (base+index)
1386 break;
1387 }
1388 case VTYPE_PTR16: {
1389 // pointer to 16-bit memory
1390 assert(vtype_index == VTYPE_INT);
1391 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1392 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1393 ASM_LOAD16_REG_REG(emit->as, REG_RET, REG_ARG_1); // load from (base+2*index)
1394 break;
1395 }
1396 default:
1397 printf("ViperTypeError: can't load from type %d\n", vtype_base);
1398 }
1399 }
1400 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
Damien George729f7b42014-04-17 22:10:53 +01001401 }
1402}
1403
Damien George7ff996c2014-09-08 23:05:16 +01001404STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001405 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001406#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001407 if (local_num == 0) {
1408 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001409 } else if (local_num == 1) {
1410 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1411 } else if (local_num == 2) {
1412 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001413 } else {
Damien George0b610de2014-09-29 16:25:04 +01001414 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1415 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001416 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001417#elif N_X86
1418 if (local_num == 0) {
1419 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001420 } else if (local_num == 1) {
1421 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001422 } else if (local_num == 2) {
1423 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001424 } else {
Damien George0b610de2014-09-29 16:25:04 +01001425 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1426 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001427 }
Damien3ef4abb2013-10-12 16:53:13 +01001428#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001429 if (local_num == 0) {
1430 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1431 } else if (local_num == 1) {
1432 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1433 } else if (local_num == 2) {
1434 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1435 } else {
Damien George0b610de2014-09-29 16:25:04 +01001436 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1437 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001438 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001439#elif N_ARM
1440 if (local_num == 0) {
1441 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1442 } else if (local_num == 1) {
1443 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1444 } else if (local_num == 2) {
1445 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1446 } else {
Damien George0b610de2014-09-29 16:25:04 +01001447 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1448 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001449 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001450#else
1451 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001452#endif
1453
1454 emit_post(emit);
1455
1456 // check types
1457 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1458 // first time this local is assigned, so give it a type of the object stored in it
1459 emit->local_vtype[local_num] = vtype;
1460 } else if (emit->local_vtype[local_num] != vtype) {
1461 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001462 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 +01001463 }
1464}
1465
Damien George7ff996c2014-09-08 23:05:16 +01001466STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001467 // not implemented
1468 assert(0);
1469}
1470
Damien George7ff996c2014-09-08 23:05:16 +01001471STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001472 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001473 vtype_kind_t vtype;
1474 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1475 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001476 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001477 emit_post(emit);
1478}
1479
Damien George7ff996c2014-09-08 23:05:16 +01001480STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001481 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001482 if (vtype == VTYPE_PYOBJ) {
1483 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1484 } else {
1485 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001486 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 +01001487 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001488 }
Damien George7ff996c2014-09-08 23:05:16 +01001489 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001490 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001491}
1492
Damien George7ff996c2014-09-08 23:05:16 +01001493STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001494 vtype_kind_t vtype_base, vtype_val;
1495 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1496 assert(vtype_base == VTYPE_PYOBJ);
1497 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001498 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001499 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001500}
1501
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001502STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001503 DEBUG_printf("store_subscr\n");
1504 // need to compile: base[index] = value
1505
1506 // pop: index, base, value
1507 // optimise case where index is an immediate
1508 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1509
1510 if (vtype_base == VTYPE_PYOBJ) {
1511 // standard Python call
1512 vtype_kind_t vtype_index, vtype_value;
1513 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1514 assert(vtype_index == VTYPE_PYOBJ);
1515 assert(vtype_value == VTYPE_PYOBJ);
1516 emit_call(emit, MP_F_OBJ_SUBSCR);
1517 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001518 // viper store
1519 // TODO The different machine architectures have very different
1520 // capabilities and requirements for stores, so probably best to
1521 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001522 stack_info_t *top = peek_stack(emit, 0);
1523 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1524 // index is an immediate
1525 mp_int_t index_value = top->u_imm;
1526 emit_pre_pop_discard(emit); // discard index
1527 vtype_kind_t vtype_value;
1528 int reg_base = REG_ARG_1;
1529 int reg_index = REG_ARG_2;
1530 int reg_value = REG_ARG_3;
1531 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001532 #if N_X86
1533 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1534 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1535 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001536 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001537 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001538 switch (vtype_base) {
1539 case VTYPE_PTR8: {
1540 // pointer to 8-bit memory
1541 // TODO optimise to use thumb strb r1, [r2, r3]
1542 if (index_value != 0) {
1543 // index is non-zero
1544 #if N_THUMB
1545 if (index_value > 0 && index_value < 32) {
1546 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1547 break;
1548 }
1549 #endif
1550 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001551 #if N_ARM
1552 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1553 return;
1554 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001555 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1556 reg_base = reg_index;
1557 }
1558 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1559 break;
1560 }
1561 case VTYPE_PTR16: {
1562 // pointer to 16-bit memory
1563 if (index_value != 0) {
1564 // index is a non-zero immediate
1565 #if N_THUMB
1566 if (index_value > 0 && index_value < 32) {
1567 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1568 break;
1569 }
1570 #endif
1571 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001572 #if N_ARM
1573 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1574 return;
1575 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001576 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1577 reg_base = reg_index;
1578 }
1579 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1580 break;
1581 }
1582 default:
1583 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1584 }
1585 } else {
1586 // index is not an immediate
1587 vtype_kind_t vtype_index, vtype_value;
1588 int reg_index = REG_ARG_2;
1589 int reg_value = REG_ARG_3;
1590 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1591 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001592 #if N_X86
1593 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1594 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1595 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001596 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001597 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001598 switch (vtype_base) {
1599 case VTYPE_PTR8: {
1600 // pointer to 8-bit memory
1601 // TODO optimise to use thumb strb r1, [r2, r3]
1602 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001603 #if N_ARM
1604 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1605 break;
1606 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001607 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1608 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1609 break;
1610 }
1611 case VTYPE_PTR16: {
1612 // pointer to 16-bit memory
1613 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001614 #if N_ARM
1615 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1616 break;
1617 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001618 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1619 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1620 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1621 break;
1622 }
1623 default:
1624 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1625 }
1626 }
1627
1628 }
Damien13ed3a62013-10-08 09:05:10 +01001629}
1630
Damien George7ff996c2014-09-08 23:05:16 +01001631STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001632 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001633 // could support for Python types, just set to None (so GC can reclaim it)
Paul Sokolovskydb1ac362015-01-01 09:43:42 +02001634 // local is automatically deleted for exception block "as" var, and the message
1635 // breaks tests.
1636 //mp_emitter_warning(emit->pass, "Native codegeneration doesn't support deleting local");
Damien13ed3a62013-10-08 09:05:10 +01001637}
1638
Damien George7ff996c2014-09-08 23:05:16 +01001639STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001640 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001641}
1642
Damien Georgee6ce10a2014-09-06 18:38:20 +01001643STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1644 emit_native_pre(emit);
1645 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1646 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001647}
1648
Damien Georgee6ce10a2014-09-06 18:38:20 +01001649STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1650 emit_native_pre(emit);
1651 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1652 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001653}
1654
Damien Georgee6ce10a2014-09-06 18:38:20 +01001655STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001656 vtype_kind_t vtype_base;
1657 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1658 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001659 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 +01001660 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001661}
1662
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001663STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001664 vtype_kind_t vtype_index, vtype_base;
1665 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1666 assert(vtype_index == VTYPE_PYOBJ);
1667 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001668 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 +01001669}
1670
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001671STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001672 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001673 vtype_kind_t vtype;
Damien George21ca2d72014-10-19 19:00:51 +01001674 int reg = REG_TEMP0;
1675 emit_pre_pop_reg_flexible(emit, &vtype, &reg, -1, -1);
1676 emit_post_push_reg_reg(emit, vtype, reg, vtype, reg);
Damien13ed3a62013-10-08 09:05:10 +01001677}
1678
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001679STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001680 vtype_kind_t vtype0, vtype1;
1681 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1682 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1683}
1684
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001685STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001686 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001687 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001688 emit_post(emit);
1689}
1690
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001691STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001692 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001693 vtype_kind_t vtype0, vtype1;
1694 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1695 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001696}
1697
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001698STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001699 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001700 vtype_kind_t vtype0, vtype1, vtype2;
1701 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1702 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1703}
1704
Damien George7ff996c2014-09-08 23:05:16 +01001705STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001706 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001707 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001708 // need to commit stack because we are jumping elsewhere
1709 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001710 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001711 emit_post(emit);
1712}
1713
Damien George7ff996c2014-09-08 23:05:16 +01001714STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001715 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001716 switch (vtype) {
1717 case VTYPE_PYOBJ:
1718 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1719 if (!pop) {
1720 adjust_stack(emit, 1);
1721 }
1722 emit_call(emit, MP_F_OBJ_IS_TRUE);
1723 break;
1724 case VTYPE_BOOL:
1725 case VTYPE_INT:
1726 case VTYPE_UINT:
1727 emit_pre_pop_reg(emit, &vtype, REG_RET);
1728 if (!pop) {
1729 adjust_stack(emit, 1);
1730 }
1731 break;
1732 default:
1733 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1734 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001735 }
Damien George21ca2d72014-10-19 19:00:51 +01001736 // For non-pop need to save the vtype so that emit_native_adjust_stack_size
1737 // can use it. This is a bit of a hack.
1738 if (!pop) {
1739 emit->saved_stack_vtype = vtype;
1740 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001741 // need to commit stack because we may jump elsewhere
1742 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001743}
1744
Damien George7ff996c2014-09-08 23:05:16 +01001745STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001746 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001747 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001748 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001749 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001750}
Damien1a6633a2013-11-03 13:58:19 +00001751
Damien George7ff996c2014-09-08 23:05:16 +01001752STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001753 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001754 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001755 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001756 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001757}
Damien Georgea32c1e42014-05-07 18:30:52 +01001758
Damien George7ff996c2014-09-08 23:05:16 +01001759STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001760 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001761 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001762 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001763 adjust_stack(emit, -1);
1764 emit_post(emit);
1765}
1766
Damien George7ff996c2014-09-08 23:05:16 +01001767STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001768 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001769 emit_native_jump_helper(emit, label, false);
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 adjust_stack(emit, -1);
1772 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001773}
1774
Damien George7ff996c2014-09-08 23:05:16 +01001775STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001776 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001777}
Damien Georgea32c1e42014-05-07 18:30:52 +01001778
Damien George7ff996c2014-09-08 23:05:16 +01001779STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001780 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001781}
Damien Georgea32c1e42014-05-07 18:30:52 +01001782
Damien George7ff996c2014-09-08 23:05:16 +01001783STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001784 // not supported, or could be with runtime call
1785 assert(0);
1786}
Damien Georgeb601d952014-06-30 05:17:25 +01001787
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001788STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001789 assert(0);
1790}
Damien Georgeb601d952014-06-30 05:17:25 +01001791
Damien George7ff996c2014-09-08 23:05:16 +01001792STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001793 emit_native_pre(emit);
1794 // need to commit stack because we may jump elsewhere
1795 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001796 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 +01001797 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001798 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001799 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001800}
Damien Georgeb601d952014-06-30 05:17:25 +01001801
Damien George7ff996c2014-09-08 23:05:16 +01001802STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001803 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001804}
Damien Georgeb601d952014-06-30 05:17:25 +01001805
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001806STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001807 emit_pre_pop_discard(emit);
1808 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001809}
Damiend2755ec2013-10-16 23:58:48 +01001810
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001811STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001812 // perhaps the difficult one, as we want to rewrite for loops using native code
1813 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001814
1815 vtype_kind_t vtype;
1816 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1817 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001818 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001819 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001820}
Damiend2755ec2013-10-16 23:58:48 +01001821
Damien George7ff996c2014-09-08 23:05:16 +01001822STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001823 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001824 vtype_kind_t vtype;
1825 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1826 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001827 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001828 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1829 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001830 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1831}
1832
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001833STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001834 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001835 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001836 adjust_stack(emit, -1);
1837 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001838}
1839
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001840STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001841 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001842 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001843 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001844 emit_post(emit);
1845}
1846
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001847STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001848 /*
1849 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001850 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001851 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001852 emit_post(emit);
1853 */
Damien13ed3a62013-10-08 09:05:10 +01001854}
1855
Damien Georged17926d2014-03-30 13:35:08 +01001856STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001857 vtype_kind_t vtype;
1858 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1859 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001860 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001861 // we need to synthesise this operation by converting to bool first
1862 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001863 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001864 }
Damien Georged6230f62014-09-23 14:10:03 +00001865 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1866 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001867}
1868
Damien Georged17926d2014-03-30 13:35:08 +01001869STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001870 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001871 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1872 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001873 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001874 #if N_X64 || N_X86
1875 // special cases for x86 and shifting
1876 if (op == MP_BINARY_OP_LSHIFT
1877 || op == MP_BINARY_OP_INPLACE_LSHIFT
1878 || op == MP_BINARY_OP_RSHIFT
1879 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1880 #if N_X64
1881 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1882 #else
1883 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1884 #endif
1885 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1886 ASM_LSL_REG(emit->as, REG_RET);
1887 } else {
1888 ASM_ASR_REG(emit->as, REG_RET);
1889 }
1890 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1891 return;
1892 }
1893 #endif
1894 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001895 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001896 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1897 if (0) {
1898 // dummy
1899 #if !(N_X64 || N_X86)
1900 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1901 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001902 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001903 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1904 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1905 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1906 #endif
Damien George1ef23482014-10-12 14:21:06 +01001907 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1908 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1909 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1910 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1911 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1912 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1913 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1914 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1915 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001916 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1917 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1918 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1919 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1920 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1921 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1922 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1923 // comparison ops are (in enum order):
1924 // MP_BINARY_OP_LESS
1925 // MP_BINARY_OP_MORE
1926 // MP_BINARY_OP_EQUAL
1927 // MP_BINARY_OP_LESS_EQUAL
1928 // MP_BINARY_OP_MORE_EQUAL
1929 // MP_BINARY_OP_NOT_EQUAL
Damien George21ca2d72014-10-19 19:00:51 +01001930 need_reg_single(emit, REG_RET, 0);
Damien George3112cde2014-09-29 18:45:42 +01001931 #if N_X64
1932 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1933 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1934 static byte ops[6] = {
1935 ASM_X64_CC_JL,
1936 ASM_X64_CC_JG,
1937 ASM_X64_CC_JE,
1938 ASM_X64_CC_JLE,
1939 ASM_X64_CC_JGE,
1940 ASM_X64_CC_JNE,
1941 };
1942 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1943 #elif N_X86
1944 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1945 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1946 static byte ops[6] = {
1947 ASM_X86_CC_JL,
1948 ASM_X86_CC_JG,
1949 ASM_X86_CC_JE,
1950 ASM_X86_CC_JLE,
1951 ASM_X86_CC_JGE,
1952 ASM_X86_CC_JNE,
1953 };
1954 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1955 #elif N_THUMB
1956 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1957 static uint16_t ops[6] = {
1958 ASM_THUMB_OP_ITE_GE,
1959 ASM_THUMB_OP_ITE_GT,
1960 ASM_THUMB_OP_ITE_EQ,
1961 ASM_THUMB_OP_ITE_GT,
1962 ASM_THUMB_OP_ITE_GE,
1963 ASM_THUMB_OP_ITE_EQ,
1964 };
1965 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1966 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1967 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1968 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1969 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001970 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1971 static uint ccs[6] = {
1972 ASM_ARM_CC_LT,
1973 ASM_ARM_CC_GT,
1974 ASM_ARM_CC_EQ,
1975 ASM_ARM_CC_LE,
1976 ASM_ARM_CC_GE,
1977 ASM_ARM_CC_NE,
1978 };
1979 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001980 #else
1981 #error not implemented
1982 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001983 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1984 } else {
1985 // TODO other ops not yet implemented
1986 assert(0);
1987 }
Damien13ed3a62013-10-08 09:05:10 +01001988 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001989 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001990 bool invert = false;
1991 if (op == MP_BINARY_OP_NOT_IN) {
1992 invert = true;
1993 op = MP_BINARY_OP_IN;
1994 } else if (op == MP_BINARY_OP_IS_NOT) {
1995 invert = true;
1996 op = MP_BINARY_OP_IS;
1997 }
Damien George7fe21912014-08-16 22:31:57 +01001998 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001999 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01002000 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00002001 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
2002 }
Damien13ed3a62013-10-08 09:05:10 +01002003 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2004 } else {
2005 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01002006 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002007 }
2008}
2009
Damien George7ff996c2014-09-08 23:05:16 +01002010STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01002011 // for viper: call runtime, with types of args
2012 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00002013 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002014 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002015 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01002016 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01002017}
2018
Damien George7ff996c2014-09-08 23:05:16 +01002019STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002020 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002021 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002022 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002023 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
2024}
2025
Damien George7ff996c2014-09-08 23:05:16 +01002026STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01002027 // only used in list comprehension
2028 vtype_kind_t vtype_list, vtype_item;
2029 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2030 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
2031 assert(vtype_list == VTYPE_PYOBJ);
2032 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002033 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01002034 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002035}
2036
Damien George7ff996c2014-09-08 23:05:16 +01002037STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002038 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01002039 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002040 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
2041}
2042
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002043STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002044 vtype_kind_t vtype_key, vtype_value, vtype_map;
2045 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
2046 assert(vtype_key == VTYPE_PYOBJ);
2047 assert(vtype_value == VTYPE_PYOBJ);
2048 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002049 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01002050 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
2051}
2052
Damien George7ff996c2014-09-08 23:05:16 +01002053STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01002054 // only used in list comprehension
2055 vtype_kind_t vtype_map, vtype_key, vtype_value;
2056 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
2057 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
2058 assert(vtype_map == VTYPE_PYOBJ);
2059 assert(vtype_key == VTYPE_PYOBJ);
2060 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002061 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01002062 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002063}
2064
Damien Georgee37dcaa2014-12-27 17:07:16 +00002065#if MICROPY_PY_BUILTINS_SET
Damien George7ff996c2014-09-08 23:05:16 +01002066STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00002067 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00002068 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01002069 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01002070 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
2071}
2072
Damien George7ff996c2014-09-08 23:05:16 +01002073STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01002074 // only used in set comprehension
2075 vtype_kind_t vtype_set, vtype_item;
2076 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
2077 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
2078 assert(vtype_set == VTYPE_PYOBJ);
2079 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002080 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01002081 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01002082}
Damien Georgee37dcaa2014-12-27 17:07:16 +00002083#endif
Damiend2755ec2013-10-16 23:58:48 +01002084
Damien George83204f32014-12-27 17:20:41 +00002085#if MICROPY_PY_BUILTINS_SLICE
Damien George7ff996c2014-09-08 23:05:16 +01002086STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002087 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01002088 if (n_args == 2) {
2089 vtype_kind_t vtype_start, vtype_stop;
2090 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
2091 assert(vtype_start == VTYPE_PYOBJ);
2092 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002093 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 +01002094 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2095 } else {
2096 assert(n_args == 3);
2097 vtype_kind_t vtype_start, vtype_stop, vtype_step;
2098 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
2099 assert(vtype_start == VTYPE_PYOBJ);
2100 assert(vtype_stop == VTYPE_PYOBJ);
2101 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002102 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01002103 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2104 }
Damien13ed3a62013-10-08 09:05:10 +01002105}
Damien George83204f32014-12-27 17:20:41 +00002106#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01002107
Damien George7ff996c2014-09-08 23:05:16 +01002108STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002109 DEBUG_printf("unpack_sequence %d\n", n_args);
2110 vtype_kind_t vtype_base;
2111 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2112 assert(vtype_base == VTYPE_PYOBJ);
2113 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01002114 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01002115}
Damien Georgecdd96df2014-04-06 12:58:40 +01002116
Damien George7ff996c2014-09-08 23:05:16 +01002117STATIC 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 +01002118 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
2119 vtype_kind_t vtype_base;
2120 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
2121 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01002122 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 +01002123 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 +01002124}
2125
Damien George7ff996c2014-09-08 23:05:16 +01002126STATIC 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 +01002127 // 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 +00002128 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002129 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002130 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 +01002131 } else {
2132 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2133 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2134 assert(vtype_def_tuple == VTYPE_PYOBJ);
2135 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002136 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 +01002137 }
Damien13ed3a62013-10-08 09:05:10 +01002138 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2139}
2140
Damien George7ff996c2014-09-08 23:05:16 +01002141STATIC void emit_native_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_over, mp_uint_t n_pos_defaults, mp_uint_t n_kw_defaults) {
Damien13ed3a62013-10-08 09:05:10 +01002142 assert(0);
2143}
2144
Damien George7ff996c2014-09-08 23:05:16 +01002145STATIC 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 +00002146 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2147
Damien Georgee9dac3b2014-09-29 22:10:41 +01002148 // TODO: in viper mode, call special runtime routine with type info for args,
2149 // and wanted type info for return, to remove need for boxing/unboxing
2150
Damien George922ddd62014-04-09 12:43:17 +01002151 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002152
Damien Georgee9dac3b2014-09-29 22:10:41 +01002153 emit_native_pre(emit);
2154 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2155 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2156 // casting operator
2157 assert(n_positional == 1 && n_keyword == 0);
2158 DEBUG_printf(" cast to %d\n", vtype_fun);
2159 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2160 switch (peek_vtype(emit, 0)) {
2161 case VTYPE_PYOBJ: {
2162 vtype_kind_t vtype;
2163 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2164 emit_pre_pop_discard(emit);
2165 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2166 emit_post_push_reg(emit, vtype_cast, REG_RET);
2167 break;
2168 }
2169 case VTYPE_BOOL:
2170 case VTYPE_INT:
2171 case VTYPE_UINT:
2172 case VTYPE_PTR:
2173 case VTYPE_PTR8:
2174 case VTYPE_PTR16:
2175 case VTYPE_PTR_NONE:
2176 emit_fold_stack_top(emit, REG_ARG_1);
2177 emit_post_top_set_vtype(emit, vtype_cast);
2178 break;
2179 default:
2180 assert(!"TODO: convert obj to int");
2181 }
2182 } else {
2183 if (n_positional != 0 || n_keyword != 0) {
2184 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2185 }
Damien13ed3a62013-10-08 09:05:10 +01002186 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2187 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002188 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2189 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002190 }
Damien13ed3a62013-10-08 09:05:10 +01002191}
2192
Damien George7ff996c2014-09-08 23:05:16 +01002193STATIC 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 +01002194 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002195 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002196 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 +01002197 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 +01002198 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2199}
2200
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002201STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002202 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002203 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002204 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2205 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002206 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002207 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002208 } else {
2209 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002210 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002211 } else {
2212 vtype_kind_t vtype;
2213 emit_pre_pop_reg(emit, &vtype, REG_RET);
2214 if (vtype != emit->return_vtype) {
2215 printf("ViperTypeError: incompatible return type\n");
2216 }
Damien George2ac4af62014-08-15 16:45:41 +01002217 }
Damien13ed3a62013-10-08 09:05:10 +01002218 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002219 vtype_kind_t vtype;
2220 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002221 assert(vtype == VTYPE_PYOBJ);
2222 }
2223 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002224 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2225 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002226}
2227
Damien George7ff996c2014-09-08 23:05:16 +01002228STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002229 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002230 vtype_kind_t vtype_exc;
2231 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2232 if (vtype_exc != VTYPE_PYOBJ) {
2233 printf("ViperTypeError: must raise an object\n");
2234 }
2235 // 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 +01002236 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002237}
Damien Georgeb601d952014-06-30 05:17:25 +01002238
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002239STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002240 // not supported (for now)
2241 assert(0);
2242}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002243STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002244 // not supported (for now)
2245 assert(0);
2246}
2247
Damien Georgeb601d952014-06-30 05:17:25 +01002248STATIC void emit_native_start_except_handler(emit_t *emit) {
2249 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2250 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2251 // the first 2 elements, so we can get the thrown value.
2252 adjust_stack(emit, 2);
2253 vtype_kind_t vtype_nlr;
2254 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002255 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002256 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
2257}
2258
2259STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002260 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002261}
2262
Damien13ed3a62013-10-08 09:05:10 +01002263const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002264 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002265 emit_native_start_pass,
2266 emit_native_end_pass,
2267 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002268 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002269 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002270
2271 emit_native_load_id,
2272 emit_native_store_id,
2273 emit_native_delete_id,
2274
2275 emit_native_label_assign,
2276 emit_native_import_name,
2277 emit_native_import_from,
2278 emit_native_import_star,
2279 emit_native_load_const_tok,
2280 emit_native_load_const_small_int,
2281 emit_native_load_const_int,
2282 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002283 emit_native_load_const_str,
Damien Georgedab13852015-01-13 15:55:54 +00002284 emit_native_load_const_obj,
Damien George3558f622014-04-20 17:50:40 +01002285 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002286 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002287 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002288 emit_native_load_name,
2289 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002290 emit_native_load_attr,
2291 emit_native_load_method,
2292 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002293 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002294 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002295 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002296 emit_native_store_name,
2297 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002298 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002299 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002300 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002301 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002302 emit_native_delete_name,
2303 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002304 emit_native_delete_attr,
2305 emit_native_delete_subscr,
2306 emit_native_dup_top,
2307 emit_native_dup_top_two,
2308 emit_native_pop_top,
2309 emit_native_rot_two,
2310 emit_native_rot_three,
2311 emit_native_jump,
2312 emit_native_pop_jump_if_true,
2313 emit_native_pop_jump_if_false,
2314 emit_native_jump_if_true_or_pop,
2315 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002316 emit_native_break_loop,
2317 emit_native_continue_loop,
2318 emit_native_setup_with,
2319 emit_native_with_cleanup,
2320 emit_native_setup_except,
2321 emit_native_setup_finally,
2322 emit_native_end_finally,
2323 emit_native_get_iter,
2324 emit_native_for_iter,
2325 emit_native_for_iter_end,
2326 emit_native_pop_block,
2327 emit_native_pop_except,
2328 emit_native_unary_op,
2329 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002330 emit_native_build_tuple,
2331 emit_native_build_list,
2332 emit_native_list_append,
2333 emit_native_build_map,
2334 emit_native_store_map,
2335 emit_native_map_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002336 #if MICROPY_PY_BUILTINS_SET
Damien13ed3a62013-10-08 09:05:10 +01002337 emit_native_build_set,
2338 emit_native_set_add,
Damien Georgee37dcaa2014-12-27 17:07:16 +00002339 #endif
Damien George83204f32014-12-27 17:20:41 +00002340 #if MICROPY_PY_BUILTINS_SLICE
Damien13ed3a62013-10-08 09:05:10 +01002341 emit_native_build_slice,
Damien George83204f32014-12-27 17:20:41 +00002342 #endif
Damien13ed3a62013-10-08 09:05:10 +01002343 emit_native_unpack_sequence,
2344 emit_native_unpack_ex,
2345 emit_native_make_function,
2346 emit_native_make_closure,
2347 emit_native_call_function,
2348 emit_native_call_method,
2349 emit_native_return_value,
2350 emit_native_raise_varargs,
2351 emit_native_yield_value,
2352 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002353
2354 emit_native_start_except_handler,
2355 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002356};
2357
Damien Georgec90f59e2014-09-06 23:06:36 +01002358#endif