blob: 4e39f641da2614f09a36cd1a91e60179c88862fb [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien13ed3a62013-10-08 09:05:10 +010027// Essentially normal Python has 1 type: Python objects
28// Viper has more than 1 type, and is just a more complicated (a superset of) Python.
29// If you declare everything in Viper as a Python object (ie omit type decls) then
30// it should in principle be exactly the same as Python native.
31// Having types means having more opcodes, like binary_op_nat_nat, binary_op_nat_obj etc.
32// In practice we won't have a VM but rather do this in asm which is actually very minimal.
33
34// Because it breaks strict Python equivalence it should be a completely separate
35// decorator. It breaks equivalence because overflow on integers wraps around.
36// It shouldn't break equivalence if you don't use the new types, but since the
37// type decls might be used in normal Python for other reasons, it's probably safest,
38// cleanest and clearest to make it a separate decorator.
39
40// Actually, it does break equivalence because integers default to native integers,
41// not Python objects.
42
43// for x in l[0:8]: can be compiled into a native loop if l has pointer type
44
xbeefe34222014-03-16 00:14:26 -070045#include <stdbool.h>
Damien13ed3a62013-10-08 09:05:10 +010046#include <stdint.h>
47#include <stdio.h>
48#include <string.h>
49#include <assert.h>
50
Damiend99b0522013-12-21 18:17:45 +000051#include "mpconfig.h"
Damien Georgeb601d952014-06-30 05:17:25 +010052#include "nlr.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030053#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000054#include "qstr.h"
Damien13ed3a62013-10-08 09:05:10 +010055#include "lexer.h"
Damien13ed3a62013-10-08 09:05:10 +010056#include "parse.h"
Damien Georgedf8127a2014-04-13 11:04:33 +010057#include "obj.h"
58#include "emitglue.h"
Damien13ed3a62013-10-08 09:05:10 +010059#include "scope.h"
Damiend99b0522013-12-21 18:17:45 +000060#include "runtime0.h"
Damien13ed3a62013-10-08 09:05:10 +010061#include "emit.h"
Damiend99b0522013-12-21 18:17:45 +000062#include "runtime.h"
Damien13ed3a62013-10-08 09:05:10 +010063
Damien Georgecdd96df2014-04-06 12:58:40 +010064#if 0 // print debugging info
65#define DEBUG_PRINT (1)
66#define DEBUG_printf DEBUG_printf
67#else // don't print debugging info
68#define DEBUG_printf(...) (void)0
69#endif
70
Damien13ed3a62013-10-08 09:05:10 +010071// wrapper around everything in this file
Damien Georgec90f59e2014-09-06 23:06:36 +010072#if (MICROPY_EMIT_X64 && N_X64) \
73 || (MICROPY_EMIT_X86 && N_X86) \
74 || (MICROPY_EMIT_THUMB && N_THUMB) \
75 || (MICROPY_EMIT_ARM && N_ARM)
Damien13ed3a62013-10-08 09:05:10 +010076
Damien3ef4abb2013-10-12 16:53:13 +010077#if N_X64
Damien13ed3a62013-10-08 09:05:10 +010078
79// x64 specific stuff
80
81#include "asmx64.h"
82
Damien13ed3a62013-10-08 09:05:10 +010083#define EXPORT_FUN(name) emit_native_x64_##name
84
Damien George0b610de2014-09-29 16:25:04 +010085#define REG_RET ASM_X64_REG_RAX
86#define REG_ARG_1 ASM_X64_REG_RDI
87#define REG_ARG_2 ASM_X64_REG_RSI
88#define REG_ARG_3 ASM_X64_REG_RDX
89#define REG_ARG_4 ASM_X64_REG_RCX
Damien Georgec90f59e2014-09-06 23:06:36 +010090
Damien George81057362014-09-07 01:06:19 +010091// caller-save
Damien George0b610de2014-09-29 16:25:04 +010092#define REG_TEMP0 ASM_X64_REG_RAX
93#define REG_TEMP1 ASM_X64_REG_RDI
94#define REG_TEMP2 ASM_X64_REG_RSI
Damien George81057362014-09-07 01:06:19 +010095
96// callee-save
Damien George0b610de2014-09-29 16:25:04 +010097#define REG_LOCAL_1 ASM_X64_REG_RBX
98#define REG_LOCAL_2 ASM_X64_REG_R12
99#define REG_LOCAL_3 ASM_X64_REG_R13
Damien George81057362014-09-07 01:06:19 +0100100#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100101
102#define ASM_PASS_COMPUTE ASM_X64_PASS_COMPUTE
103#define ASM_PASS_EMIT ASM_X64_PASS_EMIT
104
105#define ASM_T asm_x64_t
106#define ASM_NEW asm_x64_new
107#define ASM_FREE asm_x64_free
108#define ASM_GET_CODE asm_x64_get_code
109#define ASM_GET_CODE_SIZE asm_x64_get_code_size
110#define ASM_START_PASS asm_x64_start_pass
111#define ASM_END_PASS asm_x64_end_pass
112#define ASM_ENTRY asm_x64_entry
113#define ASM_EXIT asm_x64_exit
114
115#define ASM_LABEL_ASSIGN asm_x64_label_assign
116#define ASM_JUMP asm_x64_jmp_label
117#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
118 do { \
119 asm_x64_test_r8_with_r8(as, reg, reg); \
120 asm_x64_jcc_label(as, ASM_X64_CC_JZ, label); \
121 } while (0)
122#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
123 do { \
124 asm_x64_test_r8_with_r8(as, reg, reg); \
125 asm_x64_jcc_label(as, ASM_X64_CC_JNZ, label); \
126 } while (0)
127#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
128 do { \
129 asm_x64_cmp_r64_with_r64(as, reg1, reg2); \
130 asm_x64_jcc_label(as, ASM_X64_CC_JE, label); \
131 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100132#define ASM_CALL_IND(as, ptr, idx) asm_x64_call_ind(as, ptr, ASM_X64_REG_RAX)
Damien Georgec90f59e2014-09-06 23:06:36 +0100133
134#define ASM_MOV_REG_TO_LOCAL asm_x64_mov_r64_to_local
135#define ASM_MOV_IMM_TO_REG asm_x64_mov_i64_to_r64_optimised
136#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x64_mov_i64_to_r64_aligned
137#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
138 do { \
139 asm_x64_mov_i64_to_r64_optimised(as, (imm), (reg_temp)); \
140 asm_x64_mov_r64_to_local(as, (reg_temp), (local_num)); \
141 } while (false)
142#define ASM_MOV_LOCAL_TO_REG asm_x64_mov_local_to_r64
Damien George3112cde2014-09-29 18:45:42 +0100143#define ASM_MOV_REG_REG(as, reg_dest, reg_src) asm_x64_mov_r64_r64((as), (reg_dest), (reg_src))
Damien Georgec90f59e2014-09-06 23:06:36 +0100144#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x64_mov_local_addr_to_r64
145
Damien George3112cde2014-09-29 18:45:42 +0100146#define ASM_LSL_REG(as, reg) asm_x64_shl_r64_cl((as), (reg))
147#define ASM_ASR_REG(as, reg) asm_x64_sar_r64_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100148#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x64_or_r64_r64((as), (reg_dest), (reg_src))
149#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x64_xor_r64_r64((as), (reg_dest), (reg_src))
150#define ASM_AND_REG_REG(as, reg_dest, reg_src) asm_x64_and_r64_r64((as), (reg_dest), (reg_src))
Damien George3112cde2014-09-29 18:45:42 +0100151#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x64_add_r64_r64((as), (reg_dest), (reg_src))
152#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x64_sub_r64_r64((as), (reg_dest), (reg_src))
153
Damien Georgee9dac3b2014-09-29 22:10:41 +0100154#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x64_mov_r64_to_disp((as), (reg_src), (reg_base), 0)
155#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x64_mov_r8_to_disp((as), (reg_src), (reg_base), 0)
156#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x64_mov_r16_to_disp((as), (reg_src), (reg_base), 0)
157
Damien Georgec90f59e2014-09-06 23:06:36 +0100158#elif N_X86
159
160// x86 specific stuff
161
162#include "asmx86.h"
163
164STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = {
165 [MP_F_CONVERT_OBJ_TO_NATIVE] = 2,
166 [MP_F_CONVERT_NATIVE_TO_OBJ] = 2,
167 [MP_F_LOAD_CONST_INT] = 1,
168 [MP_F_LOAD_CONST_DEC] = 1,
169 [MP_F_LOAD_CONST_STR] = 1,
170 [MP_F_LOAD_CONST_BYTES] = 1,
171 [MP_F_LOAD_NAME] = 1,
172 [MP_F_LOAD_GLOBAL] = 1,
173 [MP_F_LOAD_BUILD_CLASS] = 0,
174 [MP_F_LOAD_ATTR] = 2,
175 [MP_F_LOAD_METHOD] = 3,
176 [MP_F_STORE_NAME] = 2,
177 [MP_F_STORE_GLOBAL] = 2,
178 [MP_F_STORE_ATTR] = 3,
179 [MP_F_OBJ_SUBSCR] = 3,
180 [MP_F_OBJ_IS_TRUE] = 1,
181 [MP_F_UNARY_OP] = 2,
182 [MP_F_BINARY_OP] = 3,
183 [MP_F_BUILD_TUPLE] = 2,
184 [MP_F_BUILD_LIST] = 2,
185 [MP_F_LIST_APPEND] = 2,
186 [MP_F_BUILD_MAP] = 1,
187 [MP_F_STORE_MAP] = 3,
188#if MICROPY_PY_BUILTINS_SET
189 [MP_F_BUILD_SET] = 2,
190 [MP_F_STORE_SET] = 2,
191#endif
192 [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3,
193 [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3,
194 [MP_F_CALL_METHOD_N_KW] = 3,
195 [MP_F_GETITER] = 1,
196 [MP_F_ITERNEXT] = 1,
197 [MP_F_NLR_PUSH] = 1,
198 [MP_F_NLR_POP] = 0,
199 [MP_F_NATIVE_RAISE] = 1,
200 [MP_F_IMPORT_NAME] = 3,
201 [MP_F_IMPORT_FROM] = 2,
202 [MP_F_IMPORT_ALL] = 1,
203#if MICROPY_PY_BUILTINS_SLICE
204 [MP_F_NEW_SLICE] = 3,
205#endif
206 [MP_F_UNPACK_SEQUENCE] = 3,
207 [MP_F_UNPACK_EX] = 3,
208 [MP_F_DELETE_NAME] = 1,
209 [MP_F_DELETE_GLOBAL] = 1,
210};
211
212#define EXPORT_FUN(name) emit_native_x86_##name
213
Damien George0b610de2014-09-29 16:25:04 +0100214#define REG_RET ASM_X86_REG_EAX
Damien George6eae8612014-09-08 22:16:35 +0000215#define REG_ARG_1 ASM_X86_REG_ARG_1
216#define REG_ARG_2 ASM_X86_REG_ARG_2
217#define REG_ARG_3 ASM_X86_REG_ARG_3
Damien George81057362014-09-07 01:06:19 +0100218
Damien George25d90412014-09-06 23:24:32 +0000219// caller-save, so can be used as temporaries
Damien George0b610de2014-09-29 16:25:04 +0100220#define REG_TEMP0 ASM_X86_REG_EAX
221#define REG_TEMP1 ASM_X86_REG_ECX
222#define REG_TEMP2 ASM_X86_REG_EDX
Damien Georgec90f59e2014-09-06 23:06:36 +0100223
Damien George25d90412014-09-06 23:24:32 +0000224// callee-save, so can be used as locals
Damien George0b610de2014-09-29 16:25:04 +0100225#define REG_LOCAL_1 ASM_X86_REG_EBX
226#define REG_LOCAL_2 ASM_X86_REG_ESI
227#define REG_LOCAL_3 ASM_X86_REG_EDI
Damien George25d90412014-09-06 23:24:32 +0000228#define REG_LOCAL_NUM (3)
Damien Georgec90f59e2014-09-06 23:06:36 +0100229
230#define ASM_PASS_COMPUTE ASM_X86_PASS_COMPUTE
231#define ASM_PASS_EMIT ASM_X86_PASS_EMIT
232
233#define ASM_T asm_x86_t
234#define ASM_NEW asm_x86_new
235#define ASM_FREE asm_x86_free
236#define ASM_GET_CODE asm_x86_get_code
237#define ASM_GET_CODE_SIZE asm_x86_get_code_size
238#define ASM_START_PASS asm_x86_start_pass
239#define ASM_END_PASS asm_x86_end_pass
240#define ASM_ENTRY asm_x86_entry
241#define ASM_EXIT asm_x86_exit
242
243#define ASM_LABEL_ASSIGN asm_x86_label_assign
244#define ASM_JUMP asm_x86_jmp_label
245#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
246 do { \
247 asm_x86_test_r8_with_r8(as, reg, reg); \
248 asm_x86_jcc_label(as, ASM_X86_CC_JZ, label); \
249 } while (0)
250#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
251 do { \
252 asm_x86_test_r8_with_r8(as, reg, reg); \
253 asm_x86_jcc_label(as, ASM_X86_CC_JNZ, label); \
254 } while (0)
255#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
256 do { \
257 asm_x86_cmp_r32_with_r32(as, reg1, reg2); \
258 asm_x86_jcc_label(as, ASM_X86_CC_JE, label); \
259 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100260#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 +0100261
262#define ASM_MOV_REG_TO_LOCAL asm_x86_mov_r32_to_local
263#define ASM_MOV_IMM_TO_REG asm_x86_mov_i32_to_r32
264#define ASM_MOV_ALIGNED_IMM_TO_REG asm_x86_mov_i32_to_r32_aligned
265#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
266 do { \
267 asm_x86_mov_i32_to_r32(as, (imm), (reg_temp)); \
268 asm_x86_mov_r32_to_local(as, (reg_temp), (local_num)); \
269 } while (false)
270#define ASM_MOV_LOCAL_TO_REG asm_x86_mov_local_to_r32
Damien George3112cde2014-09-29 18:45:42 +0100271#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 +0100272#define ASM_MOV_LOCAL_ADDR_TO_REG asm_x86_mov_local_addr_to_r32
Damien13ed3a62013-10-08 09:05:10 +0100273
Damien George3112cde2014-09-29 18:45:42 +0100274#define ASM_LSL_REG(as, reg) asm_x86_shl_r32_cl((as), (reg))
275#define ASM_ASR_REG(as, reg) asm_x86_sar_r32_cl((as), (reg))
Damien George1ef23482014-10-12 14:21:06 +0100276#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_x86_or_r32_r32((as), (reg_dest), (reg_src))
277#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_x86_xor_r32_r32((as), (reg_dest), (reg_src))
278#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 +0100279#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_x86_add_r32_r32((as), (reg_dest), (reg_src))
280#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_x86_sub_r32_r32((as), (reg_dest), (reg_src))
281
Damien Georgee9dac3b2014-09-29 22:10:41 +0100282#define ASM_STORE_REG_REG(as, reg_src, reg_base) asm_x86_mov_r32_to_disp((as), (reg_src), (reg_base), 0)
283#define ASM_STORE8_REG_REG(as, reg_src, reg_base) asm_x86_mov_r8_to_disp((as), (reg_src), (reg_base), 0)
284#define ASM_STORE16_REG_REG(as, reg_src, reg_base) asm_x86_mov_r16_to_disp((as), (reg_src), (reg_base), 0)
285
Damien3ef4abb2013-10-12 16:53:13 +0100286#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +0100287
288// thumb specific stuff
289
290#include "asmthumb.h"
291
Damien13ed3a62013-10-08 09:05:10 +0100292#define EXPORT_FUN(name) emit_native_thumb_##name
293
Damien George0b610de2014-09-29 16:25:04 +0100294#define REG_RET ASM_THUMB_REG_R0
295#define REG_ARG_1 ASM_THUMB_REG_R0
296#define REG_ARG_2 ASM_THUMB_REG_R1
297#define REG_ARG_3 ASM_THUMB_REG_R2
298#define REG_ARG_4 ASM_THUMB_REG_R3
Damien George81057362014-09-07 01:06:19 +0100299
Damien George0b610de2014-09-29 16:25:04 +0100300#define REG_TEMP0 ASM_THUMB_REG_R0
301#define REG_TEMP1 ASM_THUMB_REG_R1
302#define REG_TEMP2 ASM_THUMB_REG_R2
Damien Georgec90f59e2014-09-06 23:06:36 +0100303
Damien George0b610de2014-09-29 16:25:04 +0100304#define REG_LOCAL_1 ASM_THUMB_REG_R4
305#define REG_LOCAL_2 ASM_THUMB_REG_R5
306#define REG_LOCAL_3 ASM_THUMB_REG_R6
Damien Georgec90f59e2014-09-06 23:06:36 +0100307#define REG_LOCAL_NUM (3)
308
309#define ASM_PASS_COMPUTE ASM_THUMB_PASS_COMPUTE
310#define ASM_PASS_EMIT ASM_THUMB_PASS_EMIT
311
312#define ASM_T asm_thumb_t
313#define ASM_NEW asm_thumb_new
314#define ASM_FREE asm_thumb_free
315#define ASM_GET_CODE asm_thumb_get_code
316#define ASM_GET_CODE_SIZE asm_thumb_get_code_size
317#define ASM_START_PASS asm_thumb_start_pass
318#define ASM_END_PASS asm_thumb_end_pass
319#define ASM_ENTRY asm_thumb_entry
320#define ASM_EXIT asm_thumb_exit
321
322#define ASM_LABEL_ASSIGN asm_thumb_label_assign
323#define ASM_JUMP asm_thumb_b_label
324#define ASM_JUMP_IF_REG_ZERO(as, reg, label) \
325 do { \
326 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100327 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100328 } while (0)
329#define ASM_JUMP_IF_REG_NONZERO(as, reg, label) \
330 do { \
331 asm_thumb_cmp_rlo_i8(as, reg, 0); \
Damien George0b610de2014-09-29 16:25:04 +0100332 asm_thumb_bcc_label(as, ASM_THUMB_CC_NE, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100333 } while (0)
334#define ASM_JUMP_IF_REG_EQ(as, reg1, reg2, label) \
335 do { \
336 asm_thumb_cmp_rlo_rlo(as, reg1, reg2); \
Damien George0b610de2014-09-29 16:25:04 +0100337 asm_thumb_bcc_label(as, ASM_THUMB_CC_EQ, label); \
Damien Georgec90f59e2014-09-06 23:06:36 +0100338 } while (0)
Damien George0b610de2014-09-29 16:25:04 +0100339#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 +0100340
341#define ASM_MOV_REG_TO_LOCAL(as, reg, local_num) asm_thumb_mov_local_reg(as, (local_num), (reg))
342#define ASM_MOV_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_optimised(as, (reg), (imm))
343#define ASM_MOV_ALIGNED_IMM_TO_REG(as, imm, reg) asm_thumb_mov_reg_i32_aligned(as, (reg), (imm))
344#define ASM_MOV_IMM_TO_LOCAL_USING(as, imm, local_num, reg_temp) \
345 do { \
346 asm_thumb_mov_reg_i32_optimised(as, (reg_temp), (imm)); \
347 asm_thumb_mov_local_reg(as, (local_num), (reg_temp)); \
348 } while (false)
349#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 +0100350#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 +0100351#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 +0100352
Damien George3112cde2014-09-29 18:45:42 +0100353#define ASM_LSL_REG_REG(as, reg_dest, reg_shift) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_LSL, (reg_dest), (reg_shift))
354#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 +0100355#define ASM_OR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_ORR, (reg_dest), (reg_src))
356#define ASM_XOR_REG_REG(as, reg_dest, reg_src) asm_thumb_format_4((as), ASM_THUMB_FORMAT_4_EOR, (reg_dest), (reg_src))
357#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 +0100358#define ASM_ADD_REG_REG(as, reg_dest, reg_src) asm_thumb_add_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
359#define ASM_SUB_REG_REG(as, reg_dest, reg_src) asm_thumb_sub_rlo_rlo_rlo((as), (reg_dest), (reg_dest), (reg_src))
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
369#include "asmarm.h"
370
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
Fabian Vogte5268962014-10-04 00:53:46 +0200440#define ASM_STORE_REG_REG(as, reg_value, reg_base) asm_arm_str_reg_reg((as), (reg_value), (reg_base))
441#define ASM_STORE8_REG_REG(as, reg_value, reg_base) asm_arm_strb_reg_reg((as), (reg_value), (reg_base))
442#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 +0100443
Damien Georgec90f59e2014-09-06 23:06:36 +0100444#else
445
446#error unknown native emitter
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200447
Damien13ed3a62013-10-08 09:05:10 +0100448#endif
449
450typedef enum {
Damienff8ed772013-10-08 22:18:32 +0100451 STACK_VALUE,
452 STACK_REG,
453 STACK_IMM,
454} stack_info_kind_t;
Damien13ed3a62013-10-08 09:05:10 +0100455
Damien Georgee9dac3b2014-09-29 22:10:41 +0100456// these enums must be distinct and the bottom 2 bits
457// must correspond to the correct MP_NATIVE_TYPE_xxx value
Damien13ed3a62013-10-08 09:05:10 +0100458typedef enum {
Damien Georgee9dac3b2014-09-29 22:10:41 +0100459 VTYPE_PYOBJ = 0x00 | MP_NATIVE_TYPE_OBJ,
460 VTYPE_BOOL = 0x00 | MP_NATIVE_TYPE_BOOL,
461 VTYPE_INT = 0x00 | MP_NATIVE_TYPE_INT,
462 VTYPE_UINT = 0x00 | MP_NATIVE_TYPE_UINT,
463
464 VTYPE_PTR = 0x10 | MP_NATIVE_TYPE_UINT, // pointer to word sized entity
465 VTYPE_PTR8 = 0x20 | MP_NATIVE_TYPE_UINT,
466 VTYPE_PTR16 = 0x30 | MP_NATIVE_TYPE_UINT,
467 VTYPE_PTR_NONE = 0x40 | MP_NATIVE_TYPE_UINT,
468
469 VTYPE_UNBOUND = 0x50 | MP_NATIVE_TYPE_OBJ,
470 VTYPE_BUILTIN_CAST = 0x60 | MP_NATIVE_TYPE_OBJ,
Damien13ed3a62013-10-08 09:05:10 +0100471} vtype_kind_t;
472
Damienff8ed772013-10-08 22:18:32 +0100473typedef struct _stack_info_t {
474 vtype_kind_t vtype;
475 stack_info_kind_t kind;
476 union {
477 int u_reg;
Damien George40f3c022014-07-03 13:25:24 +0100478 mp_int_t u_imm;
Damienff8ed772013-10-08 22:18:32 +0100479 };
480} stack_info_t;
481
Damien13ed3a62013-10-08 09:05:10 +0100482struct _emit_t {
483 int pass;
484
485 bool do_viper_types;
Damienff8ed772013-10-08 22:18:32 +0100486
Damien George2ac4af62014-08-15 16:45:41 +0100487 vtype_kind_t return_vtype;
488
Damien George7ff996c2014-09-08 23:05:16 +0100489 mp_uint_t local_vtype_alloc;
Damien13ed3a62013-10-08 09:05:10 +0100490 vtype_kind_t *local_vtype;
Damienff8ed772013-10-08 22:18:32 +0100491
Damien George7ff996c2014-09-08 23:05:16 +0100492 mp_uint_t stack_info_alloc;
Damienff8ed772013-10-08 22:18:32 +0100493 stack_info_t *stack_info;
494
Damien13ed3a62013-10-08 09:05:10 +0100495 int stack_start;
496 int stack_size;
497
498 bool last_emit_was_return_value;
499
Damien13ed3a62013-10-08 09:05:10 +0100500 scope_t *scope;
501
Damien Georgec90f59e2014-09-06 23:06:36 +0100502 ASM_T *as;
Damien13ed3a62013-10-08 09:05:10 +0100503};
504
Damien George7ff996c2014-09-08 23:05:16 +0100505emit_t *EXPORT_FUN(new)(mp_uint_t max_num_labels) {
Damien George36db6bc2014-05-07 17:24:22 +0100506 emit_t *emit = m_new0(emit_t, 1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100507 emit->as = ASM_NEW(max_num_labels);
Damien13ed3a62013-10-08 09:05:10 +0100508 return emit;
509}
510
Damien George41d02b62014-01-24 22:42:28 +0000511void EXPORT_FUN(free)(emit_t *emit) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100512 ASM_FREE(emit->as, false);
Damien George36db6bc2014-05-07 17:24:22 +0100513 m_del(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc);
514 m_del(stack_info_t, emit->stack_info, emit->stack_info_alloc);
Paul Sokolovskyf46d87a2014-01-24 16:20:11 +0200515 m_del_obj(emit_t, emit);
516}
517
Damien George2ac4af62014-08-15 16:45:41 +0100518STATIC void emit_native_set_native_type(emit_t *emit, mp_uint_t op, mp_uint_t arg1, qstr arg2) {
519 switch (op) {
520 case MP_EMIT_NATIVE_TYPE_ENABLE:
521 emit->do_viper_types = arg1;
522 break;
523
524 default: {
525 vtype_kind_t type;
526 switch (arg2) {
527 case MP_QSTR_object: type = VTYPE_PYOBJ; break;
528 case MP_QSTR_bool: type = VTYPE_BOOL; break;
529 case MP_QSTR_int: type = VTYPE_INT; break;
530 case MP_QSTR_uint: type = VTYPE_UINT; break;
Damien Georgee9dac3b2014-09-29 22:10:41 +0100531 case MP_QSTR_ptr: type = VTYPE_PTR; break;
532 case MP_QSTR_ptr8: type = VTYPE_PTR8; break;
533 case MP_QSTR_ptr16: type = VTYPE_PTR16; break;
Damien George2ac4af62014-08-15 16:45:41 +0100534 default: printf("ViperTypeError: unknown type %s\n", qstr_str(arg2)); return;
535 }
536 if (op == MP_EMIT_NATIVE_TYPE_RETURN) {
537 emit->return_vtype = type;
538 } else {
539 assert(arg1 < emit->local_vtype_alloc);
540 emit->local_vtype[arg1] = type;
541 }
542 break;
543 }
544 }
Damien13ed3a62013-10-08 09:05:10 +0100545}
546
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200547STATIC void emit_native_start_pass(emit_t *emit, pass_kind_t pass, scope_t *scope) {
Damien Georged6230f62014-09-23 14:10:03 +0000548 DEBUG_printf("start_pass(pass=%u, scope=%p)\n", pass, scope);
549
Damien13ed3a62013-10-08 09:05:10 +0100550 emit->pass = pass;
551 emit->stack_start = 0;
552 emit->stack_size = 0;
553 emit->last_emit_was_return_value = false;
Damien13ed3a62013-10-08 09:05:10 +0100554 emit->scope = scope;
555
Damien George36db6bc2014-05-07 17:24:22 +0100556 // allocate memory for keeping track of the types of locals
557 if (emit->local_vtype_alloc < scope->num_locals) {
558 emit->local_vtype = m_renew(vtype_kind_t, emit->local_vtype, emit->local_vtype_alloc, scope->num_locals);
559 emit->local_vtype_alloc = scope->num_locals;
Damienff8ed772013-10-08 22:18:32 +0100560 }
Damien George36db6bc2014-05-07 17:24:22 +0100561
562 // allocate memory for keeping track of the objects on the stack
563 // XXX don't know stack size on entry, and it should be maximum over all scopes
Damienff8ed772013-10-08 22:18:32 +0100564 if (emit->stack_info == NULL) {
Damien George36db6bc2014-05-07 17:24:22 +0100565 emit->stack_info_alloc = scope->stack_size + 50;
Damienff8ed772013-10-08 22:18:32 +0100566 emit->stack_info = m_new(stack_info_t, emit->stack_info_alloc);
Damien13ed3a62013-10-08 09:05:10 +0100567 }
568
Damien George2ac4af62014-08-15 16:45:41 +0100569 // set default type for return and arguments
570 emit->return_vtype = VTYPE_PYOBJ;
Damien Georgea5190a72014-08-15 22:39:08 +0100571 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100572 emit->local_vtype[i] = VTYPE_PYOBJ;
573 }
Damien Georgea5190a72014-08-15 22:39:08 +0100574
575 // local variables begin unbound, and have unknown type
576 for (mp_uint_t i = emit->scope->num_pos_args; i < emit->local_vtype_alloc; i++) {
577 emit->local_vtype[i] = VTYPE_UNBOUND;
578 }
579
580 // values on stack begin unbound
581 for (mp_uint_t i = 0; i < emit->stack_info_alloc; i++) {
Damien George2ac4af62014-08-15 16:45:41 +0100582 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgea5190a72014-08-15 22:39:08 +0100583 emit->stack_info[i].vtype = VTYPE_UNBOUND;
Damien13ed3a62013-10-08 09:05:10 +0100584 }
585
Damien Georgec90f59e2014-09-06 23:06:36 +0100586 ASM_START_PASS(emit->as, pass == MP_PASS_EMIT ? ASM_PASS_EMIT : ASM_PASS_COMPUTE);
Damien13ed3a62013-10-08 09:05:10 +0100587
588 // entry to function
589 int num_locals = 0;
Damien George36db6bc2014-05-07 17:24:22 +0100590 if (pass > MP_PASS_SCOPE) {
Damien13ed3a62013-10-08 09:05:10 +0100591 num_locals = scope->num_locals - REG_LOCAL_NUM;
592 if (num_locals < 0) {
593 num_locals = 0;
594 }
595 emit->stack_start = num_locals;
596 num_locals += scope->stack_size;
597 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100598 ASM_ENTRY(emit->as, num_locals);
Damien13ed3a62013-10-08 09:05:10 +0100599
600 // initialise locals from parameters
Damien3ef4abb2013-10-12 16:53:13 +0100601#if N_X64
Damien George2827d622014-04-27 15:50:52 +0100602 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100603 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100604 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100605 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100606 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100607 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100608 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien George81057362014-09-07 01:06:19 +0100609 } else if (i == 3) {
610 asm_x64_mov_r64_to_local(emit->as, REG_ARG_4, i - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +0100611 } else {
612 // TODO not implemented
613 assert(0);
614 }
615 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100616#elif N_X86
617 for (int i = 0; i < scope->num_pos_args; i++) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100618 if (i == 0) {
Damien George03281b32014-09-06 22:38:50 +0000619 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_1);
Damien Georgec90f59e2014-09-06 23:06:36 +0100620 } else if (i == 1) {
Damien George03281b32014-09-06 22:38:50 +0000621 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +0000622 } else if (i == 2) {
623 asm_x86_mov_arg_to_r32(emit->as, i, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +0100624 } else {
Damien George03281b32014-09-06 22:38:50 +0000625 asm_x86_mov_arg_to_r32(emit->as, i, REG_TEMP0);
626 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, i - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +0100627 }
628 }
Damien3ef4abb2013-10-12 16:53:13 +0100629#elif N_THUMB
Damien George2827d622014-04-27 15:50:52 +0100630 for (int i = 0; i < scope->num_pos_args; i++) {
Damien13ed3a62013-10-08 09:05:10 +0100631 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100632 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +0100633 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100634 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Damien13ed3a62013-10-08 09:05:10 +0100635 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100636 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Damien13ed3a62013-10-08 09:05:10 +0100637 } else if (i == 3) {
638 asm_thumb_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
639 } else {
640 // TODO not implemented
641 assert(0);
642 }
643 }
644
Damien Georgee9dac3b2014-09-29 22:10:41 +0100645 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100646 asm_thumb_mov_reg_i32(emit->as, ASM_THUMB_REG_R7, (mp_uint_t)mp_fun_table);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200647#elif N_ARM
648 for (int i = 0; i < scope->num_pos_args; i++) {
649 if (i == 0) {
Damien George3112cde2014-09-29 18:45:42 +0100650 ASM_MOV_REG_REG(emit->as, REG_LOCAL_1, REG_ARG_1);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200651 } else if (i == 1) {
Damien George3112cde2014-09-29 18:45:42 +0100652 ASM_MOV_REG_REG(emit->as, REG_LOCAL_2, REG_ARG_2);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200653 } else if (i == 2) {
Damien George3112cde2014-09-29 18:45:42 +0100654 ASM_MOV_REG_REG(emit->as, REG_LOCAL_3, REG_ARG_3);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +0200655 } else if (i == 3) {
656 asm_arm_mov_local_reg(emit->as, i - REG_LOCAL_NUM, REG_ARG_4);
657 } else {
658 // TODO not implemented
659 assert(0);
660 }
661 }
662
Damien Georgee9dac3b2014-09-29 22:10:41 +0100663 // TODO don't load r7 if we don't need it
Damien George0b610de2014-09-29 16:25:04 +0100664 asm_arm_mov_reg_i32(emit->as, ASM_ARM_REG_R7, (mp_uint_t)mp_fun_table);
Damien Georgec90f59e2014-09-06 23:06:36 +0100665#else
666 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +0100667#endif
668}
669
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200670STATIC void emit_native_end_pass(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100671 if (!emit->last_emit_was_return_value) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100672 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100673 }
Damien Georgec90f59e2014-09-06 23:06:36 +0100674 ASM_END_PASS(emit->as);
Damien13ed3a62013-10-08 09:05:10 +0100675
676 // check stack is back to zero size
677 if (emit->stack_size != 0) {
678 printf("ERROR: stack size not back to zero; got %d\n", emit->stack_size);
679 }
680
Damien George36db6bc2014-05-07 17:24:22 +0100681 if (emit->pass == MP_PASS_EMIT) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100682 void *f = ASM_GET_CODE(emit->as);
683 mp_uint_t f_len = ASM_GET_CODE_SIZE(emit->as);
Damien George2ac4af62014-08-15 16:45:41 +0100684
685 // compute type signature
Damien Georgee9dac3b2014-09-29 22:10:41 +0100686 // note that the lower 2 bits of a vtype are tho correct MP_NATIVE_TYPE_xxx
Damien George2ac4af62014-08-15 16:45:41 +0100687 mp_uint_t type_sig = emit->return_vtype & 3;
688 for (mp_uint_t i = 0; i < emit->scope->num_pos_args; i++) {
689 type_sig |= (emit->local_vtype[i] & 3) << (i * 2 + 2);
690 }
691
692 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 +0100693 }
694}
695
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200696STATIC bool emit_native_last_emit_was_return_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100697 return emit->last_emit_was_return_value;
698}
699
Damien Georged6230f62014-09-23 14:10:03 +0000700STATIC void adjust_stack(emit_t *emit, mp_int_t stack_size_delta) {
701 DEBUG_printf(" adjust_stack; stack_size=%d, delta=%d\n", emit->stack_size, stack_size_delta);
702 assert((mp_int_t)emit->stack_size + stack_size_delta >= 0);
Damien13ed3a62013-10-08 09:05:10 +0100703 emit->stack_size += stack_size_delta;
Damien George36db6bc2014-05-07 17:24:22 +0100704 if (emit->pass > MP_PASS_SCOPE && emit->stack_size > emit->scope->stack_size) {
Damien13ed3a62013-10-08 09:05:10 +0100705 emit->scope->stack_size = emit->stack_size;
706 }
707}
708
Damien Georged6230f62014-09-23 14:10:03 +0000709STATIC void emit_native_adjust_stack_size(emit_t *emit, mp_int_t delta) {
710 // If we are adjusting the stack in a positive direction (pushing) then we
711 // need to fill in values for the stack kind and vtype of the newly-pushed
712 // entries. These should be set to "value" (ie not reg or imm) because we
713 // should only need to adjust the stack due to a jump to this part in the
714 // code (and hence we have settled the stack before the jump).
715 for (mp_int_t i = 0; i < delta; i++) {
716 stack_info_t *si = &emit->stack_info[emit->stack_size + i];
717 si->kind = STACK_VALUE;
718 si->vtype = VTYPE_PYOBJ; // XXX we don't know the vtype...
719 }
720 adjust_stack(emit, delta);
721}
722
723STATIC void emit_native_set_source_line(emit_t *emit, mp_uint_t source_line) {
724}
725
Damienff8ed772013-10-08 22:18:32 +0100726/*
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200727STATIC void emit_pre_raw(emit_t *emit, int stack_size_delta) {
Damien13ed3a62013-10-08 09:05:10 +0100728 adjust_stack(emit, stack_size_delta);
729 emit->last_emit_was_return_value = false;
730}
Damienff8ed772013-10-08 22:18:32 +0100731*/
Damien13ed3a62013-10-08 09:05:10 +0100732
Damienff8ed772013-10-08 22:18:32 +0100733// this must be called at start of emit functions
Damien Georgece8f07a2014-03-27 23:30:26 +0000734STATIC void emit_native_pre(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100735 emit->last_emit_was_return_value = false;
736 // settle the stack
737 /*
738 if (regs_needed != 0) {
739 for (int i = 0; i < emit->stack_size; i++) {
740 switch (emit->stack_info[i].kind) {
741 case STACK_VALUE:
742 break;
743
744 case STACK_REG:
745 // TODO only push reg if in regs_needed
746 emit->stack_info[i].kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100747 ASM_MOV_REG_TO_LOCAL(emit->as, emit->stack_info[i].u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100748 break;
749
750 case STACK_IMM:
751 // don't think we ever need to push imms for settling
752 //ASM_MOV_IMM_TO_LOCAL(emit->last_imm, emit->stack_start + i);
753 break;
754 }
755 }
756 }
757 */
Damien13ed3a62013-10-08 09:05:10 +0100758}
759
Damien George3112cde2014-09-29 18:45:42 +0100760// depth==0 is top, depth==1 is before top, etc
761STATIC stack_info_t *peek_stack(emit_t *emit, mp_uint_t depth) {
762 return &emit->stack_info[emit->stack_size - 1 - depth];
763}
764
765// depth==0 is top, depth==1 is before top, etc
766STATIC vtype_kind_t peek_vtype(emit_t *emit, mp_uint_t depth) {
767 return peek_stack(emit, depth)->vtype;
Damienff8ed772013-10-08 22:18:32 +0100768}
Damien13ed3a62013-10-08 09:05:10 +0100769
Damiend2755ec2013-10-16 23:58:48 +0100770// pos=1 is TOS, pos=2 is next, etc
771// use pos=0 for no skipping
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200772STATIC void need_reg_single(emit_t *emit, int reg_needed, int skip_stack_pos) {
Damiend2755ec2013-10-16 23:58:48 +0100773 skip_stack_pos = emit->stack_size - skip_stack_pos;
Damienff8ed772013-10-08 22:18:32 +0100774 for (int i = 0; i < emit->stack_size; i++) {
Damiend2755ec2013-10-16 23:58:48 +0100775 if (i != skip_stack_pos) {
776 stack_info_t *si = &emit->stack_info[i];
777 if (si->kind == STACK_REG && si->u_reg == reg_needed) {
778 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100779 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100780 }
Damienff8ed772013-10-08 22:18:32 +0100781 }
782 }
783}
Damien13ed3a62013-10-08 09:05:10 +0100784
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200785STATIC void need_reg_all(emit_t *emit) {
Damienff8ed772013-10-08 22:18:32 +0100786 for (int i = 0; i < emit->stack_size; i++) {
787 stack_info_t *si = &emit->stack_info[i];
788 if (si->kind == STACK_REG) {
789 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100790 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damienff8ed772013-10-08 22:18:32 +0100791 }
Damien13ed3a62013-10-08 09:05:10 +0100792 }
793}
794
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200795STATIC void need_stack_settled(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +0000796 DEBUG_printf(" need_stack_settled; stack_size=%d\n", emit->stack_size);
Damiend2755ec2013-10-16 23:58:48 +0100797 for (int i = 0; i < emit->stack_size; i++) {
798 stack_info_t *si = &emit->stack_info[i];
799 if (si->kind == STACK_REG) {
Damien Georged6230f62014-09-23 14:10:03 +0000800 DEBUG_printf(" reg(%u) to local(%u)\n", si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100801 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100802 ASM_MOV_REG_TO_LOCAL(emit->as, si->u_reg, emit->stack_start + i);
Damiend2755ec2013-10-16 23:58:48 +0100803 }
804 }
805 for (int i = 0; i < emit->stack_size; i++) {
806 stack_info_t *si = &emit->stack_info[i];
807 if (si->kind == STACK_IMM) {
Damien Georged6230f62014-09-23 14:10:03 +0000808 DEBUG_printf(" imm(" INT_FMT ") to local(%u)\n", si->u_imm, emit->stack_start + i);
Damien George02d95d72014-08-29 20:05:32 +0100809 si->kind = STACK_VALUE;
Damien Georgec90f59e2014-09-06 23:06:36 +0100810 ASM_MOV_IMM_TO_LOCAL_USING(emit->as, si->u_imm, emit->stack_start + i, REG_TEMP0);
Damiend2755ec2013-10-16 23:58:48 +0100811 }
812 }
813}
814
815// pos=1 is TOS, pos=2 is next, etc
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200816STATIC void emit_access_stack(emit_t *emit, int pos, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100817 need_reg_single(emit, reg_dest, pos);
818 stack_info_t *si = &emit->stack_info[emit->stack_size - pos];
Damienff8ed772013-10-08 22:18:32 +0100819 *vtype = si->vtype;
820 switch (si->kind) {
821 case STACK_VALUE:
Damien Georgec90f59e2014-09-06 23:06:36 +0100822 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - pos, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100823 break;
824
Damienff8ed772013-10-08 22:18:32 +0100825 case STACK_REG:
826 if (si->u_reg != reg_dest) {
Damien George3112cde2014-09-29 18:45:42 +0100827 ASM_MOV_REG_REG(emit->as, reg_dest, si->u_reg);
Damien13ed3a62013-10-08 09:05:10 +0100828 }
829 break;
830
Damienff8ed772013-10-08 22:18:32 +0100831 case STACK_IMM:
Damien Georgec90f59e2014-09-06 23:06:36 +0100832 ASM_MOV_IMM_TO_REG(emit->as, si->u_imm, reg_dest);
Damien13ed3a62013-10-08 09:05:10 +0100833 break;
834 }
Damien13ed3a62013-10-08 09:05:10 +0100835}
836
Damien Georgee9dac3b2014-09-29 22:10:41 +0100837// does an efficient X=pop(); discard(); push(X)
838// needs a (non-temp) register in case the poped element was stored in the stack
839STATIC void emit_fold_stack_top(emit_t *emit, int reg_dest) {
840 stack_info_t *si = &emit->stack_info[emit->stack_size - 2];
841 si[0] = si[1];
842 if (si->kind == STACK_VALUE) {
843 // if folded element was on the stack we need to put it in a register
844 ASM_MOV_LOCAL_TO_REG(emit->as, emit->stack_start + emit->stack_size - 1, reg_dest);
845 si->kind = STACK_REG;
846 si->u_reg = reg_dest;
847 }
848 adjust_stack(emit, -1);
849}
850
851// If stacked value is in a register and the register is not r1 or r2, then
852// *reg_dest is set to that register. Otherwise the value is put in *reg_dest.
853STATIC 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 +0100854 emit->last_emit_was_return_value = false;
855 stack_info_t *si = peek_stack(emit, 0);
Damien Georgee9dac3b2014-09-29 22:10:41 +0100856 if (si->kind == STACK_REG && si->u_reg != not_r1 && si->u_reg != not_r2) {
Damien George3112cde2014-09-29 18:45:42 +0100857 *vtype = si->vtype;
858 *reg_dest = si->u_reg;
859 need_reg_single(emit, *reg_dest, 1);
860 } else {
861 emit_access_stack(emit, 1, vtype, *reg_dest);
862 }
863 adjust_stack(emit, -1);
864}
865
Damien Georgee6ce10a2014-09-06 18:38:20 +0100866STATIC void emit_pre_pop_discard(emit_t *emit) {
Damien Georgeccc85ea2014-05-10 13:40:46 +0100867 emit->last_emit_was_return_value = false;
868 adjust_stack(emit, -1);
869}
870
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200871STATIC void emit_pre_pop_reg(emit_t *emit, vtype_kind_t *vtype, int reg_dest) {
Damiend2755ec2013-10-16 23:58:48 +0100872 emit->last_emit_was_return_value = false;
873 emit_access_stack(emit, 1, vtype, reg_dest);
874 adjust_stack(emit, -1);
875}
876
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200877STATIC 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 +0100878 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100879 emit_pre_pop_reg(emit, vtypeb, regb);
Damien13ed3a62013-10-08 09:05:10 +0100880}
881
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200882STATIC 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 +0100883 emit_pre_pop_reg(emit, vtypea, rega);
Damienff8ed772013-10-08 22:18:32 +0100884 emit_pre_pop_reg(emit, vtypeb, regb);
885 emit_pre_pop_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100886}
887
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200888STATIC void emit_post(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +0100889}
890
Damien Georgee9dac3b2014-09-29 22:10:41 +0100891STATIC void emit_post_top_set_vtype(emit_t *emit, vtype_kind_t new_vtype) {
892 stack_info_t *si = &emit->stack_info[emit->stack_size - 1];
893 si->vtype = new_vtype;
894}
895
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200896STATIC void emit_post_push_reg(emit_t *emit, vtype_kind_t vtype, int reg) {
Damienff8ed772013-10-08 22:18:32 +0100897 stack_info_t *si = &emit->stack_info[emit->stack_size];
898 si->vtype = vtype;
899 si->kind = STACK_REG;
900 si->u_reg = reg;
Damien13ed3a62013-10-08 09:05:10 +0100901 adjust_stack(emit, 1);
902}
903
Damien George40f3c022014-07-03 13:25:24 +0100904STATIC void emit_post_push_imm(emit_t *emit, vtype_kind_t vtype, mp_int_t imm) {
Damienff8ed772013-10-08 22:18:32 +0100905 stack_info_t *si = &emit->stack_info[emit->stack_size];
906 si->vtype = vtype;
907 si->kind = STACK_IMM;
908 si->u_imm = imm;
909 adjust_stack(emit, 1);
910}
911
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200912STATIC 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 +0100913 emit_post_push_reg(emit, vtypea, rega);
914 emit_post_push_reg(emit, vtypeb, regb);
915}
916
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200917STATIC 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 +0100918 emit_post_push_reg(emit, vtypea, rega);
919 emit_post_push_reg(emit, vtypeb, regb);
920 emit_post_push_reg(emit, vtypec, regc);
Damien13ed3a62013-10-08 09:05:10 +0100921}
922
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200923STATIC 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 +0100924 emit_post_push_reg(emit, vtypea, rega);
925 emit_post_push_reg(emit, vtypeb, regb);
926 emit_post_push_reg(emit, vtypec, regc);
927 emit_post_push_reg(emit, vtyped, regd);
Damien13ed3a62013-10-08 09:05:10 +0100928}
929
Damien George7fe21912014-08-16 22:31:57 +0100930STATIC void emit_call(emit_t *emit, mp_fun_kind_t fun_kind) {
Damiend2755ec2013-10-16 23:58:48 +0100931 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100932 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100933}
934
Damien George7fe21912014-08-16 22:31:57 +0100935STATIC 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 +0100936 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100937 ASM_MOV_IMM_TO_REG(emit->as, arg_val, arg_reg);
938 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien13ed3a62013-10-08 09:05:10 +0100939}
940
Damien George40f3c022014-07-03 13:25:24 +0100941// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100942STATIC 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 +0100943 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100944 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val, arg_reg);
945 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgea32c1e42014-05-07 18:30:52 +0100946}
947
Damien George7fe21912014-08-16 22:31:57 +0100948STATIC 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 +0000949 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100950 ASM_MOV_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
951 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
952 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecd82e022014-02-02 13:11:48 +0000953}
954
Damien George40f3c022014-07-03 13:25:24 +0100955// the first arg is stored in the code aligned on a mp_uint_t boundary
Damien George7fe21912014-08-16 22:31:57 +0100956STATIC 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 +0100957 need_reg_all(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +0100958 ASM_MOV_ALIGNED_IMM_TO_REG(emit->as, arg_val1, arg_reg1);
959 ASM_MOV_IMM_TO_REG(emit->as, arg_val2, arg_reg2);
960 ASM_MOV_IMM_TO_REG(emit->as, arg_val3, arg_reg3);
961 ASM_CALL_IND(emit->as, mp_fun_table[fun_kind], fun_kind);
Damien Georgecdd96df2014-04-06 12:58:40 +0100962}
963
Damien George86de21b2014-08-16 22:06:11 +0100964// vtype of all n_pop objects is VTYPE_PYOBJ
965// Will convert any items that are not VTYPE_PYOBJ to this type and put them back on the stack.
966// If any conversions of non-immediate values are needed, then it uses REG_ARG_1, REG_ARG_2 and REG_RET.
967// Otherwise, it does not use any temporary registers (but may use reg_dest before loading it with stack pointer).
968STATIC void emit_get_stack_pointer_to_reg_for_pop(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_pop) {
969 need_reg_all(emit);
Damien13ed3a62013-10-08 09:05:10 +0100970
Damien George86de21b2014-08-16 22:06:11 +0100971 // First, store any immediate values to their respective place on the stack.
972 for (mp_uint_t i = 0; i < n_pop; i++) {
973 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
974 // must push any imm's to stack
975 // must convert them to VTYPE_PYOBJ for viper code
976 if (si->kind == STACK_IMM) {
977 si->kind = STACK_VALUE;
978 switch (si->vtype) {
979 case VTYPE_PYOBJ:
Damien Georgec90f59e2014-09-06 23:06:36 +0100980 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 +0100981 break;
982 case VTYPE_BOOL:
983 if (si->u_imm == 0) {
Damien Georgec90f59e2014-09-06 23:06:36 +0100984 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 +0100985 } else {
Damien Georgec90f59e2014-09-06 23:06:36 +0100986 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 +0100987 }
988 si->vtype = VTYPE_PYOBJ;
989 break;
990 case VTYPE_INT:
991 case VTYPE_UINT:
Damien Georgec90f59e2014-09-06 23:06:36 +0100992 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 +0100993 si->vtype = VTYPE_PYOBJ;
994 break;
995 default:
996 // not handled
997 assert(0);
998 }
999 }
1000
1001 // verify that this value is on the stack
1002 assert(si->kind == STACK_VALUE);
Damien13ed3a62013-10-08 09:05:10 +01001003 }
Damien George86de21b2014-08-16 22:06:11 +01001004
1005 // Second, convert any non-VTYPE_PYOBJ to that type.
1006 for (mp_uint_t i = 0; i < n_pop; i++) {
1007 stack_info_t *si = &emit->stack_info[emit->stack_size - 1 - i];
1008 if (si->vtype != VTYPE_PYOBJ) {
1009 mp_uint_t local_num = emit->stack_start + emit->stack_size - 1 - i;
Damien Georgec90f59e2014-09-06 23:06:36 +01001010 ASM_MOV_LOCAL_TO_REG(emit->as, local_num, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001011 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 +01001012 ASM_MOV_REG_TO_LOCAL(emit->as, REG_RET, local_num);
Damien George86de21b2014-08-16 22:06:11 +01001013 si->vtype = VTYPE_PYOBJ;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001014 DEBUG_printf(" convert_native_to_obj(local_num=" UINT_FMT ")\n", local_num);
Damien George86de21b2014-08-16 22:06:11 +01001015 }
1016 }
1017
1018 // Adujust the stack for a pop of n_pop items, and load the stack pointer into reg_dest.
1019 adjust_stack(emit, -n_pop);
Damien Georgec90f59e2014-09-06 23:06:36 +01001020 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001021}
1022
1023// vtype of all n_push objects is VTYPE_PYOBJ
1024STATIC void emit_get_stack_pointer_to_reg_for_push(emit_t *emit, mp_uint_t reg_dest, mp_uint_t n_push) {
1025 need_reg_all(emit);
1026 for (mp_uint_t i = 0; i < n_push; i++) {
1027 emit->stack_info[emit->stack_size + i].kind = STACK_VALUE;
1028 emit->stack_info[emit->stack_size + i].vtype = VTYPE_PYOBJ;
1029 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001030 ASM_MOV_LOCAL_ADDR_TO_REG(emit->as, emit->stack_start + emit->stack_size, reg_dest);
Damien George86de21b2014-08-16 22:06:11 +01001031 adjust_stack(emit, n_push);
1032}
1033
Damien George7ff996c2014-09-08 23:05:16 +01001034STATIC void emit_native_load_id(emit_t *emit, qstr qst) {
1035 emit_common_load_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001036}
1037
Damien George7ff996c2014-09-08 23:05:16 +01001038STATIC void emit_native_store_id(emit_t *emit, qstr qst) {
1039 emit_common_store_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001040}
1041
Damien George7ff996c2014-09-08 23:05:16 +01001042STATIC void emit_native_delete_id(emit_t *emit, qstr qst) {
1043 emit_common_delete_id(emit, &EXPORT_FUN(method_table), emit->scope, qst);
Damien13ed3a62013-10-08 09:05:10 +01001044}
1045
Damien George7ff996c2014-09-08 23:05:16 +01001046STATIC void emit_native_label_assign(emit_t *emit, mp_uint_t l) {
Damien Georged6230f62014-09-23 14:10:03 +00001047 DEBUG_printf("label_assign(" UINT_FMT ")\n", l);
Damien Georgece8f07a2014-03-27 23:30:26 +00001048 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001049 // need to commit stack because we can jump here from elsewhere
1050 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001051 ASM_LABEL_ASSIGN(emit->as, l);
Damien6ba13142013-11-02 20:34:54 +00001052 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001053}
1054
Damien Georgecdd96df2014-04-06 12:58:40 +01001055STATIC void emit_native_import_name(emit_t *emit, qstr qst) {
1056 DEBUG_printf("import_name %s\n", qstr_str(qst));
1057 vtype_kind_t vtype_fromlist;
1058 vtype_kind_t vtype_level;
1059 emit_pre_pop_reg_reg(emit, &vtype_fromlist, REG_ARG_2, &vtype_level, REG_ARG_3); // arg2 = fromlist, arg3 = level
1060 assert(vtype_fromlist == VTYPE_PYOBJ);
1061 assert(vtype_level == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001062 emit_call_with_imm_arg(emit, MP_F_IMPORT_NAME, qst, REG_ARG_1); // arg1 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001063 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001064}
1065
Damien Georgecdd96df2014-04-06 12:58:40 +01001066STATIC void emit_native_import_from(emit_t *emit, qstr qst) {
1067 DEBUG_printf("import_from %s\n", qstr_str(qst));
1068 emit_native_pre(emit);
1069 vtype_kind_t vtype_module;
1070 emit_access_stack(emit, 1, &vtype_module, REG_ARG_1); // arg1 = module
1071 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001072 emit_call_with_imm_arg(emit, MP_F_IMPORT_FROM, qst, REG_ARG_2); // arg2 = import name
Damien Georgecdd96df2014-04-06 12:58:40 +01001073 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001074}
1075
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001076STATIC void emit_native_import_star(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001077 DEBUG_printf("import_star\n");
1078 vtype_kind_t vtype_module;
1079 emit_pre_pop_reg(emit, &vtype_module, REG_ARG_1); // arg1 = module
1080 assert(vtype_module == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001081 emit_call(emit, MP_F_IMPORT_ALL);
Damien Georgecdd96df2014-04-06 12:58:40 +01001082 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001083}
1084
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001085STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
Damien Georged6230f62014-09-23 14:10:03 +00001086 DEBUG_printf("load_const_tok(tok=%u)\n", tok);
Damien Georgece8f07a2014-03-27 23:30:26 +00001087 emit_native_pre(emit);
Damien George39dc1452014-10-03 19:52:22 +01001088 vtype_kind_t vtype;
Damien George40f3c022014-07-03 13:25:24 +01001089 mp_uint_t val;
Damien13ed3a62013-10-08 09:05:10 +01001090 if (emit->do_viper_types) {
1091 switch (tok) {
Damiend99b0522013-12-21 18:17:45 +00001092 case MP_TOKEN_KW_NONE: vtype = VTYPE_PTR_NONE; val = 0; break;
1093 case MP_TOKEN_KW_FALSE: vtype = VTYPE_BOOL; val = 0; break;
1094 case MP_TOKEN_KW_TRUE: vtype = VTYPE_BOOL; val = 1; break;
Damien13ed3a62013-10-08 09:05:10 +01001095 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1096 }
1097 } else {
1098 vtype = VTYPE_PYOBJ;
1099 switch (tok) {
Damien George40f3c022014-07-03 13:25:24 +01001100 case MP_TOKEN_KW_NONE: val = (mp_uint_t)mp_const_none; break;
1101 case MP_TOKEN_KW_FALSE: val = (mp_uint_t)mp_const_false; break;
1102 case MP_TOKEN_KW_TRUE: val = (mp_uint_t)mp_const_true; break;
Damien13ed3a62013-10-08 09:05:10 +01001103 default: assert(0); vtype = 0; val = 0; // shouldn't happen
1104 }
1105 }
1106 emit_post_push_imm(emit, vtype, val);
1107}
1108
Damien George40f3c022014-07-03 13:25:24 +01001109STATIC void emit_native_load_const_small_int(emit_t *emit, mp_int_t arg) {
Damien Georged6230f62014-09-23 14:10:03 +00001110 DEBUG_printf("load_const_small_int(int=" INT_FMT ")\n", arg);
Damien Georgece8f07a2014-03-27 23:30:26 +00001111 emit_native_pre(emit);
Damien13ed3a62013-10-08 09:05:10 +01001112 if (emit->do_viper_types) {
1113 emit_post_push_imm(emit, VTYPE_INT, arg);
1114 } else {
1115 emit_post_push_imm(emit, VTYPE_PYOBJ, (arg << 1) | 1);
1116 }
1117}
1118
Damien Georgecdd96df2014-04-06 12:58:40 +01001119STATIC void emit_native_load_const_int(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001120 DEBUG_printf("load_const_int %s\n", qstr_str(qst));
Damien Georgecdd96df2014-04-06 12:58:40 +01001121 // for viper: load integer, check fits in 32 bits
1122 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001123 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_INT, qst, REG_ARG_1);
Damien Georgecdd96df2014-04-06 12:58:40 +01001124 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001125}
1126
Damien George7ff996c2014-09-08 23:05:16 +01001127STATIC void emit_native_load_const_dec(emit_t *emit, qstr qst) {
Damien6ba13142013-11-02 20:34:54 +00001128 // for viper, a float/complex is just a Python object
Damien Georgece8f07a2014-03-27 23:30:26 +00001129 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001130 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_DEC, qst, REG_ARG_1);
Damien6ba13142013-11-02 20:34:54 +00001131 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001132}
1133
Damien George7ff996c2014-09-08 23:05:16 +01001134STATIC void emit_native_load_const_str(emit_t *emit, qstr qst, bool bytes) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001135 emit_native_pre(emit);
Damien Georgebb295462014-09-12 23:15:06 +01001136 // TODO: Eventually we want to be able to work with raw pointers in viper to
1137 // do native array access. For now we just load them as any other object.
1138 /*
Damien13ed3a62013-10-08 09:05:10 +01001139 if (emit->do_viper_types) {
1140 // not implemented properly
1141 // load a pointer to the asciiz string?
1142 assert(0);
Damien George7ff996c2014-09-08 23:05:16 +01001143 emit_post_push_imm(emit, VTYPE_PTR, (mp_uint_t)qstr_str(qst));
Damien Georgebb295462014-09-12 23:15:06 +01001144 } else
1145 */
1146 {
Damien Georgeb601d952014-06-30 05:17:25 +01001147 if (bytes) {
Damien George7ff996c2014-09-08 23:05:16 +01001148 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_BYTES, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001149 } else {
Damien George7ff996c2014-09-08 23:05:16 +01001150 emit_call_with_imm_arg(emit, MP_F_LOAD_CONST_STR, qst, REG_ARG_1);
Damien Georgeb601d952014-06-30 05:17:25 +01001151 }
Damien13ed3a62013-10-08 09:05:10 +01001152 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1153 }
1154}
1155
Damien George3558f622014-04-20 17:50:40 +01001156STATIC void emit_native_load_null(emit_t *emit) {
1157 emit_native_pre(emit);
1158 emit_post_push_imm(emit, VTYPE_PYOBJ, 0);
1159}
1160
Damien George7ff996c2014-09-08 23:05:16 +01001161STATIC void emit_native_load_fast(emit_t *emit, qstr qst, mp_uint_t id_flags, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001162 vtype_kind_t vtype = emit->local_vtype[local_num];
1163 if (vtype == VTYPE_UNBOUND) {
Damien George7ff996c2014-09-08 23:05:16 +01001164 printf("ViperTypeError: local %s used before type known\n", qstr_str(qst));
Damien13ed3a62013-10-08 09:05:10 +01001165 }
Damien Georgece8f07a2014-03-27 23:30:26 +00001166 emit_native_pre(emit);
Damien3ef4abb2013-10-12 16:53:13 +01001167#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001168 if (local_num == 0) {
1169 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001170 } else if (local_num == 1) {
1171 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1172 } else if (local_num == 2) {
1173 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001174 } else {
Damien George0b610de2014-09-29 16:25:04 +01001175 need_reg_single(emit, REG_TEMP0, 0);
1176 asm_x64_mov_local_to_r64(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1177 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001178 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001179#elif N_X86
1180 if (local_num == 0) {
1181 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001182 } else if (local_num == 1) {
1183 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001184 } else if (local_num == 2) {
1185 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001186 } else {
Damien George0b610de2014-09-29 16:25:04 +01001187 need_reg_single(emit, REG_TEMP0, 0);
1188 asm_x86_mov_local_to_r32(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
1189 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien Georgec90f59e2014-09-06 23:06:36 +01001190 }
Damien3ef4abb2013-10-12 16:53:13 +01001191#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001192 if (local_num == 0) {
1193 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1194 } else if (local_num == 1) {
1195 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1196 } else if (local_num == 2) {
1197 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1198 } else {
Damien George0b610de2014-09-29 16:25:04 +01001199 need_reg_single(emit, REG_TEMP0, 0);
1200 asm_thumb_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1201 emit_post_push_reg(emit, vtype, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001202 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001203#elif N_ARM
1204 if (local_num == 0) {
1205 emit_post_push_reg(emit, vtype, REG_LOCAL_1);
1206 } else if (local_num == 1) {
1207 emit_post_push_reg(emit, vtype, REG_LOCAL_2);
1208 } else if (local_num == 2) {
1209 emit_post_push_reg(emit, vtype, REG_LOCAL_3);
1210 } else {
Damien George0b610de2014-09-29 16:25:04 +01001211 need_reg_single(emit, REG_TEMP0, 0);
1212 asm_arm_mov_reg_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
1213 emit_post_push_reg(emit, vtype, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001214 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001215#else
1216 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001217#endif
1218}
1219
Damien George7ff996c2014-09-08 23:05:16 +01001220STATIC void emit_native_load_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001221 // not implemented
1222 // in principle could support this quite easily (ldr r0, [r0, #0]) and then get closed over variables!
1223 assert(0);
1224}
1225
Damien George7ff996c2014-09-08 23:05:16 +01001226STATIC void emit_native_load_name(emit_t *emit, qstr qst) {
Damien Georged6230f62014-09-23 14:10:03 +00001227 DEBUG_printf("load_name(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001228 emit_native_pre(emit);
Damien George7ff996c2014-09-08 23:05:16 +01001229 emit_call_with_imm_arg(emit, MP_F_LOAD_NAME, qst, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001230 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1231}
1232
Damien George7ff996c2014-09-08 23:05:16 +01001233STATIC void emit_native_load_global(emit_t *emit, qstr qst) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001234 DEBUG_printf("load_global(%s)\n", qstr_str(qst));
Damien Georgece8f07a2014-03-27 23:30:26 +00001235 emit_native_pre(emit);
Damien Georgee9dac3b2014-09-29 22:10:41 +01001236 // check for builtin casting operators
1237 if (emit->do_viper_types && qst == MP_QSTR_int) {
1238 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_INT);
1239 } else if (emit->do_viper_types && qst == MP_QSTR_uint) {
1240 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_UINT);
1241 } else if (emit->do_viper_types && qst == MP_QSTR_ptr) {
1242 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR);
1243 } else if (emit->do_viper_types && qst == MP_QSTR_ptr8) {
1244 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR8);
1245 } else if (emit->do_viper_types && qst == MP_QSTR_ptr16) {
1246 emit_post_push_imm(emit, VTYPE_BUILTIN_CAST, VTYPE_PTR16);
1247 } else {
1248 emit_call_with_imm_arg(emit, MP_F_LOAD_GLOBAL, qst, REG_ARG_1);
1249 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1250 }
Damien13ed3a62013-10-08 09:05:10 +01001251}
1252
Damien George7ff996c2014-09-08 23:05:16 +01001253STATIC void emit_native_load_attr(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001254 // depends on type of subject:
1255 // - integer, function, pointer to integers: error
1256 // - pointer to structure: get member, quite easy
Damien Georged17926d2014-03-30 13:35:08 +01001257 // - Python object: call mp_load_attr, and needs to be typed to convert result
Damien13ed3a62013-10-08 09:05:10 +01001258 vtype_kind_t vtype_base;
1259 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1260 assert(vtype_base == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001261 emit_call_with_imm_arg(emit, MP_F_LOAD_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien13ed3a62013-10-08 09:05:10 +01001262 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1263}
1264
Damien George7ff996c2014-09-08 23:05:16 +01001265STATIC void emit_native_load_method(emit_t *emit, qstr qst) {
Damien13ed3a62013-10-08 09:05:10 +01001266 vtype_kind_t vtype_base;
1267 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1268 assert(vtype_base == VTYPE_PYOBJ);
1269 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, 2); // arg3 = dest ptr
Damien George7ff996c2014-09-08 23:05:16 +01001270 emit_call_with_imm_arg(emit, MP_F_LOAD_METHOD, qst, REG_ARG_2); // arg2 = method name
Damien13ed3a62013-10-08 09:05:10 +01001271}
1272
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001273STATIC void emit_native_load_build_class(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001274 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001275 emit_call(emit, MP_F_LOAD_BUILD_CLASS);
Damien7f5dacf2013-10-10 11:24:39 +01001276 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001277}
1278
Damien George729f7b42014-04-17 22:10:53 +01001279STATIC void emit_native_load_subscr(emit_t *emit) {
1280 vtype_kind_t vtype_lhs, vtype_rhs;
1281 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_2, &vtype_lhs, REG_ARG_1);
1282 if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George7fe21912014-08-16 22:31:57 +01001283 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 +01001284 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1285 } else {
1286 printf("ViperTypeError: can't do subscr of types %d and %d\n", vtype_lhs, vtype_rhs);
Damien George729f7b42014-04-17 22:10:53 +01001287 }
1288}
1289
Damien George7ff996c2014-09-08 23:05:16 +01001290STATIC void emit_native_store_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien13ed3a62013-10-08 09:05:10 +01001291 vtype_kind_t vtype;
Damien3ef4abb2013-10-12 16:53:13 +01001292#if N_X64
Damien13ed3a62013-10-08 09:05:10 +01001293 if (local_num == 0) {
1294 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George81057362014-09-07 01:06:19 +01001295 } else if (local_num == 1) {
1296 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1297 } else if (local_num == 2) {
1298 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien13ed3a62013-10-08 09:05:10 +01001299 } else {
Damien George0b610de2014-09-29 16:25:04 +01001300 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1301 asm_x64_mov_r64_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien13ed3a62013-10-08 09:05:10 +01001302 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001303#elif N_X86
1304 if (local_num == 0) {
1305 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
Damien George03281b32014-09-06 22:38:50 +00001306 } else if (local_num == 1) {
1307 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
Damien George25d90412014-09-06 23:24:32 +00001308 } else if (local_num == 2) {
1309 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
Damien Georgec90f59e2014-09-06 23:06:36 +01001310 } else {
Damien George0b610de2014-09-29 16:25:04 +01001311 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1312 asm_x86_mov_r32_to_local(emit->as, REG_TEMP0, local_num - REG_LOCAL_NUM);
Damien Georgec90f59e2014-09-06 23:06:36 +01001313 }
Damien3ef4abb2013-10-12 16:53:13 +01001314#elif N_THUMB
Damien13ed3a62013-10-08 09:05:10 +01001315 if (local_num == 0) {
1316 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1317 } else if (local_num == 1) {
1318 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1319 } else if (local_num == 2) {
1320 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1321 } else {
Damien George0b610de2014-09-29 16:25:04 +01001322 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1323 asm_thumb_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Damien13ed3a62013-10-08 09:05:10 +01001324 }
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001325#elif N_ARM
1326 if (local_num == 0) {
1327 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_1);
1328 } else if (local_num == 1) {
1329 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_2);
1330 } else if (local_num == 2) {
1331 emit_pre_pop_reg(emit, &vtype, REG_LOCAL_3);
1332 } else {
Damien George0b610de2014-09-29 16:25:04 +01001333 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1334 asm_arm_mov_local_reg(emit->as, local_num - REG_LOCAL_NUM, REG_TEMP0);
Fabian Vogtfe3d16e2014-08-16 22:55:53 +02001335 }
Damien Georgec90f59e2014-09-06 23:06:36 +01001336#else
1337 #error not implemented
Damien13ed3a62013-10-08 09:05:10 +01001338#endif
1339
1340 emit_post(emit);
1341
1342 // check types
1343 if (emit->local_vtype[local_num] == VTYPE_UNBOUND) {
1344 // first time this local is assigned, so give it a type of the object stored in it
1345 emit->local_vtype[local_num] = vtype;
1346 } else if (emit->local_vtype[local_num] != vtype) {
1347 // type of local is not the same as object stored in it
Damien George7ff996c2014-09-08 23:05:16 +01001348 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 +01001349 }
1350}
1351
Damien George7ff996c2014-09-08 23:05:16 +01001352STATIC void emit_native_store_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien9ecbcff2013-12-11 00:41:43 +00001353 // not implemented
1354 assert(0);
1355}
1356
Damien George7ff996c2014-09-08 23:05:16 +01001357STATIC void emit_native_store_name(emit_t *emit, qstr qst) {
Damien Georged17926d2014-03-30 13:35:08 +01001358 // mp_store_name, but needs conversion of object (maybe have mp_viper_store_name(obj, type))
Damien13ed3a62013-10-08 09:05:10 +01001359 vtype_kind_t vtype;
1360 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1361 assert(vtype == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001362 emit_call_with_imm_arg(emit, MP_F_STORE_NAME, qst, REG_ARG_1); // arg1 = name
Damien13ed3a62013-10-08 09:05:10 +01001363 emit_post(emit);
1364}
1365
Damien George7ff996c2014-09-08 23:05:16 +01001366STATIC void emit_native_store_global(emit_t *emit, qstr qst) {
Damien George3112cde2014-09-29 18:45:42 +01001367 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001368 if (vtype == VTYPE_PYOBJ) {
1369 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1370 } else {
1371 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
Damien George7fe21912014-08-16 22:31:57 +01001372 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 +01001373 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01001374 }
Damien George7ff996c2014-09-08 23:05:16 +01001375 emit_call_with_imm_arg(emit, MP_F_STORE_GLOBAL, qst, REG_ARG_1); // arg1 = name
Damien Georgee6c0dff2014-08-15 23:47:59 +01001376 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001377}
1378
Damien George7ff996c2014-09-08 23:05:16 +01001379STATIC void emit_native_store_attr(emit_t *emit, qstr qst) {
Damien7f5dacf2013-10-10 11:24:39 +01001380 vtype_kind_t vtype_base, vtype_val;
1381 emit_pre_pop_reg_reg(emit, &vtype_base, REG_ARG_1, &vtype_val, REG_ARG_3); // arg1 = base, arg3 = value
1382 assert(vtype_base == VTYPE_PYOBJ);
1383 assert(vtype_val == VTYPE_PYOBJ);
Damien George7ff996c2014-09-08 23:05:16 +01001384 emit_call_with_imm_arg(emit, MP_F_STORE_ATTR, qst, REG_ARG_2); // arg2 = attribute name
Damien7f5dacf2013-10-10 11:24:39 +01001385 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001386}
1387
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001388STATIC void emit_native_store_subscr(emit_t *emit) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01001389 DEBUG_printf("store_subscr\n");
1390 // need to compile: base[index] = value
1391
1392 // pop: index, base, value
1393 // optimise case where index is an immediate
1394 vtype_kind_t vtype_base = peek_vtype(emit, 1);
1395
1396 if (vtype_base == VTYPE_PYOBJ) {
1397 // standard Python call
1398 vtype_kind_t vtype_index, vtype_value;
1399 emit_pre_pop_reg_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1, &vtype_value, REG_ARG_3);
1400 assert(vtype_index == VTYPE_PYOBJ);
1401 assert(vtype_value == VTYPE_PYOBJ);
1402 emit_call(emit, MP_F_OBJ_SUBSCR);
1403 } else {
Damien Georgedfef4242014-09-29 21:41:41 +00001404 // viper store
1405 // TODO The different machine architectures have very different
1406 // capabilities and requirements for stores, so probably best to
1407 // write a completely separate store-optimiser for each one.
Damien Georgee9dac3b2014-09-29 22:10:41 +01001408 stack_info_t *top = peek_stack(emit, 0);
1409 if (top->vtype == VTYPE_INT && top->kind == STACK_IMM) {
1410 // index is an immediate
1411 mp_int_t index_value = top->u_imm;
1412 emit_pre_pop_discard(emit); // discard index
1413 vtype_kind_t vtype_value;
1414 int reg_base = REG_ARG_1;
1415 int reg_index = REG_ARG_2;
1416 int reg_value = REG_ARG_3;
1417 emit_pre_pop_reg_flexible(emit, &vtype_base, &reg_base, reg_index, reg_value);
Damien Georgedfef4242014-09-29 21:41:41 +00001418 #if N_X86
1419 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1420 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1421 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001422 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, reg_base, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001423 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001424 switch (vtype_base) {
1425 case VTYPE_PTR8: {
1426 // pointer to 8-bit memory
1427 // TODO optimise to use thumb strb r1, [r2, r3]
1428 if (index_value != 0) {
1429 // index is non-zero
1430 #if N_THUMB
1431 if (index_value > 0 && index_value < 32) {
1432 asm_thumb_strb_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1433 break;
1434 }
1435 #endif
1436 ASM_MOV_IMM_TO_REG(emit->as, index_value, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001437 #if N_ARM
1438 asm_arm_strb_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1439 return;
1440 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001441 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add index to base
1442 reg_base = reg_index;
1443 }
1444 ASM_STORE8_REG_REG(emit->as, reg_value, reg_base); // store value to (base+index)
1445 break;
1446 }
1447 case VTYPE_PTR16: {
1448 // pointer to 16-bit memory
1449 if (index_value != 0) {
1450 // index is a non-zero immediate
1451 #if N_THUMB
1452 if (index_value > 0 && index_value < 32) {
1453 asm_thumb_strh_rlo_rlo_i5(emit->as, reg_value, reg_base, index_value);
1454 break;
1455 }
1456 #endif
1457 ASM_MOV_IMM_TO_REG(emit->as, index_value << 1, reg_index);
Fabian Vogte5268962014-10-04 00:53:46 +02001458 #if N_ARM
1459 asm_arm_strh_reg_reg_reg(emit->as, reg_value, reg_base, reg_index);
1460 return;
1461 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001462 ASM_ADD_REG_REG(emit->as, reg_index, reg_base); // add 2*index to base
1463 reg_base = reg_index;
1464 }
1465 ASM_STORE16_REG_REG(emit->as, reg_value, reg_base); // store value to (base+2*index)
1466 break;
1467 }
1468 default:
1469 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1470 }
1471 } else {
1472 // index is not an immediate
1473 vtype_kind_t vtype_index, vtype_value;
1474 int reg_index = REG_ARG_2;
1475 int reg_value = REG_ARG_3;
1476 emit_pre_pop_reg_flexible(emit, &vtype_index, &reg_index, REG_ARG_1, reg_value);
1477 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1);
Damien Georgedfef4242014-09-29 21:41:41 +00001478 #if N_X86
1479 // special case: x86 needs byte stores to be from lower 4 regs (REG_ARG_3 is EDX)
1480 emit_pre_pop_reg(emit, &vtype_value, reg_value);
1481 #else
Damien Georgee9dac3b2014-09-29 22:10:41 +01001482 emit_pre_pop_reg_flexible(emit, &vtype_value, &reg_value, REG_ARG_1, reg_index);
Damien Georgedfef4242014-09-29 21:41:41 +00001483 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001484 switch (vtype_base) {
1485 case VTYPE_PTR8: {
1486 // pointer to 8-bit memory
1487 // TODO optimise to use thumb strb r1, [r2, r3]
1488 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001489 #if N_ARM
1490 asm_arm_strb_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1491 break;
1492 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001493 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1494 ASM_STORE8_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+index)
1495 break;
1496 }
1497 case VTYPE_PTR16: {
1498 // pointer to 16-bit memory
1499 assert(vtype_index == VTYPE_INT);
Fabian Vogte5268962014-10-04 00:53:46 +02001500 #if N_ARM
1501 asm_arm_strh_reg_reg_reg(emit->as, reg_value, REG_ARG_1, reg_index);
1502 break;
1503 #endif
Damien Georgee9dac3b2014-09-29 22:10:41 +01001504 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1505 ASM_ADD_REG_REG(emit->as, REG_ARG_1, reg_index); // add index to base
1506 ASM_STORE16_REG_REG(emit->as, reg_value, REG_ARG_1); // store value to (base+2*index)
1507 break;
1508 }
1509 default:
1510 printf("ViperTypeError: can't store to type %d\n", vtype_base);
1511 }
1512 }
1513
1514 }
Damien13ed3a62013-10-08 09:05:10 +01001515}
1516
Damien George7ff996c2014-09-08 23:05:16 +01001517STATIC void emit_native_delete_fast(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001518 // TODO implement me!
Damien13ed3a62013-10-08 09:05:10 +01001519 // could support for Python types, just set to None (so GC can reclaim it)
Damien13ed3a62013-10-08 09:05:10 +01001520}
1521
Damien George7ff996c2014-09-08 23:05:16 +01001522STATIC void emit_native_delete_deref(emit_t *emit, qstr qst, mp_uint_t local_num) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001523 // TODO implement me!
Damien9ecbcff2013-12-11 00:41:43 +00001524}
1525
Damien Georgee6ce10a2014-09-06 18:38:20 +01001526STATIC void emit_native_delete_name(emit_t *emit, qstr qst) {
1527 emit_native_pre(emit);
1528 emit_call_with_imm_arg(emit, MP_F_DELETE_NAME, qst, REG_ARG_1);
1529 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001530}
1531
Damien Georgee6ce10a2014-09-06 18:38:20 +01001532STATIC void emit_native_delete_global(emit_t *emit, qstr qst) {
1533 emit_native_pre(emit);
1534 emit_call_with_imm_arg(emit, MP_F_DELETE_GLOBAL, qst, REG_ARG_1);
1535 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001536}
1537
Damien Georgee6ce10a2014-09-06 18:38:20 +01001538STATIC void emit_native_delete_attr(emit_t *emit, qstr qst) {
Damien George780e54c2014-06-22 18:35:04 +01001539 vtype_kind_t vtype_base;
1540 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = base
1541 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgee6ce10a2014-09-06 18:38:20 +01001542 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 +01001543 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001544}
1545
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001546STATIC void emit_native_delete_subscr(emit_t *emit) {
Damien George729f7b42014-04-17 22:10:53 +01001547 vtype_kind_t vtype_index, vtype_base;
1548 emit_pre_pop_reg_reg(emit, &vtype_index, REG_ARG_2, &vtype_base, REG_ARG_1); // index, base
1549 assert(vtype_index == VTYPE_PYOBJ);
1550 assert(vtype_base == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001551 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 +01001552}
1553
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001554STATIC void emit_native_dup_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001555 DEBUG_printf("dup_top\n");
Damien13ed3a62013-10-08 09:05:10 +01001556 vtype_kind_t vtype;
1557 emit_pre_pop_reg(emit, &vtype, REG_TEMP0);
1558 emit_post_push_reg_reg(emit, vtype, REG_TEMP0, vtype, REG_TEMP0);
1559}
1560
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001561STATIC void emit_native_dup_top_two(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001562 vtype_kind_t vtype0, vtype1;
1563 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1564 emit_post_push_reg_reg_reg_reg(emit, vtype1, REG_TEMP1, vtype0, REG_TEMP0, vtype1, REG_TEMP1, vtype0, REG_TEMP0);
1565}
1566
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001567STATIC void emit_native_pop_top(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001568 DEBUG_printf("pop_top\n");
Damien Georgee6ce10a2014-09-06 18:38:20 +01001569 emit_pre_pop_discard(emit);
Damien13ed3a62013-10-08 09:05:10 +01001570 emit_post(emit);
1571}
1572
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001573STATIC void emit_native_rot_two(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001574 DEBUG_printf("rot_two\n");
Damienff8ed772013-10-08 22:18:32 +01001575 vtype_kind_t vtype0, vtype1;
1576 emit_pre_pop_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1);
1577 emit_post_push_reg_reg(emit, vtype0, REG_TEMP0, vtype1, REG_TEMP1);
Damien13ed3a62013-10-08 09:05:10 +01001578}
1579
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001580STATIC void emit_native_rot_three(emit_t *emit) {
Damien Georged6230f62014-09-23 14:10:03 +00001581 DEBUG_printf("rot_three\n");
Damien13ed3a62013-10-08 09:05:10 +01001582 vtype_kind_t vtype0, vtype1, vtype2;
1583 emit_pre_pop_reg_reg_reg(emit, &vtype0, REG_TEMP0, &vtype1, REG_TEMP1, &vtype2, REG_TEMP2);
1584 emit_post_push_reg_reg_reg(emit, vtype0, REG_TEMP0, vtype2, REG_TEMP2, vtype1, REG_TEMP1);
1585}
1586
Damien George7ff996c2014-09-08 23:05:16 +01001587STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001588 DEBUG_printf("jump(label=" UINT_FMT ")\n", label);
Damien Georgece8f07a2014-03-27 23:30:26 +00001589 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01001590 // need to commit stack because we are jumping elsewhere
1591 need_stack_settled(emit);
Damien Georgec90f59e2014-09-06 23:06:36 +01001592 ASM_JUMP(emit->as, label);
Damien13ed3a62013-10-08 09:05:10 +01001593 emit_post(emit);
1594}
1595
Damien George7ff996c2014-09-08 23:05:16 +01001596STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
Damien George3112cde2014-09-29 18:45:42 +01001597 vtype_kind_t vtype = peek_vtype(emit, 0);
Damien George6f813482014-09-29 16:41:37 +01001598 switch (vtype) {
1599 case VTYPE_PYOBJ:
1600 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1601 if (!pop) {
1602 adjust_stack(emit, 1);
1603 }
1604 emit_call(emit, MP_F_OBJ_IS_TRUE);
1605 break;
1606 case VTYPE_BOOL:
1607 case VTYPE_INT:
1608 case VTYPE_UINT:
1609 emit_pre_pop_reg(emit, &vtype, REG_RET);
1610 if (!pop) {
1611 adjust_stack(emit, 1);
1612 }
1613 break;
1614 default:
1615 printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
1616 assert(0);
Damien13ed3a62013-10-08 09:05:10 +01001617 }
Damien Georgea32c1e42014-05-07 18:30:52 +01001618 // need to commit stack because we may jump elsewhere
1619 need_stack_settled(emit);
Damien13ed3a62013-10-08 09:05:10 +01001620}
1621
Damien George7ff996c2014-09-08 23:05:16 +01001622STATIC void emit_native_pop_jump_if_true(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001623 DEBUG_printf("pop_jump_if_true(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001624 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001625 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien1a6633a2013-11-03 13:58:19 +00001626 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001627}
Damien1a6633a2013-11-03 13:58:19 +00001628
Damien George7ff996c2014-09-08 23:05:16 +01001629STATIC void emit_native_pop_jump_if_false(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001630 DEBUG_printf("pop_jump_if_false(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001631 emit_native_jump_helper(emit, label, true);
Damien Georgec90f59e2014-09-06 23:06:36 +01001632 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001633 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001634}
Damien Georgea32c1e42014-05-07 18:30:52 +01001635
Damien George7ff996c2014-09-08 23:05:16 +01001636STATIC void emit_native_jump_if_true_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001637 DEBUG_printf("jump_if_true_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001638 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001639 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001640 adjust_stack(emit, -1);
1641 emit_post(emit);
1642}
1643
Damien George7ff996c2014-09-08 23:05:16 +01001644STATIC void emit_native_jump_if_false_or_pop(emit_t *emit, mp_uint_t label) {
Damien Georged6230f62014-09-23 14:10:03 +00001645 DEBUG_printf("jump_if_false_or_pop(label=" UINT_FMT ")\n", label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001646 emit_native_jump_helper(emit, label, false);
Damien Georgec90f59e2014-09-06 23:06:36 +01001647 ASM_JUMP_IF_REG_ZERO(emit->as, REG_RET, label);
Damien Georgea32c1e42014-05-07 18:30:52 +01001648 adjust_stack(emit, -1);
1649 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001650}
1651
Damien George7ff996c2014-09-08 23:05:16 +01001652STATIC void emit_native_break_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien George25c84642014-05-30 15:20:41 +01001653 emit_native_jump(emit, label & ~MP_EMIT_BREAK_FROM_FOR); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001654}
Damien Georgea32c1e42014-05-07 18:30:52 +01001655
Damien George7ff996c2014-09-08 23:05:16 +01001656STATIC void emit_native_continue_loop(emit_t *emit, mp_uint_t label, mp_uint_t except_depth) {
Damien Georgea32c1e42014-05-07 18:30:52 +01001657 emit_native_jump(emit, label); // TODO properly
Damien13ed3a62013-10-08 09:05:10 +01001658}
Damien Georgea32c1e42014-05-07 18:30:52 +01001659
Damien George7ff996c2014-09-08 23:05:16 +01001660STATIC void emit_native_setup_with(emit_t *emit, mp_uint_t label) {
Damien13ed3a62013-10-08 09:05:10 +01001661 // not supported, or could be with runtime call
1662 assert(0);
1663}
Damien Georgeb601d952014-06-30 05:17:25 +01001664
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001665STATIC void emit_native_with_cleanup(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001666 assert(0);
1667}
Damien Georgeb601d952014-06-30 05:17:25 +01001668
Damien George7ff996c2014-09-08 23:05:16 +01001669STATIC void emit_native_setup_except(emit_t *emit, mp_uint_t label) {
Damien Georgeb601d952014-06-30 05:17:25 +01001670 emit_native_pre(emit);
1671 // need to commit stack because we may jump elsewhere
1672 need_stack_settled(emit);
Damien George40f3c022014-07-03 13:25:24 +01001673 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 +01001674 emit_call(emit, MP_F_NLR_PUSH);
Damien Georgec90f59e2014-09-06 23:06:36 +01001675 ASM_JUMP_IF_REG_NONZERO(emit->as, REG_RET, label);
Damien Georgeb601d952014-06-30 05:17:25 +01001676 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001677}
Damien Georgeb601d952014-06-30 05:17:25 +01001678
Damien George7ff996c2014-09-08 23:05:16 +01001679STATIC void emit_native_setup_finally(emit_t *emit, mp_uint_t label) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001680 emit_native_setup_except(emit, label);
Damien13ed3a62013-10-08 09:05:10 +01001681}
Damien Georgeb601d952014-06-30 05:17:25 +01001682
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001683STATIC void emit_native_end_finally(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01001684 emit_pre_pop_discard(emit);
1685 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001686}
Damiend2755ec2013-10-16 23:58:48 +01001687
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001688STATIC void emit_native_get_iter(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001689 // perhaps the difficult one, as we want to rewrite for loops using native code
1690 // in cases where we iterate over a Python object, can we use normal runtime calls?
Damiend2755ec2013-10-16 23:58:48 +01001691
1692 vtype_kind_t vtype;
1693 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
1694 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001695 emit_call(emit, MP_F_GETITER);
Damiend2755ec2013-10-16 23:58:48 +01001696 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001697}
Damiend2755ec2013-10-16 23:58:48 +01001698
Damien George7ff996c2014-09-08 23:05:16 +01001699STATIC void emit_native_for_iter(emit_t *emit, mp_uint_t label) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001700 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001701 vtype_kind_t vtype;
1702 emit_access_stack(emit, 1, &vtype, REG_ARG_1);
1703 assert(vtype == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001704 emit_call(emit, MP_F_ITERNEXT);
Damien Georgec90f59e2014-09-06 23:06:36 +01001705 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)MP_OBJ_STOP_ITERATION, REG_TEMP1);
1706 ASM_JUMP_IF_REG_EQ(emit->as, REG_RET, REG_TEMP1, label);
Damiend2755ec2013-10-16 23:58:48 +01001707 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1708}
1709
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001710STATIC void emit_native_for_iter_end(emit_t *emit) {
Damiend2755ec2013-10-16 23:58:48 +01001711 // adjust stack counter (we get here from for_iter ending, which popped the value for us)
Damien Georgece8f07a2014-03-27 23:30:26 +00001712 emit_native_pre(emit);
Damiend2755ec2013-10-16 23:58:48 +01001713 adjust_stack(emit, -1);
1714 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001715}
1716
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001717STATIC void emit_native_pop_block(emit_t *emit) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001718 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001719 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001720 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien13ed3a62013-10-08 09:05:10 +01001721 emit_post(emit);
1722}
1723
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001724STATIC void emit_native_pop_except(emit_t *emit) {
Damien Georgeb601d952014-06-30 05:17:25 +01001725 /*
1726 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001727 emit_call(emit, MP_F_NLR_POP);
Damien George40f3c022014-07-03 13:25:24 +01001728 adjust_stack(emit, -(mp_int_t)(sizeof(nlr_buf_t) / sizeof(mp_uint_t)));
Damien Georgeb601d952014-06-30 05:17:25 +01001729 emit_post(emit);
1730 */
Damien13ed3a62013-10-08 09:05:10 +01001731}
1732
Damien Georged17926d2014-03-30 13:35:08 +01001733STATIC void emit_native_unary_op(emit_t *emit, mp_unary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001734 vtype_kind_t vtype;
1735 emit_pre_pop_reg(emit, &vtype, REG_ARG_2);
1736 assert(vtype == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001737 if (op == MP_UNARY_OP_NOT) {
Damien Georged6230f62014-09-23 14:10:03 +00001738 // we need to synthesise this operation by converting to bool first
1739 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_BOOL, REG_ARG_1);
Damien George3112cde2014-09-29 18:45:42 +01001740 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georgeb601d952014-06-30 05:17:25 +01001741 }
Damien Georged6230f62014-09-23 14:10:03 +00001742 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, op, REG_ARG_1);
1743 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001744}
1745
Damien Georged17926d2014-03-30 13:35:08 +01001746STATIC void emit_native_binary_op(emit_t *emit, mp_binary_op_t op) {
Damien Georged6230f62014-09-23 14:10:03 +00001747 DEBUG_printf("binary_op(" UINT_FMT ")\n", op);
Damien George3112cde2014-09-29 18:45:42 +01001748 vtype_kind_t vtype_lhs = peek_vtype(emit, 1);
1749 vtype_kind_t vtype_rhs = peek_vtype(emit, 0);
Damien13ed3a62013-10-08 09:05:10 +01001750 if (vtype_lhs == VTYPE_INT && vtype_rhs == VTYPE_INT) {
Damien George3112cde2014-09-29 18:45:42 +01001751 #if N_X64 || N_X86
1752 // special cases for x86 and shifting
1753 if (op == MP_BINARY_OP_LSHIFT
1754 || op == MP_BINARY_OP_INPLACE_LSHIFT
1755 || op == MP_BINARY_OP_RSHIFT
1756 || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1757 #if N_X64
1758 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X64_REG_RCX, &vtype_lhs, REG_RET);
1759 #else
1760 emit_pre_pop_reg_reg(emit, &vtype_rhs, ASM_X86_REG_ECX, &vtype_lhs, REG_RET);
1761 #endif
1762 if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1763 ASM_LSL_REG(emit->as, REG_RET);
1764 } else {
1765 ASM_ASR_REG(emit->as, REG_RET);
1766 }
1767 emit_post_push_reg(emit, VTYPE_INT, REG_RET);
1768 return;
1769 }
1770 #endif
1771 int reg_rhs = REG_ARG_3;
Damien Georgee9dac3b2014-09-29 22:10:41 +01001772 emit_pre_pop_reg_flexible(emit, &vtype_rhs, &reg_rhs, REG_RET, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001773 emit_pre_pop_reg(emit, &vtype_lhs, REG_ARG_2);
1774 if (0) {
1775 // dummy
1776 #if !(N_X64 || N_X86)
1777 } else if (op == MP_BINARY_OP_LSHIFT || op == MP_BINARY_OP_INPLACE_LSHIFT) {
1778 ASM_LSL_REG_REG(emit->as, REG_ARG_2, reg_rhs);
Damien Georgebc1d3692014-01-11 09:47:06 +00001779 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001780 } else if (op == MP_BINARY_OP_RSHIFT || op == MP_BINARY_OP_INPLACE_RSHIFT) {
1781 ASM_ASR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1782 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1783 #endif
Damien George1ef23482014-10-12 14:21:06 +01001784 } else if (op == MP_BINARY_OP_OR || op == MP_BINARY_OP_INPLACE_OR) {
1785 ASM_OR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1786 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1787 } else if (op == MP_BINARY_OP_XOR || op == MP_BINARY_OP_INPLACE_XOR) {
1788 ASM_XOR_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1789 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1790 } else if (op == MP_BINARY_OP_AND || op == MP_BINARY_OP_INPLACE_AND) {
1791 ASM_AND_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1792 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
Damien George3112cde2014-09-29 18:45:42 +01001793 } else if (op == MP_BINARY_OP_ADD || op == MP_BINARY_OP_INPLACE_ADD) {
1794 ASM_ADD_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1795 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1796 } else if (op == MP_BINARY_OP_SUBTRACT || op == MP_BINARY_OP_INPLACE_SUBTRACT) {
1797 ASM_SUB_REG_REG(emit->as, REG_ARG_2, reg_rhs);
1798 emit_post_push_reg(emit, VTYPE_INT, REG_ARG_2);
1799 } else if (MP_BINARY_OP_LESS <= op && op <= MP_BINARY_OP_NOT_EQUAL) {
1800 // comparison ops are (in enum order):
1801 // MP_BINARY_OP_LESS
1802 // MP_BINARY_OP_MORE
1803 // MP_BINARY_OP_EQUAL
1804 // MP_BINARY_OP_LESS_EQUAL
1805 // MP_BINARY_OP_MORE_EQUAL
1806 // MP_BINARY_OP_NOT_EQUAL
1807 #if N_X64
1808 asm_x64_xor_r64_r64(emit->as, REG_RET, REG_RET);
1809 asm_x64_cmp_r64_with_r64(emit->as, reg_rhs, REG_ARG_2);
1810 static byte ops[6] = {
1811 ASM_X64_CC_JL,
1812 ASM_X64_CC_JG,
1813 ASM_X64_CC_JE,
1814 ASM_X64_CC_JLE,
1815 ASM_X64_CC_JGE,
1816 ASM_X64_CC_JNE,
1817 };
1818 asm_x64_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1819 #elif N_X86
1820 asm_x86_xor_r32_r32(emit->as, REG_RET, REG_RET);
1821 asm_x86_cmp_r32_with_r32(emit->as, reg_rhs, REG_ARG_2);
1822 static byte ops[6] = {
1823 ASM_X86_CC_JL,
1824 ASM_X86_CC_JG,
1825 ASM_X86_CC_JE,
1826 ASM_X86_CC_JLE,
1827 ASM_X86_CC_JGE,
1828 ASM_X86_CC_JNE,
1829 };
1830 asm_x86_setcc_r8(emit->as, ops[op - MP_BINARY_OP_LESS], REG_RET);
1831 #elif N_THUMB
1832 asm_thumb_cmp_rlo_rlo(emit->as, REG_ARG_2, reg_rhs);
1833 static uint16_t ops[6] = {
1834 ASM_THUMB_OP_ITE_GE,
1835 ASM_THUMB_OP_ITE_GT,
1836 ASM_THUMB_OP_ITE_EQ,
1837 ASM_THUMB_OP_ITE_GT,
1838 ASM_THUMB_OP_ITE_GE,
1839 ASM_THUMB_OP_ITE_EQ,
1840 };
1841 static byte ret[6] = { 0, 1, 1, 0, 1, 0, };
1842 asm_thumb_op16(emit->as, ops[op - MP_BINARY_OP_LESS]);
1843 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS]);
1844 asm_thumb_mov_rlo_i8(emit->as, REG_RET, ret[op - MP_BINARY_OP_LESS] ^ 1);
1845 #elif N_ARM
Fabian Vogte5268962014-10-04 00:53:46 +02001846 asm_arm_cmp_reg_reg(emit->as, REG_ARG_2, reg_rhs);
1847 static uint ccs[6] = {
1848 ASM_ARM_CC_LT,
1849 ASM_ARM_CC_GT,
1850 ASM_ARM_CC_EQ,
1851 ASM_ARM_CC_LE,
1852 ASM_ARM_CC_GE,
1853 ASM_ARM_CC_NE,
1854 };
1855 asm_arm_setcc_reg(emit->as, REG_RET, ccs[op - MP_BINARY_OP_LESS]);
Damien George3112cde2014-09-29 18:45:42 +01001856 #else
1857 #error not implemented
1858 #endif
Damien Georgebc1d3692014-01-11 09:47:06 +00001859 emit_post_push_reg(emit, VTYPE_BOOL, REG_RET);
1860 } else {
1861 // TODO other ops not yet implemented
1862 assert(0);
1863 }
Damien13ed3a62013-10-08 09:05:10 +01001864 } else if (vtype_lhs == VTYPE_PYOBJ && vtype_rhs == VTYPE_PYOBJ) {
Damien George3112cde2014-09-29 18:45:42 +01001865 emit_pre_pop_reg_reg(emit, &vtype_rhs, REG_ARG_3, &vtype_lhs, REG_ARG_2);
Damien Georged6230f62014-09-23 14:10:03 +00001866 bool invert = false;
1867 if (op == MP_BINARY_OP_NOT_IN) {
1868 invert = true;
1869 op = MP_BINARY_OP_IN;
1870 } else if (op == MP_BINARY_OP_IS_NOT) {
1871 invert = true;
1872 op = MP_BINARY_OP_IS;
1873 }
Damien George7fe21912014-08-16 22:31:57 +01001874 emit_call_with_imm_arg(emit, MP_F_BINARY_OP, op, REG_ARG_1);
Damien Georged6230f62014-09-23 14:10:03 +00001875 if (invert) {
Damien George3112cde2014-09-29 18:45:42 +01001876 ASM_MOV_REG_REG(emit->as, REG_ARG_2, REG_RET);
Damien Georged6230f62014-09-23 14:10:03 +00001877 emit_call_with_imm_arg(emit, MP_F_UNARY_OP, MP_UNARY_OP_NOT, REG_ARG_1);
1878 }
Damien13ed3a62013-10-08 09:05:10 +01001879 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1880 } else {
1881 printf("ViperTypeError: can't do binary op between types %d and %d\n", vtype_lhs, vtype_rhs);
Damien Georgea5190a72014-08-15 22:39:08 +01001882 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01001883 }
1884}
1885
Damien George7ff996c2014-09-08 23:05:16 +01001886STATIC void emit_native_build_tuple(emit_t *emit, mp_uint_t n_args) {
Damiend2755ec2013-10-16 23:58:48 +01001887 // for viper: call runtime, with types of args
1888 // if wrapped in byte_array, or something, allocates memory and fills it
Damien Georgece8f07a2014-03-27 23:30:26 +00001889 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001890 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001891 emit_call_with_imm_arg(emit, MP_F_BUILD_TUPLE, n_args, REG_ARG_1);
Damiend2755ec2013-10-16 23:58:48 +01001892 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new tuple
Damien13ed3a62013-10-08 09:05:10 +01001893}
1894
Damien George7ff996c2014-09-08 23:05:16 +01001895STATIC void emit_native_build_list(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001896 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001897 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001898 emit_call_with_imm_arg(emit, MP_F_BUILD_LIST, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001899 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new list
1900}
1901
Damien George7ff996c2014-09-08 23:05:16 +01001902STATIC void emit_native_list_append(emit_t *emit, mp_uint_t list_index) {
Damiend2755ec2013-10-16 23:58:48 +01001903 // only used in list comprehension
1904 vtype_kind_t vtype_list, vtype_item;
1905 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1906 emit_access_stack(emit, list_index, &vtype_list, REG_ARG_1);
1907 assert(vtype_list == VTYPE_PYOBJ);
1908 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001909 emit_call(emit, MP_F_LIST_APPEND);
Damiend2755ec2013-10-16 23:58:48 +01001910 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001911}
1912
Damien George7ff996c2014-09-08 23:05:16 +01001913STATIC void emit_native_build_map(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001914 emit_native_pre(emit);
Damien George7fe21912014-08-16 22:31:57 +01001915 emit_call_with_imm_arg(emit, MP_F_BUILD_MAP, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001916 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new map
1917}
1918
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02001919STATIC void emit_native_store_map(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01001920 vtype_kind_t vtype_key, vtype_value, vtype_map;
1921 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
1922 assert(vtype_key == VTYPE_PYOBJ);
1923 assert(vtype_value == VTYPE_PYOBJ);
1924 assert(vtype_map == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001925 emit_call(emit, MP_F_STORE_MAP);
Damien13ed3a62013-10-08 09:05:10 +01001926 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // map
1927}
1928
Damien George7ff996c2014-09-08 23:05:16 +01001929STATIC void emit_native_map_add(emit_t *emit, mp_uint_t map_index) {
Damiend2755ec2013-10-16 23:58:48 +01001930 // only used in list comprehension
1931 vtype_kind_t vtype_map, vtype_key, vtype_value;
1932 emit_pre_pop_reg_reg(emit, &vtype_key, REG_ARG_2, &vtype_value, REG_ARG_3);
1933 emit_access_stack(emit, map_index, &vtype_map, REG_ARG_1);
1934 assert(vtype_map == VTYPE_PYOBJ);
1935 assert(vtype_key == VTYPE_PYOBJ);
1936 assert(vtype_value == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001937 emit_call(emit, MP_F_STORE_MAP);
Damiend2755ec2013-10-16 23:58:48 +01001938 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001939}
1940
Damien George7ff996c2014-09-08 23:05:16 +01001941STATIC void emit_native_build_set(emit_t *emit, mp_uint_t n_args) {
Damien Georgece8f07a2014-03-27 23:30:26 +00001942 emit_native_pre(emit);
Damien Georgecd82e022014-02-02 13:11:48 +00001943 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_2, n_args); // pointer to items
Damien George7fe21912014-08-16 22:31:57 +01001944 emit_call_with_imm_arg(emit, MP_F_BUILD_SET, n_args, REG_ARG_1);
Damien13ed3a62013-10-08 09:05:10 +01001945 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); // new set
1946}
1947
Damien George7ff996c2014-09-08 23:05:16 +01001948STATIC void emit_native_set_add(emit_t *emit, mp_uint_t set_index) {
Damiend2755ec2013-10-16 23:58:48 +01001949 // only used in set comprehension
1950 vtype_kind_t vtype_set, vtype_item;
1951 emit_pre_pop_reg(emit, &vtype_item, REG_ARG_2);
1952 emit_access_stack(emit, set_index, &vtype_set, REG_ARG_1);
1953 assert(vtype_set == VTYPE_PYOBJ);
1954 assert(vtype_item == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001955 emit_call(emit, MP_F_STORE_SET);
Damiend2755ec2013-10-16 23:58:48 +01001956 emit_post(emit);
Damien13ed3a62013-10-08 09:05:10 +01001957}
Damiend2755ec2013-10-16 23:58:48 +01001958
Damien George7ff996c2014-09-08 23:05:16 +01001959STATIC void emit_native_build_slice(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001960 DEBUG_printf("build_slice %d\n", n_args);
Damien Georgeb601d952014-06-30 05:17:25 +01001961 if (n_args == 2) {
1962 vtype_kind_t vtype_start, vtype_stop;
1963 emit_pre_pop_reg_reg(emit, &vtype_stop, REG_ARG_2, &vtype_start, REG_ARG_1); // arg1 = start, arg2 = stop
1964 assert(vtype_start == VTYPE_PYOBJ);
1965 assert(vtype_stop == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001966 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 +01001967 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1968 } else {
1969 assert(n_args == 3);
1970 vtype_kind_t vtype_start, vtype_stop, vtype_step;
1971 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
1972 assert(vtype_start == VTYPE_PYOBJ);
1973 assert(vtype_stop == VTYPE_PYOBJ);
1974 assert(vtype_step == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01001975 emit_call(emit, MP_F_NEW_SLICE);
Damien Georgeb601d952014-06-30 05:17:25 +01001976 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
1977 }
Damien13ed3a62013-10-08 09:05:10 +01001978}
Damien Georgecdd96df2014-04-06 12:58:40 +01001979
Damien George7ff996c2014-09-08 23:05:16 +01001980STATIC void emit_native_unpack_sequence(emit_t *emit, mp_uint_t n_args) {
Damien Georgecdd96df2014-04-06 12:58:40 +01001981 DEBUG_printf("unpack_sequence %d\n", n_args);
1982 vtype_kind_t vtype_base;
1983 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1984 assert(vtype_base == VTYPE_PYOBJ);
1985 emit_get_stack_pointer_to_reg_for_push(emit, REG_ARG_3, n_args); // arg3 = dest ptr
Damien George7fe21912014-08-16 22:31:57 +01001986 emit_call_with_imm_arg(emit, MP_F_UNPACK_SEQUENCE, n_args, REG_ARG_2); // arg2 = n_args
Damien13ed3a62013-10-08 09:05:10 +01001987}
Damien Georgecdd96df2014-04-06 12:58:40 +01001988
Damien George7ff996c2014-09-08 23:05:16 +01001989STATIC 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 +01001990 DEBUG_printf("unpack_ex %d %d\n", n_left, n_right);
1991 vtype_kind_t vtype_base;
1992 emit_pre_pop_reg(emit, &vtype_base, REG_ARG_1); // arg1 = seq
1993 assert(vtype_base == VTYPE_PYOBJ);
Damien Georgeb601d952014-06-30 05:17:25 +01001994 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 +01001995 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 +01001996}
1997
Damien George7ff996c2014-09-08 23:05:16 +01001998STATIC 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 +01001999 // 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 +00002000 emit_native_pre(emit);
Damien Georgea32c1e42014-05-07 18:30:52 +01002001 if (n_pos_defaults == 0 && n_kw_defaults == 0) {
Damien George7fe21912014-08-16 22:31:57 +01002002 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 +01002003 } else {
2004 vtype_kind_t vtype_def_tuple, vtype_def_dict;
2005 emit_pre_pop_reg_reg(emit, &vtype_def_dict, REG_ARG_3, &vtype_def_tuple, REG_ARG_2);
2006 assert(vtype_def_tuple == VTYPE_PYOBJ);
2007 assert(vtype_def_dict == VTYPE_PYOBJ);
Damien George7fe21912014-08-16 22:31:57 +01002008 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 +01002009 }
Damien13ed3a62013-10-08 09:05:10 +01002010 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2011}
2012
Damien George7ff996c2014-09-08 23:05:16 +01002013STATIC 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 +01002014 assert(0);
2015}
2016
Damien George7ff996c2014-09-08 23:05:16 +01002017STATIC 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 +00002018 DEBUG_printf("call_function(n_pos=" UINT_FMT ", n_kw=" UINT_FMT ", star_flags=" UINT_FMT ")\n", n_positional, n_keyword, star_flags);
2019
Damien Georgee9dac3b2014-09-29 22:10:41 +01002020 // TODO: in viper mode, call special runtime routine with type info for args,
2021 // and wanted type info for return, to remove need for boxing/unboxing
2022
Damien George922ddd62014-04-09 12:43:17 +01002023 assert(!star_flags);
Damien Georgecd82e022014-02-02 13:11:48 +00002024
Damien Georgee9dac3b2014-09-29 22:10:41 +01002025 emit_native_pre(emit);
2026 vtype_kind_t vtype_fun = peek_vtype(emit, n_positional + 2 * n_keyword);
2027 if (vtype_fun == VTYPE_BUILTIN_CAST) {
2028 // casting operator
2029 assert(n_positional == 1 && n_keyword == 0);
2030 DEBUG_printf(" cast to %d\n", vtype_fun);
2031 vtype_kind_t vtype_cast = peek_stack(emit, 1)->u_imm;
2032 switch (peek_vtype(emit, 0)) {
2033 case VTYPE_PYOBJ: {
2034 vtype_kind_t vtype;
2035 emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
2036 emit_pre_pop_discard(emit);
2037 emit_call_with_imm_arg(emit, MP_F_CONVERT_OBJ_TO_NATIVE, MP_NATIVE_TYPE_UINT, REG_ARG_2); // arg2 = type
2038 emit_post_push_reg(emit, vtype_cast, REG_RET);
2039 break;
2040 }
2041 case VTYPE_BOOL:
2042 case VTYPE_INT:
2043 case VTYPE_UINT:
2044 case VTYPE_PTR:
2045 case VTYPE_PTR8:
2046 case VTYPE_PTR16:
2047 case VTYPE_PTR_NONE:
2048 emit_fold_stack_top(emit, REG_ARG_1);
2049 emit_post_top_set_vtype(emit, vtype_cast);
2050 break;
2051 default:
2052 assert(!"TODO: convert obj to int");
2053 }
2054 } else {
2055 if (n_positional != 0 || n_keyword != 0) {
2056 emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword); // pointer to args
2057 }
Damien13ed3a62013-10-08 09:05:10 +01002058 emit_pre_pop_reg(emit, &vtype_fun, REG_ARG_1); // the function
2059 assert(vtype_fun == VTYPE_PYOBJ);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002060 emit_call_with_imm_arg(emit, MP_F_NATIVE_CALL_FUNCTION_N_KW, n_positional | (n_keyword << 8), REG_ARG_2);
2061 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
Damien Georgecd82e022014-02-02 13:11:48 +00002062 }
Damien13ed3a62013-10-08 09:05:10 +01002063}
2064
Damien George7ff996c2014-09-08 23:05:16 +01002065STATIC 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 +01002066 assert(!star_flags);
Damien Georgece8f07a2014-03-27 23:30:26 +00002067 emit_native_pre(emit);
Damien Georgecdd96df2014-04-06 12:58:40 +01002068 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 +01002069 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 +01002070 emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
2071}
2072
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002073STATIC void emit_native_return_value(emit_t *emit) {
Damien Georgecdd96df2014-04-06 12:58:40 +01002074 DEBUG_printf("return_value\n");
Damien13ed3a62013-10-08 09:05:10 +01002075 if (emit->do_viper_types) {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002076 if (peek_vtype(emit, 0) == VTYPE_PTR_NONE) {
2077 emit_pre_pop_discard(emit);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002078 if (emit->return_vtype == VTYPE_PYOBJ) {
Damien Georgec90f59e2014-09-06 23:06:36 +01002079 ASM_MOV_IMM_TO_REG(emit->as, (mp_uint_t)mp_const_none, REG_RET);
Damien Georgee9dac3b2014-09-29 22:10:41 +01002080 } else {
2081 ASM_MOV_IMM_TO_REG(emit->as, 0, REG_RET);
Damien Georgee6c0dff2014-08-15 23:47:59 +01002082 }
Damien Georgee9dac3b2014-09-29 22:10:41 +01002083 } else {
2084 vtype_kind_t vtype;
2085 emit_pre_pop_reg(emit, &vtype, REG_RET);
2086 if (vtype != emit->return_vtype) {
2087 printf("ViperTypeError: incompatible return type\n");
2088 }
Damien George2ac4af62014-08-15 16:45:41 +01002089 }
Damien13ed3a62013-10-08 09:05:10 +01002090 } else {
Damien Georgee9dac3b2014-09-29 22:10:41 +01002091 vtype_kind_t vtype;
2092 emit_pre_pop_reg(emit, &vtype, REG_RET);
Damien13ed3a62013-10-08 09:05:10 +01002093 assert(vtype == VTYPE_PYOBJ);
2094 }
2095 emit->last_emit_was_return_value = true;
Damien Georgec90f59e2014-09-06 23:06:36 +01002096 //ASM_BREAK_POINT(emit->as); // to insert a break-point for debugging
2097 ASM_EXIT(emit->as);
Damien13ed3a62013-10-08 09:05:10 +01002098}
2099
Damien George7ff996c2014-09-08 23:05:16 +01002100STATIC void emit_native_raise_varargs(emit_t *emit, mp_uint_t n_args) {
Damien Georgeb601d952014-06-30 05:17:25 +01002101 assert(n_args == 1);
Damien George86de21b2014-08-16 22:06:11 +01002102 vtype_kind_t vtype_exc;
2103 emit_pre_pop_reg(emit, &vtype_exc, REG_ARG_1); // arg1 = object to raise
2104 if (vtype_exc != VTYPE_PYOBJ) {
2105 printf("ViperTypeError: must raise an object\n");
2106 }
2107 // 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 +01002108 emit_call(emit, MP_F_NATIVE_RAISE);
Damien13ed3a62013-10-08 09:05:10 +01002109}
Damien Georgeb601d952014-06-30 05:17:25 +01002110
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002111STATIC void emit_native_yield_value(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002112 // not supported (for now)
2113 assert(0);
2114}
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02002115STATIC void emit_native_yield_from(emit_t *emit) {
Damien13ed3a62013-10-08 09:05:10 +01002116 // not supported (for now)
2117 assert(0);
2118}
2119
Damien Georgeb601d952014-06-30 05:17:25 +01002120STATIC void emit_native_start_except_handler(emit_t *emit) {
2121 // This instruction follows an nlr_pop, so the stack counter is back to zero, when really
2122 // it should be up by a whole nlr_buf_t. We then want to pop the nlr_buf_t here, but save
2123 // the first 2 elements, so we can get the thrown value.
2124 adjust_stack(emit, 2);
2125 vtype_kind_t vtype_nlr;
2126 emit_pre_pop_reg(emit, &vtype_nlr, REG_ARG_1); // get the thrown value
Damien Georgee6ce10a2014-09-06 18:38:20 +01002127 emit_pre_pop_discard(emit); // discard the linked-list pointer in the nlr_buf
Damien Georgeb601d952014-06-30 05:17:25 +01002128 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
2129}
2130
2131STATIC void emit_native_end_except_handler(emit_t *emit) {
Damien Georgee6ce10a2014-09-06 18:38:20 +01002132 adjust_stack(emit, -2);
Damien Georgeb601d952014-06-30 05:17:25 +01002133}
2134
Damien13ed3a62013-10-08 09:05:10 +01002135const emit_method_table_t EXPORT_FUN(method_table) = {
Damien George2ac4af62014-08-15 16:45:41 +01002136 emit_native_set_native_type,
Damien13ed3a62013-10-08 09:05:10 +01002137 emit_native_start_pass,
2138 emit_native_end_pass,
2139 emit_native_last_emit_was_return_value,
Damien Georged66ae182014-04-10 17:28:54 +00002140 emit_native_adjust_stack_size,
Damien George08335002014-01-18 23:24:36 +00002141 emit_native_set_source_line,
Damien13ed3a62013-10-08 09:05:10 +01002142
2143 emit_native_load_id,
2144 emit_native_store_id,
2145 emit_native_delete_id,
2146
2147 emit_native_label_assign,
2148 emit_native_import_name,
2149 emit_native_import_from,
2150 emit_native_import_star,
2151 emit_native_load_const_tok,
2152 emit_native_load_const_small_int,
2153 emit_native_load_const_int,
2154 emit_native_load_const_dec,
Damien13ed3a62013-10-08 09:05:10 +01002155 emit_native_load_const_str,
Damien George3558f622014-04-20 17:50:40 +01002156 emit_native_load_null,
Damien13ed3a62013-10-08 09:05:10 +01002157 emit_native_load_fast,
Damien13ed3a62013-10-08 09:05:10 +01002158 emit_native_load_deref,
Damien9ecbcff2013-12-11 00:41:43 +00002159 emit_native_load_name,
2160 emit_native_load_global,
Damien13ed3a62013-10-08 09:05:10 +01002161 emit_native_load_attr,
2162 emit_native_load_method,
2163 emit_native_load_build_class,
Damien George729f7b42014-04-17 22:10:53 +01002164 emit_native_load_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002165 emit_native_store_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002166 emit_native_store_deref,
Damien13ed3a62013-10-08 09:05:10 +01002167 emit_native_store_name,
2168 emit_native_store_global,
Damien13ed3a62013-10-08 09:05:10 +01002169 emit_native_store_attr,
Damien13ed3a62013-10-08 09:05:10 +01002170 emit_native_store_subscr,
Damien13ed3a62013-10-08 09:05:10 +01002171 emit_native_delete_fast,
Damien9ecbcff2013-12-11 00:41:43 +00002172 emit_native_delete_deref,
Damien13ed3a62013-10-08 09:05:10 +01002173 emit_native_delete_name,
2174 emit_native_delete_global,
Damien13ed3a62013-10-08 09:05:10 +01002175 emit_native_delete_attr,
2176 emit_native_delete_subscr,
2177 emit_native_dup_top,
2178 emit_native_dup_top_two,
2179 emit_native_pop_top,
2180 emit_native_rot_two,
2181 emit_native_rot_three,
2182 emit_native_jump,
2183 emit_native_pop_jump_if_true,
2184 emit_native_pop_jump_if_false,
2185 emit_native_jump_if_true_or_pop,
2186 emit_native_jump_if_false_or_pop,
Damien13ed3a62013-10-08 09:05:10 +01002187 emit_native_break_loop,
2188 emit_native_continue_loop,
2189 emit_native_setup_with,
2190 emit_native_with_cleanup,
2191 emit_native_setup_except,
2192 emit_native_setup_finally,
2193 emit_native_end_finally,
2194 emit_native_get_iter,
2195 emit_native_for_iter,
2196 emit_native_for_iter_end,
2197 emit_native_pop_block,
2198 emit_native_pop_except,
2199 emit_native_unary_op,
2200 emit_native_binary_op,
Damien13ed3a62013-10-08 09:05:10 +01002201 emit_native_build_tuple,
2202 emit_native_build_list,
2203 emit_native_list_append,
2204 emit_native_build_map,
2205 emit_native_store_map,
2206 emit_native_map_add,
2207 emit_native_build_set,
2208 emit_native_set_add,
2209 emit_native_build_slice,
2210 emit_native_unpack_sequence,
2211 emit_native_unpack_ex,
2212 emit_native_make_function,
2213 emit_native_make_closure,
2214 emit_native_call_function,
2215 emit_native_call_method,
2216 emit_native_return_value,
2217 emit_native_raise_varargs,
2218 emit_native_yield_value,
2219 emit_native_yield_from,
Damien Georgeb601d952014-06-30 05:17:25 +01002220
2221 emit_native_start_except_handler,
2222 emit_native_end_except_handler,
Damien13ed3a62013-10-08 09:05:10 +01002223};
2224
Damien Georgec90f59e2014-09-06 23:06:36 +01002225#endif